Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions packages/event-handler/src/rest/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,15 @@ class Router {
// this response should be overwritten by the handler, if it isn't
// it means something went wrong with the middleware chain
res: new Response('', { status: 500 }),
params: {},
};

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

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

const handlerMiddleware: Middleware = async ({
params,
reqCtx,
next,
}) => {
const handlerMiddleware: Middleware = async ({ reqCtx, next }) => {
if (route === null) {
const notFoundRes = await this.handleError(
new NotFoundError(`Route ${path} for method ${method} not found`),
Expand All @@ -251,7 +248,7 @@ class Router {
? route.handler
: route.handler.bind(options.scope);

const handlerResult = await handler(params, reqCtx);
const handlerResult = await handler(reqCtx);
reqCtx.res = handlerResultToWebResponse(
handlerResult,
reqCtx.res.headers
Expand All @@ -267,8 +264,8 @@ class Router {
handlerMiddleware,
]);

requestContext.params = route?.params ?? {};
const middlewareResult = await middleware({
params: route?.params ?? {},
reqCtx: requestContext,
next: () => Promise.resolve(),
});
Expand Down
3 changes: 1 addition & 2 deletions packages/event-handler/src/rest/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export const isAPIGatewayProxyResult = (
* ```
*/
export const composeMiddleware = (middleware: Middleware[]): Middleware => {
return async ({ params, reqCtx, next }): Promise<HandlerResponse | void> => {
return async ({ reqCtx, next }): Promise<HandlerResponse | void> => {
let index = -1;
let result: HandlerResponse | undefined;

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

const middlewareResult = await middlewareFn({
params,
reqCtx,
next: nextFn,
});
Expand Down
9 changes: 4 additions & 5 deletions packages/event-handler/src/types/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type RequestContext = {
event: APIGatewayProxyEvent;
context: Context;
res: Response;
params: Record<string, string>;
};

type ErrorResolveOptions = RequestContext & ResolveOptions;
Expand Down Expand Up @@ -60,10 +61,9 @@ type DynamicRoute = Route & CompiledRoute;

type HandlerResponse = Response | JSONObject;

type RouteHandler<
TParams = Record<string, unknown>,
TReturn = HandlerResponse,
> = (args: TParams, reqCtx: RequestContext) => Promise<TReturn>;
type RouteHandler<TReturn = HandlerResponse> = (
reqCtx: RequestContext
) => Promise<TReturn>;

type HttpMethod = keyof typeof HttpVerbs;

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

type Middleware = (args: {
params: Record<string, string>;
reqCtx: RequestContext;
next: NextFunction;
}) => Promise<void | HandlerResponse>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe('Class: Router - Basic Routing', () => {
const app = new Router();
const testEvent = createTestEvent('/test', 'GET');

app.get('/test', async (_params, reqCtx) => {
app.get('/test', async (reqCtx) => {
return {
hasRequest: reqCtx.req instanceof Request,
hasEvent: reqCtx.event === testEvent,
Expand Down Expand Up @@ -126,8 +126,8 @@ describe('Class: Router - Basic Routing', () => {
app.post('/', async () => {
return { actualPath: '/todos' };
});
app.get('/:todoId', async ({ todoId }) => {
return { actualPath: `/todos/${todoId}` };
app.get('/:todoId', async (reqCtx) => {
return { actualPath: `/todos/${reqCtx.params.todoId}` };
});

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ describe('Class: Router - Decorators', () => {

class Lambda {
@app.get('/test')
public async getTest(_params: any, reqCtx: any) {
public async getTest(reqCtx: any) {
return {
hasRequest: reqCtx.req instanceof Request,
hasEvent: reqCtx.event === testEvent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ describe('Class: Router - Middleware', () => {
let middlewareParams: Record<string, string> | undefined;
let middlewareOptions: RequestContext | undefined;

app.use(async ({ params, reqCtx, next }) => {
middlewareParams = params;
app.use(async ({ reqCtx, next }) => {
middlewareParams = reqCtx.params;
middlewareOptions = reqCtx;
await next();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { gzipSync } from 'node:zlib';
import context from '@aws-lambda-powertools/testing-utils/context';
import { Router } from 'src/rest/Router.js';
import { beforeEach, describe, expect, it } from 'vitest';
import { compress } from '../../../../src/rest/middleware/index.js';
import { Router } from '../../../../src/rest/Router.js';
import { createSettingHeadersMiddleware, createTestEvent } from '../helpers.js';

describe('Compress Middleware', () => {
Expand Down
Loading