New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@trayio/threadneedle

Package Overview
Dependencies
Maintainers
1
Versions
67
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@trayio/threadneedle - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

tests/globalize_test.js

8

lib/addMethod/globalize/afterFailure.js

@@ -8,3 +8,3 @@ /*

module.exports = function (afterFailure, err) {
module.exports = function (config, err) {
var threadneedle = this;

@@ -17,3 +17,3 @@ return when.promise(function (resolve, reject) {

.then(function () {
if (_.isFunction(threadneedle._globalOptions.afterFailure)) {
if (_.isFunction(threadneedle._globalOptions.afterFailure) && config.globals !== false) {
return when(threadneedle._globalOptions.afterFailure(err));

@@ -25,4 +25,4 @@ }

.then(function () {
if (_.isFunction(afterFailure)) {
return when(afterFailure(err));
if (_.isFunction(config.afterFailure)) {
return when(config.afterFailure(err));
}

@@ -29,0 +29,0 @@ })

@@ -8,3 +8,3 @@ /*

module.exports = function (afterSuccess, body) {
module.exports = function (config, body) {
var threadneedle = this;

@@ -17,3 +17,3 @@ return when.promise(function (resolve, reject) {

.then(function () {
if (_.isFunction(threadneedle._globalOptions.afterSuccess)) {
if (_.isFunction(threadneedle._globalOptions.afterSuccess) && config.globals !== false) {
return when(threadneedle._globalOptions.afterSuccess(body));

@@ -25,4 +25,4 @@ }

.then(function () {
if (_.isFunction(afterSuccess)) {
return when(afterSuccess(body));
if (_.isFunction(config.afterSuccess)) {
return when(config.afterSuccess(body));
}

@@ -29,0 +29,0 @@ })

@@ -12,3 +12,3 @@ /*

module.exports = function (before, params) {
module.exports = function (config, params) {
var threadneedle = this;

@@ -21,3 +21,3 @@ return when.promise(function (resolve, reject) {

.then(function () {
if (_.isFunction(threadneedle._globalOptions.before)) {
if (_.isFunction(threadneedle._globalOptions.before) && config.globals !== false) {
return when(threadneedle._globalOptions.before(params));

@@ -29,4 +29,4 @@ }

.then(function () {
if (_.isFunction(before)) {
return when(before(params));
if (_.isFunction(config.before)) {
return when(config.before(params));
}

@@ -33,0 +33,0 @@ })

@@ -8,3 +8,3 @@ /*

module.exports = function (beforeRequest, request) {
module.exports = function (config, request) {
var threadneedle = this;

@@ -24,4 +24,4 @@ return when.promise(function (resolve, reject) {

.then(function () {
if (_.isFunction(beforeRequest)) {
return when(beforeRequest(request));
if (_.isFunction(config.beforeRequest)) {
return when(config.beforeRequest(request));
}

@@ -28,0 +28,0 @@ })

@@ -10,9 +10,13 @@ /*

module.exports = function (expects) {
module.exports = function (config) {
var globalExpects = normalizeExpects(this._globalOptions.expects || {});
var localExpects = normalizeExpects(expects || {});
var localExpects = normalizeExpects(config.expects || {});
return _.defaults(localExpects, globalExpects);
if (config.globals === false) {
return localExpects;
} else {
return _.defaults(localExpects, globalExpects);
}
};

@@ -10,9 +10,13 @@ /*

module.exports = function (notExpects) {
module.exports = function (config) {
var globalNotExpects = normalizeExpects(this._globalOptions.notExpects || {});
var localNotExpects = normalizeExpects(notExpects || {});
var localNotExpects = normalizeExpects(config.notExpects || {});
return _.defaults(localNotExpects, globalNotExpects);
if (config.globals === false) {
return localNotExpects;
} else {
return _.defaults(localNotExpects, globalNotExpects);
}
};

@@ -10,9 +10,13 @@ /*

module.exports = function (key, object, params) {
module.exports = function (key, config, params) {
var subbedGlobalObject = substitute(this._globalOptions[key], params || {});
var subbedObject = substitute(object || {}, params || {});
var subbedObject = substitute(config[key] || {}, params || {});
return _.defaultsDeep(subbedObject, subbedGlobalObject);
if (config.globals === false) {
return subbedObject;
} else {
return _.defaultsDeep(subbedObject, subbedGlobalObject);
}
};

@@ -10,6 +10,6 @@ /*

module.exports = function (url, params) {
module.exports = function (config, params) {
var subbedGlobalUrl = substitute(this._globalOptions.url, params);
var subbedUrl = substitute(url, params);
var subbedUrl = substitute(config.url, params);

@@ -19,3 +19,4 @@ // Add the URL

!startsWith(subbedUrl, 'http://') &&
!startsWith(subbedUrl, 'https://')) {
!startsWith(subbedUrl, 'https://') &&
config.globals !== false) {
return subbedGlobalUrl + subbedUrl;

@@ -22,0 +23,0 @@ }

@@ -49,3 +49,3 @@ var when = require('when');

.then(function () {
return globalize.before.call(threadneedle, config.before, params);
return globalize.before.call(threadneedle, config, params);
})

@@ -57,7 +57,8 @@

// The URL endpoint. Query parameters allowed from here.
var url = globalize.url.call(threadneedle, config.url, params);
var url = globalize.url.call(threadneedle, config, params);
// var url = globalize.call(threadneedle, config.globals, 'url', config, params);
// Add query parameters intelligently, without conflicting from query parameters
// already specified in the URL.
var query = globalize.object.call(threadneedle, 'query', config.query, params);
var query = globalize.object.call(threadneedle, 'query', config, params);
_.each(query, function (value, key) {

@@ -68,3 +69,3 @@ url = setParam(url, value);

// The request options, substituted
var options = globalize.object.call(threadneedle, 'options', config.options, params);
var options = globalize.object.call(threadneedle, 'options', config, params);

@@ -74,3 +75,3 @@ // Post/put/delete data

if (method !== 'get') {
data = globalize.object.call(threadneedle, 'data', config.data, params);
data = globalize.object.call(threadneedle, 'data', config, params);
}

@@ -87,3 +88,3 @@

.then(function (request) {
return globalize.beforeRequest.call(threadneedle, config.beforeRequest, request);
return globalize.beforeRequest.call(threadneedle, config, request);
})

@@ -105,3 +106,3 @@

// Validate `expects`
var expects = globalize.expects.call(threadneedle, config.expects);
var expects = globalize.expects.call(threadneedle, config);
validationError = validateExpects(res, expects);

@@ -113,3 +114,3 @@ if (validationError) {

// Validate `notExpects`
var notExpects = globalize.notExpects.call(threadneedle, config.notExpects);
var notExpects = globalize.notExpects.call(threadneedle, config);
validationError = validateNotExpects(res, notExpects);

@@ -140,15 +141,5 @@ if (validationError) {

.done(function (body) {
globalize.afterSuccess.call(threadneedle, config.afterSuccess, body).done(resolve, reject);
// if (_.isFunction(config.afterSuccess)) {
// when(config.afterSuccess(body)).done(resolve, reject); // TODO right way to handle rejections?
// } else {
// resolve(body);
// }
globalize.afterSuccess.call(threadneedle, config, body).done(resolve, reject);
}, function (err) {
globalize.afterFailure.call(threadneedle, config.afterFailure, err).done(reject, reject);
// if (_.isFunction(config.afterFailure)) {
// when(config.afterFailure(err)).done(reject, reject); // TODO right way to handle rejections?
// } else {
// reject(err);
// }
globalize.afterFailure.call(threadneedle, config, err).done(reject, reject);
});

@@ -155,0 +146,0 @@

{
"name": "@trayio/threadneedle",
"version": "1.0.2",
"version": "1.0.3",
"description": "A framework for simplifying working with HTTP-based APIs.",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -546,429 +546,4 @@ var assert = require('assert');

describe('#globalize', function () {
describe('#url', function () {
it('should add the global url on the front unless it starts with http(s)://', function () {
var sample = {
_globalOptions: {
url: 'http://mydomain.com'
}
};
assert.strictEqual(
globalize.url.call(sample, '/mypath', {}),
'http://mydomain.com/mypath'
);
assert.strictEqual(
globalize.url.call(sample, 'http://yourdomain.com/mypath', {}),
'http://yourdomain.com/mypath'
);
assert.strictEqual(
globalize.url.call(sample, 'https://yourdomain.com/mypath', {}),
'https://yourdomain.com/mypath'
);
});
it('should substitute parameters to string urls', function () {
var sample = {
_globalOptions: {
url: 'http://{{dc}}.mydomain.com'
}
};
assert.strictEqual(
globalize.url.call(sample, '/mypath/{{id}}', {
dc: 'us5',
id: '123'
}),
'http://us5.mydomain.com/mypath/123'
);
});
it('should substitute parameters to function urls', function () {
var sample = {
_globalOptions: {
url: function (params) {
return 'http://'+params.dc+'.mydomain.com';
}
}
};
assert.strictEqual(
globalize.url.call(sample, function(params) {
return '/mypath/' + params.id;
}, {
dc: 'us5',
id: '123'
}),
'http://us5.mydomain.com/mypath/123'
);
});
});
describe('#object', function () {
it('should globalize to an object on a shallow level', function () {
var sample = {
_globalOptions: {
data: {
id: '123',
name: 'Chris'
}
}
};
assert.deepEqual(
globalize.object.call(sample, 'data', {
age: 25,
height: 180
}, {}), {
id: '123',
name: 'Chris',
age: 25,
height: 180
}
);
});
it('should globalize to an object on a deep level', function () {
var sample = {
_globalOptions: {
data: {
id: '123',
name: 'Chris',
height: {
m: 1.9
}
}
}
};
assert.deepEqual(
globalize.object.call(sample, 'data', {
age: 25,
height: {
cm: 180,
m: 1.8
}
}, {}), {
id: '123',
name: 'Chris',
age: 25,
height: {
cm: 180,
m: 1.8
}
}
);
});
it('should substitute to an global object on a deep level', function () {
var sample = {
_globalOptions: {
data: {
id: '123',
firstName: '{{firstName}}',
lastName: '{{lastName}}'
}
}
};
assert.deepEqual(
globalize.object.call(sample, 'data', {
name: '{{name}}'
}, {
name: 'Chris Houghton',
firstName: 'Chris',
lastName: 'Houghton'
}), {
id: '123',
name: 'Chris Houghton',
firstName: 'Chris',
lastName: 'Houghton'
}
);
});
});
describe('#before', function () {
it('should run the global before method when declared', function (done) {
var sample = {
_globalOptions: {
before: function (params) {
params.dc = 'us5';
}
}
};
globalize.before.call(sample, undefined, {
url: '/mydomain'
}).done(function (params) {
assert.deepEqual(params, { url: '/mydomain', dc: 'us5' });
done();
});
});
it('should allow for a global promise async', function (done) {
var sample = {
_globalOptions: {
before: function (params) {
return when.promise(function (resolve, reject) {
params.dc = 'us5';
resolve();
});
}
}
};
globalize.before.call(sample, undefined, {
url: '/mydomain'
}).done(function (params) {
assert.deepEqual(params, { url: '/mydomain', dc: 'us5' });
done();
});
});
it('should call the global promise before the local one', function (done) {
var calledFirst;
var calls = 0;
var sample = {
_globalOptions: {
before: function (params) {
if (!calledFirst) calledFirst = 'global';
calls++;
}
}
};
globalize.before.call(sample, function () {
if (!calledFirst) calledFirst = 'local';
calls++
}, {}).done(function (params) {
assert.equal(calledFirst, 'global');
assert.equal(calls, 2);
done();
});
});
});
describe('#expects', function () {
it('should set the expects object when specified in global', function () {
var sample = {
_globalOptions: {
expects: 200
}
};
assert.deepEqual(globalize.expects.call(sample), { statusCode: [200] });
var sample = {
_globalOptions: {
expects: {
statusCode: [200, 201],
body: 'chris'
}
}
};
assert.deepEqual(globalize.expects.call(sample), {
statusCode: [200, 201],
body: ['chris']
});
});
it('should be overridden by the local config', function () {
var sample = {
_globalOptions: {
expects: 200
}
};
assert.deepEqual(globalize.expects.call(sample, {
statusCode: 201
}), {
statusCode: [201]
});
assert.deepEqual(globalize.expects.call(sample, 202), {
statusCode: [202]
});
});
});
describe('#notExpects', function () {
it('should set the expects object when specified in global', function () {
var sample = {
_globalOptions: {
notExpects: 200
}
};
assert.deepEqual(globalize.notExpects.call(sample), { statusCode: [200] });
var sample = {
_globalOptions: {
notExpects: {
statusCode: [200, 201],
body: 'chris'
}
}
};
assert.deepEqual(globalize.notExpects.call(sample), {
statusCode: [200, 201],
body: ['chris']
});
});
it('should be overridden by the local config', function () {
var sample = {
_globalOptions: {
notExpects: 200
}
};
assert.deepEqual(globalize.notExpects.call(sample, {
statusCode: 201
}), {
statusCode: [201]
});
assert.deepEqual(globalize.notExpects.call(sample, 202), {
statusCode: [202]
});
});
});
describe('#afterSuccess', function () {
it('should run the global before method when declared', function (done) {
var sample = {
_globalOptions: {
afterSuccess: function (body) {
body.success = true;
}
}
};
globalize.afterSuccess.call(sample, undefined, {}).done(function (body) {
assert.deepEqual(body, { success: true });
done();
});
});
it('should allow for a global promise async', function (done) {
var sample = {
_globalOptions: {
afterSuccess: function (body) {
return when.promise(function (resolve, reject) {
body.success = true;
resolve();
});
}
}
};
globalize.afterSuccess.call(sample, undefined, {}).done(function (body) {
assert.deepEqual(body, { success: true });
done();
});
});
it('should call the global promise before the local one', function (done) {
var calledFirst;
var calls = 0;
var sample = {
_globalOptions: {
afterSuccess: function (params) {
if (!calledFirst) calledFirst = 'global';
calls++;
}
}
};
globalize.afterSuccess.call(sample, function () {
if (!calledFirst) calledFirst = 'local';
calls++
}, {}).done(function (params) {
assert.equal(calledFirst, 'global');
assert.equal(calls, 2);
done();
});
});
});
describe('#afterFailure', function () {
it('should run the global before method when declared', function (done) {
var sample = {
_globalOptions: {
afterFailure: function (err) {
err.code = 'oauth_refresh';
}
}
};
globalize.afterFailure.call(sample, undefined, {}).done(function (err) {
assert.deepEqual(err, { code: 'oauth_refresh' });
done();
});
});
it('should allow for a global promise async', function (done) {
var sample = {
_globalOptions: {
afterFailure: function (err) {
return when.promise(function (resolve, reject) {
err.code = 'oauth_refresh';
resolve();
});
}
}
};
globalize.afterFailure.call(sample, undefined, {}).done(function (err) {
assert.deepEqual(err, { code: 'oauth_refresh' });
done();
});
});
it('should call the global promise before the local one', function (done) {
var calledFirst;
var calls = 0;
var sample = {
_globalOptions: {
afterFailure: function () {
if (!calledFirst) calledFirst = 'global';
calls++;
}
}
};
globalize.afterFailure.call(sample, function () {
if (!calledFirst) calledFirst = 'local';
calls++
}, {}).done(function () {
assert.equal(calledFirst, 'global');
assert.equal(calls, 2);
done();
});
});
});
});
});

@@ -975,0 +550,0 @@

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