-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
afterHeaders now validates response is an object
tests updated
- Loading branch information
1 parent
e3e3d34
commit 187315d
Showing
2 changed files
with
168 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,53 @@ | ||
/* | ||
* Run the global and local `afterHeaders` method. | ||
*/ | ||
var when = require('when'); | ||
var _ = require('lodash'); | ||
|
||
var localOnly = require('./localOnly'); | ||
* Run the global and local `afterHeaders` method. | ||
*/ | ||
|
||
const _ = require('lodash'); | ||
const when = require('when'); | ||
|
||
const localOnly = require('./localOnly'); | ||
|
||
const AFTER_HEADERS_RETURN_ERROR = '`afterHeaders` must return an object.'; | ||
function validateResult (returnedHeaders) { | ||
if (_.isUndefined(returnedHeaders) || _.isPlainObject(returnedHeaders)) { | ||
return returnedHeaders; | ||
} | ||
if (process.env.NODE_ENV === 'development') { | ||
throw new Error(AFTER_HEADERS_RETURN_ERROR); | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.warn(AFTER_HEADERS_RETURN_ERROR); | ||
} | ||
} | ||
|
||
module.exports = function (config, error, body, params, res) { | ||
var threadneedle = this; | ||
return when.promise(function (resolve, reject) { | ||
|
||
when() | ||
|
||
// Run global promise first | ||
.then(function () { | ||
if (_.isFunction(threadneedle._globalOptions.afterHeaders) && !localOnly(config, 'afterHeaders')) { | ||
return when(threadneedle._globalOptions.afterHeaders(error, params, body, res)); | ||
} | ||
}) | ||
|
||
// Then run the local prmoise | ||
.then(function (result) { | ||
|
||
// if result returned, set headers as that. If not, | ||
// set as blank object | ||
result = ( _.isUndefined(result) ? {} : result ); | ||
|
||
if (_.isFunction(config.afterHeaders)) { | ||
return _.defaultsDeep( | ||
when(config.afterHeaders(error, params, body, res)), | ||
result | ||
); | ||
} | ||
|
||
return result; | ||
|
||
}) | ||
|
||
.then(function (result) { | ||
// if result returned, set body as that. If not, | ||
// set as blank object | ||
return ( _.isUndefined(result) ? {} : result ); | ||
}) | ||
|
||
.done(resolve, reject); | ||
|
||
const { _globalOptions } = this; | ||
|
||
//Start by executing globalAfterHeadersExec if provided and globals true | ||
const globalAfterHeadersExec = ( | ||
_.isFunction(_globalOptions.afterHeaders) && !localOnly(config, 'afterHeaders') ? | ||
_globalOptions.afterHeaders(error, params, body, res) : | ||
{} | ||
); | ||
|
||
return when(globalAfterHeadersExec) | ||
|
||
.then(validateResult) | ||
|
||
.then((globalAfterHeadersResult = {}) => { | ||
return when(( | ||
config.afterHeaders ? | ||
config.afterHeaders(error, params, body, res) : | ||
globalAfterHeadersResult | ||
)) | ||
.then(validateResult) | ||
.then((methodAfterHeadersResult = {}) => { | ||
return _.defaultsDeep( | ||
methodAfterHeadersResult, | ||
globalAfterHeadersResult | ||
); | ||
}); | ||
}); | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters