sls-promise
Minimal promise-based responses for serverless. Return JSON or full response.
Install
$ npm install --save sls-promise
Usage
var slsp = // By default, treats return value as the body of a response.// Adds statusCode 200 and Content-Type application/json.exportshandler = // Create a custom response with slsp.response() as return value.exportswithCustomResponse = // By default, rejected promise is treated as an error body.// statusCode 500 is added.exportswithRejectedPromise = // Recommended: use node-http-error for custom errors.const HttpError = exportswithCustomError =
API
slsPromise(handler)
-> wrappedHandler
Returns a function that takes (event, context) like a normal serverless function,
except it expects a promise to be returned from handler
.
Response Types
- Promise resolved with any value: responds with
{statusCode: 200, headers: {'Content-Type': 'application/json'}, body: givenValue}
- Promise resolved with an instance of
slsPromise.response
: Responds with the given response object. - Promise rejected or error thrown: Sets statusCode to 500 if no statusCode is given, and sets error.message to response.body. Recommended: use node-http-error for custom HTTP errors.
License
MIT © Andrew Joslin