varapi=newAPISDK([// An API definition list here'POST /articles','GET /articles/:id','PUT /articles/:id','DELETE /articles/:id'],{'host': '/api',// 'promise' and 'http' MUST be provided'promise': Promise,'http': function(params){console.log(params);}});varid=123;// { "method": "get", "url": "/api/articles/123", "data": { "token": 789 } }api.articles(id).get({token: 789});// Dynamic parameter is supportedvarinc=0;varnextArticle=api.articles(function(){returninc++;});// { method: "GET", url: "/api/articles/0", data: undefined }nextArticle.get();// { method: "GET", url: "/api/articles/1", data: undefined }nextArticle.get();// Asynchronous parameter is supportdvarasyncParam=newPromise(function(resolve){setTimeout(resolve,1000,123);});// The request will be launched after the promise resolvedapi.articles(asyncParam).get();
Friendly with RESTful API
/** * GET /users/:user_id/ * GET /users/:user_id/orders * POST /users/:user_id/orders * GET /users/:user_id/orders/:order_id * POST /users/:user_id/orders/:order_id/payment * POST /users/:user_id/orders/:order_id/rating * POST /users/:user_id/orders/:order_id/cancel * POST /users/:user_id/orders/:order_id/refunding**/varcurrentUserId=123;// Create a 'user' object for current uservaruser=api.users(currentUserId);// Get current user profileuser.get();// Create an 'order' objectvarorder=user.orders(123);// Get order informationorder.get();// Post a payment request for this orderorder.payment.post();// Post a rating for this orderorder.rating.post();// Post a order canceling request for this orderorder.cancel.post();// Post refunding request for this orderorder.refunding.post();