Coder Social home page Coder Social logo

restfulapi-sdk's Introduction

restfulapi-sdk

NPM version Downloads

build Build Status Coverage Status

standard restful api sdk with cache and promise.

What achieved

  1. standard restful api sdk for get, post, put, delete, patch and options methods.
  2. save data in browser side, use cache method.
  3. promise callback.

usage

  $ npm install restfulapi-sdk
  $ bower install restfulapi-sdk // or use bower

Include sdk.js in /dist by tag , you can also require js by require('restfulapi-sdk')

  var urls = {
    order: '/api/order/:id',
    user: {
      password: '/api/user/:id/password'
    }
  };

  var config = {
    urlPrefix: 'api/', // you can add a urlPrefix, default ''
    cache: false, // open cache, default false (only valid with get method)
    cachePrefix: 'api', // cache prefix, default 'api'
    overdue: 3600, // cache time, unit second, default 3600
    overdueDay: null, // cache days number,prior to overdue,default null
    isSession: false // use sessionStorage for cache or not
  };

  const APISDK = require('restfulapi-sdk');
  const API = new APISDK(urls, config);

  var todo1 = API.order.get({
    id: id,
    otherRouteParams: ...
  });

  var todo2 = API.order.post({
    id: id,
    otherRouteParams: ...
  });

  // cache data with get method, will get remote data first
  API.order.cache({
    id: id,
    otherRouteParams: ...
  }).then((res, httpCode, refer) => {
    // success
    // do something
    refer(true, ...newRes); // arguments in next success()
    refer(false, ...newErr); // arguments in next error()
  }, (err, httpCode) => {
    // no data or overdue
  });

  APISDK.all([todo1, todo2])
  .then((successArr, errorArr, refer) => { // only trigger in success function
    // successArr[0] = [indexinPromiseArr, ...returnParams]
  });

  API.user.put({
    id: id
  }, {
    newPassword: newPassword
  }).then(() => {}, () => {}); // while use api data

  /*  static method for httpCode  */
  APISDK.httpCode((code) => {
    console.log(code);
  });

  /*  static method for remove Cache  */
  APISDK.remove(key, isSession); // cache key, and use sessionStorage for cache or not

develop

  $ clone restfulapi-sdk && make dev // then view http://localhost:8888

restfulapi-sdk's People

Contributors

youngerheart avatar coolzjy avatar

Watchers

James Cloos avatar  avatar

restfulapi-sdk's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.