Skip to content

Commit 5b4ee1a

Browse files
authored
chore(event-handler): rename variables to reflect that options object is now a RequestContext (#4460)
1 parent 23eddfd commit 5b4ee1a

File tree

9 files changed

+66
-68
lines changed

9 files changed

+66
-68
lines changed

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

Lines changed: 18 additions & 20 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();
@@ -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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ 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

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

Lines changed: 8 additions & 8 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

@@ -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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,13 @@ describe('Class: Router - Error Handling', () => {
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', () => {

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ describe('Class: Router - Middleware', () => {
4444
const app = new Router();
4545
const executionOrder: string[] = [];
4646

47-
app.use(async (_params, _options, next) => {
47+
app.use(async (_params, _reqCtx, next) => {
4848
executionOrder.push('global-middleware');
4949
await next();
5050
});
5151

5252
const middleware: Middleware[] = middlewareNames.map(
53-
(name) => async (_params, _options, next) => {
53+
(name) => async (_params, _reqCtx, next) => {
5454
executionOrder.push(name);
5555
await next();
5656
}
@@ -137,9 +137,9 @@ describe('Class: Router - Middleware', () => {
137137
let middlewareParams: Record<string, string> | undefined;
138138
let middlewareOptions: RequestContext | undefined;
139139

140-
app.use(async (params, options, next) => {
140+
app.use(async (params, reqCtx, next) => {
141141
middlewareParams = params;
142-
middlewareOptions = options;
142+
middlewareOptions = reqCtx;
143143
await next();
144144
});
145145

@@ -161,7 +161,7 @@ describe('Class: Router - Middleware', () => {
161161
vi.stubEnv('POWERTOOLS_DEV', 'true');
162162
const app = new Router();
163163

164-
app.use(async (_params, _options, next) => {
164+
app.use(async (_params, _reqCtx, next) => {
165165
await next();
166166
await next();
167167
});
@@ -215,7 +215,7 @@ describe('Class: Router - Middleware', () => {
215215
const app = new Router();
216216
const executionOrder: string[] = [];
217217

218-
app.use(async (_params, _options, next) => {
218+
app.use(async (_params, _reqCtx, next) => {
219219
executionOrder.push('middleware1-start');
220220
await next();
221221
executionOrder.push('middleware1-end');
@@ -336,10 +336,10 @@ describe('Class: Router - Middleware', () => {
336336
// Prepare
337337
const app = new Router();
338338

339-
app.use(async (_params, options, next) => {
339+
app.use(async (_params, reqCtx, next) => {
340340
await next();
341-
options.res.headers.set('x-custom-header', 'middleware-value');
342-
options.res.headers.set('x-request-id', '12345');
341+
reqCtx.res.headers.set('x-custom-header', 'middleware-value');
342+
reqCtx.res.headers.set('x-request-id', '12345');
343343
});
344344

345345
app.get('/test', async () => ({ success: true }));
@@ -367,10 +367,10 @@ describe('Class: Router - Middleware', () => {
367367
// Prepare
368368
const app = new Router();
369369

370-
app.use(async (_params, options, next) => {
370+
app.use(async (_params, reqCtx, next) => {
371371
await next();
372-
const originalBody = await options.res.text();
373-
options.res = new Response(`Modified: ${originalBody}`, {
372+
const originalBody = await reqCtx.res.text();
373+
reqCtx.res = new Response(`Modified: ${originalBody}`, {
374374
headers: { 'content-type': 'text/plain' },
375375
});
376376
});
@@ -396,8 +396,8 @@ describe('Class: Router - Middleware', () => {
396396
// Prepare
397397
const app = new Router();
398398

399-
app.use(async (_params, options, next) => {
400-
options.res.headers.set('x-before-handler', 'middleware-value');
399+
app.use(async (_params, reqCtx, next) => {
400+
reqCtx.res.headers.set('x-before-handler', 'middleware-value');
401401
await next();
402402
});
403403

@@ -425,14 +425,14 @@ describe('Class: Router - Middleware', () => {
425425
// Prepare
426426
const app = new Router();
427427

428-
app.use(async (_params, options, next) => {
429-
options.res.headers.set('x-test-header', 'before-next');
428+
app.use(async (_params, reqCtx, next) => {
429+
reqCtx.res.headers.set('x-test-header', 'before-next');
430430
await next();
431431
});
432432

433-
app.use(async (_params, options, next) => {
433+
app.use(async (_params, reqCtx, next) => {
434434
await next();
435-
options.res.headers.set('x-test-header', 'after-next');
435+
reqCtx.res.headers.set('x-test-header', 'after-next');
436436
});
437437

438438
app.get('/test', async () => ({ success: true }));
@@ -460,7 +460,7 @@ describe('Class: Router - Middleware', () => {
460460
const app = new Router();
461461
const executionOrder: string[] = [];
462462

463-
app.use(async (_params, _options, next) => {
463+
app.use(async (_params, _reqCtx, next) => {
464464
executionOrder.push('middleware-start');
465465
await next();
466466
executionOrder.push('middleware-end');

0 commit comments

Comments
 (0)