request/api

Name: api

Owner: request

Description: Sugar API for @request/interface consumers

Created: 2016-02-05 22:07:29.0

Updated: 2017-05-18 23:06:27.0

Pushed: 2016-05-03 16:36:21.0

Homepage:

Size: 33

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

@request/api

npm-version travis-ci coveralls-status

Table of Contents
Options
api = require('@request/api')
request = api({
 required
pe: 'basic', // or 'chain'
 required
fine: {
// HTTP request function
// that accepts @request/interface options object
request: require('@request/client')

 optional
nfig: {
// define your own methods and method aliases


Basic API
est('url')
est({options})
est('url', function callback (err, res, body) {})
est({options}, function callback (err, res, body) {})
est('url', {options}, function callback (err, res, body) {})

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

request = api({
pe: 'basic',
fine: {
request: client



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

Chain API
api = require('@request/api')
client = require('@request/client')

request = api({
pe: 'chain',
fine: {
request: client



ET http://localhost:6767?a=1
est
et('http://localhost:6767')
s({a: 1})
allback((err, res, body) => {
// request callback

equest()
Chain API Config
api = require('@request/api')
client = require('@request/client')

request = api({
pe: 'chain',
 API methods configuration
nfig: {
// 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
fine: {
// `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)
}



ET http://localhost:6767?a=1
est
elect('http://localhost:6767')
here({a: 1})
etch((err, res, body) => {
// request callback

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

tion wrap (options) {
r promise = new Promise((resolve, reject) => {
options.callback = (err, res, body) => {
  ;(err) ? reject(err) : resolve([res, body])
}

ient(options)
turn promise

s
request = api({
pe: 'basic',
fine: {
request: wrap


ET http://localhost:6767?a=1
est.get('http://localhost:6767', {qs: 1})
atch((err) => {})
hen((result) => {})
s
request = api({
pe: 'chain',
fine: {
request: wrap


ET http://localhost:6767?a=1
est
et('http://localhost:6767')
s({a: 1})
equest()
atch((err) => ())
hen((result) => ())

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.