Skip to content

Commit

Permalink
Merge pull request #74 from trayio/CPS-206/f/threadneedle-1-12-0-release
Browse files Browse the repository at this point in the history
CPS-206: Threadneedle 1.12.0 release
  • Loading branch information
johnbastian-trayio authored Jun 8, 2020
2 parents 0dcbe07 + 54382e1 commit b7ffb8d
Show file tree
Hide file tree
Showing 7 changed files with 1,404 additions and 371 deletions.
72 changes: 34 additions & 38 deletions lib/addMethod/addMethodFunction.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var _ = require('lodash');
var when = require('when');
var logger = require('../logger');
const _ = require('lodash');
const when = require('when');
const logger = require('../logger');

var globalize = require('./globalize');
const globalize = require('./globalize');

//The format of the response should always be an object with headers and body
var formatResponse = require('./formatResponse');
const formatResponse = require('./formatResponse');

module.exports = function (threadneedle, config, afterHeadersFunction, params) {

var afterHeadersConfig = {};
const afterHeadersConfig = {};
if (_.isFunction(afterHeadersFunction)) {
afterHeadersConfig.afterHeaders = afterHeadersFunction;
} else {
Expand All @@ -18,38 +18,34 @@ module.exports = function (threadneedle, config, afterHeadersFunction, params) {
}
}

return when.promise(function(resolve, reject) {
when(config.call(threadneedle, params))
.done(
function(body) {

globalize.afterHeaders.call(threadneedle, afterHeadersConfig, null, body, params, null)

.done(
function(headers) {
resolve(formatResponse(headers, body));
},
function(afterHeadersError) {
reject(formatResponse({}, afterHeadersError));
}
);

},
function(functionError) {

globalize.afterHeaders.call(threadneedle, afterHeadersConfig, functionError, null, params, null)

.done(
function(headers) {
reject(formatResponse(headers, functionError));
},
function(afterHeadersError) {
reject(formatResponse({}, afterHeadersError || functionError));
}
);

return when.promise((resolve, reject) => {
new Promise((resolve, reject) => {
try {
resolve(config.call(threadneedle, params));
} catch (functionCallError) {
reject(functionCallError);
}
);
})
.then((body) => {
globalize.afterHeaders.call(threadneedle, afterHeadersConfig, null, body, params, null)

.then((headers) => {
resolve(formatResponse(headers, body));
})
.catch((afterHeadersError) => {
reject(formatResponse({}, afterHeadersError));
});
})
.catch((functionError) => {
globalize.afterHeaders.call(threadneedle, afterHeadersConfig, functionError, null, params, null)

.then((headers) => {
reject(formatResponse(headers, functionError));
})
.catch((afterHeadersError) => {
reject(formatResponse({}, afterHeadersError || functionError));
});
});
});

};
};
27 changes: 15 additions & 12 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
var winston = require('winston');
const winston = require('winston');

const logger = new winston.Logger({});

var logger = new (winston.Logger)({});
const enableLogs = process.env.NODE_ENV === 'development' ||
!!process.env.THREADNEEDLE_ENABLE_LOGS;

// Log into the console
logger.add(winston.transports.Console, {
colorize: true,
level: (process.env.NODE_ENV === 'development') ? 'info' : 'warning'
level: enableLogs
? 'info'
: 'warning'
});

logger.setLevels({
emerg: 0,
alert: 1,
crit: 2,
error: 3, // tweaked from default
warning: 4,
notice: 5,
info: 6,
debug: 7 // moved down
emerg: 0,
alert: 1,
crit: 2,
error: 3, // tweaked from default
warning: 4,
notice: 5,
info: 6,
debug: 7 // moved down
});

module.exports = logger;
Loading

0 comments on commit b7ffb8d

Please sign in to comment.