GithubHelp home page GithubHelp logo

mocker12345 / apisdk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yanagieiichi/apisdk

0.0 1.0 0.0 217 KB

Build a SDK from API document

License: MIT License

JavaScript 41.18% HTML 58.82%

apisdk's Introduction

apisdk.js

Build a SDK from API document

Get Started

var api = new APISDK([
  // 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); }
});


var id = 123;
// { "method": "get", "url": "/api/articles/123", "data": { "token": 789 } }
api.articles(id).get({ token: 789 });

// Dynamic parameter is supported
var inc = 0;
var nextArticle = api.articles(function() { return inc++; });
// { method: "GET", url: "/api/articles/0", data: undefined }
nextArticle.get();
// { method: "GET", url: "/api/articles/1", data: undefined }
nextArticle.get();

// Asynchronous parameter is supportd
var asyncParam = new Promise(function(resolve){ setTimeout(resolve, 1000, 123); });

// The request will be launched after the promise resolved
api.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
**/

var currentUserId = 123;

// Create a 'user' object for current user
var user = api.users(currentUserId);

// Get current user profile
user.get();

// Create an 'order' object
var order = user.orders(123);

// Get order information
order.get();

// Post a payment request for this order
order.payment.post();

// Post a rating for this order
order.rating.post();

// Post a order canceling request for this order
order.cancel.post();

// Post refunding request for this order
order.refunding.post();

Install

bower install apisdk
<script src="/bower_components/apisdk/apisdk.js"></script>

apisdk's People

Contributors

yanagieiichi avatar

Watchers

mocker avatar

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.