Giter Club home page Giter Club logo

lambda-response's Introduction

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
yarn add @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'} } }
}

Headers

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

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

Default headers can be passed when creating a new response or set on an instance.

CORS

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

CORS is enabled by default. Customize cors settings when creating a new response.

Examples

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 async function handler(event, context) {
  const req = { query: event.queryStringParameters || {} }
  const res = new Response()

  try {
    await route(req, res);
    context.succeed(res);
  } catch (err) {
    context.fail(err);
  }
}

Check out the tests for more examples.

CLI

You can use the CLI for local development if you've installed the package globally.

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

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

Middleware

const server = require('express')()
const { middleware } = require('@jschr/lambda-response')

server.use(middleware(require('./foo/bar')))

Use the express middleware for custom servers.

lambda-response's People

Stargazers

 avatar

Watchers

 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.