@minisylar/express-typed-router 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -1
- package/dist/zod-router.cjs +1 -188
- package/dist/zod-router.cjs.map +1 -1
- package/dist/zod-router.d.cts +39 -44
- package/dist/zod-router.d.ts +39 -44
- package/dist/zod-router.js +1 -163
- package/dist/zod-router.js.map +1 -1
- package/package.json +7 -8
package/dist/zod-router.d.cts
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
1
|
import express, { NextFunction, Request, Response } from "express";
|
|
2
|
-
import
|
|
2
|
+
import * as z3 from "zod/v3";
|
|
3
|
+
import * as z4 from "zod/v4/core";
|
|
3
4
|
|
|
4
5
|
//#region src/zod-router.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Extract route parameters from Express.js route patterns.
|
|
7
|
-
*
|
|
8
|
-
* Supports all Express.js routing patterns:
|
|
9
|
-
* - Named parameters: /users/:userId → { userId: string }
|
|
10
|
-
* - Multiple parameters: /users/:userId/books/:bookId → { userId: string; bookId: string }
|
|
11
|
-
* - Parameters with separators: /flights/:from-:to → { from: string; to: string }
|
|
12
|
-
* - Dot notation: /plantae/:genus.:species → { genus: string; species: string }
|
|
13
|
-
* - Regex constraints: /user/:id(\\d+) → { id: string }
|
|
14
|
-
* - Optional parameters: /posts/:year/:month? → { year: string; month?: string }
|
|
15
|
-
* - Wildcard parameters: /files/* → { "0": string }
|
|
16
|
-
* - Multiple wildcards: /a/star/b/star → { "0": string; "1": string }
|
|
17
|
-
*/
|
|
18
6
|
|
|
7
|
+
type AnyZodType = z3.ZodTypeAny | z4.$ZodType;
|
|
8
|
+
declare function isZod4Schema(schema: AnyZodType): schema is z4.$ZodType;
|
|
9
|
+
type InferOutput<T extends AnyZodType> = T extends z4.$ZodType ? z4.output<T> : T extends z3.ZodTypeAny ? z3.output<T> : never;
|
|
10
|
+
type InferInput<T extends AnyZodType> = T extends z4.$ZodType ? z4.input<T> : T extends z3.ZodTypeAny ? z3.input<T> : never;
|
|
11
|
+
declare function parseSchema<T extends AnyZodType>(schema: T, data: unknown): InferOutput<T>;
|
|
12
|
+
type SafeParseResult<T extends AnyZodType> = T extends z4.$ZodType ? ReturnType<typeof z4.safeParse<T>> : T extends z3.ZodTypeAny ? z3.SafeParseReturnType<z3.input<T>, z3.output<T>> : never;
|
|
13
|
+
declare function safeParseSchema<T extends AnyZodType>(schema: T, data: unknown): SafeParseResult<T>;
|
|
14
|
+
declare function isZodError(error: unknown): error is z3.ZodError;
|
|
19
15
|
/**
|
|
20
16
|
* Extract route parameters from Express.js route patterns.
|
|
21
17
|
*
|
|
@@ -85,12 +81,12 @@ type RequestOnlyMiddleware<TReq extends Record<string, any>> = TypedMiddleware<T
|
|
|
85
81
|
type LocalsOnlyMiddleware<TLocals extends Record<string, any>> = TypedMiddleware<{}, TLocals>;
|
|
86
82
|
type InferMiddlewareProps<T extends readonly TypedMiddleware<any, any>[]> = T extends readonly [infer First, ...infer Rest] ? First extends TypedMiddleware<infer FirstReq, any> ? Rest extends readonly TypedMiddleware<any, any>[] ? FirstReq & InferMiddlewareProps<Rest> : FirstReq : {} : {};
|
|
87
83
|
type InferMiddlewareLocals<T extends readonly TypedMiddleware<any, any>[]> = T extends readonly [infer First, ...infer Rest] ? First extends TypedMiddleware<any, infer FirstLocals> ? Rest extends readonly TypedMiddleware<any, any>[] ? FirstLocals & InferMiddlewareLocals<Rest> : FirstLocals : {} : {};
|
|
88
|
-
type ZodRequest<Path extends string = string, BodySchema extends
|
|
84
|
+
type ZodRequest<Path extends string = string, BodySchema extends AnyZodType | unknown = unknown, QuerySchema extends AnyZodType | unknown = unknown, MiddlewareProps extends Record<string, any> = {}> = Omit<Request, "params" | "query" | "body"> & {
|
|
89
85
|
params: ExtractRouteParams<Path>;
|
|
90
|
-
body: BodySchema extends
|
|
91
|
-
query: QuerySchema extends
|
|
86
|
+
body: BodySchema extends AnyZodType ? InferOutput<BodySchema> : unknown;
|
|
87
|
+
query: QuerySchema extends AnyZodType ? InferOutput<QuerySchema> : unknown;
|
|
92
88
|
} & MiddlewareProps;
|
|
93
|
-
type ZodRouteHandler<Path extends string = string, BodySchema extends
|
|
89
|
+
type ZodRouteHandler<Path extends string = string, BodySchema extends AnyZodType | unknown = unknown, QuerySchema extends AnyZodType | unknown = unknown, MiddlewareProps extends Record<string, any> = {}, ResponseLocals extends Record<string, any> = {}> = (req: ZodRequest<Path, BodySchema, QuerySchema, MiddlewareProps>, res: Response<any, ResponseLocals>, next?: NextFunction) => void | Promise<void> | Response | Promise<Response>;
|
|
94
90
|
/**
|
|
95
91
|
* Options for defining a typed route, including schemas and middleware.
|
|
96
92
|
*
|
|
@@ -100,9 +96,9 @@ type ZodRouteHandler<Path extends string = string, BodySchema extends ZodType<an
|
|
|
100
96
|
* @property querySchema - Optional Zod schema for validating the query string.
|
|
101
97
|
* @property middleware - Optional array of TypedMiddleware for this route.
|
|
102
98
|
*/
|
|
103
|
-
interface RouteOptions<BodySchema extends
|
|
104
|
-
bodySchema?: BodySchema
|
|
105
|
-
querySchema?: QuerySchema
|
|
99
|
+
interface RouteOptions<BodySchema extends AnyZodType | unknown = unknown, QuerySchema extends AnyZodType | unknown = unknown> {
|
|
100
|
+
bodySchema?: BodySchema;
|
|
101
|
+
querySchema?: QuerySchema;
|
|
106
102
|
middleware?: TypedMiddleware<any, any>[];
|
|
107
103
|
}
|
|
108
104
|
type HttpMethod = "get" | "post" | "put" | "delete" | "patch" | "options" | "head" | "all";
|
|
@@ -127,11 +123,11 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
127
123
|
*/
|
|
128
124
|
getRouter(): express.Router;
|
|
129
125
|
get<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
130
|
-
get<Path extends string, BodySchema extends
|
|
126
|
+
get<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown>(path: Path, options: RouteOptions<BodySchema, QuerySchema>, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
131
127
|
get<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
132
128
|
middleware: Middleware;
|
|
133
129
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
134
|
-
get<Path extends string, BodySchema extends
|
|
130
|
+
get<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: RouteOptions<BodySchema, QuerySchema> & {
|
|
135
131
|
middleware: [...M];
|
|
136
132
|
},
|
|
137
133
|
// Using tuple spread pattern
|
|
@@ -139,7 +135,7 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
139
135
|
// Make it readonly for type inference
|
|
140
136
|
// Make it readonly for type inference
|
|
141
137
|
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
142
|
-
post<Path extends string, BodySchema extends
|
|
138
|
+
post<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
143
139
|
bodySchema: BodySchema;
|
|
144
140
|
querySchema?: QuerySchema;
|
|
145
141
|
middleware: [...M];
|
|
@@ -147,7 +143,7 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
147
143
|
// Make it readonly for type inference
|
|
148
144
|
// Make it readonly for type inference
|
|
149
145
|
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
150
|
-
post<Path extends string, BodySchema extends
|
|
146
|
+
post<Path extends string, BodySchema extends AnyZodType, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
151
147
|
bodySchema: BodySchema;
|
|
152
148
|
middleware: [...M];
|
|
153
149
|
},
|
|
@@ -164,83 +160,83 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
164
160
|
// Make it readonly for type inference
|
|
165
161
|
// Make it readonly for type inference
|
|
166
162
|
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
167
|
-
post<Path extends string, BodySchema extends
|
|
163
|
+
post<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown>(path: Path, options: RouteOptions<BodySchema, QuerySchema>, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
168
164
|
post<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
169
|
-
put<Path extends string, BodySchema extends
|
|
165
|
+
put<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
170
166
|
bodySchema: BodySchema;
|
|
171
167
|
querySchema?: QuerySchema;
|
|
172
168
|
middleware: Middleware;
|
|
173
169
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
174
|
-
put<Path extends string, BodySchema extends
|
|
170
|
+
put<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
175
171
|
bodySchema: BodySchema;
|
|
176
172
|
middleware: Middleware;
|
|
177
173
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
178
174
|
put<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
179
175
|
middleware: Middleware;
|
|
180
176
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
181
|
-
put<Path extends string, BodySchema extends
|
|
177
|
+
put<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown>(path: Path, options: RouteOptions<BodySchema, QuerySchema>, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
182
178
|
put<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
183
|
-
patch<Path extends string, BodySchema extends
|
|
179
|
+
patch<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
184
180
|
bodySchema: BodySchema;
|
|
185
181
|
querySchema?: QuerySchema;
|
|
186
182
|
middleware: Middleware;
|
|
187
183
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
188
|
-
patch<Path extends string, BodySchema extends
|
|
184
|
+
patch<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
189
185
|
bodySchema: BodySchema;
|
|
190
186
|
middleware: Middleware;
|
|
191
187
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
192
188
|
patch<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
193
189
|
middleware: Middleware;
|
|
194
190
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
195
|
-
patch<Path extends string, BodySchema extends
|
|
191
|
+
patch<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown>(path: Path, options: RouteOptions<BodySchema, QuerySchema>, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
196
192
|
patch<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
197
|
-
delete<Path extends string, QuerySchema extends
|
|
193
|
+
delete<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
198
194
|
querySchema: QuerySchema;
|
|
199
195
|
middleware: Middleware;
|
|
200
196
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
201
|
-
delete<Path extends string, QuerySchema extends
|
|
197
|
+
delete<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
202
198
|
querySchema: QuerySchema;
|
|
203
199
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
204
200
|
delete<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
205
201
|
middleware: Middleware;
|
|
206
202
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
207
203
|
delete<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
208
|
-
options<Path extends string, QuerySchema extends
|
|
204
|
+
options<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
209
205
|
querySchema: QuerySchema;
|
|
210
206
|
middleware: Middleware;
|
|
211
207
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
212
|
-
options<Path extends string, QuerySchema extends
|
|
208
|
+
options<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
213
209
|
querySchema: QuerySchema;
|
|
214
210
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
215
211
|
options<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
216
212
|
middleware: Middleware;
|
|
217
213
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
218
214
|
options<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
219
|
-
head<Path extends string, QuerySchema extends
|
|
215
|
+
head<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
220
216
|
querySchema: QuerySchema;
|
|
221
217
|
middleware: Middleware;
|
|
222
218
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
223
|
-
head<Path extends string, QuerySchema extends
|
|
219
|
+
head<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
224
220
|
querySchema: QuerySchema;
|
|
225
221
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
226
222
|
head<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
227
223
|
middleware: Middleware;
|
|
228
224
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
229
225
|
head<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
230
|
-
all<Path extends string, BodySchema extends
|
|
226
|
+
all<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
231
227
|
bodySchema: BodySchema;
|
|
232
228
|
querySchema?: QuerySchema;
|
|
233
229
|
middleware: Middleware;
|
|
234
230
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
235
|
-
all<Path extends string, BodySchema extends
|
|
231
|
+
all<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
236
232
|
bodySchema: BodySchema;
|
|
237
233
|
middleware: Middleware;
|
|
238
234
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
239
|
-
all<Path extends string, QuerySchema extends
|
|
235
|
+
all<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
240
236
|
querySchema: QuerySchema;
|
|
241
237
|
middleware: Middleware;
|
|
242
238
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
243
|
-
all<Path extends string, BodySchema extends
|
|
239
|
+
all<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown>(path: Path, options: RouteOptions<BodySchema, QuerySchema>, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
244
240
|
all<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
245
241
|
middleware: Middleware;
|
|
246
242
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
@@ -312,7 +308,6 @@ declare function createTypedRouterWithConfig<RouterMiddlewareProps extends Recor
|
|
|
312
308
|
* const router = createTypedRouterWithMiddleware(authMiddleware, loggingMiddleware);
|
|
313
309
|
*/
|
|
314
310
|
declare function createTypedRouterWithMiddleware<T extends Record<string, any>>(...middleware: TypedMiddleware<any, any>[]): TypedRouter<T>;
|
|
315
|
-
|
|
316
311
|
//#endregion
|
|
317
|
-
export { ExtractRouteParams, HttpMethod, LocalsOnlyMiddleware, RequestOnlyMiddleware, RouteOptions, RouterConfig, TypedMiddleware, ZodRequest, ZodRouteHandler, createTypedRouter, createTypedRouterWithConfig, createTypedRouterWithMiddleware };
|
|
312
|
+
export { AnyZodType, ExtractRouteParams, HttpMethod, InferInput, InferOutput, LocalsOnlyMiddleware, RequestOnlyMiddleware, RouteOptions, RouterConfig, SafeParseResult, TypedMiddleware, ZodRequest, ZodRouteHandler, createTypedRouter, createTypedRouterWithConfig, createTypedRouterWithMiddleware, isZod4Schema, isZodError, parseSchema, safeParseSchema };
|
|
318
313
|
//# sourceMappingURL=zod-router.d.cts.map
|
package/dist/zod-router.d.ts
CHANGED
|
@@ -1,21 +1,17 @@
|
|
|
1
1
|
import express, { NextFunction, Request, Response } from "express";
|
|
2
|
-
import
|
|
2
|
+
import * as z3 from "zod/v3";
|
|
3
|
+
import * as z4 from "zod/v4/core";
|
|
3
4
|
|
|
4
5
|
//#region src/zod-router.d.ts
|
|
5
|
-
/**
|
|
6
|
-
* Extract route parameters from Express.js route patterns.
|
|
7
|
-
*
|
|
8
|
-
* Supports all Express.js routing patterns:
|
|
9
|
-
* - Named parameters: /users/:userId → { userId: string }
|
|
10
|
-
* - Multiple parameters: /users/:userId/books/:bookId → { userId: string; bookId: string }
|
|
11
|
-
* - Parameters with separators: /flights/:from-:to → { from: string; to: string }
|
|
12
|
-
* - Dot notation: /plantae/:genus.:species → { genus: string; species: string }
|
|
13
|
-
* - Regex constraints: /user/:id(\\d+) → { id: string }
|
|
14
|
-
* - Optional parameters: /posts/:year/:month? → { year: string; month?: string }
|
|
15
|
-
* - Wildcard parameters: /files/* → { "0": string }
|
|
16
|
-
* - Multiple wildcards: /a/star/b/star → { "0": string; "1": string }
|
|
17
|
-
*/
|
|
18
6
|
|
|
7
|
+
type AnyZodType = z3.ZodTypeAny | z4.$ZodType;
|
|
8
|
+
declare function isZod4Schema(schema: AnyZodType): schema is z4.$ZodType;
|
|
9
|
+
type InferOutput<T extends AnyZodType> = T extends z4.$ZodType ? z4.output<T> : T extends z3.ZodTypeAny ? z3.output<T> : never;
|
|
10
|
+
type InferInput<T extends AnyZodType> = T extends z4.$ZodType ? z4.input<T> : T extends z3.ZodTypeAny ? z3.input<T> : never;
|
|
11
|
+
declare function parseSchema<T extends AnyZodType>(schema: T, data: unknown): InferOutput<T>;
|
|
12
|
+
type SafeParseResult<T extends AnyZodType> = T extends z4.$ZodType ? ReturnType<typeof z4.safeParse<T>> : T extends z3.ZodTypeAny ? z3.SafeParseReturnType<z3.input<T>, z3.output<T>> : never;
|
|
13
|
+
declare function safeParseSchema<T extends AnyZodType>(schema: T, data: unknown): SafeParseResult<T>;
|
|
14
|
+
declare function isZodError(error: unknown): error is z3.ZodError;
|
|
19
15
|
/**
|
|
20
16
|
* Extract route parameters from Express.js route patterns.
|
|
21
17
|
*
|
|
@@ -85,12 +81,12 @@ type RequestOnlyMiddleware<TReq extends Record<string, any>> = TypedMiddleware<T
|
|
|
85
81
|
type LocalsOnlyMiddleware<TLocals extends Record<string, any>> = TypedMiddleware<{}, TLocals>;
|
|
86
82
|
type InferMiddlewareProps<T extends readonly TypedMiddleware<any, any>[]> = T extends readonly [infer First, ...infer Rest] ? First extends TypedMiddleware<infer FirstReq, any> ? Rest extends readonly TypedMiddleware<any, any>[] ? FirstReq & InferMiddlewareProps<Rest> : FirstReq : {} : {};
|
|
87
83
|
type InferMiddlewareLocals<T extends readonly TypedMiddleware<any, any>[]> = T extends readonly [infer First, ...infer Rest] ? First extends TypedMiddleware<any, infer FirstLocals> ? Rest extends readonly TypedMiddleware<any, any>[] ? FirstLocals & InferMiddlewareLocals<Rest> : FirstLocals : {} : {};
|
|
88
|
-
type ZodRequest<Path extends string = string, BodySchema extends
|
|
84
|
+
type ZodRequest<Path extends string = string, BodySchema extends AnyZodType | unknown = unknown, QuerySchema extends AnyZodType | unknown = unknown, MiddlewareProps extends Record<string, any> = {}> = Omit<Request, "params" | "query" | "body"> & {
|
|
89
85
|
params: ExtractRouteParams<Path>;
|
|
90
|
-
body: BodySchema extends
|
|
91
|
-
query: QuerySchema extends
|
|
86
|
+
body: BodySchema extends AnyZodType ? InferOutput<BodySchema> : unknown;
|
|
87
|
+
query: QuerySchema extends AnyZodType ? InferOutput<QuerySchema> : unknown;
|
|
92
88
|
} & MiddlewareProps;
|
|
93
|
-
type ZodRouteHandler<Path extends string = string, BodySchema extends
|
|
89
|
+
type ZodRouteHandler<Path extends string = string, BodySchema extends AnyZodType | unknown = unknown, QuerySchema extends AnyZodType | unknown = unknown, MiddlewareProps extends Record<string, any> = {}, ResponseLocals extends Record<string, any> = {}> = (req: ZodRequest<Path, BodySchema, QuerySchema, MiddlewareProps>, res: Response<any, ResponseLocals>, next?: NextFunction) => void | Promise<void> | Response | Promise<Response>;
|
|
94
90
|
/**
|
|
95
91
|
* Options for defining a typed route, including schemas and middleware.
|
|
96
92
|
*
|
|
@@ -100,9 +96,9 @@ type ZodRouteHandler<Path extends string = string, BodySchema extends ZodType<an
|
|
|
100
96
|
* @property querySchema - Optional Zod schema for validating the query string.
|
|
101
97
|
* @property middleware - Optional array of TypedMiddleware for this route.
|
|
102
98
|
*/
|
|
103
|
-
interface RouteOptions<BodySchema extends
|
|
104
|
-
bodySchema?: BodySchema
|
|
105
|
-
querySchema?: QuerySchema
|
|
99
|
+
interface RouteOptions<BodySchema extends AnyZodType | unknown = unknown, QuerySchema extends AnyZodType | unknown = unknown> {
|
|
100
|
+
bodySchema?: BodySchema;
|
|
101
|
+
querySchema?: QuerySchema;
|
|
106
102
|
middleware?: TypedMiddleware<any, any>[];
|
|
107
103
|
}
|
|
108
104
|
type HttpMethod = "get" | "post" | "put" | "delete" | "patch" | "options" | "head" | "all";
|
|
@@ -127,11 +123,11 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
127
123
|
*/
|
|
128
124
|
getRouter(): express.Router;
|
|
129
125
|
get<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
130
|
-
get<Path extends string, BodySchema extends
|
|
126
|
+
get<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown>(path: Path, options: RouteOptions<BodySchema, QuerySchema>, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
131
127
|
get<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
132
128
|
middleware: Middleware;
|
|
133
129
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
134
|
-
get<Path extends string, BodySchema extends
|
|
130
|
+
get<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: RouteOptions<BodySchema, QuerySchema> & {
|
|
135
131
|
middleware: [...M];
|
|
136
132
|
},
|
|
137
133
|
// Using tuple spread pattern
|
|
@@ -139,7 +135,7 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
139
135
|
// Make it readonly for type inference
|
|
140
136
|
// Make it readonly for type inference
|
|
141
137
|
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
142
|
-
post<Path extends string, BodySchema extends
|
|
138
|
+
post<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
143
139
|
bodySchema: BodySchema;
|
|
144
140
|
querySchema?: QuerySchema;
|
|
145
141
|
middleware: [...M];
|
|
@@ -147,7 +143,7 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
147
143
|
// Make it readonly for type inference
|
|
148
144
|
// Make it readonly for type inference
|
|
149
145
|
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
150
|
-
post<Path extends string, BodySchema extends
|
|
146
|
+
post<Path extends string, BodySchema extends AnyZodType, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
151
147
|
bodySchema: BodySchema;
|
|
152
148
|
middleware: [...M];
|
|
153
149
|
},
|
|
@@ -164,83 +160,83 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
164
160
|
// Make it readonly for type inference
|
|
165
161
|
// Make it readonly for type inference
|
|
166
162
|
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
167
|
-
post<Path extends string, BodySchema extends
|
|
163
|
+
post<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown>(path: Path, options: RouteOptions<BodySchema, QuerySchema>, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
168
164
|
post<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
169
|
-
put<Path extends string, BodySchema extends
|
|
165
|
+
put<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
170
166
|
bodySchema: BodySchema;
|
|
171
167
|
querySchema?: QuerySchema;
|
|
172
168
|
middleware: Middleware;
|
|
173
169
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
174
|
-
put<Path extends string, BodySchema extends
|
|
170
|
+
put<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
175
171
|
bodySchema: BodySchema;
|
|
176
172
|
middleware: Middleware;
|
|
177
173
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
178
174
|
put<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
179
175
|
middleware: Middleware;
|
|
180
176
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
181
|
-
put<Path extends string, BodySchema extends
|
|
177
|
+
put<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown>(path: Path, options: RouteOptions<BodySchema, QuerySchema>, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
182
178
|
put<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
183
|
-
patch<Path extends string, BodySchema extends
|
|
179
|
+
patch<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
184
180
|
bodySchema: BodySchema;
|
|
185
181
|
querySchema?: QuerySchema;
|
|
186
182
|
middleware: Middleware;
|
|
187
183
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
188
|
-
patch<Path extends string, BodySchema extends
|
|
184
|
+
patch<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
189
185
|
bodySchema: BodySchema;
|
|
190
186
|
middleware: Middleware;
|
|
191
187
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
192
188
|
patch<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
193
189
|
middleware: Middleware;
|
|
194
190
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
195
|
-
patch<Path extends string, BodySchema extends
|
|
191
|
+
patch<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown>(path: Path, options: RouteOptions<BodySchema, QuerySchema>, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
196
192
|
patch<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
197
|
-
delete<Path extends string, QuerySchema extends
|
|
193
|
+
delete<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
198
194
|
querySchema: QuerySchema;
|
|
199
195
|
middleware: Middleware;
|
|
200
196
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
201
|
-
delete<Path extends string, QuerySchema extends
|
|
197
|
+
delete<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
202
198
|
querySchema: QuerySchema;
|
|
203
199
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
204
200
|
delete<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
205
201
|
middleware: Middleware;
|
|
206
202
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
207
203
|
delete<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
208
|
-
options<Path extends string, QuerySchema extends
|
|
204
|
+
options<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
209
205
|
querySchema: QuerySchema;
|
|
210
206
|
middleware: Middleware;
|
|
211
207
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
212
|
-
options<Path extends string, QuerySchema extends
|
|
208
|
+
options<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
213
209
|
querySchema: QuerySchema;
|
|
214
210
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
215
211
|
options<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
216
212
|
middleware: Middleware;
|
|
217
213
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
218
214
|
options<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
219
|
-
head<Path extends string, QuerySchema extends
|
|
215
|
+
head<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
220
216
|
querySchema: QuerySchema;
|
|
221
217
|
middleware: Middleware;
|
|
222
218
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
223
|
-
head<Path extends string, QuerySchema extends
|
|
219
|
+
head<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
224
220
|
querySchema: QuerySchema;
|
|
225
221
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
226
222
|
head<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
227
223
|
middleware: Middleware;
|
|
228
224
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
229
225
|
head<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
230
|
-
all<Path extends string, BodySchema extends
|
|
226
|
+
all<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
231
227
|
bodySchema: BodySchema;
|
|
232
228
|
querySchema?: QuerySchema;
|
|
233
229
|
middleware: Middleware;
|
|
234
230
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
235
|
-
all<Path extends string, BodySchema extends
|
|
231
|
+
all<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
236
232
|
bodySchema: BodySchema;
|
|
237
233
|
middleware: Middleware;
|
|
238
234
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
239
|
-
all<Path extends string, QuerySchema extends
|
|
235
|
+
all<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
240
236
|
querySchema: QuerySchema;
|
|
241
237
|
middleware: Middleware;
|
|
242
238
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
243
|
-
all<Path extends string, BodySchema extends
|
|
239
|
+
all<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown>(path: Path, options: RouteOptions<BodySchema, QuerySchema>, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
244
240
|
all<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
245
241
|
middleware: Middleware;
|
|
246
242
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
@@ -312,7 +308,6 @@ declare function createTypedRouterWithConfig<RouterMiddlewareProps extends Recor
|
|
|
312
308
|
* const router = createTypedRouterWithMiddleware(authMiddleware, loggingMiddleware);
|
|
313
309
|
*/
|
|
314
310
|
declare function createTypedRouterWithMiddleware<T extends Record<string, any>>(...middleware: TypedMiddleware<any, any>[]): TypedRouter<T>;
|
|
315
|
-
|
|
316
311
|
//#endregion
|
|
317
|
-
export { ExtractRouteParams, HttpMethod, LocalsOnlyMiddleware, RequestOnlyMiddleware, RouteOptions, RouterConfig, TypedMiddleware, ZodRequest, ZodRouteHandler, createTypedRouter, createTypedRouterWithConfig, createTypedRouterWithMiddleware };
|
|
312
|
+
export { AnyZodType, ExtractRouteParams, HttpMethod, InferInput, InferOutput, LocalsOnlyMiddleware, RequestOnlyMiddleware, RouteOptions, RouterConfig, SafeParseResult, TypedMiddleware, ZodRequest, ZodRouteHandler, createTypedRouter, createTypedRouterWithConfig, createTypedRouterWithMiddleware, isZod4Schema, isZodError, parseSchema, safeParseSchema };
|
|
318
313
|
//# sourceMappingURL=zod-router.d.ts.map
|
package/dist/zod-router.js
CHANGED
|
@@ -1,164 +1,2 @@
|
|
|
1
|
-
import express from "
|
|
2
|
-
import { z } from "zod";
|
|
3
|
-
|
|
4
|
-
//#region src/zod-router.ts
|
|
5
|
-
var TypedRouter = class {
|
|
6
|
-
router;
|
|
7
|
-
constructor() {
|
|
8
|
-
this.router = express.Router();
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Add typed middleware that extends the request with additional properties
|
|
12
|
-
* and/or adds properties to response.locals
|
|
13
|
-
*/
|
|
14
|
-
/**
|
|
15
|
-
* Add typed middleware to the router.
|
|
16
|
-
* This middleware will apply to all routes defined after this call.
|
|
17
|
-
*
|
|
18
|
-
* @template TReq - Type extensions for the request object
|
|
19
|
-
* @template TLocals - Type extensions for response.locals
|
|
20
|
-
* @param middleware - The typed middleware function
|
|
21
|
-
* @returns A new router instance with updated types
|
|
22
|
-
*/
|
|
23
|
-
useMiddleware(middleware) {
|
|
24
|
-
this.router.use(middleware);
|
|
25
|
-
return this;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Get the underlying Express router
|
|
29
|
-
*/
|
|
30
|
-
getRouter() {
|
|
31
|
-
return this.router;
|
|
32
|
-
}
|
|
33
|
-
get(path, optionsOrHandler, handler) {
|
|
34
|
-
return this.registerRoute("get", path, optionsOrHandler, handler);
|
|
35
|
-
}
|
|
36
|
-
post(path, optionsOrHandler, handler) {
|
|
37
|
-
return this.registerRoute("post", path, optionsOrHandler, handler);
|
|
38
|
-
}
|
|
39
|
-
put(path, optionsOrHandler, handler) {
|
|
40
|
-
return this.registerRoute("put", path, optionsOrHandler, handler);
|
|
41
|
-
}
|
|
42
|
-
patch(path, optionsOrHandler, handler) {
|
|
43
|
-
return this.registerRoute("patch", path, optionsOrHandler, handler);
|
|
44
|
-
}
|
|
45
|
-
delete(path, optionsOrHandler, handler) {
|
|
46
|
-
return this.registerRoute("delete", path, optionsOrHandler, handler);
|
|
47
|
-
}
|
|
48
|
-
options(path, optionsOrHandler, handler) {
|
|
49
|
-
return this.registerRoute("options", path, optionsOrHandler, handler);
|
|
50
|
-
}
|
|
51
|
-
head(path, optionsOrHandler, handler) {
|
|
52
|
-
return this.registerRoute("head", path, optionsOrHandler, handler);
|
|
53
|
-
}
|
|
54
|
-
all(path, optionsOrHandler, handler) {
|
|
55
|
-
return this.registerRoute("all", path, optionsOrHandler, handler);
|
|
56
|
-
}
|
|
57
|
-
registerRoute(method, path, optionsOrHandler, handler) {
|
|
58
|
-
const middlewares = [];
|
|
59
|
-
if (typeof optionsOrHandler === "object") {
|
|
60
|
-
const options = optionsOrHandler;
|
|
61
|
-
if (options.middleware) middlewares.push(...options.middleware);
|
|
62
|
-
if (options.bodySchema) middlewares.push(this.createBodyValidationMiddleware(options.bodySchema));
|
|
63
|
-
if (options.querySchema) middlewares.push(this.createQueryValidationMiddleware(options.querySchema));
|
|
64
|
-
middlewares.push(handler);
|
|
65
|
-
} else middlewares.push(optionsOrHandler);
|
|
66
|
-
this.router[method](path, ...middlewares);
|
|
67
|
-
return this;
|
|
68
|
-
}
|
|
69
|
-
createBodyValidationMiddleware(schema) {
|
|
70
|
-
return (req, res, next) => {
|
|
71
|
-
try {
|
|
72
|
-
req.body = schema.parse(req.body);
|
|
73
|
-
next();
|
|
74
|
-
} catch (error) {
|
|
75
|
-
if (error instanceof z.ZodError) res.status(400).json({
|
|
76
|
-
error: "Validation failed",
|
|
77
|
-
details: error.errors
|
|
78
|
-
});
|
|
79
|
-
else next(error);
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
createQueryValidationMiddleware(schema) {
|
|
84
|
-
return (req, res, next) => {
|
|
85
|
-
try {
|
|
86
|
-
req.query = schema.parse(req.query);
|
|
87
|
-
next();
|
|
88
|
-
} catch (error) {
|
|
89
|
-
if (error instanceof z.ZodError) res.status(400).json({
|
|
90
|
-
error: "Validation failed",
|
|
91
|
-
details: error.errors
|
|
92
|
-
});
|
|
93
|
-
else next(error);
|
|
94
|
-
}
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
/**
|
|
99
|
-
* Create a new strongly-typed Express router instance.
|
|
100
|
-
*
|
|
101
|
-
* This is the simplest way to get started with @minisylar/express-typed-router.
|
|
102
|
-
*
|
|
103
|
-
* @example
|
|
104
|
-
* import { createTypedRouter } from '@minisylar/express-typed-router';
|
|
105
|
-
*
|
|
106
|
-
* // Create a router and add a typed GET route
|
|
107
|
-
* const router = createTypedRouter();
|
|
108
|
-
* router.get('/hello/:name', (req, res) => {
|
|
109
|
-
* // req.params.name is typed as string
|
|
110
|
-
* res.json({ message: `Hello, ${req.params.name}!` });
|
|
111
|
-
* });
|
|
112
|
-
*
|
|
113
|
-
* // Use with Express
|
|
114
|
-
* import express from 'express';
|
|
115
|
-
* const app = express();
|
|
116
|
-
* app.use('/api', router.getRouter());
|
|
117
|
-
*/
|
|
118
|
-
function createTypedRouter() {
|
|
119
|
-
return new TypedRouter();
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Create a new typed router with optional configuration.
|
|
123
|
-
*
|
|
124
|
-
* Use this if you want to add a global error handler or future global options.
|
|
125
|
-
*
|
|
126
|
-
* @param config - Optional configuration for the router (e.g. error handler).
|
|
127
|
-
* @returns A new TypedRouter instance.
|
|
128
|
-
*
|
|
129
|
-
* @example
|
|
130
|
-
* import { createTypedRouterWithConfig } from '@minisylar/express-typed-router';
|
|
131
|
-
*
|
|
132
|
-
* const router = createTypedRouterWithConfig({
|
|
133
|
-
* errorHandler: (err, req, res, next) => {
|
|
134
|
-
* res.status(500).json({ error: 'Something went wrong', details: err });
|
|
135
|
-
* }
|
|
136
|
-
* });
|
|
137
|
-
*/
|
|
138
|
-
function createTypedRouterWithConfig(config) {
|
|
139
|
-
const router = new TypedRouter();
|
|
140
|
-
if (config?.errorHandler) router.getRouter().use(config.errorHandler);
|
|
141
|
-
return router;
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Create a new typed router with pre-configured middleware.
|
|
145
|
-
*
|
|
146
|
-
* This is useful for setting up router-level middleware in a single call.
|
|
147
|
-
*
|
|
148
|
-
* @param middleware - One or more TypedMiddleware functions to apply to all routes.
|
|
149
|
-
* @returns A new TypedRouter instance with the middleware applied.
|
|
150
|
-
*
|
|
151
|
-
* @example
|
|
152
|
-
* import { createTypedRouterWithMiddleware } from '@minisylar/express-typed-router';
|
|
153
|
-
*
|
|
154
|
-
* const router = createTypedRouterWithMiddleware(authMiddleware, loggingMiddleware);
|
|
155
|
-
*/
|
|
156
|
-
function createTypedRouterWithMiddleware(...middleware) {
|
|
157
|
-
let router = new TypedRouter();
|
|
158
|
-
for (const mw of middleware) router = router.useMiddleware(mw);
|
|
159
|
-
return router;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
//#endregion
|
|
163
|
-
export { createTypedRouter, createTypedRouterWithConfig, createTypedRouterWithMiddleware };
|
|
1
|
+
import e from"express";import*as t from"zod/v3";import*as n from"zod/v4/core";function r(e){return`_zod`in e}function i(e,t){return r(e)?n.parse(e,t):e.parse(t)}function a(e,t){return r(e)?n.safeParse(e,t):e.safeParse(t)}function o(e){return e instanceof t.ZodError||typeof e==`object`&&!!e&&`issues`in e&&Array.isArray(e.issues)}var s=class{router;constructor(){this.router=e.Router()}useMiddleware(e){return this.router.use(e),this}getRouter(){return this.router}get(e,t,n){return this.registerRoute(`get`,e,t,n)}post(e,t,n){return this.registerRoute(`post`,e,t,n)}put(e,t,n){return this.registerRoute(`put`,e,t,n)}patch(e,t,n){return this.registerRoute(`patch`,e,t,n)}delete(e,t,n){return this.registerRoute(`delete`,e,t,n)}options(e,t,n){return this.registerRoute(`options`,e,t,n)}head(e,t,n){return this.registerRoute(`head`,e,t,n)}all(e,t,n){return this.registerRoute(`all`,e,t,n)}registerRoute(e,t,n,r){let i=[];if(typeof n==`object`){let e=n;e.middleware&&i.push(...e.middleware),e.bodySchema&&i.push(this.createBodyValidationMiddleware(e.bodySchema)),e.querySchema&&i.push(this.createQueryValidationMiddleware(e.querySchema)),i.push(r)}else i.push(n);return this.router[e](t,...i),this}createBodyValidationMiddleware(e){return(t,n,r)=>{try{t.body=i(e,t.body),r()}catch(e){o(e)?n.status(400).json({error:`Validation failed`,details:e.errors||e.issues}):r(e)}}}createQueryValidationMiddleware(e){return(t,n,r)=>{try{t.query=i(e,t.query),r()}catch(e){o(e)?n.status(400).json({error:`Validation failed`,details:e.errors||e.issues}):r(e)}}}};function c(){return new s}function l(e){let t=new s;return e?.errorHandler&&t.getRouter().use(e.errorHandler),t}function u(...e){let t=new s;for(let n of e)t=t.useMiddleware(n);return t}export{c as createTypedRouter,l as createTypedRouterWithConfig,u as createTypedRouterWithMiddleware,r as isZod4Schema,o as isZodError,i as parseSchema,a as safeParseSchema};
|
|
164
2
|
//# sourceMappingURL=zod-router.js.map
|