2.0.1 • Published 6 years ago

@repit/lambda-send v2.0.1

Weekly downloads
2
License
MIT
Repository
-
Last release
6 years ago

Lambda Send

AWS Lambda responses simplified.

Installation

$ npm install @repit/lambda-send --save

Usage

'use strict'

const send = require('@repit/lambda-send')

exports.handler = (event, context, callback) => {
  // Send a default success response.
  send(callback, 200)

  // Send a custom success message.
  send(callback, 200, 'All right.')

  // Send a custom success message.
  send(callback, 200, {
    countryCode: 'US'
  })

  // Send a default error response.
  send(callback, 400)

  // Send a custom error response.
  send(callback, 400, 'Something went wrong.')

  // Send a custom error response.
  send(callback, 400, {
    reason: 'Something went wrong.'
  })

  // Disable CORS.
  send(callback, 200, null, {
    cors: false // Default: true.
  })

  // Send custom headers.
  send(callback, 200, null, {
    headers: {
      //
    }
  })

  // Add properties to the response body.
  send(callback, 200, null, {
    body: {
      //
    }
  })
}

Response Structure

  • Success response:
{
  "ok": true, // statusCode === 200
  "payload": null || <user defined payload>,
  <user defined property name>: <user defined property value>
}
  • Error response:
{
  "ok": false, // statusCode !== 200
  "code": <user defined status code>,
  "payload": "Resource is not available." || <user defined payload>,
  <user defined property name>: <user defined property value>
}

Testing

$ npm test

License

MIT