serverless-helpers
Serverless Request/Response Handlers
Usage
Request
, Response
, and LambdaResponseFormatter
Require
const Request = require('@itcutives/serverless-helpers/src/request');
const Response = require('@itcutives/serverless-helpers/src/response');
const LambdaResponseFormatter = require('@itcutives/serverless-helpers/src/lambdaResponseFormatter');
Usage
module.exports.handler = async () => {
const request = REQ.normaliseLambdaRequest(event);
const response = new RES();
try {
const resp = await handleEvent(request, response);
return LambdaResponseFormatter.responseHandler(resp);
} catch (e) {
return LambdaResponseFormatter.errorHandler(e);
}
};
LambdaResponseFormatter
middleware
To handle the clean up operations before responding. such as close db connection etc...
const LambdaResponseFormatter = require('@itcutives/serverless-helpers/src/lambdaResponseFormatter');
class ResponseHandler extends LambdaResponseFormatter {
static async middleware(response) {
return true;
}
}
module.exports = ResponseHandler;
Once you have above, your handler
function should use ResponseHandler
class instead of LambdaResponseFormatter
boom-to-jsonapi
Require
const boomToJsonAPI = require('@itcutives/serverless-helpers/src/boom-to-jsonapi');
Unclassified Error
boomToJsonAPI(new Error('random error'));
Boom Error
boomToJsonAPI(Boom.badRequest('BAD Request'));
Boom Error with Code
boomToJsonAPI(Boom.badRequest('CODE :Some Error:it happened'));
Example
cd example
npm i
npm run offline