1- import { type ZodType } from "zod" ;
21import { parseRequestBody , resolveRequestBody } from "./core/body" ;
32import { resolveParams } from "./core/params" ;
43import parsePathParams from "./core/path-params" ;
@@ -7,6 +6,7 @@ import parseSearchParams from "./core/search-params";
76import type { HttpMethod } from "./types/http" ;
87import type { RouteHandler , RouteMethodHandler } from "./types/next" ;
98import type { ResponseDefinition } from "./types/response" ;
9+ import type { ZodType , ZodTypeDef } from "zod" ;
1010
1111type ActionSource < PathParams , QueryParams , RequestBody > = {
1212 pathParams : PathParams ,
@@ -20,31 +20,39 @@ type RouteWithoutBody = {
2020 hasFormData ?: boolean ,
2121} ;
2222
23- type RouteWithBody < Body > = {
23+ type RouteWithBody < I , O > = {
2424 method : Exclude < HttpMethod , "GET" | "DELETE" | "HEAD" > ,
25- requestBody ?: ZodType < Body > | string ,
25+ requestBody ?: ZodType < O , ZodTypeDef , I > | string ,
2626 hasFormData ?: boolean ,
2727} ;
2828
29- type RouteOptions < Method , PathParams , QueryParams , RequestBody > = {
29+ type RouteOptions <
30+ Method ,
31+ PathParamsInput ,
32+ PathParamsOutput ,
33+ QueryParamsInput ,
34+ QueryParamsOutput ,
35+ RequestBodyInput ,
36+ RequestBodyOutput ,
37+ > = {
3038 operationId : string ,
3139 method : Method ,
3240 summary : string ,
3341 description : string ,
3442 tags : string [ ] ,
35- pathParams ?: ZodType < PathParams > ,
36- queryParams ?: ZodType < QueryParams > ,
37- action : ( source : ActionSource < PathParams , QueryParams , RequestBody > ) => Response | Promise < Response > ,
43+ pathParams ?: ZodType < PathParamsOutput , ZodTypeDef , PathParamsInput > ,
44+ queryParams ?: ZodType < QueryParamsOutput , ZodTypeDef , QueryParamsInput > ,
45+ action : ( source : ActionSource < PathParamsOutput , QueryParamsOutput , RequestBodyOutput > ) => Response | Promise < Response > ,
3846 responses : Record < string , ResponseDefinition > ,
39- } & ( RouteWithBody < RequestBody > | RouteWithoutBody ) ;
47+ } & ( RouteWithBody < RequestBodyInput , RequestBodyOutput > | RouteWithoutBody ) ;
4048
41- export default function createRoute < M extends HttpMethod , PP , QP , RB > ( input : RouteOptions < M , PP , QP , RB > ) {
42- const handler : RouteMethodHandler < PP > = async ( request , props ) => {
49+ function createRoute < M extends HttpMethod , PPI , PPO , QPI , QPO , RBI , RBO > ( input : RouteOptions < M , PPI , PPO , QPI , QPO , RBI , RBO > ) {
50+ const handler : RouteMethodHandler < PPI > = async ( request , props ) => {
4351 try {
4452 const { searchParams } = new URL ( request . url ) ;
45- const pathParams = parsePathParams < PP > ( props . params , input . pathParams ) as PP ;
46- const queryParams = parseSearchParams ( searchParams , input . queryParams ) as QP ;
47- const body = await parseRequestBody ( request , input . method , input . requestBody ?? undefined , input . hasFormData ) as RB ;
53+ const pathParams = parsePathParams ( props . params , input . pathParams ) as PPO ;
54+ const queryParams = parseSearchParams ( searchParams , input . queryParams ) as QPO ;
55+ const body = await parseRequestBody ( request , input . method , input . requestBody ?? undefined , input . hasFormData ) as RBO ;
4856 return await input . action ( { pathParams, queryParams, body } ) ;
4957 } catch ( error ) {
5058 if ( error instanceof Error ) {
@@ -83,5 +91,7 @@ export default function createRoute<M extends HttpMethod, PP, QP, RB>(input: Rou
8391 responses : responses ,
8492 } ;
8593
86- return { [ input . method ] : handler } as RouteHandler < M , PP > ;
94+ return { [ input . method ] : handler } as RouteHandler < M , PPI > ;
8795}
96+
97+ export default createRoute ;
0 commit comments