Skip to content

Commit 3e33b69

Browse files
committed
feat: middleware in definer
1 parent ff50a03 commit 3e33b69

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/core/definer.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type RouteOptions<
3535
QueryParamsOutput,
3636
RequestBodyInput,
3737
RequestBodyOutput,
38+
Req extends Request,
39+
Res extends Response,
3840
> = {
3941
operationId: string,
4042
method: Method,
@@ -43,37 +45,39 @@ type RouteOptions<
4345
tags: string[],
4446
pathParams?: ZodType<PathParamsOutput, ZodTypeDef, PathParamsInput>,
4547
queryParams?: ZodType<QueryParamsOutput, ZodTypeDef, QueryParamsInput>,
46-
action: (source: ActionSource<PathParamsOutput, QueryParamsOutput, RequestBodyOutput>) => Response | Promise<Response>,
48+
action: (source: ActionSource<PathParamsOutput, QueryParamsOutput, RequestBodyOutput>) => Response | Promise<Res>,
4749
responses: Record<string, ResponseDefinition>,
48-
handleErrors?: (errorType: typeof customErrorTypes[number] | "UNKNOWN_ERROR", issues?: ZodIssue[]) => Response,
50+
handleErrors?: (errorType: typeof customErrorTypes[number] | "UNKNOWN_ERROR", issues?: ZodIssue[]) => Res,
51+
middleware?: (hander: RouteMethodHandler<PathParamsInput, Req, Res>) =>
52+
RouteMethodHandler<PathParamsInput, Req, Res>,
4953
} & (RouteWithBody<RequestBodyInput, RequestBodyOutput> | RouteWithoutBody);
5054

51-
function defineRoute<M extends HttpMethod, PPI, PPO, QPI, QPO, RBI, RBO>(input: RouteOptions<M, PPI, PPO, QPI, QPO, RBI, RBO>) {
52-
const handler: RouteMethodHandler<PPI> = async (request, props) => {
55+
function defineRoute<M extends HttpMethod, PPI, PPO, QPI, QPO, RBI, RBO, MwReq extends Request, MwRes extends Response>(input: RouteOptions<M, PPI, PPO, QPI, QPO, RBI, RBO, MwReq, MwRes>) {
56+
const handler: RouteMethodHandler<PPI, MwReq, MwRes> = async (request, props) => {
5357
try {
5458
const { searchParams } = new URL(request.url);
5559
const pathParams = parsePathParams(props.params, input.pathParams) as PPO;
5660
const queryParams = parseSearchParams(searchParams, input.queryParams) as QPO;
5761
const body = await parseRequestBody(request, input.method, input.requestBody ?? undefined, input.hasFormData) as RBO;
58-
return await input.action({ pathParams, queryParams, body });
62+
return await input.action({ pathParams, queryParams, body }) as MwRes;
5963
} catch (error) {
6064
if (input.handleErrors) {
6165
if (error instanceof Error) {
6266
const errorMessage = error.message as typeof customErrorTypes[number];
6367
if (customErrorTypes.includes(errorMessage)) {
64-
return input.handleErrors(errorMessage, error.cause as ZodIssue[]);
68+
return input.handleErrors(errorMessage, error.cause as ZodIssue[]) as MwRes;
6569
}
6670
}
67-
return input.handleErrors("UNKNOWN_ERROR");
71+
return input.handleErrors("UNKNOWN_ERROR") as MwRes;
6872
}
6973
if (error instanceof Error) {
7074
switch (error.message) {
7175
case "PARSE_FORM_DATA":
7276
case "PARSE_REQUEST_BODY":
7377
case "PARSE_SEARCH_PARAMS":
74-
return new Response(null, { status: 400 });
78+
return new Response(null, { status: 400 }) as MwRes;
7579
case "PARSE_PATH_PARAMS":
76-
return new Response(null, { status: 404 });
80+
return new Response(null, { status: 404 }) as MwRes;
7781
case "UNNECESSARY_PATH_PARAMS": {
7882
if (process.env.NODE_ENV !== "production") {
7983
// eslint-disable-next-line no-console
@@ -86,7 +90,7 @@ function defineRoute<M extends HttpMethod, PPI, PPO, QPI, QPO, RBI, RBO>(input:
8690
}
8791
}
8892
}
89-
return new Response(null, { status: 500 });
93+
return new Response(null, { status: 500 }) as MwRes;
9094
}
9195
};
9296

@@ -112,7 +116,11 @@ function defineRoute<M extends HttpMethod, PPI, PPO, QPI, QPO, RBI, RBO>(input:
112116
responses: responses,
113117
};
114118

115-
return { [input.method]: handler } as RouteHandler<M, PPI>;
119+
if (input.middleware) {
120+
return { [input.method]: input.middleware(handler) } as RouteHandler<M, PPI, MwReq, MwRes>;
121+
}
122+
123+
return { [input.method]: handler } as RouteHandler<M, PPI, MwReq, MwRes>;
116124
}
117125

118126
export default defineRoute;

src/types/next.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ type RouteHandlerProps<PathParams> = {
55
params?: PathParams,
66
};
77

8-
export type RouteMethodHandler<PathParamsInput> = ((
9-
request: Request,
8+
export type RouteMethodHandler<PathParamsInput, Req, Res> = ((
9+
request: Req,
1010
props: RouteHandlerProps<PathParamsInput>
11-
) => Promise<Response>) & {
11+
) => Promise<Res>) & {
1212
apiData?: OperationObject,
1313
};
1414

15-
export type RouteHandler<HM extends HttpMethod, PathParamsInput> = {
16-
[key in HM]: RouteMethodHandler<PathParamsInput>;
15+
export type RouteHandler<HM extends HttpMethod, PathParamsInput, Req, Res> = {
16+
[key in HM]: RouteMethodHandler<PathParamsInput, Req, Res>;
1717
};

0 commit comments

Comments
 (0)