{"id":16490177,"url":"https://github.com/jschr/lambda-response","last_synced_at":"2025-04-04T17:49:39.380Z","repository":{"id":20617691,"uuid":"90440143","full_name":"jschr/lambda-response","owner":"jschr","description":"Express-like API for sending responses from Lambda / APIG + CLI for development.","archived":false,"fork":false,"pushed_at":"2022-12-06T23:35:31.000Z","size":63,"stargazers_count":1,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-10T09:10:03.463Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jschr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-06T05:39:59.000Z","updated_at":"2017-11-18T19:05:23.000Z","dependencies_parsed_at":"2023-01-12T03:30:47.633Z","dependency_job_id":null,"html_url":"https://github.com/jschr/lambda-response","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jschr%2Flambda-response","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jschr%2Flambda-response/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jschr%2Flambda-response/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jschr%2Flambda-response/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jschr","download_url":"https://codeload.github.com/jschr/lambda-response/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247226195,"owners_count":20904465,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T13:46:52.883Z","updated_at":"2025-04-04T17:49:39.354Z","avatar_url":"https://github.com/jschr.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lambda-response\n\n[![npm](https://img.shields.io/npm/v/@jschr/lambda-response.svg)](https://www.npmjs.com/package/@jschr/lambda-response)\n[![Build Status](https://img.shields.io/travis/jschr/lambda-response/master.svg)](https://travis-ci.org/jschr/lambda-response)\n\nExpress-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.\n\nIncludes a CLI tool and express middleware for local development.\n\n## Install\n\n```\nnpm install @jschr/lambda-response\nyarn add @jschr/lambda-response\n```\n\n## Usage\n\n```js\nconst { Response } = require('@jschr/lambda-response')\n\nexport default function handler(event, context) {\n  const res = new Response()\n\n  context.succeed(res.send('OK'))\n  // =\u003e { statusCode: 200, body: 'OK' }\n\n  context.succeed(res.json({ foo: bar }))\n  // =\u003e { statusCode: 200, body: '{\"foo\":\"bar\"}' }\n\n  context.succeed(res.status(404).json({ message: 'Not found.' }))\n  // =\u003e { statusCode: 404, body: '{\"message\":\"Not found.\"}' }\n\n  context.succeed(res.redirect('https://github.com'))\n  // =\u003e { statusCode: 302, headers: { Location: 'https://github.com'} } }\n}\n```\n### Headers\n\n```js\n\nconst headers = { 'Content-Type': 'application/json' }\nconst res = new Response({ headers })\n\nconst res = new Response()\nconst headers = { 'Content-Type': 'application/json' }\nres.set(headers)\n```\nDefault headers can be passed when creating a new response or set on an instance.\n\n### CORS\n\n```js\nconst cors = { origin: 'example.com', methods: ['GET'], headers: ['X-Api-Key'] }\nconst res = new Response({ cors })\n```\nCORS is enabled by default. Customize cors settings when creating a new response.\n\n### Examples\nWith async/await\n```js\nconst { Response } = require('@jschr/lambda-response')\n\nasync function route(req, res) {\n  const data = await someAsyncFunction(req.query.id)\n\n  if (data) {\n    res.json(data)\n  } else {\n    res.status(404).json({ message: 'Not found'. })\n  }\n}\n\nexport default async function handler(event, context) {\n  const req = { query: event.queryStringParameters || {} }\n  const res = new Response()\n\n  try {\n    await route(req, res);\n    context.succeed(res);\n  } catch (err) {\n    context.fail(err);\n  }\n}\n```\nCheck out the [tests](src/Response.spec.ts) for more examples.\n\n## CLI\n\nYou can use the CLI for local development if you've installed the package globally.\n\n```bash\n$ lambda-response foo/bar.default --port 8080\n```\nWhere `foo/bar` is the path to your lambda handler and `default` is the exported function name.\n\n## Middleware\n```js\nconst server = require('express')()\nconst { middleware } = require('@jschr/lambda-response')\n\nserver.use(middleware(require('./foo/bar')))\n```\nUse the express middleware for custom servers.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjschr%2Flambda-response","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjschr%2Flambda-response","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjschr%2Flambda-response/lists"}