Skip to content

Commit c8da9a3

Browse files
committed
chore: simplify proxyEventV1ToWebRequest cognitive load
1 parent 8cec59b commit c8da9a3

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

packages/event-handler/src/rest/converters.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,15 @@ const createBody = (body: string | null, isBase64Encoded: boolean) => {
4444
};
4545

4646
/**
47-
* Converts an API Gateway proxy event to a Web API Request object.
47+
* Populates headers from single and multi-value header entries.
4848
*
49+
* @param headers - The Headers object to populate
4950
* @param event - The API Gateway proxy event
50-
* @returns A Web API Request object
5151
*/
52-
const proxyEventV1ToWebRequest = (event: APIGatewayProxyEvent): Request => {
53-
const { httpMethod, path } = event;
54-
const { domainName } = event.requestContext;
55-
56-
const headers = new Headers();
52+
const populateV1Headers = (
53+
headers: Headers,
54+
event: APIGatewayProxyEvent
55+
): void => {
5756
for (const [name, value] of Object.entries(event.headers ?? {})) {
5857
if (value !== undefined) headers.set(name, value);
5958
}
@@ -66,11 +65,15 @@ const proxyEventV1ToWebRequest = (event: APIGatewayProxyEvent): Request => {
6665
}
6766
}
6867
}
69-
const hostname = headers.get('Host') ?? domainName;
70-
const protocol = headers.get('X-Forwarded-Proto') ?? 'https';
71-
72-
const url = new URL(path, `${protocol}://${hostname}/`);
68+
};
7369

70+
/**
71+
* Populates URL search parameters from single and multi-value query string parameters.
72+
*
73+
* @param url - The URL object to populate
74+
* @param event - The API Gateway proxy event
75+
*/
76+
const populateV1QueryParams = (url: URL, event: APIGatewayProxyEvent): void => {
7477
for (const [name, value] of Object.entries(
7578
event.queryStringParameters ?? {}
7679
)) {
@@ -86,6 +89,27 @@ const proxyEventV1ToWebRequest = (event: APIGatewayProxyEvent): Request => {
8689
url.searchParams.append(name, value);
8790
}
8891
}
92+
};
93+
94+
/**
95+
* Converts an API Gateway proxy event to a Web API Request object.
96+
*
97+
* @param event - The API Gateway proxy event
98+
* @returns A Web API Request object
99+
*/
100+
const proxyEventV1ToWebRequest = (event: APIGatewayProxyEvent): Request => {
101+
const { httpMethod, path } = event;
102+
const { domainName } = event.requestContext;
103+
104+
const headers = new Headers();
105+
populateV1Headers(headers, event);
106+
107+
const hostname = headers.get('Host') ?? domainName;
108+
const protocol = headers.get('X-Forwarded-Proto') ?? 'https';
109+
110+
const url = new URL(path, `${protocol}://${hostname}/`);
111+
populateV1QueryParams(url, event);
112+
89113
return new Request(url.toString(), {
90114
method: httpMethod,
91115
headers,

0 commit comments

Comments
 (0)