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
Size: 33
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
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
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
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()
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
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) => ())