Skip to content

Commit f27c945

Browse files
committed
changed the middleware signature to object
1 parent 0a16f5e commit f27c945

File tree

8 files changed

+93
-68
lines changed

8 files changed

+93
-68
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class Router {
164164
*
165165
* @example
166166
* ```typescript
167-
* const authMiddleware: Middleware = async (params, reqCtx, next) => {
167+
* const authMiddleware: Middleware = async ({params, reqCtx, next}) => {
168168
* // Authentication logic
169169
* if (!isAuthenticated(reqCtx.request)) {
170170
* return new Response('Unauthorized', { status: 401 });
@@ -231,7 +231,11 @@ class Router {
231231

232232
const route = this.routeRegistry.resolve(method, path);
233233

234-
const handlerMiddleware: Middleware = async (params, reqCtx, next) => {
234+
const handlerMiddleware: Middleware = async ({
235+
params,
236+
reqCtx,
237+
next,
238+
}) => {
235239
if (route === null) {
236240
const notFoundRes = await this.handleError(
237241
new NotFoundError(`Route ${path} for method ${method} not found`),
@@ -263,11 +267,11 @@ class Router {
263267
handlerMiddleware,
264268
]);
265269

266-
const middlewareResult = await middleware(
267-
route?.params ?? {},
268-
requestContext,
269-
() => Promise.resolve()
270-
);
270+
const middlewareResult = await middleware({
271+
params: route?.params ?? {},
272+
reqCtx: requestContext,
273+
next: () => Promise.resolve(),
274+
});
271275

272276
// middleware result takes precedence to allow short-circuiting
273277
const result = middlewareResult ?? requestContext.res;

packages/event-handler/src/rest/middleware/compress.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const compress = (options?: CompressionOptions): Middleware => {
6767
const threshold =
6868
options?.threshold ?? DEFAULT_COMPRESSION_RESPONSE_THRESHOLD;
6969

70-
return async (_, reqCtx, next) => {
70+
return async ({ reqCtx, next }) => {
7171
await next();
7272

7373
if (

packages/event-handler/src/rest/middleware/cors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export const cors = (options?: CorsOptions): Middleware => {
9696
}
9797
};
9898

99-
return async (_params, reqCtx, next) => {
99+
return async ({ reqCtx, next }) => {
100100
const requestOrigin = reqCtx.request.headers.get('Origin');
101101
if (!isOriginAllowed(requestOrigin)) {
102102
await next();

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ export const isAPIGatewayProxyResult = (
129129
*
130130
* @example
131131
* ```typescript
132-
* const middleware1: Middleware = async (params, options, next) => {
132+
* const middleware1: Middleware = async ({params, options, next}) => {
133133
* console.log('middleware1 start');
134134
* await next();
135135
* console.log('middleware1 end');
136136
* };
137137
*
138-
* const middleware2: Middleware = async (params, options, next) => {
138+
* const middleware2: Middleware = async ({params, options, next}) => {
139139
* console.log('middleware2 start');
140140
* await next();
141141
* console.log('middleware2 end');
@@ -151,11 +151,7 @@ export const isAPIGatewayProxyResult = (
151151
* ```
152152
*/
153153
export const composeMiddleware = (middleware: Middleware[]): Middleware => {
154-
return async (
155-
params: Record<string, string>,
156-
reqCtx: RequestContext,
157-
next: () => Promise<HandlerResponse | void>
158-
): Promise<HandlerResponse | void> => {
154+
return async ({ params, reqCtx, next }): Promise<HandlerResponse | void> => {
159155
let index = -1;
160156
let result: HandlerResponse | undefined;
161157

@@ -181,7 +177,11 @@ export const composeMiddleware = (middleware: Middleware[]): Middleware => {
181177
return result;
182178
};
183179

184-
const middlewareResult = await middlewareFn(params, reqCtx, nextFn);
180+
const middlewareResult = await middlewareFn({
181+
params,
182+
reqCtx,
183+
next: nextFn,
184+
});
185185

186186
if (nextPromise && !nextAwaited && i < middleware.length - 1) {
187187
throw new Error(

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ type RestRouteOptions = {
8686

8787
type NextFunction = () => Promise<HandlerResponse | void>;
8888

89-
type Middleware = (
90-
params: Record<string, string>,
91-
reqCtx: RequestContext,
92-
next: NextFunction
93-
) => Promise<void | HandlerResponse>;
89+
type Middleware = (args: {
90+
params: Record<string, string>;
91+
reqCtx: RequestContext;
92+
next: NextFunction;
93+
}) => Promise<void | HandlerResponse>;
9494

9595
type RouteRegistryOptions = {
9696
/**

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

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

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

5252
const middleware: Middleware[] = middlewareNames.map(
53-
(name) => async (_params, _reqCtx, next) => {
54-
executionOrder.push(name);
55-
await next();
56-
}
53+
(name) =>
54+
async ({ next }) => {
55+
executionOrder.push(name);
56+
await next();
57+
}
5758
);
5859

5960
app.get(path as Path, middleware, async () => {
@@ -137,7 +138,7 @@ describe('Class: Router - Middleware', () => {
137138
let middlewareParams: Record<string, string> | undefined;
138139
let middlewareOptions: RequestContext | undefined;
139140

140-
app.use(async (params, reqCtx, next) => {
141+
app.use(async ({ params, reqCtx, next }) => {
141142
middlewareParams = params;
142143
middlewareOptions = reqCtx;
143144
await next();
@@ -161,7 +162,7 @@ describe('Class: Router - Middleware', () => {
161162
vi.stubEnv('POWERTOOLS_DEV', 'true');
162163
const app = new Router();
163164

164-
app.use(async (_params, _reqCtx, next) => {
165+
app.use(async ({ next }) => {
165166
await next();
166167
await next();
167168
});
@@ -185,11 +186,11 @@ describe('Class: Router - Middleware', () => {
185186
vi.stubEnv('POWERTOOLS_DEV', 'true');
186187
const app = new Router();
187188

188-
app.use(async (_params, _reqCtx, next) => {
189+
app.use(async ({ next }) => {
189190
await next();
190191
});
191192

192-
app.use(async (_params, _reqCtx, next) => {
193+
app.use(async ({ next }) => {
193194
next();
194195
});
195196

@@ -241,7 +242,7 @@ describe('Class: Router - Middleware', () => {
241242
const app = new Router();
242243
const executionOrder: string[] = [];
243244

244-
app.use(async (_params, _reqCtx, next) => {
245+
app.use(async ({ next }) => {
245246
executionOrder.push('middleware1-start');
246247
await next();
247248
executionOrder.push('middleware1-end');
@@ -362,7 +363,7 @@ describe('Class: Router - Middleware', () => {
362363
// Prepare
363364
const app = new Router();
364365

365-
app.use(async (_params, reqCtx, next) => {
366+
app.use(async ({ reqCtx, next }) => {
366367
await next();
367368
reqCtx.res.headers.set('x-custom-header', 'middleware-value');
368369
reqCtx.res.headers.set('x-request-id', '12345');
@@ -393,7 +394,7 @@ describe('Class: Router - Middleware', () => {
393394
// Prepare
394395
const app = new Router();
395396

396-
app.use(async (_params, reqCtx, next) => {
397+
app.use(async ({ reqCtx, next }) => {
397398
await next();
398399
const originalBody = await reqCtx.res.text();
399400
reqCtx.res = new Response(`Modified: ${originalBody}`, {
@@ -422,7 +423,7 @@ describe('Class: Router - Middleware', () => {
422423
// Prepare
423424
const app = new Router();
424425

425-
app.use(async (_params, reqCtx, next) => {
426+
app.use(async ({ reqCtx, next }) => {
426427
reqCtx.res.headers.set('x-before-handler', 'middleware-value');
427428
await next();
428429
});
@@ -451,7 +452,7 @@ describe('Class: Router - Middleware', () => {
451452
// Prepare
452453
const app = new Router();
453454

454-
app.use(async (_params, reqCtx, next) => {
455+
app.use(async ({ reqCtx, next }) => {
455456
reqCtx.res.headers.set('x-before-handler', 'middleware-value');
456457
await next();
457458
});
@@ -478,12 +479,12 @@ describe('Class: Router - Middleware', () => {
478479
// Prepare
479480
const app = new Router();
480481

481-
app.use(async (_params, reqCtx, next) => {
482+
app.use(async ({ reqCtx, next }) => {
482483
reqCtx.res.headers.set('x-test-header', 'before-next');
483484
await next();
484485
});
485486

486-
app.use(async (_params, reqCtx, next) => {
487+
app.use(async ({ reqCtx, next }) => {
487488
await next();
488489
reqCtx.res.headers.set('x-test-header', 'after-next');
489490
});
@@ -531,7 +532,7 @@ describe('Class: Router - Middleware', () => {
531532
const app = new Router();
532533
const executionOrder: string[] = [];
533534

534-
app.use(async (_params, _reqCtx, next) => {
535+
app.use(async ({ next }) => {
535536
executionOrder.push('middleware-start');
536537
await next();
537538
executionOrder.push('middleware-end');

packages/event-handler/tests/unit/rest/helpers.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const createTrackingMiddleware = (
2828
name: string,
2929
executionOrder: string[]
3030
): Middleware => {
31-
return async (_params, _options, next) => {
31+
return async ({ next }) => {
3232
executionOrder.push(`${name}-start`);
3333
await next();
3434
executionOrder.push(`${name}-end`);
@@ -40,7 +40,7 @@ export const createThrowingMiddleware = (
4040
executionOrder: string[],
4141
errorMessage: string
4242
): Middleware => {
43-
return async (_params, _options, _next) => {
43+
return async () => {
4444
executionOrder.push(name);
4545
throw new Error(errorMessage);
4646
};
@@ -51,7 +51,7 @@ export const createReturningMiddleware = (
5151
executionOrder: string[],
5252
response: any
5353
): Middleware => {
54-
return async (_params, _options, _next) => {
54+
return async () => {
5555
executionOrder.push(name);
5656
return response;
5757
};
@@ -61,7 +61,7 @@ export const createNoNextMiddleware = (
6161
name: string,
6262
executionOrder: string[]
6363
): Middleware => {
64-
return async (_params, _options, _next) => {
64+
return async () => {
6565
executionOrder.push(name);
6666
// Intentionally doesn't call next()
6767
};
@@ -70,21 +70,21 @@ export const createNoNextMiddleware = (
7070
export const createSettingHeadersMiddleware = (headers: {
7171
[key: string]: string;
7272
}): Middleware => {
73-
return async (_params, options, next) => {
73+
return async ({ reqCtx, next }) => {
7474
await next();
7575
Object.entries(headers).forEach(([key, value]) => {
76-
options.res.headers.set(key, value);
76+
reqCtx.res.headers.set(key, value);
7777
});
7878
};
7979
};
8080

8181
export const createHeaderCheckMiddleware = (headers: {
8282
[key: string]: string;
8383
}): Middleware => {
84-
return async (_params, options, next) => {
85-
options.res.headers.forEach((value, key) => {
84+
return async ({ reqCtx, next }) => {
85+
reqCtx.res.headers.forEach((value, key) => {
8686
headers[key] = value;
8787
});
8888
await next();
8989
};
90-
};
90+
};

0 commit comments

Comments
 (0)