Giter VIP home page Giter VIP logo

sls-promise's Introduction

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

sls-promise's People

Contributors

ajoslin avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.