Skip to content

Commit 278fca0

Browse files
authored
improv(event-handler): changed path parameter in middleware and routehandler signature (#4532)
1 parent 7ea49c7 commit 278fca0

File tree

7 files changed

+16
-21
lines changed

7 files changed

+16
-21
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,15 @@ class Router {
224224
// this response should be overwritten by the handler, if it isn't
225225
// it means something went wrong with the middleware chain
226226
res: new Response('', { status: 500 }),
227+
params: {},
227228
};
228229

229230
try {
230231
const path = new URL(req.url).pathname as Path;
231232

232233
const route = this.routeRegistry.resolve(method, path);
233234

234-
const handlerMiddleware: Middleware = async ({
235-
params,
236-
reqCtx,
237-
next,
238-
}) => {
235+
const handlerMiddleware: Middleware = async ({ reqCtx, next }) => {
239236
if (route === null) {
240237
const notFoundRes = await this.handleError(
241238
new NotFoundError(`Route ${path} for method ${method} not found`),
@@ -251,7 +248,7 @@ class Router {
251248
? route.handler
252249
: route.handler.bind(options.scope);
253250

254-
const handlerResult = await handler(params, reqCtx);
251+
const handlerResult = await handler(reqCtx);
255252
reqCtx.res = handlerResultToWebResponse(
256253
handlerResult,
257254
reqCtx.res.headers
@@ -267,8 +264,8 @@ class Router {
267264
handlerMiddleware,
268265
]);
269266

267+
requestContext.params = route?.params ?? {};
270268
const middlewareResult = await middleware({
271-
params: route?.params ?? {},
272269
reqCtx: requestContext,
273270
next: () => Promise.resolve(),
274271
});

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export const isAPIGatewayProxyResult = (
150150
* ```
151151
*/
152152
export const composeMiddleware = (middleware: Middleware[]): Middleware => {
153-
return async ({ params, reqCtx, next }): Promise<HandlerResponse | void> => {
153+
return async ({ reqCtx, next }): Promise<HandlerResponse | void> => {
154154
let index = -1;
155155
let result: HandlerResponse | undefined;
156156

@@ -177,7 +177,6 @@ export const composeMiddleware = (middleware: Middleware[]): Middleware => {
177177
};
178178

179179
const middlewareResult = await middlewareFn({
180-
params,
181180
reqCtx,
182181
next: nextFn,
183182
});

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type RequestContext = {
1919
event: APIGatewayProxyEvent;
2020
context: Context;
2121
res: Response;
22+
params: Record<string, string>;
2223
};
2324

2425
type ErrorResolveOptions = RequestContext & ResolveOptions;
@@ -60,10 +61,9 @@ type DynamicRoute = Route & CompiledRoute;
6061

6162
type HandlerResponse = Response | JSONObject;
6263

63-
type RouteHandler<
64-
TParams = Record<string, unknown>,
65-
TReturn = HandlerResponse,
66-
> = (args: TParams, reqCtx: RequestContext) => Promise<TReturn>;
64+
type RouteHandler<TReturn = HandlerResponse> = (
65+
reqCtx: RequestContext
66+
) => Promise<TReturn>;
6767

6868
type HttpMethod = keyof typeof HttpVerbs;
6969

@@ -87,7 +87,6 @@ type RestRouteOptions = {
8787
type NextFunction = () => Promise<HandlerResponse | void>;
8888

8989
type Middleware = (args: {
90-
params: Record<string, string>;
9190
reqCtx: RequestContext;
9291
next: NextFunction;
9392
}) => Promise<void | HandlerResponse>;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe('Class: Router - Basic Routing', () => {
8989
const app = new Router();
9090
const testEvent = createTestEvent('/test', 'GET');
9191

92-
app.get('/test', async (_params, reqCtx) => {
92+
app.get('/test', async (reqCtx) => {
9393
return {
9494
hasRequest: reqCtx.req instanceof Request,
9595
hasEvent: reqCtx.event === testEvent,
@@ -126,8 +126,8 @@ describe('Class: Router - Basic Routing', () => {
126126
app.post('/', async () => {
127127
return { actualPath: '/todos' };
128128
});
129-
app.get('/:todoId', async ({ todoId }) => {
130-
return { actualPath: `/todos/${todoId}` };
129+
app.get('/:todoId', async (reqCtx) => {
130+
return { actualPath: `/todos/${reqCtx.params.todoId}` };
131131
});
132132

133133
// Act

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ describe('Class: Router - Decorators', () => {
398398

399399
class Lambda {
400400
@app.get('/test')
401-
public async getTest(_params: any, reqCtx: any) {
401+
public async getTest(reqCtx: any) {
402402
return {
403403
hasRequest: reqCtx.req instanceof Request,
404404
hasEvent: reqCtx.event === testEvent,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ describe('Class: Router - Middleware', () => {
138138
let middlewareParams: Record<string, string> | undefined;
139139
let middlewareOptions: RequestContext | undefined;
140140

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { gzipSync } from 'node:zlib';
22
import context from '@aws-lambda-powertools/testing-utils/context';
3-
import { Router } from 'src/rest/Router.js';
43
import { beforeEach, describe, expect, it } from 'vitest';
54
import { compress } from '../../../../src/rest/middleware/index.js';
5+
import { Router } from '../../../../src/rest/Router.js';
66
import { createSettingHeadersMiddleware, createTestEvent } from '../helpers.js';
77

88
describe('Compress Middleware', () => {

0 commit comments

Comments
 (0)