1.3.1 • Published 3 years ago

@kdcio/api-gw-req v1.3.1

Weekly downloads
89
License
MIT
Repository
github
Last release
3 years ago

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 NameTypeDescription
pathstringPath of the query
queryobjectQuery string object
headersobjectHTTP headers
ipstringIP Address of the client
userAgentstringUser Agent of the http client
paramsobjectPath parameters if defined in API Gateway
bodyobjectBody parsed as json or as urlencoded form data.
authorizerobjectAuthorizer object
stagestringDeployment stage

See also

@kdcio/api-gw-resp

1.3.1

3 years ago

1.3.0

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.0

4 years ago