{"id":21010626,"url":"https://github.com/geoffdutton/lambda-proxy-utils","last_synced_at":"2025-07-15T07:35:50.627Z","repository":{"id":16566146,"uuid":"80159516","full_name":"geoffdutton/lambda-proxy-utils","owner":"geoffdutton","description":"Lambda event helpers for AWS API Gateway lambda-proxy integration","archived":false,"fork":false,"pushed_at":"2024-10-08T01:47:39.000Z","size":758,"stargazers_count":13,"open_issues_count":15,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-07T02:49:33.390Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/geoffdutton.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,"zenodo":null}},"created_at":"2017-01-26T21:56:52.000Z","updated_at":"2025-02-06T11:30:39.000Z","dependencies_parsed_at":"2025-05-15T02:44:58.593Z","dependency_job_id":null,"html_url":"https://github.com/geoffdutton/lambda-proxy-utils","commit_stats":null,"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"purl":"pkg:github/geoffdutton/lambda-proxy-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geoffdutton%2Flambda-proxy-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geoffdutton%2Flambda-proxy-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geoffdutton%2Flambda-proxy-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geoffdutton%2Flambda-proxy-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/geoffdutton","download_url":"https://codeload.github.com/geoffdutton/lambda-proxy-utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geoffdutton%2Flambda-proxy-utils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265418438,"owners_count":23761817,"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-11-19T09:21:48.584Z","updated_at":"2025-07-15T07:35:50.566Z","avatar_url":"https://github.com/geoffdutton.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lambda-proxy-utils\n[![npm version](https://badge.fury.io/js/lambda-proxy-utils.svg)](https://badge.fury.io/js/lambda-proxy-utils)\n[![Build Status](https://travis-ci.org/geoffdutton/lambda-proxy-utils.svg?branch=master)](https://travis-ci.org/geoffdutton/lambda-proxy-utils)\n[![Coverage Status](https://coveralls.io/repos/github/geoffdutton/lambda-proxy-utils/badge.svg?branch=master)](https://coveralls.io/github/geoffdutton/lambda-proxy-utils?branch=master)\n[![Dependency Status](https://david-dm.org/geoffdutton/lambda-proxy-utils.svg)](https://david-dm.org/geoffdutton/lambda-proxy-utils/)\n[![Standard - JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](http://standardjs.com/)\n\nLambda event helpers for AWS API Gateway lambda-proxy integration\n\n## Install\n```\nnpm install --save lambda-proxy-utils\n```\n\n## Update for version 2+\nNode 4 is unsupported.\n\n## Update for version 1.4.0\nAWS Lambda doesn't allow arrays for headers, so this is the hack way of returning multiple cookies per this thread:\nhttps://forums.aws.amazon.com/thread.jspa?threadID=205782\n\nBasically you need to set multiple varations of `Set-Cookie` on the headers response object passed to the lambda callback like:\n```javascript\nconst response = {\n  body: 'something',\n  headers: {\n    'Content-Type': 'text/plain',\n    'Set-Cookie': 'some=cookie; Path=/',\n    'Set-cookie': 'another=cookie; Path=/',\n    'SEt-cookie': 'and_another=cookie; Path=/',\n  },\n  statusCode: 200\n}\n```\n\nUsing [binary-case](https://www.npmjs.com/package/binary-case), we can generate 512 variations of `Set-Cookie`, so there's a hard limit, but hopefully you aren't setting 512 cookies.\n\n## Request\nTakes an [API Gateway](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-set-up-lambda-proxy-integration-on-proxy-resource) lambda proxy integration event and returns an object that is similar to an express.js Request object.\n```javascript\n// Example API Gateway proxy integration event passed to lambda\n{\n  \"resource\": \"/api/pipe/{pathParam}\",\n  \"path\": \"/api/pipe/hooray/\",\n  \"httpMethod\": \"GET\",\n  \"headers\": {\n    \"Accept\": \"*/*\",\n    \"Accept-Encoding\": \"gzip, deflate, sdch, br\",\n    \"Accept-Language\": \"en-US,en;q=0.8\",\n    \"Cache-Control\": \"no-cache\",\n    \"CloudFront-Forwarded-Proto\": \"https\",\n    \"CloudFront-Is-Desktop-Viewer\": \"true\",\n    \"CloudFront-Is-Mobile-Viewer\": \"false\",\n    \"CloudFront-Is-SmartTV-Viewer\": \"false\",\n    \"CloudFront-Is-Tablet-Viewer\": \"false\",\n    \"CloudFront-Viewer-Country\": \"US\",\n    \"Cookie\": \"some=thing; testbool=false; testnull=null\",\n    \"Host\": \"services.cheekyroad.com\",\n    \"Pragma\": \"no-cache\",\n    \"Referer\": \"https://cheekyroad.com/paht/?cool=true\",\n    \"User-Agent\": \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36\",\n    \"Via\": \"1.1 1a1a1a1.cloudfront.net (CloudFront)\",\n    \"X-Amz-Cf-Id\": \"2b2b2b2b2==\",\n    \"X-Forwarded-For\": \"111.111.111.111, 222.222.222.222\",\n    \"X-Forwarded-Port\": \"443\",\n    \"X-Forwarded-Proto\": \"https\"\n  },\n  \"queryStringParameters\": {\n    \"et\": \"something\"\n  },\n  \"pathParameters\": {\n    \"pathParam\": \"hooray\"\n  },\n  \"stageVariables\": null,\n  \"requestContext\": {\n    \"accountId\": \"111111111111\",\n    \"resourceId\": \"blah\",\n    \"stage\": \"dev\",\n    \"requestId\": \"08e3e2d0-daca-11e6-8d84-394b4374a71a\",\n    \"identity\": {\n      \"cognitoIdentityPoolId\": null,\n      \"accountId\": null,\n      \"cognitoIdentityId\": null,\n      \"caller\": null,\n      \"apiKey\": null,\n      \"sourceIp\": \"111.111.111.111\",\n      \"accessKey\": null,\n      \"cognitoAuthenticationType\": null,\n      \"cognitoAuthenticationProvider\": null,\n      \"userArn\": null,\n      \"userAgent\": \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36\",\n      \"user\": null\n    },\n    \"resourcePath\": \"/api/pipe/{pathParam}\",\n    \"httpMethod\": \"GET\",\n    \"apiId\": \"cdcd4\"\n  },\n  \"body\": null,\n  \"isBase64Encoded\": false\n}\n\nconst Request = require('lambda-proxy-utils').Request\n\nmodule.exports.lambdaHandler = function(event, context, callback) {\n  const req = new Request(event)\n  req.ip // '111.111.111.111'\n  req.userAgent // 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'\n  \n  // Get's a field value in the order of query string param -\u003e cookie -\u003e header\n  req.get('host') // 'services.cheekyroad.com'\n  req.get('testnull') // null\n  \n  // Or be specific\n  req.getHeader('x-forwarded-proto') // 'https'\n  \n  // Check the type\n  req.is('html') // false\n  \n  // Get an AWS API Gateway requestContext property\n  req.context('requestId') // '08e3e2d0-daca-11e6-8d84-394b4374a71a'\n  \n  // Get the unmodified Lambda Proxy event\n  req.getLambdaEvent()\n}\n```\n\n## Response\nCreates an express.js-like Response object, and outputs the API Gateway [response format](http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-simple-proxy-for-lambda-output-format)\n```javascript\nconst Response = require('lambda-proxy-utils').Response\n\nmodule.exports.lambdaHandler = function(event, context, callback) {\n  const res = new Response()\n  // stringifies objects and set correct content type header\n  callback(null, res.send({ some: 'object' }))\n   /*\n    {\n      statusCode: 200,\n      headers: {\n          'Content-Type': 'application/json'\n      },\n      body: '{ \"some\": \"object\" }'\n    }\n   */\n  \n  // Support for CORS\n  const res = new Response({ cors: true })\n  callback(null, res.send({ some: 'object' }))\n  /*\n    {\n      statusCode: 200,\n      headers: {\n          'Content-Type': 'application/json',\n          'Access-Control-Allow-Origin': '*'\n      },\n      body: '{ \"some\": \"object\" }'\n    }\n   */\n   \n  // Add a cookie\n  const res = new Response()\n  res.cookie('cookie', 'monster')\n  callback(null, res.send({ some: 'object' }))\n  /*\n    {\n      statusCode: 200,\n      headers: {\n          'Content-Type': 'application/json',\n          'Set-Cookie': 'cookie=monster; Path=/'\n      },\n      body: '{ \"some\": \"object\" }'\n    }\n  */\n  \n  // Add a header\n  const res = new Response()\n  res.set('X-Random-Header', 1)\n  callback(null, res.send({ some: 'object' }))\n  /*\n    {\n      statusCode: 200,\n      headers: {\n          'Content-Type': 'application/json',\n          'X-Random-Header': '1'\n      },\n      body: '{ \"some\": \"object\" }'\n    }\n  */\n}\n```\n\n## Contributing\nI'd happily welcome pull requests. I've chosen to use Standard as the style with a few slight modifications. I'd like to keep the code coverage as high as possible.\n\n## Credits\nI borrowed a lot from [express](https://github.com/expressjs/express)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeoffdutton%2Flambda-proxy-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgeoffdutton%2Flambda-proxy-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeoffdutton%2Flambda-proxy-utils/lists"}