@request/api
Advanced tools
Comparing version 0.3.0 to 0.4.0
70
index.js
function API (config, custom) { | ||
this.options = {} | ||
module.exports = (config, custom) => { | ||
var api = {} | ||
var wrap = { | ||
method: (key) => ((url) => { | ||
this.options.method = key.toUpperCase() | ||
this.options.url = url || '' | ||
return api | ||
}), | ||
option: (key) => ((value) => { | ||
this.options[key] = value | ||
return api | ||
}), | ||
custom: (key) => custom[key].bind(api, this.options) | ||
method: function (key) { | ||
return (value) => { | ||
if (!this._options) { | ||
var api = init() | ||
api._options = {} | ||
return api[key](value) | ||
} | ||
this._options.method = key.toUpperCase() | ||
this._options.url = value || '' | ||
return this | ||
} | ||
}, | ||
option: function (key) { | ||
return (value) => { | ||
if (!this._options) { | ||
var api = init() | ||
api._options = {} | ||
return api[key](value) | ||
} | ||
this._options[key] = value | ||
return this | ||
} | ||
}, | ||
custom: function (key) { | ||
return function () { | ||
if (!this._options) { | ||
var api = init() | ||
api._options = {} | ||
return custom[key].apply(api, arguments) | ||
} | ||
return custom[key].apply(this, arguments) | ||
}.bind(this) | ||
} | ||
} | ||
Object.keys(config).forEach((type) => { | ||
Object.keys(config[type]).forEach((method) => { | ||
api[method] = wrap[type](method) | ||
function init () { | ||
var api = {} | ||
config[type][method].forEach((alias) => { | ||
api[alias] = wrap[type](method) | ||
Object.keys(config).forEach((type) => { | ||
Object.keys(config[type]).forEach((method) => { | ||
api[method] = wrap[type].call(api, method) | ||
config[type][method].forEach((alias) => { | ||
api[alias] = wrap[type].call(api, method) | ||
}) | ||
}) | ||
}) | ||
}) | ||
return api | ||
} | ||
return api | ||
} | ||
module.exports = (config, custom) => { | ||
return new API(config, custom) | ||
return init() | ||
} |
{ | ||
"name": "@request/api", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Sugar API for @request/core consumers", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -6,4 +6,2 @@ | ||
Use this module to create sugar API for your HTTP client. You can also allow your users to define their own method aliases. | ||
```js | ||
@@ -13,3 +11,3 @@ var api = require('@request/api') | ||
// all API methods and their aliases | ||
// API methods configuration | ||
var config = { | ||
@@ -22,40 +20,34 @@ // HTTP methods | ||
option: { | ||
qs: ['where'], | ||
callback: ['done'] | ||
qs: ['where'] | ||
}, | ||
// custom methods | ||
custom: { | ||
check: [], | ||
submit: ['gimme'] | ||
request: ['fetch', 'snatch', 'submit'] | ||
} | ||
} | ||
// custom method implementation | ||
// custom methods implementation | ||
var custom = { | ||
// `host` is parameter passed to your method | ||
check: function (options, host) { | ||
if (/localhost/.test(host)) { | ||
options.url += ':6767' | ||
// pass any arguments to your custom methods | ||
request: function (callback) { | ||
if (callback) { | ||
// `this._options` contains the generated options object | ||
this._options.callback = callback | ||
} | ||
// `this` contains the API itself, for chaining purposes | ||
return this | ||
}, | ||
// `options` contains the generated options object from the preceding methods | ||
submit: function (options) { | ||
// the last method should return a @request/core consumer | ||
return client(options) | ||
// the last method should return @request/core consumer | ||
return client(this._options) | ||
// return `this` if you want to chain further | ||
} | ||
} | ||
// initialize the API | ||
var request = api(config, custom) | ||
// GET http://localhost:6767?a=b | ||
// GET http://localhost:6767?a=1 | ||
request | ||
.select('http://localhost') | ||
.check('localhost') | ||
.where({a: 'b'}) | ||
.done((err, res, body) => { | ||
.select('http://localhost:6767') | ||
.where({a: 1}) | ||
.fetch((err, res, body) => { | ||
// request callback | ||
}) | ||
.gimme() | ||
``` | ||
@@ -62,0 +54,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12820
50
47304
64