Skip to content

Commit 70a47a8

Browse files
committed
Added TypedRequestContext to Middleware type
1 parent 0649425 commit 70a47a8

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

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

Lines changed: 9 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+
TypedRequestContext,
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 as any).valid = {
28+
const typedReqCtx = reqCtx as TypedRequestContext<TReqBody, TResBody>;
29+
typedReqCtx.valid = {
2930
req: {} as ValidatedRequest<TReqBody>,
3031
res: {} as ValidatedResponse<TResBody>,
3132
};
@@ -53,19 +54,19 @@ export const createValidationMiddleware = <
5354
bodyData,
5455
'body'
5556
);
56-
((reqCtx as any).valid.req as ValidatedRequest<TReqBody>).body =
57+
(typedReqCtx.valid.req as ValidatedRequest<TReqBody>).body =
5758
validatedBody as TReqBody;
5859
}
5960
if (reqSchemas.headers) {
6061
const headers = Object.fromEntries(reqCtx.req.headers.entries());
61-
(reqCtx as any).valid.req.headers = (await validateRequest(
62+
typedReqCtx.valid.req.headers = (await validateRequest(
6263
reqSchemas.headers,
6364
headers,
6465
'headers'
6566
)) as Record<string, string>;
6667
}
6768
if (reqSchemas.path) {
68-
(reqCtx as any).valid.req.path = (await validateRequest(
69+
typedReqCtx.valid.req.path = (await validateRequest(
6970
reqSchemas.path,
7071
reqCtx.params,
7172
'path'
@@ -75,7 +76,7 @@ export const createValidationMiddleware = <
7576
const query = Object.fromEntries(
7677
new URL(reqCtx.req.url).searchParams.entries()
7778
);
78-
(reqCtx as any).valid.req.query = (await validateRequest(
79+
typedReqCtx.valid.req.query = (await validateRequest(
7980
reqSchemas.query,
8081
query,
8182
'query'
@@ -97,7 +98,7 @@ export const createValidationMiddleware = <
9798
? await clonedResponse.json()
9899
: await clonedResponse.text();
99100

100-
(reqCtx as any).valid.res.body = (await validateResponse(
101+
typedReqCtx.valid.res.body = (await validateResponse(
101102
resSchemas.body,
102103
bodyData,
103104
'body'
@@ -106,7 +107,7 @@ export const createValidationMiddleware = <
106107

107108
if (resSchemas.headers) {
108109
const headers = Object.fromEntries(response.headers.entries());
109-
(reqCtx as any).valid.res.headers = (await validateResponse(
110+
typedReqCtx.valid.res.headers = (await validateResponse(
110111
resSchemas.headers,
111112
headers,
112113
'headers'

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ type HttpRouteOptions = {
154154
type NextFunction = () => Promise<HandlerResponse | void>;
155155

156156
type Middleware = (args: {
157-
reqCtx: RequestContext;
157+
reqCtx: RequestContext | TypedRequestContext;
158158
next: NextFunction;
159159
// biome-ignore lint/suspicious/noConfusingVoidType: To ensure next function is awaited
160160
}) => Promise<HandlerResponse | void>;
@@ -346,6 +346,7 @@ export type {
346346
Middleware,
347347
Path,
348348
RequestContext,
349+
TypedRequestContext,
349350
ResponseType,
350351
ResponseTypeMap,
351352
HttpRouterOptions,

0 commit comments

Comments
 (0)