@trayio/threadneedle
Advanced tools
Comparing version 1.1.5 to 1.2.0
@@ -8,3 +8,3 @@ /* | ||
module.exports = function (config, body, params) { | ||
module.exports = function (config, body, params, res) { | ||
var threadneedle = this; | ||
@@ -18,3 +18,3 @@ return when.promise(function (resolve, reject) { | ||
if (_.isFunction(threadneedle._globalOptions.afterFailure) && !require('./localOnly')(config, 'afterFailure')) { | ||
return when(threadneedle._globalOptions.afterFailure(body, params)); | ||
return when(threadneedle._globalOptions.afterFailure(body, params, res)); | ||
} | ||
@@ -34,3 +34,3 @@ }) | ||
if (_.isFunction(config.afterFailure)) { | ||
return when(config.afterFailure(body, params)); | ||
return when(config.afterFailure(body, params, res)); | ||
} | ||
@@ -37,0 +37,0 @@ }) |
@@ -115,66 +115,71 @@ var when = require('when'); | ||
.then(function (request) { | ||
return when.promise(function (resolve, reject) { | ||
return when.promise(function(resolve, reject) { | ||
var handleResponse = function (err, res, body) { | ||
if (err) { | ||
var handleResponse = function(err, res, body) { | ||
// Specifically handle socket hang ups nicely | ||
if (_.isError(err) && err.message === 'socket hang up') { | ||
return reject({ | ||
code: 'api_timeout', | ||
response: {}, | ||
message: 'API call timeout. Looks like the API you\'re calling is having a wobble. Please try again later.' | ||
}); | ||
} | ||
function handleReject(payload) { | ||
return reject({ | ||
payload: payload, | ||
response: res | ||
}); | ||
} | ||
else { | ||
reject(err); | ||
} | ||
if (err) { | ||
} | ||
// Specifically handle socket hang ups nicely | ||
if (_.isError(err) && err.message === 'socket hang up') { | ||
return handleReject( { | ||
code: 'api_timeout', | ||
response: | ||
{}, | ||
message: 'API call timeout. Looks like the API you\'re calling is having a wobble. Please try again later.' | ||
}); | ||
} | ||
else { | ||
else { | ||
return handleReject(err); | ||
} | ||
logger.info(methodName+': got response', res.statusCode, JSON.stringify(body)); | ||
} else { | ||
var validationError; | ||
logger.info(methodName + ': got response', res.statusCode, JSON.stringify(body)); | ||
// Validate `expects` | ||
var expects = globalize.expects.call(threadneedle, config); | ||
validationError = validateExpects(res, expects); | ||
if (validationError) { | ||
return reject(validationError); | ||
} | ||
var validationError; | ||
// Validate `notExpects` | ||
var notExpects = globalize.notExpects.call(threadneedle, config); | ||
validationError = validateNotExpects(res, notExpects); | ||
if (validationError) { | ||
return reject(validationError); | ||
} | ||
// Validate `expects` | ||
var expects = globalize.expects.call(threadneedle, config); | ||
validationError = validateExpects(res, expects); | ||
if (validationError){ | ||
return handleReject(validationError); | ||
} | ||
// We're valid! | ||
// Validate `notExpects` | ||
var notExpects = globalize.notExpects.call(threadneedle, config); | ||
validationError = validateNotExpects(res, notExpects); | ||
if (validationError){ | ||
return handleReject(validationError); | ||
} | ||
resolve({ | ||
body: body, | ||
response: res | ||
}); | ||
// We're valid! | ||
resolve({ | ||
body: body, | ||
response: res | ||
}); | ||
} | ||
}; | ||
} | ||
}; | ||
// console.log(options); | ||
// console.log(options); | ||
logger.info(methodName + ': running ' + method + ' request', request); | ||
logger.info(methodName+': running '+method+' request', request); | ||
// Run a different method for get to not include data | ||
switch (method) { | ||
case 'get': | ||
case 'head': | ||
needle[method](request.url, request.options, handleResponse); | ||
break; | ||
default: | ||
needle[method](request.url, request.data, request.options, handleResponse); | ||
} | ||
// Run a different method for get to not include data | ||
switch (method) { | ||
case 'get': | ||
case 'head': | ||
needle[method](request.url, request.options, handleResponse); | ||
break; | ||
default: | ||
needle[method](request.url, request.data, request.options, handleResponse); | ||
} | ||
}); | ||
@@ -189,3 +194,5 @@ }) | ||
logger.info(methodName+': running `afterFailure` hook', err); | ||
globalize.afterFailure.call(threadneedle, config, err, params).done(reject, reject); | ||
var payload = ( err.payload ? err.payload : err ), | ||
response = ( err.response ? err.response : {} ); | ||
globalize.afterFailure.call(threadneedle, config, payload, params, response).done(reject, reject); | ||
}); | ||
@@ -192,0 +199,0 @@ |
{ | ||
"name": "@trayio/threadneedle", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"description": "A framework for simplifying working with HTTP-based APIs.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
103123
2558