Package Exports
- @compeon/lambda-helpers
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@compeon/lambda-helpers) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
COMPEON Lambda Helpers
Useful functions that will probably be used across all Lambda functions.
Note: Type definitions in this README are meant as a help and do not strictly follow TypeScript or Flow conventions.
handleError(error: any, status?: number): Object
Used to log the provided error to cloudwatch. Returns AWS Lambda conform response object. If error is not a string, it gets JSON-stringified.
status defaults to 500.
Example:
handleError('not found', 404)
// => logs 'not found' to cloudwatch console
// => { body: 'not found', statusCode: 404 }axiosErrorHandler(axiosResponse: Object): Object
Mostly used inside catch of an axios request. Extracts error from the response and calls handleError with it.
Note: Will only work with responses that follow the structure of an axios response.
Example:
axios.get(apiUrl)
.then(someResponseHandler)
.catch(axiosErrorHandler)okayResponse(body?: string|object): Object
Returns an AWS Lambda conform response with the preferred body. body defaults to 'ok'.
Example:
okayResponse()
// => { body: 'ok, statusCode: 200 }
// May also be used as a last handler in a promise chain to directly transform the previous' handlers response as the response of the Lambda itself:
exports.lambda_handler = (event) => {
// omitted ...
return axios.get(apiUrl)
.then(someResponseHandler)
.then(okayResponse)
}
// If an empty response instead of the previous body is explicitly wanted:
return axios.get(apiUrl)
.then(someResponseHandler)
.then(() => okayResponse())