1.0.0 • Published 6 years ago
rest-utils v1.0.0
RESTful Utils
requestWrapper(uri, data, params, method)
- uri - request api url
- data - request data, for GET like request, it's query string, for POST it's request body
- params - only used for POST like request query string
- method - request method
const { url, body } = requestWrapper('/api/products/:id', { id: 1, title: 'BitCoin' }, { type: 'cash' }, 'UPDATE');
// url = /api/products/1?type=cash
// body = { title: 'BitCoin' }
request({ url, body }); // use your preferred request lib
responseWrapper(res, options)
- res - { data, statusCode }
- options - { simple: , resolveWithFullResponse } same with request-promise lib
const result = responseWrapper(res, options = {});
restWrapper(request, url, defaultOptions)
- request - wrapper of your request function
- url - restful api endpoint
- defaultOptions - your preferred request lib default options
const rest = restWrapper(request, '/api/products/:id');
/*
rest.get(data, options)
rest.query(data, options)
rest.create(data, options)
rest.update(data, options)
rest.remove(data, options)
*/
License
MIT © Lin Lou
1.0.0
6 years ago