{"id":19853848,"url":"https://github.com/magnetikonline/aws-lambda-proxy-response","last_synced_at":"2025-05-02T01:30:23.582Z","repository":{"id":57188520,"uuid":"76027982","full_name":"magnetikonline/aws-lambda-proxy-response","owner":"magnetikonline","description":"Node.js module generating response payloads for AWS Lambda functions behind API Gateway 'Lambda proxy' integrations.","archived":false,"fork":false,"pushed_at":"2024-05-10T02:18:58.000Z","size":15,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-22T17:58:52.013Z","etag":null,"topics":["api-gateway","aws","http","lambda","lambda-proxy","npm-package"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/awslambdaproxyresponse","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magnetikonline.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-12-09T10:57:19.000Z","updated_at":"2024-05-10T02:19:02.000Z","dependencies_parsed_at":"2024-05-10T03:27:01.351Z","dependency_job_id":"b221a40a-8595-46ed-b339-114253ceb04b","html_url":"https://github.com/magnetikonline/aws-lambda-proxy-response","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magnetikonline%2Faws-lambda-proxy-response","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magnetikonline%2Faws-lambda-proxy-response/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magnetikonline%2Faws-lambda-proxy-response/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magnetikonline%2Faws-lambda-proxy-response/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magnetikonline","download_url":"https://codeload.github.com/magnetikonline/aws-lambda-proxy-response/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251969228,"owners_count":21673179,"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":["api-gateway","aws","http","lambda","lambda-proxy","npm-package"],"created_at":"2024-11-12T14:07:48.214Z","updated_at":"2025-05-02T01:30:23.299Z","avatar_url":"https://github.com/magnetikonline.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AWS Lambda proxy response\n\nA Node.js module which generates response payloads for [API Gateway](https://aws.amazon.com/api-gateway/) fronted [Lambda](https://aws.amazon.com/lambda/) functions integrated via the [Lambda proxy](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#lambda-proxy-integration-with-proxy-resource) method.\n\nThe [response structure](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format) takes the following form:\n\n```js\n{\n  statusCode: httpStatusCode,\n  headers: { headerName: 'headerValue' },\n  body: '...'\n}\n```\n\n- [Installation](#installation)\n- [Methods](#methods)\n\t- [AWSLambdaProxyResponse([statusCode])](#awslambdaproxyresponsestatuscode)\n\t- [AWSLambdaProxyResponse.setStatusCode(statusCode)](#awslambdaproxyresponsesetstatuscodestatuscode)\n\t- [AWSLambdaProxyResponse.addHeader(name[,value])](#awslambdaproxyresponseaddheadernamevalue)\n\t- [AWSLambdaProxyResponse.setBody(body)](#awslambdaproxyresponsesetbodybody)\n\t- [AWSLambdaProxyResponse.getPayload()](#awslambdaproxyresponsegetpayload)\n- [Constants](#constants)\n\t- [AWSLambdaProxyResponse.HTTP_STATUS](#awslambdaproxyresponsehttp_status)\n- [Example usage](#example-usage)\n- [Reference](#reference)\n\n## Installation\n\n```sh\n$ npm install awslambdaproxyresponse\n```\n\n## Methods\n\n### `AWSLambdaProxyResponse([statusCode])`\n\n- Creates new `AWSLambdaProxyResponse` instance.\n- Optional `statusCode` sets the [HTTP status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes) for the response, otherwise defaults to `200 / OK`.\n- Collection of valid HTTP codes defined at [`AWSLambdaProxyResponse.HTTP_STATUS`](#awslambdaproxyresponsehttp_status).\n- Constructor will throw an exception if given `statusCode` is not within this collection.\n\nExample:\n\n```js\nconst AWSLambdaProxyResponse = require('awslambdaproxyresponse');\n\nconst resp = new AWSLambdaProxyResponse(\n  AWSLambdaProxyResponse.HTTP_STATUS.FOUND\n);\n```\n\n### `AWSLambdaProxyResponse.setStatusCode(statusCode)`\n\n- Sets the HTTP `statusCode` for a response.\n- Throws exception if `statusCode` not within [`AWSLambdaProxyResponse.HTTP_STATUS`](#awslambdaproxyresponsehttp_status) collection.\n- Returns `AWSLambdaProxyResponse` instance.\n\n### `AWSLambdaProxyResponse.addHeader(name[,value])`\n\n- Adds HTTP headers to the Lambda proxy response.\n- Single HTTP header can be added by providing a `name` / `value` pair.\n- Multiple headers can be added by providing an object collection as `name` only.\n- Throws exception if header name(s) do not match regular expression `/^[A-Za-z-]+$/`.\n- Returns `AWSLambdaProxyResponse` instance.\n\nExample:\n\n```js\nconst AWSLambdaProxyResponse = require('awslambdaproxyresponse');\nconst resp = new AWSLambdaProxyResponse();\n\n// lets add a single header\nresp.addHeader('Content-Type','text/html');\n\n// add several others\nresp.addHeader({\n  'x-custom-header': 'value',\n  'x-user-auth': 'Donald Duck'\n});\n```\n\n### `AWSLambdaProxyResponse.setBody(body)`\n\n- Sets the response body payload.\n- If `body` is not of type `string`, will be automatically serialized via `JSON.stringify()`.\n- Returns `AWSLambdaProxyResponse` instance.\n\n### `AWSLambdaProxyResponse.getPayload()`\n\nReturns a valid Lambda proxy response structure object.\n\n## Constants\n\n### `AWSLambdaProxyResponse.HTTP_STATUS`\n\nCollection of HTTP status codes for use with the `AWSLambdaProxyResponse()` constructor or `setStatusCode(statusCode)` method:\n\n```js\nconst AWSLambdaProxyResponse = require('awslambdaproxyresponse');\nconsole.dir(AWSLambdaProxyResponse.HTTP_STATUS);\n\n/*\n{\n  OK: 200,\n  MOVED: 301,\n  FOUND: 302,\n  BAD_REQUEST: 400,\n  UNAUTHORIZED: 401,\n  FORBIDDEN: 403,\n  NOT_FOUND: 404,\n  SERVER_ERROR: 500,\n  NOT_IMPLEMENTED: 501,\n  BAD_GATEWAY: 502,\n  SERVICE_UNAVAILABLE: 503,\n  GATEWAY_TIMEOUT: 504\n}\n*/\n```\n\n## Example usage\n\nWithin the context of a Lambda function:\n\n```js\nconst AWSLambdaProxyResponse = require('awslambdaproxyresponse');\n\nexports.myHandler = (event,context,callback) =\u003e {\n  // create our response\n  const resp = new AWSLambdaProxyResponse();\n  resp.setBody('Hello world');\n\n  // return from Lambda\n  callback(null,resp.getPayload());\n\n  /*\n  console.dir(resp.getPayload());\n  {\n    statusCode: 200,\n    headers: {},\n    body: 'Hello world'\n  }\n  */\n};\n```\n\nA Lambda response that results in a redirect:\n\n```js\nconst AWSLambdaProxyResponse = require('awslambdaproxyresponse');\n\nexports.myHandler = (event,context,callback) =\u003e {\n  // create our response\n  const resp = new AWSLambdaProxyResponse();\n\n  resp.setStatusCode(AWSLambdaProxyResponse.HTTP_STATUS.MOVED);\n  resp.addHeader('Location','https://my.new.domain.com/');\n\n  // return from Lambda\n  callback(null,resp.getPayload());\n\n  /*\n  console.dir(resp.getPayload());\n  {\n    statusCode: 301,\n    headers: { Location: 'https://my.new.domain.com/' },\n    body: ''\n  }\n  */\n};\n```\n\n## Reference\n\n- https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html\n- https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#lambda-proxy-integration-with-proxy-resource\n- https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagnetikonline%2Faws-lambda-proxy-response","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagnetikonline%2Faws-lambda-proxy-response","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagnetikonline%2Faws-lambda-proxy-response/lists"}