https://github.com/jschr/lambda-response
Express-like API for sending responses from Lambda / APIG + CLI for development.
https://github.com/jschr/lambda-response
Last synced: over 1 year ago
JSON representation
Express-like API for sending responses from Lambda / APIG + CLI for development.
- Host: GitHub
- URL: https://github.com/jschr/lambda-response
- Owner: jschr
- Created: 2017-05-06T05:39:59.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2022-12-06T23:35:31.000Z (over 3 years ago)
- Last Synced: 2025-03-10T09:10:03.463Z (over 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 61.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# lambda-response
[](https://www.npmjs.com/package/@jschr/lambda-response)
[](https://travis-ci.org/jschr/lambda-response)
Express-like API for sending responses from [Lambda Integration Proxy](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html) 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
```js
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
```js
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
```js
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
```js
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](src/Response.spec.ts) for more examples.
## CLI
You can use the CLI for local development if you've installed the package globally.
```bash
$ 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
```js
const server = require('express')()
const { middleware } = require('@jschr/lambda-response')
server.use(middleware(require('./foo/bar')))
```
Use the express middleware for custom servers.