Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

rest-facade

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rest-facade - npm Package Compare versions

Comparing version 1.10.2 to 1.11.0

3

package.json
{
"name": "rest-facade",
"version": "1.10.2",
"version": "1.11.0",
"description": "Simple abstraction for consuming REST API endpoints",

@@ -33,2 +33,3 @@ "main": "src/index.js",

"coveralls": "^2.13.3",
"istanbul": "^0.4.5",
"mocha": "^2.3.3",

@@ -35,0 +36,0 @@ "mocha-lcov-reporter": "^1.0.0",

@@ -273,2 +273,15 @@ # rest-facade [![Build Status](https://travis-ci.org/ngonzalvez/rest-facade.svg?branch=master)](https://travis-ci.org/ngonzalvez/rest-facade)

Request customizer functions may be asynchronous. If you require an asynchronous customizer function, use a callback. For example:
```js
customizer(req, params, cb) {
someAsynchronousFunction
.then((resultOfYourFunction) => {
req.set('Authorization', `Bearer ${resultOfYourFunction}`);
return cb();
})
.catch(err => cb(err));
},
```
### Proxy support

@@ -275,0 +288,0 @@

@@ -29,3 +29,3 @@ var extend = require('util')._extend;

this.options = deepmerge(defaultOptions, options || {});
this.options = deepmerge(defaultOptions, options || {}, { clone: false });

@@ -335,44 +335,75 @@ this.url = url.parse(resourceUrl);

// Run request customizer (from constructor options)
if (isFunction(reqCustomizer)) {
reqCustomizer(req, params);
if (reqCustomizer.length === 3) { // check if callback has been defined
reqCustomizer(req, params, runParamsRequestCustomizer);
} else {
// if no callback (run synchronously)
reqCustomizer(req, params);
runParamsRequestCustomizer();
}
} else {
runParamsRequestCustomizer();
}
if (isFunction(params._requestCustomizer)) {
params._requestCustomizer(req, params);
// Run request customizer (from request params)
function runParamsRequestCustomizer(err) {
if (err) {
return reject(err);
}
if (isFunction(params._requestCustomizer)) {
if (params._requestCustomizer.length === 3) { // check if callback has been defined
params._requestCustomizer(req, params, sendRequest);
} else {
// if no callback (run synchronously)
params._requestCustomizer(req, params);
sendRequest();
}
} else {
sendRequest();
}
}
// Send the request.
req
.set('Accept', 'application/json')
.end(function (err, res) {
if (err) {
var reqinfo = { method : method, url : options.url };
var response = err.response || {};
var data = response.body;
var status = err.status;
var error;
// send the request
function sendRequest(err) {
if (err) {
return reject(err);
}
var name = resolveAPIErrorArg(errorFormatter.name, data, 'APIError');
var message = resolveAPIErrorArg(errorFormatter.message, data, [data, err.message]);
// Send the request.
req
.set('Accept', 'application/json')
.end(function (err, res) {
if (err) {
var reqinfo = { method : method, url : options.url };
var response = err.response || {};
var data = response.body;
var status = err.status;
var error;
var name = resolveAPIErrorArg(errorFormatter.name, data, 'APIError');
var message = resolveAPIErrorArg(errorFormatter.message, data, [data, err.message]);
return reject(new APIError(name, message, status, reqinfo, err))
}
return reject(new APIError(name, message, status, reqinfo, err))
}
// If case conversion is enabled for the body of the response, convert
// the properties of the body to the specified case.
if (convertCaseRes) {
for (var key in res.body) {
if (res.body.hasOwnProperty(key)) {
res.body[convertCaseRes(key)] = res.body[key];
// If case conversion is enabled for the body of the response, convert
// the properties of the body to the specified case.
if (convertCaseRes) {
for (var key in res.body) {
if (res.body.hasOwnProperty(key)) {
res.body[convertCaseRes(key)] = res.body[key];
if (key !== convertCaseRes(key)) {
delete res.body[key];
if (key !== convertCaseRes(key)) {
delete res.body[key];
}
}
}
}
}
resolve(res.body, res.header);
});
});
resolve(res.body, res.header);
});
}
});

@@ -379,0 +410,0 @@ if (!callback) return promise;

@@ -645,2 +645,24 @@ var extend = require('util')._extend;

'should allow for an asynchronous request customizer, that is called for each request, in constructor options':
function (done) {
var customizerAsync = function (req, params, cb) {
setTimeout(function(){
req.send({alias: 'Jane Doe'});
return cb(null, req)
}, 10);
}
var options = { request: { customizer : customizerAsync }};
var expected = { first_name: 'Kevin', last_name: 'Spacey', alias: 'Jane Doe' };
var client = this.client = new Client(domain + endpoint, options);
var request = nock(domain).post(endpoint, expected).reply(200);
client.create({ first_name: 'Kevin', last_name: 'Spacey' }, function (err) {
expect(request.isDone()).to.be.true;
done();
});
},
'should call a request customizer function given in params':

@@ -662,2 +684,25 @@ function (done) {

'should call an asynchronous request customizer function given in params':
function (done) {
var customizerAsync = function (req, params, cb) {
setTimeout(function(){
req.set('X-Fake', '1');
return cb(null, req)
}, 10);
}
var expected = { first_name: 'John', last_name: 'Doe' };
var client = this.client = new Client(domain + endpoint);
var request = nock(domain, { reqheaders : {
'X-Fake': '1'
} }).post(endpoint, expected).reply(200);
var reqfn = function(req, params) { req.set('X-Fake', '1'); };
client.create({_requestCustomizer : customizerAsync}, { first_name: 'John', last_name: 'Doe' }, function (err) {
expect(request.isDone()).to.be.true;
done();
});
},
'should use the proxy defined in the global settings':

@@ -664,0 +709,0 @@ function (done) {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc