Skip to content

Commit 70181fa

Browse files
adamthom-amznsrchase
authored andcommitted
Add support for payload format 1.0
1 parent 1d13f86 commit 70181fa

File tree

2 files changed

+89
-8
lines changed

2 files changed

+89
-8
lines changed

smithy-typescript-ssdk-libs/server-apigateway/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
"prepublishOnly": "yarn build",
99
"pretest": "yarn build",
1010
"build": "tsc -p tsconfig.json",
11-
"postbuild": "downlevel-dts dist/types dist/types/ts3.4",
11+
"postbuild": "rimraf dist/types/ts3.4 && downlevel-dts dist/types dist/types/ts3.4",
1212
"test": "jest"
1313
},
1414
"repository": {
1515
"type": "git",
1616
"url": "git+https://github.com/awslabs/smithy-typescript.git",
17-
"directory": "smithy-typescript-libs/smithy-server-apigateway"
17+
"directory": "smithy-typescript-ssdk-libs/server-apigateway"
1818
},
1919
"author": "AWS Smithy Team",
2020
"license": "Apache-2.0",
@@ -29,7 +29,8 @@
2929
"@types/node": "^14.14.37",
3030
"downlevel-dts": "^0.7.0",
3131
"jest": "^26.1.0",
32-
"typescript": "~4.2.2"
32+
"typescript": "~4.2.2",
33+
"rimraf": "^3.0.2"
3334
},
3435
"files": [
3536
"dist/cjs/**/*.js",

smithy-typescript-ssdk-libs/server-apigateway/src/lambda.ts

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,39 @@
1616
import { HttpRequest, HttpResponse } from "@aws-sdk/protocol-http";
1717
import { HeaderBag, QueryParameterBag } from "@aws-sdk/types";
1818
import {
19+
APIGatewayProxyEvent,
1920
APIGatewayProxyEventHeaders,
21+
APIGatewayProxyEventMultiValueHeaders,
22+
APIGatewayProxyEventMultiValueQueryStringParameters,
2023
APIGatewayProxyEventQueryStringParameters,
2124
APIGatewayProxyEventV2,
25+
APIGatewayProxyResult,
2226
APIGatewayProxyResultV2,
2327
} from "aws-lambda";
2428
import { Readable } from "stream";
2529

26-
export function convertEvent(event: APIGatewayProxyEventV2): HttpRequest {
30+
export function convertEvent(event: APIGatewayProxyEvent): HttpRequest;
31+
export function convertEvent(event: APIGatewayProxyEventV2): HttpRequest;
32+
33+
export function convertEvent(event: APIGatewayProxyEvent | APIGatewayProxyEventV2): HttpRequest {
34+
if (isV2Event(event)) {
35+
return convertV2Event(event);
36+
} else {
37+
return convertV1Event(event);
38+
}
39+
}
40+
41+
function convertV1Event(event: APIGatewayProxyEvent): HttpRequest {
42+
return new HttpRequest({
43+
method: event.httpMethod,
44+
headers: convertMultiValueHeaders(event.multiValueHeaders),
45+
query: convertMultiValueQueryStringParameters(event.multiValueQueryStringParameters),
46+
path: event.path,
47+
...(event.body ? { body: Readable.from(Buffer.from(event.body, event.isBase64Encoded ? "base64" : "utf8")) } : {}),
48+
});
49+
}
50+
51+
function convertV2Event(event: APIGatewayProxyEventV2): HttpRequest {
2752
return new HttpRequest({
2853
method: event.requestContext.http.method,
2954
headers: convertHeaders(event.headers),
@@ -33,6 +58,7 @@ export function convertEvent(event: APIGatewayProxyEventV2): HttpRequest {
3358
});
3459
}
3560

61+
export const convertVersion2Response = convertResponse;
3662
export function convertResponse(response: HttpResponse): APIGatewayProxyResultV2 {
3763
return {
3864
statusCode: response.statusCode,
@@ -42,14 +68,69 @@ export function convertResponse(response: HttpResponse): APIGatewayProxyResultV2
4268
};
4369
}
4470

71+
export function convertVersion1Response(response: HttpResponse): APIGatewayProxyResult {
72+
return {
73+
statusCode: response.statusCode,
74+
multiValueHeaders: convertResponseHeaders(response.headers),
75+
body: response.body,
76+
isBase64Encoded: false,
77+
};
78+
}
79+
function convertResponseHeaders(headers: HeaderBag) {
80+
const retVal: { [key: string]: string[] } = {};
81+
for (const [key, val] of Object.entries(headers)) {
82+
retVal[key] = val.split(",").map((v) => v.trim());
83+
}
84+
return retVal;
85+
}
86+
87+
function isV2Event(event: APIGatewayProxyEvent | APIGatewayProxyEventV2): event is APIGatewayProxyEventV2 {
88+
return hasVersion(event) && event.version === "2.0";
89+
}
90+
91+
function hasVersion(event: any): event is Record<"version", string> {
92+
return event.hasOwnProperty("version");
93+
}
94+
95+
function convertMultiValueHeaders(multiValueHeaders: APIGatewayProxyEventMultiValueHeaders | null) {
96+
const retVal: { [key: string]: string } = {};
97+
98+
if (multiValueHeaders === null) {
99+
return retVal;
100+
}
101+
102+
for (const [key, val] of Object.entries(multiValueHeaders)) {
103+
if (val !== undefined) {
104+
retVal[key] = val.join(", ");
105+
}
106+
}
107+
108+
return retVal;
109+
}
110+
45111
// TODO: this can be rewritten with arrow functions / Object.fromEntries / filter
46112
// but first we need to split up generated client and servers so we can have different
47113
// language version targets.
48114
function convertHeaders(headers: APIGatewayProxyEventHeaders): HeaderBag {
49115
const retVal: { [key: string]: string } = {};
50116

51-
for (const key in headers) {
52-
const val = headers[key];
117+
for (const [key, val] of Object.entries(headers)) {
118+
if (val !== undefined) {
119+
retVal[key] = val;
120+
}
121+
}
122+
123+
return retVal;
124+
}
125+
126+
function convertMultiValueQueryStringParameters(params: APIGatewayProxyEventMultiValueQueryStringParameters | null) {
127+
if (params === undefined || params === null) {
128+
return undefined;
129+
}
130+
131+
const retVal: { [key: string]: string[] } = {};
132+
133+
for (const [key, val] of Object.entries(params)) {
53134
if (val !== undefined) {
54135
retVal[key] = val;
55136
}
@@ -66,8 +147,7 @@ function convertQuery(params: APIGatewayProxyEventQueryStringParameters | undefi
66147

67148
const retVal: { [key: string]: string | string[] } = {};
68149

69-
for (const key in params) {
70-
const val = params[key];
150+
for (const [key, val] of Object.entries(params)) {
71151
if (val !== undefined) {
72152
if (val.indexOf(",") !== -1) {
73153
retVal[key] = val;

0 commit comments

Comments
 (0)