@request/api
Advanced tools
Comparing version 0.4.0 to 0.5.0
61
index.js
module.exports = (config, custom) => { | ||
var basic = require('./lib/basic') | ||
var chain = require('./lib/chain') | ||
var config = require('./config/methods') | ||
var wrap = { | ||
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) | ||
} | ||
} | ||
function init () { | ||
var api = {} | ||
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 | ||
module.exports = (options) => { | ||
if (options.type === 'basic') { | ||
return basic(options.config || config, options.request) | ||
} | ||
return init() | ||
else if (options.type === 'chain') { | ||
return chain(options.config, options.define) | ||
} | ||
} |
{ | ||
"name": "@request/api", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Sugar API for @request/core consumers", | ||
@@ -19,6 +19,12 @@ "keywords": [ | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"deep-copy": "^1.1.2" | ||
}, | ||
"devDependencies": { | ||
"@request/client": "^0.1.0", | ||
"coveralls": "^2.11.6", | ||
"eslint": "^2.9.0", | ||
"eslint-config-standard": "^5.3.0", | ||
"eslint-plugin-promise": "^1.1.0", | ||
"eslint-plugin-standard": "^1.3.2", | ||
"istanbul": "^0.4.2", | ||
@@ -29,2 +35,4 @@ "mocha": "^2.4.5" | ||
"files": [ | ||
"config/", | ||
"lib/", | ||
"LICENSE", | ||
@@ -35,6 +43,7 @@ "README.md", | ||
"scripts": { | ||
"test": "npm run test-ci", | ||
"test": "npm run lint && npm run test-ci", | ||
"test-ci": "mocha", | ||
"test-cov": "istanbul cover _mocha" | ||
"test-cov": "istanbul cover _mocha", | ||
"lint": "eslint lib/ test/ && echo Lint Passed" | ||
} | ||
} |
104
README.md
@@ -6,39 +6,81 @@ | ||
## Basic API | ||
```js | ||
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) {}) | ||
``` | ||
```js | ||
var api = require('@request/api') | ||
var client = require('@request/client') | ||
// API methods configuration | ||
var config = { | ||
// HTTP methods | ||
method: { | ||
get: ['select'] | ||
}, | ||
// @request/core option methods | ||
option: { | ||
qs: ['where'] | ||
}, | ||
// custom methods | ||
custom: { | ||
request: ['fetch', 'snatch', 'submit'] | ||
} | ||
} | ||
// initialize the API | ||
var request = api({ | ||
type: 'basic', | ||
// pass HTTP request function | ||
// that accepts @request/interface options | ||
request: client | ||
}) | ||
// custom methods implementation | ||
var custom = { | ||
// pass any arguments to your custom methods | ||
request: function (callback) { | ||
if (callback) { | ||
// `this._options` contains the generated options object | ||
this._options.callback = callback | ||
// GET http://localhost:6767?a=1 | ||
request.get('http://localhost:6767', {qs: {a: 1}}, (err, res, body) => { | ||
// request callback | ||
}) | ||
``` | ||
## Chain API | ||
```js | ||
var api = require('@request/api') | ||
var client = require('@request/client') | ||
// initialize the API | ||
var request = api({ | ||
type: 'chain', | ||
// API methods configuration | ||
config: { | ||
// HTTP methods | ||
method: { | ||
get: ['select'], | ||
// ... | ||
}, | ||
// @request/interface option methods | ||
option: { | ||
qs: ['where'], | ||
// ... | ||
}, | ||
// custom methods | ||
custom: { | ||
request: ['fetch', 'snatch', 'submit'], | ||
// ... | ||
} | ||
// the last method should return @request/core consumer | ||
return client(this._options) | ||
// return `this` if you want to chain further | ||
}, | ||
// custom methods implementation | ||
define: { | ||
// pass any arguments to your custom methods | ||
request: function (callback) { | ||
if (callback) { | ||
// `this._options` contains the generated options object | ||
this._options.callback = callback | ||
} | ||
// the last method should return @request/interface consumer | ||
return client(this._options) | ||
// or | ||
return this // if you want to chain further | ||
} | ||
} | ||
} | ||
}) | ||
// initialize the API | ||
var request = api(config, custom) | ||
// GET http://localhost:6767?a=1 | ||
@@ -53,5 +95,3 @@ request | ||
> See [@request/core][request-core] for more details. | ||
[npm-version]: http://img.shields.io/npm/v/@request/api.svg?style=flat-square (NPM Version) | ||
@@ -64,3 +104,1 @@ [travis-ci]: https://img.shields.io/travis/request/api/master.svg?style=flat-square (Build Status) | ||
[coveralls]: https://coveralls.io/r/request/api?branch=master | ||
[request-core]: https://github.com/request/core |
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
15527
7
113
102
1
8
+ Addeddeep-copy@^1.1.2
+ Addeddeep-copy@1.4.2(transitive)