Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@jschr/lambda-response

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jschr/lambda-response

Express-like API for sending responses from Lambda Integration Proxy to API Gateway and a CLI for local development.

  • 0.1.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
9
increased by200%
Maintainers
1
Weekly downloads
 
Created
Source

lambda-response

npm Build Status

Express-like API for sending responses from Lambda Integration Proxy to API Gateway.

Includes a CLI tool and express middleware for local development.

Install

npm install @jschr/lambda-response

Usage

const { Response } = require('@jschr/lambda-response')

export default function handler(event, context) {
  const res = new Response()

  context.succeed(res.send('OK'))
  // => { statusCode: 200, body: 'OK' }

  context.succeed(res.json({ foo: bar }))
  // => { statusCode: 200, body: '{"foo":"bar"}' }

  context.succeed(res.status(404).json({ message: 'Not found.' }))
  // => { statusCode: 404, body: '{"message":"Not found."}' }

  context.succeed(res.redirect('https://github.com'))
  // => { statusCode: 302, headers: { Location: 'https://github.com'} } }
}

Even more express-like with async/await:

const { Response } = require('@jschr/lambda-response')

async function route(req, res) {
  const data = await someAsyncFunction(req.query.id)

  if (data) {
    res.json(data)
  } else {
    res.status(404).json({ message: 'Not found'. })
  }
}

export default function handler(event, context) {
  const req = { query: event.queryStringParameters || {} }
  const res = new Response()

  route(req, res)
    .then(() => context.succeed(res))
    .catch(context.fail)
}

Headers

Default headers can be passed when creating a new response:


const headers = { 'Content-Type': 'application/json' }
const res = new Response({ headers })

Or on an instance:


const res = new Response()
const headers = { 'Content-Type': 'application/json' }
res.set(headers)

CORS

CORS is enabled by default. You can pass in cors options when creating a new response:

const cors = { origin: 'example.com', methods: ['GET'], headers: ['X-Api-Key'] }
const res = new Response({ cors })

Check out the tests for more examples.

CLI

You can use the CLI for local development. If you've installed @jschr/lambda-response globally:

$ lambda-response foo/bar.default --port 8080

Where foo/bar is the path to your lambda handler and default is the exported function.

Middleware

For advanced use cases you can use the lambda-response express middleware:

import * as express from 'express'
import { middleware } from '@jschr/lambda-response'

import handler from './foo/bar'

const app = express()

app.use(middleware(handler))

app.listen(8080)

FAQs

Package last updated on 07 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc