aws-core-utils
Advanced tools
Comparing version 7.1.1 to 7.2.0
@@ -139,3 +139,3 @@ 'use strict'; | ||
succeedLambdaCallback(callback, response, context); | ||
succeedLambdaCallback(callback, response, event, context); | ||
}) | ||
@@ -229,2 +229,8 @@ .catch(err => { | ||
} | ||
if (from.preSuccessCallback && !to.preSuccessCallback) { | ||
to.preSuccessCallback = from.preSuccessCallback; | ||
} | ||
if (from.preFailureCallback && !to.preFailureCallback) { | ||
to.preFailureCallback = from.preFailureCallback; | ||
} | ||
} | ||
@@ -235,2 +241,35 @@ return to; | ||
/** | ||
* Executes the custom configured `preSuccessCallback` function (if any). | ||
* @param {Object} response - a normal or Lambda Proxy integration response to be returned | ||
* @param {AWSEvent} event - the AWS event passed to your handler | ||
* @param {StandardHandlerContext} context - the context to use | ||
* @param {PreSuccessCallback|undefined} [context.handler.preSuccessCallback] - an optional function to be used by an | ||
* AWS Lambda `handler` to run any needed shutdown logic immediately before succeeding the Lambda callback | ||
* @return {Promise.<*>} a promise of anything - any errors are logged, but no rejections can escape | ||
*/ | ||
function executePreSuccessCallback(response, event, context) { | ||
const preSuccessCallback = context.handler && context.handler.preSuccessCallback; | ||
return typeof preSuccessCallback === 'function' ? | ||
Promises.try(() => preSuccessCallback(response, event, context)).catch(err => context.error(err)) : | ||
Promise.resolve(); | ||
} | ||
/** | ||
* Executes the custom configured `preSuccessCallback` function (if any). | ||
* @param {AppError} error - the error with which your Lambda was failed | ||
* @param {Object} errorResponse - the error response derived from the error with which your Lambda was failed | ||
* @param {AWSEvent} event - the AWS event passed to your handler | ||
* @param {StandardHandlerContext} context - the context being used | ||
* @param {PreFailureCallback|undefined} [context.handler.preFailureCallback] - an optional function to be used by an | ||
* AWS Lambda `handler` to run any needed shutdown logic immediately before failing the Lambda callback | ||
* @return {Promise.<*>} a promise of anything - any errors are logged, but no rejections can escape | ||
*/ | ||
function executePreFailureCallback(error, errorResponse, event, context) { | ||
const preFailureCallback = context.handler && context.handler.preFailureCallback; | ||
return typeof preFailureCallback === 'function' ? | ||
Promises.try(() => preFailureCallback(error, errorResponse, event, context)).catch(err => context.error(err)) : | ||
Promise.resolve(); | ||
} | ||
/** | ||
* Succeeds the given callback of an API Gateway exposed AWS Lambda, by invoking the given callback with the given | ||
@@ -240,5 +279,6 @@ * response. | ||
* @param {Object} response - a normal or Lambda Proxy integration response to be returned | ||
* @param {AWSEvent} event - the AWS event passed to your handler | ||
* @param {StandardHandlerContext} context - the context to use | ||
*/ | ||
function succeedLambdaCallback(callback, response, context) { | ||
function succeedLambdaCallback(callback, response, event, context) { | ||
if (context.handler && context.handler.useLambdaProxy) { | ||
@@ -248,5 +288,7 @@ const statusCode = response && isNotBlank(response.statusCode) ? response.statusCode : 200; | ||
const proxyResponse = toLambdaProxyResponse(statusCode, response && response.headers, body, context.handler.defaultHeaders); | ||
callback(null, proxyResponse); | ||
executePreSuccessCallback(proxyResponse, event, context) | ||
.then(() => callback(null, proxyResponse)); | ||
} else { | ||
callback(null, response); | ||
executePreSuccessCallback(response, event, context) | ||
.then(() => callback(null, response)); | ||
} | ||
@@ -268,3 +310,3 @@ } | ||
* @param {string|undefined} [error.awsRequestId] - an optional AWS request ID | ||
* @param {Object} event - the AWS event passed to your handler | ||
* @param {AWSEvent} event - the AWS event passed to your handler | ||
* @param {StandardHandlerContext} context - the context being used | ||
@@ -289,6 +331,8 @@ */ | ||
const proxyResponse = toLambdaProxyResponse(statusCode, error.headers, body, defaultHeaders); | ||
callback(null, proxyResponse); | ||
executePreFailureCallback(apiError, proxyResponse, event, context) | ||
.then(() => callback(null, proxyResponse)); | ||
} else { | ||
const errorResponse = toCustomOrDefaultErrorResponse(apiError, event, context); | ||
callback(JSON.stringify(errorResponse), null); | ||
executePreFailureCallback(apiError, errorResponse, event, context) | ||
.then(() => callback(JSON.stringify(errorResponse), null)); | ||
} | ||
@@ -322,17 +366,19 @@ } | ||
* using the configured `toErrorResponse` function (if any); otherwise the `defaultToErrorResponse` function. | ||
* @param {AppError} apiError - the error with which your Lambda was failed | ||
* @param {AppError} error - the error with which your Lambda was failed | ||
* @param {Object} event - the AWS event passed to your handler | ||
* @param {StandardHandlerContext} context - the context being used | ||
* @param {ToErrorResponse|undefined} [context.handler.toErrorResponse] - an optional function to be used by an AWS | ||
* Lambda `handler` function to convert an AppError into an appropriate error response object | ||
* @return {Object} the error response (to be subsequently stringified & returned) | ||
*/ | ||
function toCustomOrDefaultErrorResponseBody(apiError, event, context) { | ||
function toCustomOrDefaultErrorResponseBody(error, event, context) { | ||
try { | ||
const toErrorResponse = context.handler && context.handler.toErrorResponse; | ||
return typeof toErrorResponse === 'function' ? | ||
toErrorResponse(apiError, event, context) || toDefaultErrorResponseBody(apiError) : | ||
toDefaultErrorResponseBody(apiError); | ||
toErrorResponse(error, event, context) || toDefaultErrorResponseBody(error) : | ||
toDefaultErrorResponseBody(error); | ||
} catch (err) { | ||
console.error('ERROR', err); | ||
// fallback to default function if given function fails | ||
return toDefaultErrorResponseBody(apiError); | ||
return toDefaultErrorResponseBody(error); | ||
} | ||
@@ -344,22 +390,29 @@ } | ||
* function (if any); otherwise using the `toDefaultErrorResponse` function. | ||
* @param {AppError} apiError - the error with which your Lambda was failed | ||
* @param {AppError} error - the error with which your Lambda was failed | ||
* @param {Object} event - the AWS event passed to your handler | ||
* @param {StandardHandlerContext} context - the context being used | ||
* @param {ToErrorResponse|undefined} [context.handler.toErrorResponse] - an optional function to be used by an AWS | ||
* Lambda `handler` function to convert an AppError into an appropriate error response object | ||
* @return {Object} the error response (to be subsequently stringified & returned) | ||
*/ | ||
function toCustomOrDefaultErrorResponse(apiError, event, context) { | ||
function toCustomOrDefaultErrorResponse(error, event, context) { | ||
try { | ||
const toErrorResponse = context.handler && context.handler.toErrorResponse; | ||
return typeof toErrorResponse === 'function' ? | ||
toErrorResponse(apiError, event, context) || toDefaultErrorResponse(apiError) : | ||
toDefaultErrorResponse(apiError); | ||
toErrorResponse(error, event, context) || toDefaultErrorResponse(error) : | ||
toDefaultErrorResponse(error); | ||
} catch (err) { | ||
console.error('ERROR', err); | ||
// fallback to default function if given function fails | ||
return toDefaultErrorResponse(apiError); | ||
return toDefaultErrorResponse(error); | ||
} | ||
} | ||
function toDefaultErrorResponseBody(apiError) { | ||
const json = apiError && apiError.toJSON(); | ||
/** | ||
* A default conversion from an error to an error response body for a Lambda Proxy error response | ||
* @param {AppError} error - an error to convert | ||
* @return {Object} an error response body | ||
*/ | ||
function toDefaultErrorResponseBody(error) { | ||
const json = error && error.toJSON(); | ||
if (json.httpStatus) { | ||
@@ -374,4 +427,9 @@ delete json.httpStatus; // don't really need `httpStatus` inside `body` too, since have it in response as `statusCode` | ||
function toDefaultErrorResponse(apiError) { | ||
return apiError && apiError.toJSON(); | ||
/** | ||
* A default conversion from an error to an error response | ||
* @param {AppError} error - an error to convert | ||
* @return {Object} an error response | ||
*/ | ||
function toDefaultErrorResponse(error) { | ||
return error && error.toJSON(); | ||
} | ||
@@ -378,0 +436,0 @@ |
@@ -106,3 +106,3 @@ 'use strict'; | ||
succeedLambdaCallback(callback, response, context); | ||
succeedLambdaCallback(callback, response, event, context); | ||
}) | ||
@@ -184,2 +184,8 @@ .catch(err => { | ||
} | ||
if (from.preSuccessCallback && !to.preSuccessCallback) { | ||
to.preSuccessCallback = from.preSuccessCallback; | ||
} | ||
if (from.preFailureCallback && !to.preFailureCallback) { | ||
to.preFailureCallback = from.preFailureCallback; | ||
} | ||
} | ||
@@ -190,9 +196,44 @@ return to; | ||
/** | ||
* Executes the custom configured `preSuccessCallback` function (if any). | ||
* @param {Object} response - a normal or Lambda Proxy integration response to be returned | ||
* @param {AWSEvent} event - the AWS event passed to your handler | ||
* @param {StandardHandlerContext} context - the context to use | ||
* @param {PreSuccessCallback|undefined} [context.handler.preSuccessCallback] - an optional function to be used by an | ||
* AWS Lambda `handler` to run any needed shutdown logic immediately before succeeding the Lambda callback | ||
* @return {Promise.<*>} a promise of anything - any errors are logged, but no rejections can escape | ||
*/ | ||
function executePreSuccessCallback(response, event, context) { | ||
const preSuccessCallback = context.handler && context.handler.preSuccessCallback; | ||
return typeof preSuccessCallback === 'function' ? | ||
Promises.try(() => preSuccessCallback(response, event, context)).catch(err => context.error(err)) : | ||
Promise.resolve(); | ||
} | ||
/** | ||
* Executes the custom configured `preSuccessCallback` function (if any). | ||
* @param {AppError} error - the error with which your Lambda was failed | ||
* @param {Object} errorResponse - the error response derived from the error with which your Lambda was failed | ||
* @param {AWSEvent} event - the AWS event passed to your handler | ||
* @param {StandardHandlerContext} context - the context being used | ||
* @param {PreFailureCallback|undefined} [context.handler.preFailureCallback] - an optional function to be used by an | ||
* AWS Lambda `handler` to run any needed shutdown logic immediately before failing the Lambda callback | ||
* @return {Promise.<*>} a promise of anything - any errors are logged, but no rejections can escape | ||
*/ | ||
function executePreFailureCallback(error, errorResponse, event, context) { | ||
const preFailureCallback = context.handler && context.handler.preFailureCallback; | ||
return typeof preFailureCallback === 'function' ? | ||
Promises.try(() => preFailureCallback(error, errorResponse, event, context)).catch(err => context.error(err)) : | ||
Promise.resolve(); | ||
} | ||
/** | ||
* Succeeds the given callback of an AWS Lambda, by invoking the given callback with the given response. | ||
* @param {Function} callback - the callback function passed as the last argument to your Lambda function on invocation. | ||
* @param {Object} response - a normal or Lambda Proxy integration response to be returned | ||
* @param {AWSEvent} event - the AWS event passed to your handler | ||
* @param {StandardHandlerContext} context - the context to use | ||
*/ | ||
function succeedLambdaCallback(callback, response, context) { | ||
callback(null, response); | ||
function succeedLambdaCallback(callback, response, event, context) { | ||
executePreSuccessCallback(response, event, context) | ||
.then(() => callback(null, response)); | ||
} | ||
@@ -211,3 +252,3 @@ | ||
* @param {string|undefined} [error.awsRequestId] - an optional AWS request ID | ||
* @param {Object} event - the AWS event passed to your handler | ||
* @param {AWSEvent} event - the AWS event passed to your handler | ||
* @param {StandardHandlerContext} context - the context being used | ||
@@ -227,3 +268,5 @@ */ | ||
const errorResponse = toCustomOrDefaultErrorResponse(apiError, event, context); | ||
callback(JSON.stringify(errorResponse), null); | ||
executePreFailureCallback(apiError, errorResponse, event, context) | ||
.then(() => callback(JSON.stringify(errorResponse), null)); | ||
} | ||
@@ -234,22 +277,29 @@ | ||
* function (if any); otherwise using the `toDefaultErrorResponse` function. | ||
* @param {AppError} apiError - the error with which your Lambda was failed | ||
* @param {AppError} error - the error with which your Lambda was failed | ||
* @param {Object} event - the AWS event passed to your handler | ||
* @param {StandardHandlerContext} context - the context being used | ||
* @param {ToErrorResponse|undefined} [context.handler.toErrorResponse] - an optional function to be used by an AWS | ||
* Lambda `handler` function to convert an AppError into an appropriate error response object | ||
* @return {Object} the error response (to be subsequently stringified & returned) | ||
*/ | ||
function toCustomOrDefaultErrorResponse(apiError, event, context) { | ||
function toCustomOrDefaultErrorResponse(error, event, context) { | ||
try { | ||
const toErrorResponse = context.handler && context.handler.toErrorResponse; | ||
return typeof toErrorResponse === 'function' ? | ||
toErrorResponse(apiError, event, context) || toDefaultErrorResponse(apiError) : | ||
toDefaultErrorResponse(apiError); | ||
toErrorResponse(error, event, context) || toDefaultErrorResponse(error) : | ||
toDefaultErrorResponse(error); | ||
} catch (err) { | ||
console.error('ERROR', err); | ||
// fallback to default function if given function fails | ||
return toDefaultErrorResponse(apiError); | ||
return toDefaultErrorResponse(error); | ||
} | ||
} | ||
function toDefaultErrorResponse(apiError) { | ||
return apiError && apiError.toJSON(); | ||
/** | ||
* A default conversion from an error to an error response | ||
* @param {AppError} error - an error to convert | ||
* @return {Object} an error response | ||
*/ | ||
function toDefaultErrorResponse(error) { | ||
return error && error.toJSON(); | ||
} |
{ | ||
"name": "aws-core-utils", | ||
"version": "7.1.1", | ||
"version": "7.2.0", | ||
"description": "Core utilities for working with Amazon Web Services (AWS), including ARNs, regions, stages, Lambdas, AWS errors, stream events, Kinesis, DynamoDB.DocumentClients, etc.", | ||
@@ -5,0 +5,0 @@ "author": "Byron du Preez", |
@@ -1,2 +0,2 @@ | ||
# aws-core-utils v7.1.1 | ||
# aws-core-utils v7.2.0 | ||
@@ -3,0 +3,0 @@ Core utilities for working with Amazon Web Services (AWS), including ARNs, regions, stages, Lambdas, AWS errors, stream events, Kinesis, DynamoDB.DocumentClients, etc. |
## Changes | ||
### 7.2.0 | ||
- Changes to `api-lambdas` module: | ||
- Added support for configuring a custom `preSuccessCallback` function to be run before succeeding the Lambda callback | ||
- Added support for configuring a custom `preFailureCallback` function to be run before failing the Lambda callback | ||
- Minor breaking changes to API of "new" `failLambdaCallback` and `succeedLambdaCallback` functions added in 7.1.0 | ||
- Changes to `other-lambdas` module: | ||
- Added support for configuring a custom `preSuccessCallback` function to be run before succeeding the Lambda callback | ||
- Added support for configuring a custom `preFailureCallback` function to be run before failing the Lambda callback | ||
- Minor breaking changes to API of "new" `failLambdaCallback` and `succeedLambdaCallback` functions added in 7.1.0 | ||
### 7.1.1 | ||
@@ -4,0 +14,0 @@ - Committed updated `package-lock.json` |
@@ -230,2 +230,7 @@ 'use strict'; | ||
}; | ||
const expectedResponse = { | ||
statusCode: 200, | ||
headers: {hdr1: 'h1', hdr3: 'h3', hdr2: 'dh2'}, // merged headers | ||
body: JSON.stringify(body) | ||
}; | ||
@@ -235,5 +240,23 @@ // Create a sample function to be executed within the Lambda handler function | ||
const context = {}; | ||
const preSuccessCallback = function preSuccessCallback(response1, event1, context1) { | ||
t.pass(`*** preSuccessCallback must be invoked`); | ||
t.ok(response1, `preSuccessCallback response1 must exist`); | ||
t.deepEqual(response1, expectedResponse, `preSuccessCallback response1 must be expectedResponse`); | ||
t.equal(event1, event, `preSuccessCallback event1 must be event`); | ||
t.equal(context1, context, `preSuccessCallback context1 must be context`); | ||
}; | ||
// noinspection JSUnusedLocalSymbols | ||
const preFailureCallback = function preFailureCallback(error1, errorResponse1, event1, context1) { | ||
t.fail(`### preFailureCallback must NOT be invoked with error (${error1})`); | ||
}; | ||
// Create a sample AWS Lambda handler function | ||
const createContext = () => ({}); | ||
const createSettings = () => undefined; | ||
const createContext = () => context; | ||
const handlerSettings = { | ||
toErrorResponse: toCustomErrorResponse, | ||
preSuccessCallback: preSuccessCallback, | ||
preFailureCallback: preFailureCallback | ||
}; | ||
const createSettings = () => ({handler: handlerSettings}); | ||
const createOptions = () => require('./sample-api-handler-options-2.json'); | ||
@@ -247,3 +270,2 @@ | ||
}; | ||
const handler = apiLambdas.generateHandlerFunction(createContext, createSettings, createOptions, fn, opts); | ||
@@ -258,8 +280,2 @@ | ||
t.pass(`handler should have passed`); | ||
const expectedResponse = { | ||
statusCode: 200, | ||
headers: {hdr1: 'h1', hdr3: 'h3', hdr2: 'dh2'}, // merged headers | ||
body: JSON.stringify(body) | ||
}; | ||
t.deepEqual(response, expectedResponse, `response must be ${JSON.stringify(expectedResponse)}`); | ||
@@ -370,5 +386,23 @@ t.end(); | ||
const context = {}; | ||
// noinspection JSUnusedLocalSymbols | ||
const preSuccessCallback = function preSuccessCallback(response1, event1, context1) { | ||
t.fail(`### preSuccessCallback must NOT be invoked with response (${JSON.stringify(response1)})`); | ||
}; | ||
const preFailureCallback = function preFailureCallback(error1, errorResponse1, event1, context1) { | ||
t.pass(`*** preFailureCallback must be invoked`); | ||
t.equal(error1, error, `preFailureCallback error1 must be ${error}`); | ||
t.ok(errorResponse1, `preFailureCallback errorResponse1 must exist`); | ||
t.equal(event1, event, `preFailureCallback event1 must be event`); | ||
t.equal(context1, context, `preFailureCallback context1 must be context`); | ||
}; | ||
// Create a sample AWS Lambda handler function | ||
const createContext = () => ({}); | ||
const createSettings = () => ({handler: {toErrorResponse: toCustomErrorResponse}}); | ||
const createContext = () => context; | ||
const handlerSettings = { | ||
toErrorResponse: toCustomErrorResponse, | ||
preSuccessCallback: preSuccessCallback, | ||
preFailureCallback: preFailureCallback | ||
}; | ||
const createSettings = () => ({handler: handlerSettings}); | ||
const createOptions = () => require('./sample-api-handler-options-2.json'); | ||
@@ -391,3 +425,3 @@ | ||
.then(response => { | ||
t.pass(`handler should have passed`); | ||
t.pass(`handler should have ended "normally" with the error`); | ||
@@ -394,0 +428,0 @@ const expectedResponse = { |
@@ -227,5 +227,23 @@ 'use strict'; | ||
const context = {}; | ||
const preSuccessCallback = function preSuccessCallback(response1, event1, context1) { | ||
t.pass(`*** preSuccessCallback must be invoked`); | ||
t.ok(response1, `preSuccessCallback response1 must exist`); | ||
t.deepEqual(response1, expectedResponse, `preSuccessCallback response1 must be expectedResponse`); | ||
t.equal(event1, event, `preSuccessCallback event1 must be event`); | ||
t.equal(context1, context, `preSuccessCallback context1 must be context`); | ||
}; | ||
// noinspection JSUnusedLocalSymbols | ||
const preFailureCallback = function preFailureCallback(error1, errorResponse1, event1, context1) { | ||
t.fail(`### preFailureCallback must NOT be invoked with error (${error1})`); | ||
}; | ||
// Create a sample AWS Lambda handler function | ||
const createContext = () => ({}); | ||
const createSettings = () => ({handler: {toErrorResponse: toCustomErrorResponse}}); | ||
const createContext = () => context; | ||
const handlerSettings = { | ||
toErrorResponse: toCustomErrorResponse, | ||
preSuccessCallback: preSuccessCallback, | ||
preFailureCallback: preFailureCallback | ||
}; | ||
const createSettings = () => ({handler: handlerSettings}); | ||
const createOptions = () => require('./sample-other-handler-options.json'); | ||
@@ -286,5 +304,23 @@ | ||
const context = {}; | ||
// noinspection JSUnusedLocalSymbols | ||
const preSuccessCallback = function preSuccessCallback(response1, event1, context1) { | ||
t.fail(`### preSuccessCallback must NOT be invoked with response (${JSON.stringify(response1)})`); | ||
}; | ||
const preFailureCallback = function preFailureCallback(error1, errorResponse1, event1, context1) { | ||
t.pass(`*** preFailureCallback must be invoked`); | ||
t.equal(error1, error, `preFailureCallback error1 must be ${error}`); | ||
t.ok(errorResponse1, `preFailureCallback errorResponse1 must exist`); | ||
t.equal(event1, event, `preFailureCallback event1 must be event`); | ||
t.equal(context1, context, `preFailureCallback context1 must be context`); | ||
}; | ||
// Create a sample AWS Lambda handler function | ||
const createContext = () => ({}); | ||
const createSettings = () => ({handler: {toErrorResponse: toCustomErrorResponse}}); | ||
const createContext = () => context; | ||
const handlerSettings = { | ||
toErrorResponse: toCustomErrorResponse, | ||
preSuccessCallback: preSuccessCallback, | ||
preFailureCallback: preFailureCallback | ||
}; | ||
const createSettings = () => ({handler: handlerSettings}); | ||
const createOptions = () => require('./sample-other-handler-options.json'); | ||
@@ -291,0 +327,0 @@ |
@@ -607,7 +607,21 @@ 'use strict'; | ||
* function to convert an AppError into an appropriate error response object | ||
* @property {PreSuccessCallback|undefined} [preSuccessCallback] - an optional function to be used by an AWS Lambda | ||
* `handler` to run any needed shutdown logic immediately before succeeding the Lambda callback | ||
* @property {PreFailureCallback|undefined} [preFailureCallback] - an optional function to be used by an AWS Lambda | ||
* `handler` to run any needed shutdown logic immediately before failing the Lambda callback | ||
*/ | ||
/** | ||
* @typedef {function(error: AppError, event: AwsEvent, context: StandardHandlerContext): Object} ToErrorResponse - a | ||
* function to use to generate an error response (or an error response body for a Lambda Proxy response) | ||
* @typedef {function(error: AppError, event: AWSEvent, context: StandardHandlerContext): Object} ToErrorResponse - | ||
* a function to use to generate an error response (or an error response body for a Lambda Proxy response) | ||
*/ | ||
/** | ||
* @typedef {function(response: Object, event: AWSEvent, context: StandardHandlerContext)} PreSuccessCallback - | ||
* a function to use to run any needed shutdown logic immediately before succeeding the Lambda callback | ||
*/ | ||
/** | ||
* @typedef {function(error: AppError, errorResponse: Object, event: AWSEvent, context: StandardHandlerContext)} PreFailureCallback - | ||
* a function to use to run any needed shutdown logic immediately before failing the Lambda callback | ||
*/ |
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
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
663625
10064