Skip to content

Commit 7352ae2

Browse files
committed
Fixed other methods
1 parent 624c61d commit 7352ae2

File tree

5 files changed

+115
-58
lines changed

5 files changed

+115
-58
lines changed

packages/event-handler/src/http/RouteHandlerRegistry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ class RouteHandlerRegistry {
116116
this.#regexRoutes.set(route.id, {
117117
...route,
118118
...compiled,
119-
});
119+
} as DynamicRoute);
120120
return;
121121
}
122122
if (compiled.isDynamic) {
123123
const dynamicRoute = {
124124
...route,
125125
...compiled,
126-
};
126+
} as DynamicRoute;
127127
if (this.#dynamicRoutesSet.has(route.id)) {
128128
this.#logger.warn(
129129
`Handler for method: ${route.method} and path: ${route.path} already exists. The previous handler will be replaced.`
@@ -145,7 +145,7 @@ class RouteHandlerRegistry {
145145
`Handler for method: ${route.method} and path: ${route.path} already exists. The previous handler will be replaced.`
146146
);
147147
}
148-
this.#staticRoutes.set(route.id, route);
148+
this.#staticRoutes.set(route.id, route as unknown as Route);
149149
}
150150
}
151151
/**

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

Lines changed: 94 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -578,11 +578,11 @@ class Router {
578578
#handleHttpMethod<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
579579
method: HttpMethod,
580580
path: Path,
581-
middlewareOrHandler?: Middleware[] | RouteHandler,
582-
handlerOrOptions?: RouteHandler | TypedRouteHandler<TReqBody, TResBody>,
581+
middlewareOrHandler?: Middleware[] | RouteHandler | TypedRouteHandler<TReqBody, TResBody>,
582+
handlerOrOptions?: RouteHandler | TypedRouteHandler<TReqBody, TResBody> | { validation: ValidationConfig<TReqBody, TResBody> },
583583
options?: { validation: ValidationConfig<TReqBody, TResBody> }
584584
): MethodDecorator | undefined {
585-
// Case 1: post(path, [middleware], handler, { validation })
585+
// Case 1: method(path, [middleware], handler, { validation })
586586
if (Array.isArray(middlewareOrHandler)) {
587587
if (handlerOrOptions && typeof handlerOrOptions === 'function') {
588588
this.route(handlerOrOptions, {
@@ -604,7 +604,7 @@ class Router {
604604
};
605605
}
606606

607-
// Case 2: post(path, handler, { validation })
607+
// Case 2: method(path, handler, { validation }) or method(path, handler)
608608
if (middlewareOrHandler && typeof middlewareOrHandler === 'function') {
609609
// Check if handlerOrOptions is an options object (not a function)
610610
if (
@@ -638,6 +638,11 @@ class Router {
638638
): void;
639639
public get(path: Path): MethodDecorator;
640640
public get(path: Path, middleware: Middleware[]): MethodDecorator;
641+
public get<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
642+
path: Path,
643+
handler: TypedRouteHandler<TReqBody, TResBody>,
644+
options: { validation: ValidationConfig<TReqBody, TResBody> }
645+
): void;
641646
public get<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
642647
path: Path,
643648
middleware: Middleware[],
@@ -646,43 +651,46 @@ class Router {
646651
): void;
647652
public get<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
648653
path: Path,
649-
middlewareOrHandler?: Middleware[] | RouteHandler,
650-
handler?: RouteHandler | TypedRouteHandler<TReqBody, TResBody>,
654+
middlewareOrHandler?: Middleware[] | RouteHandler | TypedRouteHandler<TReqBody, TResBody>,
655+
handlerOrOptions?: RouteHandler | TypedRouteHandler<TReqBody, TResBody> | { validation: ValidationConfig<TReqBody, TResBody> },
651656
options?: { validation: ValidationConfig<TReqBody, TResBody> }
652657
): MethodDecorator | undefined {
653658
return this.#handleHttpMethod<TReqBody, TResBody>(
654659
HttpVerbs.GET,
655660
path,
656661
middlewareOrHandler,
657-
handler,
662+
handlerOrOptions,
658663
options
659664
);
660665
}
661666

662667
public post(
663668
path: Path,
664669
handler: RouteHandler,
665-
options?: Omit<HttpRouteOptions, 'method' | 'path'>
666670
): void;
667671
public post(
668672
path: Path,
669673
middleware: Middleware[],
670674
handler: RouteHandler,
671-
options?: Omit<HttpRouteOptions, 'method' | 'path' | 'middleware'>
672675
): void;
673676
public post(path: Path): MethodDecorator;
674677
public post(path: Path, middleware: Middleware[]): MethodDecorator;
675678
public post<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
676679
path: Path,
677-
middlewareOrHandler?: Middleware[] | RouteHandler,
678-
handlerOrOptions?: RouteHandler | Omit<HttpRouteOptions, 'method' | 'path'>,
679-
options?: Omit<HttpRouteOptions, 'method' | 'path' | 'middleware'>
680-
): MethodDecorator | undefined;
680+
handler: TypedRouteHandler<TReqBody, TResBody>,
681+
options: { validation: ValidationConfig<TReqBody, TResBody> }
682+
): void;
681683
public post<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
682684
path: Path,
683-
middlewareOrHandler?: Middleware[] | RouteHandler,
684-
handlerOrOptions?: RouteHandler | Omit<HttpRouteOptions, 'method' | 'path'>,
685-
options?: Omit<HttpRouteOptions, 'method' | 'path' | 'middleware'>
685+
middleware: Middleware[],
686+
handler: TypedRouteHandler<TReqBody, TResBody>,
687+
options: { validation: ValidationConfig<TReqBody, TResBody> }
688+
): void;
689+
public post<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
690+
path: Path,
691+
middlewareOrHandler?: Middleware[] | RouteHandler | TypedRouteHandler<TReqBody, TResBody>,
692+
handlerOrOptions?: RouteHandler | TypedRouteHandler<TReqBody, TResBody> | { validation: ValidationConfig<TReqBody, TResBody> },
693+
options?: { validation: ValidationConfig<TReqBody, TResBody> }
686694
): MethodDecorator | undefined {
687695
return this.#handleHttpMethod<TReqBody, TResBody>(
688696
HttpVerbs.POST,
@@ -696,21 +704,30 @@ class Router {
696704
public put(
697705
path: Path,
698706
handler: RouteHandler,
699-
options?: Omit<HttpRouteOptions, 'method' | 'path'>
700707
): void;
701708
public put(
702709
path: Path,
703710
middleware: Middleware[],
704711
handler: RouteHandler,
705-
options?: Omit<HttpRouteOptions, 'method' | 'path' | 'middleware'>
706712
): void;
707713
public put(path: Path): MethodDecorator;
708714
public put(path: Path, middleware: Middleware[]): MethodDecorator;
709715
public put<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
710716
path: Path,
711-
middlewareOrHandler?: Middleware[] | RouteHandler,
712-
handlerOrOptions?: RouteHandler | Omit<HttpRouteOptions, 'method' | 'path'>,
713-
options?: Omit<HttpRouteOptions, 'method' | 'path' | 'middleware'>
717+
handler: TypedRouteHandler<TReqBody, TResBody>,
718+
options: { validation: ValidationConfig<TReqBody, TResBody> }
719+
): void;
720+
public put<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
721+
path: Path,
722+
middleware: Middleware[],
723+
handler: TypedRouteHandler<TReqBody, TResBody>,
724+
options: { validation: ValidationConfig<TReqBody, TResBody> }
725+
): void;
726+
public put<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
727+
path: Path,
728+
middlewareOrHandler?: Middleware[] | RouteHandler | TypedRouteHandler<TReqBody, TResBody>,
729+
handlerOrOptions?: RouteHandler | TypedRouteHandler<TReqBody, TResBody> | { validation: ValidationConfig<TReqBody, TResBody> },
730+
options?: { validation: ValidationConfig<TReqBody, TResBody> }
714731
): MethodDecorator | undefined {
715732
return this.#handleHttpMethod<TReqBody, TResBody>(
716733
HttpVerbs.PUT,
@@ -724,21 +741,30 @@ class Router {
724741
public patch(
725742
path: Path,
726743
handler: RouteHandler,
727-
options?: Omit<HttpRouteOptions, 'method' | 'path'>
728744
): void;
729745
public patch(
730746
path: Path,
731747
middleware: Middleware[],
732748
handler: RouteHandler,
733-
options?: Omit<HttpRouteOptions, 'method' | 'path' | 'middleware'>
734749
): void;
735750
public patch(path: Path): MethodDecorator;
736751
public patch(path: Path, middleware: Middleware[]): MethodDecorator;
737752
public patch<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
738753
path: Path,
739-
middlewareOrHandler?: Middleware[] | RouteHandler,
740-
handlerOrOptions?: RouteHandler | Omit<HttpRouteOptions, 'method' | 'path'>,
741-
options?: Omit<HttpRouteOptions, 'method' | 'path' | 'middleware'>
754+
handler: TypedRouteHandler<TReqBody, TResBody>,
755+
options: { validation: ValidationConfig<TReqBody, TResBody> }
756+
): void;
757+
public patch<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
758+
path: Path,
759+
middleware: Middleware[],
760+
handler: TypedRouteHandler<TReqBody, TResBody>,
761+
options: { validation: ValidationConfig<TReqBody, TResBody> }
762+
): void;
763+
public patch<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
764+
path: Path,
765+
middlewareOrHandler?: Middleware[] | RouteHandler | TypedRouteHandler<TReqBody, TResBody>,
766+
handlerOrOptions?: RouteHandler | TypedRouteHandler<TReqBody, TResBody> | { validation: ValidationConfig<TReqBody, TResBody> },
767+
options?: { validation: ValidationConfig<TReqBody, TResBody> }
742768
): MethodDecorator | undefined {
743769
return this.#handleHttpMethod<TReqBody, TResBody>(
744770
HttpVerbs.PATCH,
@@ -752,21 +778,30 @@ class Router {
752778
public delete(
753779
path: Path,
754780
handler: RouteHandler,
755-
options?: Omit<HttpRouteOptions, 'method' | 'path'>
756781
): void;
757782
public delete(
758783
path: Path,
759784
middleware: Middleware[],
760785
handler: RouteHandler,
761-
options?: Omit<HttpRouteOptions, 'method' | 'path' | 'middleware'>
762786
): void;
763787
public delete(path: Path): MethodDecorator;
764788
public delete(path: Path, middleware: Middleware[]): MethodDecorator;
765789
public delete<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
766790
path: Path,
767-
middlewareOrHandler?: Middleware[] | RouteHandler,
768-
handlerOrOptions?: RouteHandler | Omit<HttpRouteOptions, 'method' | 'path'>,
769-
options?: Omit<HttpRouteOptions, 'method' | 'path' | 'middleware'>
791+
handler: TypedRouteHandler<TReqBody, TResBody>,
792+
options: { validation: ValidationConfig<TReqBody, TResBody> }
793+
): void;
794+
public delete<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
795+
path: Path,
796+
middleware: Middleware[],
797+
handler: TypedRouteHandler<TReqBody, TResBody>,
798+
options: { validation: ValidationConfig<TReqBody, TResBody> }
799+
): void;
800+
public delete<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
801+
path: Path,
802+
middlewareOrHandler?: Middleware[] | RouteHandler | TypedRouteHandler<TReqBody, TResBody>,
803+
handlerOrOptions?: RouteHandler | TypedRouteHandler<TReqBody, TResBody> | { validation: ValidationConfig<TReqBody, TResBody> },
804+
options?: { validation: ValidationConfig<TReqBody, TResBody> }
770805
): MethodDecorator | undefined {
771806
return this.#handleHttpMethod<TReqBody, TResBody>(
772807
HttpVerbs.DELETE,
@@ -780,21 +815,30 @@ class Router {
780815
public head(
781816
path: Path,
782817
handler: RouteHandler,
783-
options?: Omit<HttpRouteOptions, 'method' | 'path'>
784818
): void;
785819
public head(
786820
path: Path,
787821
middleware: Middleware[],
788822
handler: RouteHandler,
789-
options?: Omit<HttpRouteOptions, 'method' | 'path' | 'middleware'>
790823
): void;
791824
public head(path: Path): MethodDecorator;
792825
public head(path: Path, middleware: Middleware[]): MethodDecorator;
793826
public head<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
794827
path: Path,
795-
middlewareOrHandler?: Middleware[] | RouteHandler,
796-
handlerOrOptions?: RouteHandler | Omit<HttpRouteOptions, 'method' | 'path'>,
797-
options?: Omit<HttpRouteOptions, 'method' | 'path' | 'middleware'>
828+
handler: TypedRouteHandler<TReqBody, TResBody>,
829+
options: { validation: ValidationConfig<TReqBody, TResBody> }
830+
): void;
831+
public head<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
832+
path: Path,
833+
middleware: Middleware[],
834+
handler: TypedRouteHandler<TReqBody, TResBody>,
835+
options: { validation: ValidationConfig<TReqBody, TResBody> }
836+
): void;
837+
public head<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
838+
path: Path,
839+
middlewareOrHandler?: Middleware[] | RouteHandler | TypedRouteHandler<TReqBody, TResBody>,
840+
handlerOrOptions?: RouteHandler | TypedRouteHandler<TReqBody, TResBody> | { validation: ValidationConfig<TReqBody, TResBody> },
841+
options?: { validation: ValidationConfig<TReqBody, TResBody> }
798842
): MethodDecorator | undefined {
799843
return this.#handleHttpMethod<TReqBody, TResBody>(
800844
HttpVerbs.HEAD,
@@ -808,21 +852,30 @@ class Router {
808852
public options(
809853
path: Path,
810854
handler: RouteHandler,
811-
options?: Omit<HttpRouteOptions, 'method' | 'path'>
812855
): void;
813856
public options(
814857
path: Path,
815858
middleware: Middleware[],
816859
handler: RouteHandler,
817-
options?: Omit<HttpRouteOptions, 'method' | 'path' | 'middleware'>
818860
): void;
819861
public options(path: Path): MethodDecorator;
820862
public options(path: Path, middleware: Middleware[]): MethodDecorator;
821863
public options<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
822864
path: Path,
823-
middlewareOrHandler?: Middleware[] | RouteHandler,
824-
handlerOrOptions?: RouteHandler | Omit<HttpRouteOptions, 'method' | 'path'>,
825-
options?: Omit<HttpRouteOptions, 'method' | 'path' | 'middleware'>
865+
handler: TypedRouteHandler<TReqBody, TResBody>,
866+
options: { validation: ValidationConfig<TReqBody, TResBody> }
867+
): void;
868+
public options<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
869+
path: Path,
870+
middleware: Middleware[],
871+
handler: TypedRouteHandler<TReqBody, TResBody>,
872+
options: { validation: ValidationConfig<TReqBody, TResBody> }
873+
): void;
874+
public options<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse>(
875+
path: Path,
876+
middlewareOrHandler?: Middleware[] | RouteHandler | TypedRouteHandler<TReqBody, TResBody>,
877+
handlerOrOptions?: RouteHandler | TypedRouteHandler<TReqBody, TResBody> | { validation: ValidationConfig<TReqBody, TResBody> },
878+
options?: { validation: ValidationConfig<TReqBody, TResBody> }
826879
): MethodDecorator | undefined {
827880
return this.#handleHttpMethod<TReqBody, TResBody>(
828881
HttpVerbs.OPTIONS,

packages/event-handler/src/http/middleware/validation.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { StandardSchemaV1 } from '@standard-schema/spec';
22
import type {
33
HandlerResponse,
44
Middleware,
5+
RequestContext,
56
ValidatedRequest,
67
ValidatedResponse,
78
ValidationConfig,
@@ -24,8 +25,8 @@ export const createValidationMiddleware = <
2425
const resSchemas = config?.res;
2526

2627
return async ({ reqCtx, next }) => {
27-
// Initialize valid object
28-
reqCtx.valid = {
28+
// Initialize valid object with proper typing
29+
(reqCtx as unknown as RequestContext<TReqBody, TResBody>).valid = {
2930
req: {} as ValidatedRequest<TReqBody>,
3031
res: {} as ValidatedResponse<TResBody>,
3132
};
@@ -48,20 +49,21 @@ export const createValidationMiddleware = <
4849
bodyData = await clonedRequest.text();
4950
}
5051

51-
reqCtx.valid.req.body = await validateRequest(reqSchemas.body, bodyData, 'body') as TReqBody;
52+
const validatedBody = await validateRequest(reqSchemas.body, bodyData, 'body');
53+
(reqCtx.valid!.req as ValidatedRequest<TReqBody>).body = validatedBody as TReqBody;
5254
}
5355
if (reqSchemas.headers) {
5456
const headers = Object.fromEntries(reqCtx.req.headers.entries());
55-
reqCtx.valid.req.headers = await validateRequest(reqSchemas.headers, headers, 'headers') as Record<string, string>;
57+
reqCtx.valid!.req.headers = await validateRequest(reqSchemas.headers, headers, 'headers') as Record<string, string>;
5658
}
5759
if (reqSchemas.path) {
58-
reqCtx.valid.req.path = await validateRequest(reqSchemas.path, reqCtx.params, 'path') as Record<string, string>;
60+
reqCtx.valid!.req.path = await validateRequest(reqSchemas.path, reqCtx.params, 'path') as Record<string, string>;
5961
}
6062
if (reqSchemas.query) {
6163
const query = Object.fromEntries(
6264
new URL(reqCtx.req.url).searchParams.entries()
6365
);
64-
reqCtx.valid.req.query = await validateRequest(reqSchemas.query, query, 'query') as Record<string, string>;
66+
reqCtx.valid!.req.query = await validateRequest(reqSchemas.query, query, 'query') as Record<string, string>;
6567
}
6668
}
6769

@@ -79,12 +81,12 @@ export const createValidationMiddleware = <
7981
? await clonedResponse.json()
8082
: await clonedResponse.text();
8183

82-
reqCtx.valid.res.body = await validateResponse(resSchemas.body, bodyData, 'body') as TResBody;
84+
reqCtx.valid!.res.body = await validateResponse(resSchemas.body, bodyData, 'body') as TResBody;
8385
}
8486

8587
if (resSchemas.headers) {
8688
const headers = Object.fromEntries(response.headers.entries());
87-
reqCtx.valid.res.headers = await validateResponse(resSchemas.headers, headers, 'headers') as Record<string, string>;
89+
reqCtx.valid!.res.headers = await validateResponse(resSchemas.headers, headers, 'headers') as Record<string, string>;
8890
}
8991
}
9092
};

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,22 @@ type ValidatedResponse<TBody extends HandlerResponse = HandlerResponse> = {
4343
headers?: Record<string, string>;
4444
};
4545

46-
type RequestContext<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse> = {
46+
type RequestContext = {
4747
req: Request;
4848
event: APIGatewayProxyEvent | APIGatewayProxyEventV2 | ALBEvent;
4949
context: Context;
5050
res: Response;
5151
params: Record<string, string>;
5252
responseType: ResponseType;
5353
isBase64Encoded?: boolean;
54-
valid?: {
54+
};
55+
56+
type TypedRequestContext<TReqBody = never, TResBody extends HandlerResponse = HandlerResponse> = RequestContext & {
57+
valid: {
5558
req: ValidatedRequest<TReqBody>;
5659
res: ValidatedResponse<TResBody>;
5760
};
58-
};
61+
}
5962

6063
type HttpResolveOptions = ResolveOptions & { isHttpStreaming?: boolean };
6164

@@ -118,7 +121,7 @@ type RouteHandler<TReturn = HandlerResponse> = (
118121
) => Promise<TReturn> | TReturn;
119122

120123
type TypedRouteHandler<TReqBody, TResBody extends HandlerResponse = HandlerResponse, TReturn = HandlerResponse> = (
121-
reqCtx: RequestContext<TReqBody, TResBody>
124+
reqCtx: TypedRequestContext<TReqBody, TResBody>
122125
) => Promise<TReturn> | TReturn;
123126

124127
type HttpMethod = keyof typeof HttpVerbs;

0 commit comments

Comments
 (0)