lambda-wrap
Advanced tools
Comparing version 2.0.1 to 3.0.0-alpha.1
@@ -44,2 +44,6 @@ | ||
callbackWaitsForEmptyEventLoop?: boolean | ||
log?: Logger | ||
logFields?: Object | ||
createLogMeta?: Function | ||
filterLogData?: Function | ||
} | ||
@@ -50,2 +54,3 @@ | ||
interface wrapperFunctionType { (event: ServerlessLambdaEvent, context: Object): any } | ||
interface Logger { | ||
@@ -55,6 +60,7 @@ log (...args:any[]): any | ||
warn (...args:any[]): any | ||
info (...args:any[]): any | ||
} | ||
interface responseHandlerType { (data: Object, event: ServerlessLambdaEvent, context: Object, callback: Function, logger?: Logger, options?: LambdaWrapOptions): any } | ||
interface errorResponseHandlerType { (error: Error, event: ServerlessLambdaEvent, context: Object, callback: Function, logger?: Logger, options?: LambdaWrapOptions): any } | ||
interface responseHandlerType { (data: Object, event: ServerlessLambdaEvent, context: Object, callback: Function, options?: LambdaWrapOptions): any } | ||
interface errorResponseHandlerType { (error: Error, event: ServerlessLambdaEvent, context: Object, callback: Function, options?: LambdaWrapOptions): any } | ||
@@ -67,5 +73,4 @@ interface beforeMiddlewareType { (event: ServerlessLambdaEvent, context?: Object): any } | ||
(wrapperFunction: wrapperFunctionType): lambdaHandler | ||
(wrapperFunction: wrapperFunctionType, localOptions: LambdaWrapOptions): lambdaHandler | ||
logger: Logger | ||
responseHandler: responseHandler | ||
@@ -79,2 +84,2 @@ errorResponseHandler: responseHandler | ||
export function lambdaWrap(options: LambdaWrapOptions): wrapFn; | ||
export function lambdaWrap(globalOptions: LambdaWrapOptions): wrapFn; |
'use strict'; | ||
const errorResponse = (error, event, context, callback, logger, options) => { | ||
const errorResponse = (error, event, context, callback, options) => { | ||
const headers = Object.assign({}, options.headers || {}); | ||
const { | ||
path, | ||
httpMethod, | ||
body: eventBody, | ||
headers: requestHeaders, | ||
queryStringParameters, | ||
pathParameters | ||
} = event; | ||
log = console | ||
} = options; | ||
const { status, message, stack } = error; | ||
@@ -22,24 +18,3 @@ const statusCode = status || 500; | ||
const logMethod = statusCode < 500 ? 'warn' : 'error'; | ||
const log = { | ||
status, | ||
path, | ||
httpMethod, | ||
queryStringParameters, | ||
pathParameters, | ||
stack | ||
}; | ||
if (event.verboseLog) { | ||
Object.assign(log, { | ||
eventBody, | ||
headers, | ||
requestHeaders | ||
}); | ||
} | ||
logger[logMethod](`Handler error: ${message}`, log); | ||
if (event.verboseError) { | ||
if (options.verboseError) { | ||
body.stack = stack; | ||
@@ -52,11 +27,20 @@ } | ||
const res = { | ||
let response = { | ||
statusCode, | ||
body: JSON.stringify(body), | ||
body, | ||
headers | ||
}; | ||
process.nextTick(() => callback(null, res)); | ||
const logMeta = options.createLogMeta(event, response, error); | ||
const logData = options.filterLogData(logMeta, options); | ||
log[logMeta.level](`request error: ${message}`, logData); | ||
response = Object.assign({}, response, { | ||
body: JSON.stringify(response.body) | ||
}); | ||
process.nextTick(() => callback(null, response)); | ||
}; | ||
module.exports = errorResponse; |
@@ -6,2 +6,3 @@ 'use strict'; | ||
const wrapFunction = require('./wrapFunction'); | ||
const { filterLogData, createLogMeta } = require('./logFilters'); | ||
@@ -65,9 +66,14 @@ const MIDDLEWARE = 'middleware'; | ||
* @constructor | ||
* @param {LambdaWrapOptions} [options] - Use to override or assign new attributes | ||
* @param {LambdaWrapOptions} [globalOptions] - Use to override or assign new attributes | ||
* @returns {Function} - the wrap function | ||
*/ | ||
function lambdaWrap (options = {}) { | ||
function lambdaWrap (globalOptions = {}) { | ||
const wrap = (fn) => { | ||
const wrap = (fn, overrideOptions) => { | ||
const options = Object.assign({ | ||
filterLogData, | ||
createLogMeta | ||
}, globalOptions, overrideOptions); | ||
const wrapped = { | ||
@@ -85,4 +91,3 @@ type: WRAP, | ||
_catchHandlers, | ||
_finallyHandlers, | ||
logger | ||
_finallyHandlers | ||
} = wrap; | ||
@@ -97,6 +102,4 @@ | ||
Object.assign(event, { | ||
verboseError: !!( | ||
options.verboseError || options.isOffline), // isOffline is deprecated | ||
verboseLog: !!( | ||
options.verboseLog || options.isOffline) // isOffline is deprecated | ||
verboseError: options.verboseError, | ||
verboseLog: options.verboseLog | ||
}); | ||
@@ -118,4 +121,4 @@ | ||
console.error(message, e); // eslint-disable-line no-console | ||
if (logger !== console) { | ||
logger.error(message, e); | ||
if (options.log !== console) { | ||
options.log.error(message, e); | ||
} | ||
@@ -128,4 +131,4 @@ callback(e); | ||
promisesToResolve | ||
.then(data => responseHandler(data, event, context, cb, logger, options)) | ||
.catch(err => errorResponseHandler(err, event, context, cb, logger, options)); | ||
.then(data => responseHandler(data, event, context, cb, options)) | ||
.catch(err => errorResponseHandler(err, event, context, cb, options)); | ||
}; | ||
@@ -152,8 +155,2 @@ }; | ||
/** | ||
* Override default logger object - `console`. | ||
* MUST implement `log`, `warn` and `error` methods. | ||
* */ | ||
wrap.logger = console; | ||
/** | ||
* Add new middleware. | ||
@@ -160,0 +157,0 @@ * |
@@ -9,5 +9,9 @@ 'use strict'; | ||
const response = (data, event, context, callback, logger, options) => { | ||
const response = (data, event, context, callback, options) => { | ||
const headers = Object.assign({}, options.headers || {}); | ||
const { | ||
log = console | ||
} = options; | ||
let res = data; | ||
@@ -39,2 +43,9 @@ | ||
Object.assign(res, { headers }); | ||
const logMeta = options.createLogMeta(event, res); | ||
const logData = options.filterLogData(logMeta, options); | ||
log[logMeta.level]('request', logData); | ||
if (res.body === null) { | ||
@@ -51,6 +62,2 @@ delete res.body; | ||
Object.assign(res, { headers }); | ||
logger.log(res); | ||
process.nextTick(() => callback(null, res)); | ||
@@ -57,0 +64,0 @@ }; |
@@ -6,10 +6,2 @@ /* | ||
let co; | ||
try { | ||
co = module.require('co'); | ||
} catch (e) { | ||
co = null; | ||
} | ||
/** | ||
@@ -25,7 +17,2 @@ * | ||
switch (fn.constructor.name) { | ||
case 'GeneratorFunction': | ||
if (co === null) { | ||
throw new Error('`co` must be present when using generators'); | ||
} | ||
return co.wrap(fn); | ||
case 'AsyncFunction': | ||
@@ -32,0 +19,0 @@ return fn; |
{ | ||
"name": "lambda-wrap", | ||
"version": "2.0.1", | ||
"version": "3.0.0-alpha.1", | ||
"description": "AWS Serverless wrapper for async generators", | ||
@@ -21,5 +21,2 @@ "main": "index.js", | ||
"license": "MIT", | ||
"optionalDependencies": { | ||
"co": "^4.6.0" | ||
}, | ||
"devDependencies": { | ||
@@ -26,0 +23,0 @@ "chai": "^4.1.2", |
@@ -30,7 +30,5 @@ 'use strict'; | ||
// Function to be passed to handler | ||
fn = function* () { | ||
return { | ||
message: 'test' | ||
}; | ||
}; | ||
fn = async () => ({ | ||
message: 'test' | ||
}); | ||
@@ -193,4 +191,4 @@ // Mock asynchronous function | ||
wrap.before(function* () { | ||
const result = yield doAsyncStuff('resolve', false); | ||
wrap.before(async () => { | ||
const result = await doAsyncStuff('resolve', false); | ||
@@ -237,4 +235,4 @@ if (!result) { | ||
const handler = wrap(function* () { | ||
throw new Error('Error'); | ||
const handler = wrap(async () => { | ||
throw new Error('My Error'); | ||
}); | ||
@@ -255,4 +253,4 @@ | ||
const handler = wrap(function* () { | ||
throw new Error('Error'); | ||
const handler = wrap(async () => { | ||
throw new Error('My Error'); | ||
}); | ||
@@ -273,7 +271,7 @@ | ||
wrap.catch(function* () { | ||
wrap.catch(() => { | ||
throw new Error('Catch error'); | ||
}); | ||
const handler = wrap(function* () { | ||
const handler = wrap(() => { | ||
throw new Error('Error'); | ||
@@ -293,4 +291,4 @@ }); | ||
wrap.catch(function* () { | ||
const result = yield doAsyncStuff('resolve', false); | ||
wrap.catch(async () => { | ||
const result = await doAsyncStuff('resolve', false); | ||
@@ -302,3 +300,3 @@ if (!result) { | ||
const handler = wrap(function* () { | ||
const handler = wrap(async () => { | ||
throw new Error('Error'); | ||
@@ -314,3 +312,3 @@ }); | ||
fn = function* () { | ||
fn = async () => { | ||
throw new Error('Some test error'); | ||
@@ -339,4 +337,4 @@ }; | ||
wrap.catch(function* () { | ||
const result = yield doAsyncStuff('resolve', false); | ||
wrap.catch(async () => { | ||
const result = await doAsyncStuff('resolve', false); | ||
@@ -348,4 +346,4 @@ if (!result) { | ||
wrap.catch(function* () { | ||
const result = yield doAsyncStuff('resolve', false); | ||
wrap.catch(async () => { | ||
const result = await doAsyncStuff('resolve', false); | ||
@@ -357,3 +355,3 @@ if (!result) { | ||
const handler = wrap(function* () { | ||
const handler = wrap(async () => { | ||
throw new Error('Error'); | ||
@@ -375,7 +373,5 @@ }); | ||
wrap.catch(function* () { | ||
return { statusCode: 200, body: { message: 'Some returned data' } }; | ||
}); | ||
wrap.catch(() => ({ statusCode: 200, body: { message: 'Some returned data' } })); | ||
const handler = wrap(function* () { | ||
const handler = wrap(async () => { | ||
throw new Error('Error'); | ||
@@ -382,0 +378,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35241
0
19
761
2