Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ tsconfig.tsbuildinfo
.claude
.amazonq
.kiro
.github/instructions
.github/instructions
aidlc-docs
8 changes: 8 additions & 0 deletions packages/event-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@
"dependencies": {
"@aws-lambda-powertools/commons": "2.29.0"
},
"peerDependencies": {
"@standard-schema/spec": "^1.0.0"
},
"peerDependenciesMeta": {
"@standard-schema/spec": {
"optional": true
}
},
"keywords": [
"aws",
"lambda",
Expand Down
11 changes: 8 additions & 3 deletions packages/event-handler/src/http/Route.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import type {
HandlerResponse,
HttpMethod,
Middleware,
Path,
RouteHandler,
TypedRouteHandler,
} from '../types/http.js';

class Route {
class Route<
TReqBody = never,
TResBody extends HandlerResponse = HandlerResponse,
> {
readonly id: string;
readonly method: string;
readonly path: Path;
readonly handler: RouteHandler;
readonly handler: RouteHandler | TypedRouteHandler<TReqBody, TResBody>;
readonly middleware: Middleware[];

constructor(
method: HttpMethod,
path: Path,
handler: RouteHandler,
handler: RouteHandler | TypedRouteHandler<TReqBody, TResBody>,
middleware: Middleware[] = []
) {
this.id = `${method}:${path}`;
Expand Down
17 changes: 11 additions & 6 deletions packages/event-handler/src/http/RouteHandlerRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import type { GenericLogger } from '@aws-lambda-powertools/commons/types';
import { isRegExp } from '@aws-lambda-powertools/commons/typeutils';
import type {
DynamicRoute,
HandlerResponse,
HttpMethod,
HttpRouteHandlerOptions,
Path,
RouteHandler,
RouteRegistryOptions,
ValidationResult,
} from '../types/http.js';
Expand Down Expand Up @@ -94,7 +96,10 @@ class RouteHandlerRegistry {
*
* @param route - The route to register
*/
public register(route: Route): void {
public register<
TReqBody = never,
TResBody extends HandlerResponse = HandlerResponse,
>(route: Route<TReqBody, TResBody>): void {
this.#shouldSort = true;
const { isValid, issues } = validatePathPattern(route.path);
if (!isValid) {
Expand All @@ -115,14 +120,14 @@ class RouteHandlerRegistry {
this.#regexRoutes.set(route.id, {
...route,
...compiled,
});
} as DynamicRoute);
return;
}
if (compiled.isDynamic) {
const dynamicRoute = {
...route,
...compiled,
};
} as DynamicRoute;
if (this.#dynamicRoutesSet.has(route.id)) {
this.#logger.warn(
`Handler for method: ${route.method} and path: ${route.path} already exists. The previous handler will be replaced.`
Expand All @@ -144,7 +149,7 @@ class RouteHandlerRegistry {
`Handler for method: ${route.method} and path: ${route.path} already exists. The previous handler will be replaced.`
);
}
this.#staticRoutes.set(route.id, route);
this.#staticRoutes.set(route.id, route as unknown as Route);
}
}
/**
Expand Down Expand Up @@ -179,7 +184,7 @@ class RouteHandlerRegistry {
const staticRoute = this.#staticRoutes.get(routeId);
if (staticRoute != null) {
return {
handler: staticRoute.handler,
handler: staticRoute.handler as RouteHandler,
rawParams: {},
params: {},
middleware: staticRoute.middleware,
Expand Down Expand Up @@ -241,7 +246,7 @@ class RouteHandlerRegistry {
}

return {
handler: route.handler,
handler: route.handler as RouteHandler,
params: processedParams,
rawParams: params,
middleware: route.middleware,
Expand Down
Loading
Loading