Skip to content

Commit 21ca074

Browse files
committed
pre-calculate response type as save on request context
1 parent c4375ac commit 21ca074

File tree

4 files changed

+61
-79
lines changed

4 files changed

+61
-79
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ class Router {
217217
throw new InvalidEventError();
218218
}
219219

220+
const responseType = isAPIGatewayProxyEventV2(event) ? 'v2' : 'v1';
221+
220222
let req: Request;
221223
try {
222224
req = proxyEventToWebRequest(event);
@@ -231,6 +233,7 @@ class Router {
231233
req: new Request('https://invalid'),
232234
res: new Response('', { status: HttpStatusCodes.METHOD_NOT_ALLOWED }),
233235
params: {},
236+
responseType,
234237
};
235238
}
236239
throw err;
@@ -244,6 +247,7 @@ class Router {
244247
// it means something went wrong with the middleware chain
245248
res: new Response('', { status: HttpStatusCodes.INTERNAL_SERVER_ERROR }),
246249
params: {},
250+
responseType,
247251
};
248252

249253
try {
@@ -346,7 +350,7 @@ class Router {
346350
options?: ResolveOptions
347351
): Promise<APIGatewayProxyResult | APIGatewayProxyStructuredResultV2> {
348352
const reqCtx = await this.#resolve(event, context, options);
349-
return webResponseToProxyResult(reqCtx.res, reqCtx.event);
353+
return webResponseToProxyResult(reqCtx.res, reqCtx.responseType);
350354
}
351355

352356
/**
@@ -380,7 +384,7 @@ class Router {
380384
) {
381385
const { headers } = webHeadersToApiGatewayHeaders(
382386
reqCtx.res.headers,
383-
reqCtx.event
387+
reqCtx.responseType
384388
);
385389
const resStream = HttpResponseStream.from(responseStream, {
386390
statusCode: reqCtx.res.status,

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

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import type {
1111
ExtendedAPIGatewayProxyResult,
1212
ExtendedAPIGatewayProxyResultBody,
1313
HandlerResponse,
14+
ResponseType,
15+
ResponseTypeMap,
1416
V1Headers,
1517
} from '../types/rest.js';
1618
import { COMPRESSION_ENCODING_TYPES } from './constants.js';
1719
import { InvalidHttpMethodError } from './errors.js';
1820
import {
19-
isAPIGatewayProxyEventV1,
2021
isAPIGatewayProxyEventV2,
2122
isExtendedAPIGatewayProxyResult,
2223
isHttpMethod,
@@ -194,27 +195,19 @@ const webHeadersToApiGatewayV2Headers = (webHeaders: Headers) => {
194195
return { headers };
195196
};
196197

197-
function webHeadersToApiGatewayHeaders(
198+
const webHeadersToApiGatewayHeaders = <T extends ResponseType>(
198199
webHeaders: Headers,
199-
event: APIGatewayProxyEventV2
200-
): { headers: Record<string, string> };
201-
function webHeadersToApiGatewayHeaders(
202-
webHeaders: Headers,
203-
event: APIGatewayProxyEvent
204-
): V1Headers;
205-
function webHeadersToApiGatewayHeaders(
206-
webHeaders: Headers,
207-
event: APIGatewayProxyEvent | APIGatewayProxyEventV2
208-
): { headers: Record<string, string> } | V1Headers;
209-
function webHeadersToApiGatewayHeaders(
210-
webHeaders: Headers,
211-
event: APIGatewayProxyEvent | APIGatewayProxyEventV2
212-
): { headers: Record<string, string> } | V1Headers {
213-
if (isAPIGatewayProxyEventV1(event)) {
214-
return webHeadersToApiGatewayV1Headers(webHeaders);
200+
responseType: T
201+
): T extends 'v1' ? V1Headers : { headers: Record<string, string> } => {
202+
if (responseType === 'v1') {
203+
return webHeadersToApiGatewayV1Headers(webHeaders) as T extends 'v1'
204+
? V1Headers
205+
: { headers: Record<string, string> };
215206
}
216-
return webHeadersToApiGatewayV2Headers(webHeaders);
217-
}
207+
return webHeadersToApiGatewayV2Headers(webHeaders) as T extends 'v1'
208+
? V1Headers
209+
: { headers: Record<string, string> };
210+
};
218211

219212
/**
220213
* Converts a Web API Response object to an API Gateway V1 proxy result.
@@ -320,27 +313,15 @@ const webResponseToProxyResultV2 = async (
320313
return result;
321314
};
322315

323-
function webResponseToProxyResult(
316+
const webResponseToProxyResult = <T extends ResponseType>(
324317
response: Response,
325-
event: APIGatewayProxyEventV2
326-
): Promise<APIGatewayProxyStructuredResultV2>;
327-
function webResponseToProxyResult(
328-
response: Response,
329-
event: APIGatewayProxyEvent
330-
): Promise<APIGatewayProxyResult>;
331-
function webResponseToProxyResult(
332-
response: Response,
333-
event: APIGatewayProxyEvent | APIGatewayProxyEventV2
334-
): Promise<APIGatewayProxyResult | APIGatewayProxyStructuredResultV2>;
335-
function webResponseToProxyResult(
336-
response: Response,
337-
event: APIGatewayProxyEvent | APIGatewayProxyEventV2
338-
): Promise<APIGatewayProxyResult | APIGatewayProxyStructuredResultV2> {
339-
if (isAPIGatewayProxyEventV1(event)) {
340-
return webResponseToProxyResultV1(response);
318+
responseType: T
319+
): Promise<ResponseTypeMap[T]> => {
320+
if (responseType === 'v1') {
321+
return webResponseToProxyResultV1(response) as Promise<ResponseTypeMap[T]>;
341322
}
342-
return webResponseToProxyResultV2(response);
343-
}
323+
return webResponseToProxyResultV2(response) as Promise<ResponseTypeMap[T]>;
324+
};
344325

345326
/**
346327
* Adds headers from an ExtendedAPIGatewayProxyResult to a Headers object.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,28 @@ import type {
77
APIGatewayProxyEvent,
88
APIGatewayProxyEventV2,
99
APIGatewayProxyResult,
10+
APIGatewayProxyStructuredResultV2,
1011
Context,
1112
} from 'aws-lambda';
1213
import type { HttpStatusCodes, HttpVerbs } from '../rest/constants.js';
1314
import type { Route } from '../rest/Route.js';
1415
import type { HttpResponseStream } from '../rest/utils.js';
1516
import type { ResolveOptions } from './common.js';
1617

18+
type ResponseType = 'v1' | 'v2';
19+
20+
type ResponseTypeMap = {
21+
v1: APIGatewayProxyResult;
22+
v2: APIGatewayProxyStructuredResultV2;
23+
};
24+
1725
type RequestContext = {
1826
req: Request;
1927
event: APIGatewayProxyEvent | APIGatewayProxyEventV2;
2028
context: Context;
2129
res: Response;
2230
params: Record<string, string>;
31+
responseType: ResponseType;
2332
};
2433

2534
type ErrorResolveOptions = RequestContext & ResolveOptions;
@@ -237,6 +246,8 @@ export type {
237246
Middleware,
238247
Path,
239248
RequestContext,
249+
ResponseType,
250+
ResponseTypeMap,
240251
RestRouterOptions,
241252
RouteHandler,
242253
ResolveStreamOptions,

0 commit comments

Comments
 (0)