GithubHelp home page GithubHelp logo

isabella232 / api-2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from request/api

0.0 0.0 0.0 31 KB

Sugar API for @request/interface consumers

License: Apache License 2.0

JavaScript 100.00%

api-2's Introduction

@request/api

npm-version travis-ci coveralls-status

Table of Contents

Options

var api = require('@request/api')
var request = api({
  // required
  type: 'basic', // or 'chain'
  // required
  define: {
    // HTTP request function
    // that accepts @request/interface options object
    request: require('@request/client')
  },
  // optional
  config: {
    // define your own methods and method aliases
  }
})

Basic API

request('url')
request({options})
request('url', function callback (err, res, body) {})
request({options}, function callback (err, res, body) {})
request('url', {options}, function callback (err, res, body) {})

request.[HTTP_VERB]('url')
request.[HTTP_VERB]({options})
request.[HTTP_VERB]('url', function callback (err, res, body) {})
request.[HTTP_VERB]({options}, function callback (err, res, body) {})
request.[HTTP_VERB]('url', {options}, function callback (err, res, body) {})
var api = require('@request/api')
var client = require('@request/client')

var request = api({
  type: 'basic',
  define: {
    request: client
  }
})

// GET http://localhost:6767?a=1
request.get('http://localhost:6767', {qs: {a: 1}}, (err, res, body) => {
  // request callback
})

Chain API

var api = require('@request/api')
var client = require('@request/client')

var request = api({
  type: 'chain',
  define: {
    request: client
  }
})

// GET http://localhost:6767?a=1
request
  .get('http://localhost:6767')
  .qs({a: 1})
  .callback((err, res, body) => {
    // request callback
  })
  .request()

Chain API Config

var api = require('@request/api')
var client = require('@request/client')

var request = api({
  type: 'chain',
  // API methods configuration
  config: {
    // HTTP methods
    method: {
      get: ['select'], // list of aliases
      // ...
    },
    // @request/interface option methods
    option: {
      qs: ['where'], // list of aliases
      // ...
    },
    // custom methods
    custom: {
      request: ['fetch', 'snatch', 'submit'], // list of aliases
      // ...
    }
  },
  // custom methods implementation
  define: {
    // `options` is always prepended as first argument
    // any other custom arguments follows after that
    request: (options, callback) => {
      if (callback) {
        // `options` contains the generated options object
        options.callback = callback
      }
      // omit the return value if you want to chain further
      return client(options)
    }
  }
})

// GET http://localhost:6767?a=1
request
  .select('http://localhost:6767')
  .where({a: 1})
  .fetch((err, res, body) => {
    // request callback
  })

Promises

var api = require('@request/api')
var client = require('@request/client')

function wrap (options) {
  var promise = new Promise((resolve, reject) => {
    options.callback = (err, res, body) => {
      ;(err) ? reject(err) : resolve([res, body])
    }
  })
  client(options)
  return promise
}
var request = api({
  type: 'basic',
  define: {
    request: wrap
  }
})
// GET http://localhost:6767?a=1
request.get('http://localhost:6767', {qs: 1})
  .catch((err) => {})
  .then((result) => {})
var request = api({
  type: 'chain',
  define: {
    request: wrap
  }
})
// GET http://localhost:6767?a=1
request
  .get('http://localhost:6767')
  .qs({a: 1})
  .request()
  .catch((err) => ())
  .then((result) => ())

api-2's People

Contributors

simov 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.