diff --git a/package-lock.json b/package-lock.json index 89ea757..72f3278 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,7 @@ "@silvermine/eslint-config": "3.2.0", "@silvermine/standardization": "2.0.0", "@silvermine/typescript-config": "1.0.0", - "@types/aws-lambda": "8.10.17", + "@types/aws-lambda": "8.10.140", "@types/chai": "4.1.7", "@types/cookie": "0.3.2", "@types/mocha": "5.2.5", @@ -1029,9 +1029,9 @@ } }, "node_modules/@types/aws-lambda": { - "version": "8.10.17", - "resolved": "https://registry.npmjs.org/@types/aws-lambda/-/aws-lambda-8.10.17.tgz", - "integrity": "sha512-KV/9fMAvv5428/v4+AP4nDOfSB4sJIBOGgBtv5YbuQVaAEapL1/Bc8IyACZ48Q3hYukVFjzAdVFq94zTxyd5Yw==", + "version": "8.10.140", + "resolved": "https://registry.npmjs.org/@types/aws-lambda/-/aws-lambda-8.10.140.tgz", + "integrity": "sha512-4Dh3dk2TUcbdfHrX0Al90mNGJDvA9NBiTQPzbrjGi/dLxzKCGOYgT8YQ47jUKNFALkAJAadifq0pzyjIUlhVhg==", "dev": true }, "node_modules/@types/chai": { diff --git a/package.json b/package.json index 594293f..12b4449 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "@silvermine/eslint-config": "3.2.0", "@silvermine/standardization": "2.0.0", "@silvermine/typescript-config": "1.0.0", - "@types/aws-lambda": "8.10.17", + "@types/aws-lambda": "8.10.140", "@types/chai": "4.1.7", "@types/cookie": "0.3.2", "@types/mocha": "5.2.5", diff --git a/src/Request.ts b/src/Request.ts index 654d810..6ffb03b 100644 --- a/src/Request.ts +++ b/src/Request.ts @@ -4,7 +4,7 @@ import qs from 'qs'; import cookie from 'cookie'; import Application from './Application'; import { RequestEvent, HandlerContext, RequestEventRequestContext, LambdaEventSourceType } from './request-response-types'; -import { StringMap, KeyValueStringObject, StringArrayOfStringsMap, StringUnknownMap } from '@silvermine/toolbox'; +import { StringMap, KeyValueStringObject, StringArrayOfStringsMap, StringUnknownMap, isUndefined } from '@silvermine/toolbox'; import ConsoleLogger from './logging/ConsoleLogger'; function safeDecode(s: string): string { @@ -479,6 +479,10 @@ export default class Request { const headers = evt.multiValueHeaders || _.mapObject(evt.headers, (v) => { return [ v ]; }); return _.reduce(headers, (memo: StringArrayOfStringsMap, v, k) => { + if (isUndefined(v)) { + return memo; + } + const key = k.toLowerCase(); memo[key] = v; @@ -554,7 +558,7 @@ export default class Request { } } - private _parseQuery(multiValQuery: StringArrayOfStringsMap, query: StringMap): { raw: string; parsed: KeyValueStringObject } { + private _parseQuery(multiValQuery: Partial, query: Partial): { raw: string; parsed: KeyValueStringObject } { let queryString; // It may seem strange to encode the URI components immediately after decoding them. @@ -566,11 +570,15 @@ export default class Request { // values that were not correct. if (_.isEmpty(multiValQuery)) { queryString = _.reduce(query, (memo, v, k) => { + if (isUndefined(v)) { + return memo; + } + return memo + `&${k}=${encodeURIComponent(safeDecode(v))}`; }, ''); } else { queryString = _.reduce(multiValQuery, (memo, vals, k) => { - _.each(vals, (v) => { + _.each(vals || [], (v) => { memo += `&${k}=${encodeURIComponent(safeDecode(v))}`; }); return memo; diff --git a/src/request-response-types.ts b/src/request-response-types.ts index ebf50ea..dcda2c8 100644 --- a/src/request-response-types.ts +++ b/src/request-response-types.ts @@ -5,8 +5,10 @@ import { APIGatewayProxyEvent, Context, APIGatewayProxyResult, + ALBEvent, + ALBEventRequestContext, } from 'aws-lambda'; -import { StringMap, StringArrayOfStringsMap } from '@silvermine/toolbox'; +import { StringArrayOfStringsMap } from '@silvermine/toolbox'; /* COMBO TYPES */ @@ -54,25 +56,14 @@ export interface APIGatewayRequestEvent extends APIGatewayProxyEvent {} export interface APIGatewayEventRequestContext extends OrigAPIGatewayEventRequestContext {} -/* APPLICATION LOAD BALANCER TYPES (these are not yet included in aws-lambda) */ +/* APPLICATION LOAD BALANCER TYPES (we export these with our own names to make it easier +to modify them if needed at a later time) */ -export interface ApplicationLoadBalancerRequestEvent { - body: string | null; - httpMethod: string; - isBase64Encoded: boolean; - path: string; - headers?: StringMap; - multiValueHeaders?: StringArrayOfStringsMap; - queryStringParameters?: StringMap; - multiValueQueryStringParameters?: StringArrayOfStringsMap; - requestContext: ApplicationLoadBalancerEventRequestContext; -} +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface ApplicationLoadBalancerRequestEvent extends ALBEvent {} -export interface ApplicationLoadBalancerEventRequestContext { - elb: { - targetGroupArn: string; - }; -} +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface ApplicationLoadBalancerEventRequestContext extends ALBEventRequestContext {} /* OTHER TYPES RELATED TO REQUESTS AND RESPONSES */ diff --git a/tests/samples.ts b/tests/samples.ts index 4c18cf6..4f6c6a8 100644 --- a/tests/samples.ts +++ b/tests/samples.ts @@ -14,7 +14,7 @@ export const handlerContext = (fillAllFields: boolean = false): Context => { logGroupName: '/aws/lambda/echo-api-prd-echo', logStreamName: '2019/01/31/[$LATEST]bb001267fb004ffa8e1710bba30b4ae7', functionName: 'echo-api-prd-echo', - memoryLimitInMB: 1024, + memoryLimitInMB: '1024', functionVersion: '$LATEST', awsRequestId: 'ed6cac60-bb31-4c1f-840d-dd34c80eb9a3', invokedFunctionArn: 'arn:aws:lambda:us-east-1:123456789012:function:echo-api-prd-echo', @@ -63,16 +63,19 @@ export const apiGatewayRequestContext = (): APIGatewayEventRequestContext => { apiKey: null, apiKeyId: null, caller: null, + clientCert: null, cognitoAuthenticationProvider: null, cognitoAuthenticationType: null, cognitoIdentityId: null, cognitoIdentityPoolId: null, + principalOrgId: null, sourceIp: '12.12.12.12', user: null, userAgent: 'curl/7.54.0', userArn: null, }, path: '/prd', + protocol: 'HTTP/1.1', stage: 'prd', requestId: 'a507736b-259e-11e9-8fcf-4f1f08c4591e', requestTimeEpoch: 1548969891530,