@trayio/threadneedle
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -12,11 +12,13 @@ /* | ||
var subbedGlobalObject = substitute(this._globalOptions[key], params || {}); | ||
var subbedObject = substitute(config[key] || {}, params || {}); | ||
params = params || {}; | ||
if (config.globals === false || !_.isObject(subbedObject)) { | ||
return subbedObject; | ||
} else { | ||
return _.defaultsDeep(subbedObject, subbedGlobalObject); | ||
} | ||
var subbedGlobalObject = substitute(this._globalOptions[key], params); | ||
var subbedObject = substitute(config[key] || {}, params); | ||
if (config.globals === false) { | ||
return subbedObject; | ||
} else { | ||
return _.defaultsDeep(subbedObject, subbedGlobalObject); | ||
} | ||
}; |
@@ -7,39 +7,54 @@ // Normalise the expects input, converting from shorthand if needed. | ||
// Function - allow for manual function call to run | ||
if (_.isFunction(input)) { | ||
return input; | ||
} | ||
// Function - allow for manual function call to run | ||
if (_.isFunction(input)) { | ||
return input; | ||
} | ||
var expects = {}; | ||
var expects = {}; | ||
// Shorthand: list of status codes | ||
if (_.isArray(input) && input.length && _.isNumber(input[0])) { | ||
expects.statusCode = input; | ||
} | ||
function shorthandStatusCode(inputStatusCode) { | ||
if (inputStatusCode === '20x' || inputStatusCode === '2xx') { | ||
expects.statusCode = ( inputStatusCode === '20x' ? new Array(10) : new Array(100) ); | ||
expects.statusCode = _.map(expects.statusCode, function (val, key) { return key + 200; }); | ||
return true; | ||
} | ||
return false; | ||
} | ||
// Shorthand: individual status code | ||
else if (_.isNumber(input)) { | ||
expects.statusCode = [input]; | ||
} | ||
// Shorthand: list of status codes | ||
if (_.isArray(input) && input.length && _.isNumber(input[0])) { | ||
expects.statusCode = input; | ||
} | ||
// Shorthand: list of bodies | ||
else if (_.isArray(input) && input.length && _.isString(input[0])) { | ||
expects.body = input; | ||
} | ||
// Shorthand: individual status code | ||
else if (_.isNumber(input)) { | ||
expects.statusCode = [input]; | ||
} | ||
// Shorthand: individual body string | ||
else if (_.isString(input)) { | ||
expects.body = [input]; | ||
} | ||
// Shorthand: list of bodies | ||
else if (_.isArray(input) && input.length && _.isString(input[0])) { | ||
expects.body = input; | ||
} | ||
// Longhand - ensuring, that fields are arrays | ||
else if (_.isObject(input)) { | ||
_.each(_.pick(input, ['statusCode', 'body']), function (value, key) { | ||
expects[key] = (_.isArray(value)) ? value : [value]; | ||
}); | ||
} | ||
// Shorthand: individual body string unless it's '20x' or '2xx' | ||
else if (_.isString(input)) { | ||
if (!shorthandStatusCode(input)) | ||
expects.body = [input]; | ||
} | ||
return expects; | ||
// Longhand - ensuring, that fields are arrays | ||
else if (_.isObject(input)) { | ||
expects = _.pick(input, ['statusCode', 'body']); | ||
shorthandStatusCode(expects.statusCode); | ||
_.each(expects, function (value, key) { | ||
expects[key] = ( _.isArray(value) ? value : [value] ); | ||
}); | ||
} | ||
return expects; | ||
}; | ||
var Mustache = require('mustache'); | ||
var _ = require('lodash'); | ||
var trim = require('mout/string/trim'); | ||
module.exports = evaluateAndProcess; | ||
module.exports = function (template, params, utils) { | ||
function evaluateAndProcess (template, params) { | ||
if (_.isFunction(template)) { | ||
return substituteFunction(template, params, utils); | ||
} | ||
if (_.isFunction(template)) return template.call(null, params); | ||
if (_.isString(template)) { | ||
return substituteString(template, params, utils); | ||
} | ||
if (_.isString(template)) return substituteString(template, params); | ||
else if (_.isArray(template)) { | ||
return substituteArray(template, params, utils); | ||
} | ||
if (_.isArray(template)) return substituteArray(template, params); | ||
else if (_.isObject(template)) { | ||
return substituteObject(template, params, utils); | ||
} | ||
if (_.isObject(template)) return substituteObject(template, params); | ||
}; | ||
return template; | ||
function substituteFunction(template, params, utils) { | ||
return template.call(null, params, utils); | ||
} | ||
function substituteString(template, params, utils) { | ||
function substituteString(template, params) { | ||
// Smart substitution. If there's a single variable that's been substituted | ||
// into a field (which is what happens most of the time), then extract the key | ||
// and return the real value. | ||
var openingBrackets = template.match(/{{/g); | ||
var closingBrackets = template.match(/}}/g); | ||
if (_.isArray(openingBrackets) && openingBrackets.length === 1 && | ||
_.isArray(closingBrackets) && closingBrackets.length === 1) { | ||
var key = trim(template.replace(/({{)|(}})/g, '')); | ||
if (!_.isUndefined(params[key]) && params[key] !== '') { | ||
return params[key]; | ||
// Smart substitution. If there's a single variable that's been substituted | ||
// into a field (which is what happens most of the time), then extract the key | ||
// and return the real value. | ||
if (template.match(/^{{([^{}]+)}}$/g)) { | ||
var key = _.trim(template.match(/(?!{{)([^{}]+)(?=}})/g)[0]); | ||
if (!_.isUndefined(params[key]) && params[key] !== '') | ||
return params[key]; | ||
} | ||
} | ||
// If the above isn't the case, then template it with Mustache. | ||
var str = Mustache.render(template, params); | ||
// If the above isn't the case, then template it with Mustache. | ||
var str = Mustache.render(template, params); | ||
return (str === '') ? undefined : str; | ||
return (str === '') ? undefined : str; | ||
} | ||
function substituteArray(template, params, utils) { | ||
return _.map(template, function (value) { | ||
if (_.isString(value)) { | ||
return substituteString(value, params, utils); | ||
} else if (_.isObject(value)) { | ||
return substituteObject(value, params, utils); | ||
} else { | ||
return value; | ||
} | ||
}); | ||
function substituteArray(template, params) { | ||
return _.map(template, function (value) { | ||
return evaluateAndProcess(value, params); | ||
}); | ||
} | ||
function substituteObject(template, params, utils) { | ||
var output = {}; | ||
_.each(template, function (value, key) { | ||
// console.log(value, key); | ||
if (_.isFunction(value)) { | ||
output[key] = substituteFunction(value, params, utils); | ||
} else if (_.isString(value)) { | ||
output[key] = substituteString(value, params, utils); | ||
} else if (_.isObject(value)) { | ||
output[key] = substituteObject(value, params, utils); | ||
} else { | ||
output[key] = value; | ||
} | ||
}); | ||
return output; | ||
function substituteObject(template, params) { | ||
//The `accumulator` variable is the output object being built | ||
return _.reduce(template, function (accumulator, value, key) { | ||
accumulator[key] = evaluateAndProcess(value, params); | ||
return accumulator; | ||
}, {}); | ||
} |
@@ -75,20 +75,3 @@ /* | ||
var niceErrors = { | ||
400: { | ||
code: 'bad_request', | ||
message: 'Bad API request. Try checking your input properties.' | ||
}, | ||
401: { | ||
code: 'unauthorized', | ||
message: 'Unauthorized request. Have you added your API details correctly?' | ||
}, | ||
403: { | ||
code: 'forbidden', | ||
message: 'Forbidden. Check you have the appropriate permissions to access this resource.' | ||
}, | ||
404: { | ||
code: 'not_found', | ||
message: 'Not found. Looks like this has been removed.' | ||
} | ||
}; | ||
var niceErrors = require('./errorCodes.js'); | ||
@@ -95,0 +78,0 @@ var statusError = { |
{ | ||
"name": "@trayio/threadneedle", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "A framework for simplifying working with HTTP-based APIs.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -40,2 +40,10 @@ var assert = require('assert'); | ||
it('should be ok for shorthand statusCode string 20x', function () { | ||
assert.deepEqual(normalizeExpects('20x'), { statusCode: [200, 201, 202, 203, 204, 205, 206, 207, 208, 209] }); | ||
}); | ||
it('should be ok for shorthand statusCode string 2xx', function () { | ||
assert.deepEqual(normalizeExpects('2xx'), { statusCode: [200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299] }); | ||
}); | ||
it('should be ok for shorthand status code list', function () { | ||
@@ -50,2 +58,2 @@ assert.deepEqual(normalizeExpects(['chris', 'steph']), { body: ['chris', 'steph'] }); | ||
}); | ||
}); |
@@ -101,2 +101,11 @@ var assert = require('assert'); | ||
it('non-templates should remain as they are', function () { | ||
var data = { x: '{{x}}', y: ['123'], z: '{{z}}' }; | ||
var output = substitute(data, { | ||
x: 'hello', | ||
z: 123 | ||
}); | ||
assert.deepEqual(output, { x: 'hello', y: ['123'], z: 123 }); | ||
}); | ||
it('should substitute into function templates', function () { | ||
@@ -103,0 +112,0 @@ var url = function (params) { |
99907
36
2451