1.0.9 • Published 5 years ago

sls-promise v1.0.9

Weekly downloads
55
License
MIT
Repository
github
Last release
5 years ago

sls-promise Build Status

Minimal promise-based responses for serverless. Return JSON or full response.

Install

$ npm install --save sls-promise

Usage

var slsp = require('sls-promise')

// By default, treats return value as the body of a response.
// Adds statusCode 200 and Content-Type application/json.
exports.handler = slsp((event, context) => {
  return { some: 'json' }
})

// Create a custom response with slsp.response() as return value.
exports.withCustomResponse = slsp((event, context) => {
  return slsp.response({
    statusCode: 204,
    body: null
  })
})

// By default, rejected promise is treated as an error body.
// statusCode 500 is added.
exports.withRejectedPromise = slsp((event, context) => {
  throw new Error('this is not good')
})

// Recommended: use node-http-error for custom errors.
const HttpError = require('node-http-error')
exports.withCustomError = slsp((event, context) => {
  if (notAllowed) throw HttpError(403, 'You cannot view this.')
})

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

1.0.9

5 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.1

6 years ago