@kdcio/api-gw-req
TypeScript icon, indicating that this package has built-in type declarations

1.3.1 • Public • Published

API Gateway Request Parser

This module will parse the event object (that came from API Gateway) in lambda and create a new object with all unnecessary fields stripped out.

ver size build Known Vulnerabilities Quality Gate Status Code Smells Coverage license

Install

npm i @kdcio/api-gw-req

Usage

import parser from '@kdcio/api-gw-req';

export const hello = async (event) => {
  const request = parser(event);
  console.log(request);
};

Sample event object:

{
  "body": null,
  "headers": {
    "Host": "localhost:8101",
    "Connection": "keep-alive",
    "Origin": "http://localhost:8100",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
    "content-type": "application/json",
    "Accept": "*/*",
    "Sec-Fetch-Site": "same-site",
    "Sec-Fetch-Mode": "cors",
    "Referrer": "http://localhost:8100/",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.9,th;q=0.8"
  },
  "httpMethod": "GET",
  "multiValueHeaders": {
    "Host": ["localhost:8101"],
    "Connection": ["keep-alive"],
    "Origin": ["http://localhost:8100"],
    "User-Agent": [
      "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"
    ],
    "content-type": ["application/json"],
    "Accept": ["*/*"],
    "Sec-Fetch-Site": ["same-site"],
    "Sec-Fetch-Mode": ["cors"],
    "Referrer": ["http://localhost:8100/"],
    "Accept-Encoding": ["gzip, deflate, br"],
    "Accept-Language": ["en-US,en;q=0.9,th;q=0.8"]
  },
  "multiValueQueryStringParameters": null,
  "path": "/users/me",
  "pathParameters": { "proxy": "me" },
  "queryStringParameters": null,
  "requestContext": {
    "accountId": "offlineContext_accountId",
    "apiId": "offlineContext_apiId",
    "httpMethod": "GET",
    "identity": {
      "accountId": "offlineContext_accountId",
      "apiKey": "offlineContext_apiKey",
      "caller": "offlineContext_caller",
      "cognitoAuthenticationProvider": "offlineContext_cognitoAuthenticationProvider",
      "cognitoAuthenticationType": "offlineContext_cognitoAuthenticationType",
      "cognitoIdentityId": "offlineContext_cognitoIdentityId",
      "cognitoIdentityPoolId": "offlineContext_cognitoIdentityPoolId",
      "sourceIp": "127.0.0.1",
      "user": "offlineContext_user",
      "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
      "userArn": "offlineContext_userArn"
    },
    "protocol": "HTTP/1.1",
    "requestId": "offlineContext_requestId_ck38asmqx000d26necwg8eki4",
    "requestTimeEpoch": 1574315489802,
    "resourceId": "offlineContext_resourceId",
    "resourcePath": "/users/{proxy*}",
    "stage": "local"
  },
  "resource": "/users/{proxy*}",
  "stageVariables": null,
  "isOffline": true
}

Parsed object of the above event:

{
  "path": "/users/me",
  "method": "GET",
  "query": {},
  "headers": {
    "host": "localhost:8101",
    "connection": "keep-alive",
    "origin": "http://localhost:8100",
    "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
    "content-type": "application/json",
    "accept": "*/*",
    "sec-fetch-site": "same-site",
    "sec-fetch-mode": "cors",
    "referrer": "http://localhost:8100/",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "en-US,en;q=0.9,th;q=0.8"
  },
  "ip": "127.0.0.1",
  "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36",
  "params": { "proxy": "me" },
  "body": {},
  "authorizer": {},
  "stage": "local"
}

Parsed Object

Field Name Type Description
path string Path of the query
query object Query string object
headers object HTTP headers
ip string IP Address of the client
userAgent string User Agent of the http client
params object Path parameters if defined in API Gateway
body object Body parsed as json or as urlencoded form data.
authorizer object Authorizer object
stage string Deployment stage

See also

@kdcio/api-gw-resp

Package Sidebar

Install

npm i @kdcio/api-gw-req

Weekly Downloads

3

Version

1.3.1

License

MIT

Unpacked Size

12.2 kB

Total Files

12

Last publish

Collaborators

  • ianpogi5
  • mbungalso