The microservice-http-endpoint starter function in Lambda contains the following code to end the function:
const done = (err, res) => callback(null, {
statusCode: err ? '400' : '200',
body: err ? err.message : JSON.stringify(res),
headers: {
'Content-Type': 'application/json',
},
});
After writing a Lambda-only API with a few endpoints I found myself with this same code in all my functions. My first instinct was to put this into a module to make the code more DRY but if I do that the callback() function will become out of scope.
I am only returning JSON from my functions so this would be the only possible Content-Type in my specific use-case.
How are people dealing with this? Is there a practical (and elegant) way to encapsulate this code somehow? Or maybe a better alternative?
global.callback = callbackas the first line of your Lambda handler function and then it will never be out of scope.global.MY_SUPER_SECRET_CALLBACK = callback.