Skip to content

Commit 326b9dc

Browse files
authored
Merge branch 'main' into dependabot/npm_and_yarn/main/biomejs/biome-2.2.4
2 parents 9900272 + d36ef55 commit 326b9dc

File tree

9 files changed

+95
-97
lines changed

9 files changed

+95
-97
lines changed

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

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Router {
7979
* Registers a custom error handler for specific error types.
8080
*
8181
* @param errorType - The error constructor(s) to handle
82-
* @param handler - The error handler function that returns an ErrorResponse
82+
* @param handler - The error handler that returns an error response
8383
*/
8484
public errorHandler<T extends Error>(
8585
errorType: ErrorConstructor<T> | ErrorConstructor<T>[],
@@ -106,7 +106,8 @@ class Router {
106106
/**
107107
* Registers a custom handler for 404 Not Found errors.
108108
*
109-
* @param handler - The error handler function for NotFoundError
109+
* @param handler - The error handler that returns an error
110+
* response
110111
*/
111112
public notFound(handler: ErrorHandler<NotFoundError>): void;
112113
public notFound(): MethodDecorator;
@@ -127,7 +128,7 @@ class Router {
127128
/**
128129
* Registers a custom handler for 405 Method Not Allowed errors.
129130
*
130-
* @param handler - The error handler function for MethodNotAllowedError
131+
* @param handler - The error handler that returns an error response
131132
*/
132133
public methodNotAllowed(handler: ErrorHandler<MethodNotAllowedError>): void;
133134
public methodNotAllowed(): MethodDecorator;
@@ -158,9 +159,9 @@ class Router {
158159
*
159160
* @example
160161
* ```typescript
161-
* const authMiddleware: Middleware = async (params, options, next) => {
162+
* const authMiddleware: Middleware = async (params, reqCtx, next) => {
162163
* // Authentication logic
163-
* if (!isAuthenticated(options.request)) {
164+
* if (!isAuthenticated(reqCtx.request)) {
164165
* return new Response('Unauthorized', { status: 401 });
165166
* }
166167
* await next();
@@ -184,13 +185,13 @@ class Router {
184185
* @param event - The Lambda event to resolve
185186
* @param context - The Lambda context
186187
* @param options - Optional resolve options for scope binding
187-
* @returns An API Gateway proxy result or undefined for incompatible events
188+
* @returns An API Gateway proxy result
188189
*/
189190
public async resolve(
190191
event: unknown,
191192
context: Context,
192193
options?: ResolveOptions
193-
): Promise<APIGatewayProxyResult | undefined> {
194+
): Promise<APIGatewayProxyResult> {
194195
if (!isAPIGatewayProxyEvent(event)) {
195196
this.logger.error(
196197
'Received an event that is not compatible with this resolver'
@@ -211,12 +212,12 @@ class Router {
211212

212213
const request = proxyEventToWebRequest(event);
213214

214-
const handlerOptions: RequestContext = {
215+
const requestContext: RequestContext = {
215216
event,
216217
context,
217218
request,
218219
// this response should be overwritten by the handler, if it isn't
219-
// it means somthing went wrong with the middleware chain
220+
// it means something went wrong with the middleware chain
220221
res: new Response('', { status: 500 }),
221222
};
222223

@@ -234,11 +235,11 @@ class Router {
234235
? route.handler.bind(options.scope)
235236
: route.handler;
236237

237-
const handlerMiddleware: Middleware = async (params, options, next) => {
238-
const handlerResult = await handler(params, options);
239-
options.res = handlerResultToWebResponse(
238+
const handlerMiddleware: Middleware = async (params, reqCtx, next) => {
239+
const handlerResult = await handler(params, reqCtx);
240+
reqCtx.res = handlerResultToWebResponse(
240241
handlerResult,
241-
options.res.headers
242+
reqCtx.res.headers
242243
);
243244

244245
await next();
@@ -252,18 +253,18 @@ class Router {
252253

253254
const middlewareResult = await middleware(
254255
route.params,
255-
handlerOptions,
256+
requestContext,
256257
() => Promise.resolve()
257258
);
258259

259260
// middleware result takes precedence to allow short-circuiting
260-
const result = middlewareResult ?? handlerOptions.res;
261+
const result = middlewareResult ?? requestContext.res;
261262

262263
return handlerResultToProxyResult(result);
263264
} catch (error) {
264265
this.logger.debug(`There was an error processing the request: ${error}`);
265266
const result = await this.handleError(error as Error, {
266-
...handlerOptions,
267+
...requestContext,
267268
scope: options?.scope,
268269
});
269270
return await webResponseToProxyResult(result);
@@ -284,7 +285,7 @@ class Router {
284285
* back to a default handler.
285286
*
286287
* @param error - The error to handle
287-
* @param options - Optional resolve options for scope binding
288+
* @param options - Error resolve options including request context and scope
288289
* @returns A Response object with appropriate status code and error details
289290
*/
290291
protected async handleError(
@@ -294,11 +295,8 @@ class Router {
294295
const handler = this.errorHandlerRegistry.resolve(error);
295296
if (handler !== null) {
296297
try {
297-
const { scope, ...handlerOptions } = options;
298-
const body = await handler.apply(scope ?? this, [
299-
error,
300-
handlerOptions,
301-
]);
298+
const { scope, ...reqCtx } = options;
299+
const body = await handler.apply(scope ?? this, [error, reqCtx]);
302300
return new Response(JSON.stringify(body), {
303301
status: body.statusCode,
304302
headers: { 'Content-Type': 'application/json' },

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export const isAPIGatewayProxyResult = (
153153
export const composeMiddleware = (middleware: Middleware[]): Middleware => {
154154
return async (
155155
params: Record<string, string>,
156-
options: RequestContext,
156+
reqCtx: RequestContext,
157157
next: () => Promise<HandlerResponse | void>
158158
): Promise<HandlerResponse | void> => {
159159
let index = -1;
@@ -172,7 +172,7 @@ export const composeMiddleware = (middleware: Middleware[]): Middleware => {
172172
}
173173

174174
const middlewareFn = middleware[i];
175-
const middlewareResult = await middlewareFn(params, options, () =>
175+
const middlewareResult = await middlewareFn(params, reqCtx, () =>
176176
dispatch(i + 1)
177177
);
178178

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type ErrorResolveOptions = RequestContext & ResolveOptions;
2525

2626
type ErrorHandler<T extends Error = Error> = (
2727
error: T,
28-
options: RequestContext
28+
reqCtx: RequestContext
2929
) => Promise<ErrorResponse>;
3030

3131
interface ErrorConstructor<T extends Error = Error> {
@@ -59,7 +59,7 @@ type HandlerResponse = Response | JSONObject;
5959
type RouteHandler<
6060
TParams = Record<string, unknown>,
6161
TReturn = HandlerResponse,
62-
> = (args: TParams, options: RequestContext) => Promise<TReturn>;
62+
> = (args: TParams, reqCtx: RequestContext) => Promise<TReturn>;
6363

6464
type HttpMethod = keyof typeof HttpVerbs;
6565

@@ -84,7 +84,7 @@ type NextFunction = () => Promise<HandlerResponse | void>;
8484

8585
type Middleware = (
8686
params: Record<string, string>,
87-
options: RequestContext,
87+
reqCtx: RequestContext,
8888
next: NextFunction
8989
) => Promise<void | HandlerResponse>;
9090

packages/event-handler/tests/unit/rest/ErrorHandlerRegistry.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88

99
const createErrorHandler =
1010
(statusCode: HttpStatusCode, message?: string) =>
11-
async (error: Error, _options: RequestContext) => ({
11+
async (error: Error, _reqCtx: RequestContext) => ({
1212
statusCode,
1313
error: error.name,
1414
message: message ?? error.message,

packages/event-handler/tests/unit/rest/Router/basic-routing.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ describe('Class: Router - Basic Routing', () => {
5050
context
5151
);
5252

53-
expect(result?.statusCode).toBe(HttpErrorCodes.METHOD_NOT_ALLOWED);
54-
expect(result?.body).toEqual('');
53+
expect(result.statusCode).toBe(HttpErrorCodes.METHOD_NOT_ALLOWED);
54+
expect(result.body).toEqual('');
5555
}
5656
);
5757

@@ -89,17 +89,17 @@ describe('Class: Router - Basic Routing', () => {
8989
const app = new Router();
9090
const testEvent = createTestEvent('/test', 'GET');
9191

92-
app.get('/test', async (_params, options) => {
92+
app.get('/test', async (_params, reqCtx) => {
9393
return {
94-
hasRequest: options.request instanceof Request,
95-
hasEvent: options.event === testEvent,
96-
hasContext: options.context === context,
94+
hasRequest: reqCtx.request instanceof Request,
95+
hasEvent: reqCtx.event === testEvent,
96+
hasContext: reqCtx.context === context,
9797
};
9898
});
9999

100100
// Act
101101
const result = await app.resolve(testEvent, context);
102-
const actual = JSON.parse(result?.body ?? '{}');
102+
const actual = JSON.parse(result.body);
103103

104104
// Assess
105105
expect(actual.hasRequest).toBe(true);

packages/event-handler/tests/unit/rest/Router/decorators.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,11 @@ describe('Class: Router - Decorators', () => {
395395

396396
class Lambda {
397397
@app.get('/test')
398-
public async getTest(_params: any, options: any) {
398+
public async getTest(_params: any, reqCtx: any) {
399399
return {
400-
hasRequest: options.request instanceof Request,
401-
hasEvent: options.event === testEvent,
402-
hasContext: options.context === context,
400+
hasRequest: reqCtx.request instanceof Request,
401+
hasEvent: reqCtx.event === testEvent,
402+
hasContext: reqCtx.context === context,
403403
};
404404
}
405405

@@ -412,7 +412,7 @@ describe('Class: Router - Decorators', () => {
412412

413413
// Act
414414
const result = await lambda.handler(testEvent, context);
415-
const actual = JSON.parse(result?.body ?? '{}');
415+
const actual = JSON.parse(result.body);
416416

417417
// Assess
418418
expect(actual.hasRequest).toBe(true);
@@ -427,14 +427,14 @@ describe('Class: Router - Decorators', () => {
427427

428428
class Lambda {
429429
@app.errorHandler(BadRequestError)
430-
public async handleBadRequest(error: BadRequestError, options: any) {
430+
public async handleBadRequest(error: BadRequestError, reqCtx: any) {
431431
return {
432432
statusCode: HttpErrorCodes.BAD_REQUEST,
433433
error: 'Bad Request',
434434
message: error.message,
435-
hasRequest: options.request instanceof Request,
436-
hasEvent: options.event === testEvent,
437-
hasContext: options.context === context,
435+
hasRequest: reqCtx.request instanceof Request,
436+
hasEvent: reqCtx.event === testEvent,
437+
hasContext: reqCtx.context === context,
438438
};
439439
}
440440

@@ -452,7 +452,7 @@ describe('Class: Router - Decorators', () => {
452452

453453
// Act
454454
const result = await lambda.handler(testEvent, context);
455-
const body = JSON.parse(result?.body ?? '{}');
455+
const body = JSON.parse(result.body);
456456

457457
// Assess
458458
expect(body.hasRequest).toBe(true);

packages/event-handler/tests/unit/rest/Router/error-handling.test.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ describe('Class: Router - Error Handling', () => {
120120
const result = await app.resolve(createTestEvent('/test', 'GET'), context);
121121

122122
// Assess
123-
expect(result?.statusCode).toBe(HttpErrorCodes.INTERNAL_SERVER_ERROR);
124-
const body = JSON.parse(result?.body ?? '{}');
123+
expect(result.statusCode).toBe(HttpErrorCodes.INTERNAL_SERVER_ERROR);
124+
const body = JSON.parse(result.body);
125125
expect(body.statusCode).toBe(HttpErrorCodes.INTERNAL_SERVER_ERROR);
126126
expect(body.error).toBe('Internal Server Error');
127127
expect(body.message).toBe('Internal Server Error');
@@ -140,8 +140,8 @@ describe('Class: Router - Error Handling', () => {
140140
const result = await app.resolve(createTestEvent('/test', 'GET'), context);
141141

142142
// Assess
143-
expect(result?.statusCode).toBe(HttpErrorCodes.INTERNAL_SERVER_ERROR);
144-
const body = JSON.parse(result?.body ?? '{}');
143+
expect(result.statusCode).toBe(HttpErrorCodes.INTERNAL_SERVER_ERROR);
144+
const body = JSON.parse(result.body);
145145
expect(body.statusCode).toBe(HttpErrorCodes.INTERNAL_SERVER_ERROR);
146146
expect(body.error).toBe('Internal Server Error');
147147
expect(body.message).toBe('Internal Server Error');
@@ -220,8 +220,8 @@ describe('Class: Router - Error Handling', () => {
220220
const result = await app.resolve(createTestEvent('/test', 'GET'), context);
221221

222222
// Assess
223-
expect(result?.statusCode).toBe(HttpErrorCodes.INTERNAL_SERVER_ERROR);
224-
const body = JSON.parse(result?.body ?? '{}');
223+
expect(result.statusCode).toBe(HttpErrorCodes.INTERNAL_SERVER_ERROR);
224+
const body = JSON.parse(result.body);
225225
expect(body.statusCode).toBe(HttpErrorCodes.INTERNAL_SERVER_ERROR);
226226
expect(body.error).toBe('Internal Server Error');
227227
expect(body.message).toBe('Internal Server Error');
@@ -242,8 +242,8 @@ describe('Class: Router - Error Handling', () => {
242242
const result = await app.resolve(createTestEvent('/test', 'GET'), context);
243243

244244
// Assess
245-
expect(result?.statusCode).toBe(HttpErrorCodes.INTERNAL_SERVER_ERROR);
246-
const body = JSON.parse(result?.body ?? '{}');
245+
expect(result.statusCode).toBe(HttpErrorCodes.INTERNAL_SERVER_ERROR);
246+
const body = JSON.parse(result.body);
247247
expect(body.statusCode).toBe(HttpErrorCodes.INTERNAL_SERVER_ERROR);
248248
expect(body.error).toBe('Internal Server Error');
249249
expect(body.message).toBe('debug error details');
@@ -363,21 +363,21 @@ describe('Class: Router - Error Handling', () => {
363363
const result = await app.resolve(createTestEvent('/test', 'GET'), context);
364364

365365
// Assess
366-
expect(result?.headers?.['content-type']).toBe('application/json');
366+
expect(result.headers?.['content-type']).toBe('application/json');
367367
});
368368

369369
it('passes request, event, and context to functional error handlers', async () => {
370370
// Prepare
371371
const app = new Router();
372372
const testEvent = createTestEvent('/test', 'GET');
373373

374-
app.errorHandler(BadRequestError, async (error, options) => ({
374+
app.errorHandler(BadRequestError, async (error, reqCtx) => ({
375375
statusCode: HttpErrorCodes.BAD_REQUEST,
376376
error: 'Bad Request',
377377
message: error.message,
378-
hasRequest: options.request instanceof Request,
379-
hasEvent: options.event === testEvent,
380-
hasContext: options.context === context,
378+
hasRequest: reqCtx.request instanceof Request,
379+
hasEvent: reqCtx.event === testEvent,
380+
hasContext: reqCtx.context === context,
381381
}));
382382

383383
app.get('/test', () => {
@@ -386,7 +386,7 @@ describe('Class: Router - Error Handling', () => {
386386

387387
// Act
388388
const result = await app.resolve(testEvent, context);
389-
const body = JSON.parse(result?.body ?? '{}');
389+
const body = JSON.parse(result.body);
390390

391391
// Assess
392392
expect(body.hasRequest).toBe(true);

0 commit comments

Comments
 (0)