@minisylar/express-typed-router 1.2.0 → 1.3.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/dist/zod-router.cjs +1 -188
- package/dist/zod-router.cjs.map +1 -1
- package/dist/zod-router.d.cts +35 -44
- package/dist/zod-router.d.ts +35 -44
- package/dist/zod-router.js +1 -163
- package/dist/zod-router.js.map +1 -1
- package/package.json +6 -5
package/dist/zod-router.d.cts
CHANGED
|
@@ -1,21 +1,9 @@
|
|
|
1
1
|
import express, { NextFunction, Request, Response } from "express";
|
|
2
|
-
import { ZodType
|
|
2
|
+
import { ZodType } from "zod";
|
|
3
3
|
|
|
4
4
|
//#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
5
|
|
|
6
|
+
type AnyZodType = ZodType<any, any, any> | ZodType<any, any>;
|
|
19
7
|
/**
|
|
20
8
|
* Extract route parameters from Express.js route patterns.
|
|
21
9
|
*
|
|
@@ -85,12 +73,16 @@ type RequestOnlyMiddleware<TReq extends Record<string, any>> = TypedMiddleware<T
|
|
|
85
73
|
type LocalsOnlyMiddleware<TLocals extends Record<string, any>> = TypedMiddleware<{}, TLocals>;
|
|
86
74
|
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
75
|
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
|
|
76
|
+
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
77
|
params: ExtractRouteParams<Path>;
|
|
90
|
-
body: BodySchema extends
|
|
91
|
-
|
|
78
|
+
body: BodySchema extends {
|
|
79
|
+
_output: infer O;
|
|
80
|
+
} ? O : unknown;
|
|
81
|
+
query: QuerySchema extends {
|
|
82
|
+
_output: infer O;
|
|
83
|
+
} ? O : unknown;
|
|
92
84
|
} & MiddlewareProps;
|
|
93
|
-
type ZodRouteHandler<Path extends string = string, BodySchema extends
|
|
85
|
+
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
86
|
/**
|
|
95
87
|
* Options for defining a typed route, including schemas and middleware.
|
|
96
88
|
*
|
|
@@ -100,9 +92,9 @@ type ZodRouteHandler<Path extends string = string, BodySchema extends ZodType<an
|
|
|
100
92
|
* @property querySchema - Optional Zod schema for validating the query string.
|
|
101
93
|
* @property middleware - Optional array of TypedMiddleware for this route.
|
|
102
94
|
*/
|
|
103
|
-
interface RouteOptions<BodySchema extends
|
|
104
|
-
bodySchema?: BodySchema
|
|
105
|
-
querySchema?: QuerySchema
|
|
95
|
+
interface RouteOptions<BodySchema extends AnyZodType | unknown = unknown, QuerySchema extends AnyZodType | unknown = unknown> {
|
|
96
|
+
bodySchema?: BodySchema;
|
|
97
|
+
querySchema?: QuerySchema;
|
|
106
98
|
middleware?: TypedMiddleware<any, any>[];
|
|
107
99
|
}
|
|
108
100
|
type HttpMethod = "get" | "post" | "put" | "delete" | "patch" | "options" | "head" | "all";
|
|
@@ -127,11 +119,11 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
127
119
|
*/
|
|
128
120
|
getRouter(): express.Router;
|
|
129
121
|
get<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
130
|
-
get<Path extends string, BodySchema extends
|
|
122
|
+
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
123
|
get<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
132
124
|
middleware: Middleware;
|
|
133
125
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
134
|
-
get<Path extends string, BodySchema extends
|
|
126
|
+
get<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: RouteOptions<BodySchema, QuerySchema> & {
|
|
135
127
|
middleware: [...M];
|
|
136
128
|
},
|
|
137
129
|
// Using tuple spread pattern
|
|
@@ -139,7 +131,7 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
139
131
|
// Make it readonly for type inference
|
|
140
132
|
// Make it readonly for type inference
|
|
141
133
|
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
142
|
-
post<Path extends string, BodySchema extends
|
|
134
|
+
post<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
143
135
|
bodySchema: BodySchema;
|
|
144
136
|
querySchema?: QuerySchema;
|
|
145
137
|
middleware: [...M];
|
|
@@ -147,7 +139,7 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
147
139
|
// Make it readonly for type inference
|
|
148
140
|
// Make it readonly for type inference
|
|
149
141
|
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
150
|
-
post<Path extends string, BodySchema extends
|
|
142
|
+
post<Path extends string, BodySchema extends AnyZodType, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
151
143
|
bodySchema: BodySchema;
|
|
152
144
|
middleware: [...M];
|
|
153
145
|
},
|
|
@@ -164,83 +156,83 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
164
156
|
// Make it readonly for type inference
|
|
165
157
|
// Make it readonly for type inference
|
|
166
158
|
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
167
|
-
post<Path extends string, BodySchema extends
|
|
159
|
+
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
160
|
post<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
169
|
-
put<Path extends string, BodySchema extends
|
|
161
|
+
put<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
170
162
|
bodySchema: BodySchema;
|
|
171
163
|
querySchema?: QuerySchema;
|
|
172
164
|
middleware: Middleware;
|
|
173
165
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
174
|
-
put<Path extends string, BodySchema extends
|
|
166
|
+
put<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
175
167
|
bodySchema: BodySchema;
|
|
176
168
|
middleware: Middleware;
|
|
177
169
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
178
170
|
put<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
179
171
|
middleware: Middleware;
|
|
180
172
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
181
|
-
put<Path extends string, BodySchema extends
|
|
173
|
+
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
174
|
put<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
183
|
-
patch<Path extends string, BodySchema extends
|
|
175
|
+
patch<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
184
176
|
bodySchema: BodySchema;
|
|
185
177
|
querySchema?: QuerySchema;
|
|
186
178
|
middleware: Middleware;
|
|
187
179
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
188
|
-
patch<Path extends string, BodySchema extends
|
|
180
|
+
patch<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
189
181
|
bodySchema: BodySchema;
|
|
190
182
|
middleware: Middleware;
|
|
191
183
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
192
184
|
patch<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
193
185
|
middleware: Middleware;
|
|
194
186
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
195
|
-
patch<Path extends string, BodySchema extends
|
|
187
|
+
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
188
|
patch<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
197
|
-
delete<Path extends string, QuerySchema extends
|
|
189
|
+
delete<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
198
190
|
querySchema: QuerySchema;
|
|
199
191
|
middleware: Middleware;
|
|
200
192
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
201
|
-
delete<Path extends string, QuerySchema extends
|
|
193
|
+
delete<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
202
194
|
querySchema: QuerySchema;
|
|
203
195
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
204
196
|
delete<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
205
197
|
middleware: Middleware;
|
|
206
198
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
207
199
|
delete<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
208
|
-
options<Path extends string, QuerySchema extends
|
|
200
|
+
options<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
209
201
|
querySchema: QuerySchema;
|
|
210
202
|
middleware: Middleware;
|
|
211
203
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
212
|
-
options<Path extends string, QuerySchema extends
|
|
204
|
+
options<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
213
205
|
querySchema: QuerySchema;
|
|
214
206
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
215
207
|
options<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
216
208
|
middleware: Middleware;
|
|
217
209
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
218
210
|
options<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
219
|
-
head<Path extends string, QuerySchema extends
|
|
211
|
+
head<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
220
212
|
querySchema: QuerySchema;
|
|
221
213
|
middleware: Middleware;
|
|
222
214
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
223
|
-
head<Path extends string, QuerySchema extends
|
|
215
|
+
head<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
224
216
|
querySchema: QuerySchema;
|
|
225
217
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
226
218
|
head<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
227
219
|
middleware: Middleware;
|
|
228
220
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
229
221
|
head<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
230
|
-
all<Path extends string, BodySchema extends
|
|
222
|
+
all<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
231
223
|
bodySchema: BodySchema;
|
|
232
224
|
querySchema?: QuerySchema;
|
|
233
225
|
middleware: Middleware;
|
|
234
226
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
235
|
-
all<Path extends string, BodySchema extends
|
|
227
|
+
all<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
236
228
|
bodySchema: BodySchema;
|
|
237
229
|
middleware: Middleware;
|
|
238
230
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
239
|
-
all<Path extends string, QuerySchema extends
|
|
231
|
+
all<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
240
232
|
querySchema: QuerySchema;
|
|
241
233
|
middleware: Middleware;
|
|
242
234
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
243
|
-
all<Path extends string, BodySchema extends
|
|
235
|
+
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
236
|
all<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
245
237
|
middleware: Middleware;
|
|
246
238
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
@@ -312,7 +304,6 @@ declare function createTypedRouterWithConfig<RouterMiddlewareProps extends Recor
|
|
|
312
304
|
* const router = createTypedRouterWithMiddleware(authMiddleware, loggingMiddleware);
|
|
313
305
|
*/
|
|
314
306
|
declare function createTypedRouterWithMiddleware<T extends Record<string, any>>(...middleware: TypedMiddleware<any, any>[]): TypedRouter<T>;
|
|
315
|
-
|
|
316
307
|
//#endregion
|
|
317
|
-
export { ExtractRouteParams, HttpMethod, LocalsOnlyMiddleware, RequestOnlyMiddleware, RouteOptions, RouterConfig, TypedMiddleware, ZodRequest, ZodRouteHandler, createTypedRouter, createTypedRouterWithConfig, createTypedRouterWithMiddleware };
|
|
308
|
+
export { AnyZodType, ExtractRouteParams, HttpMethod, LocalsOnlyMiddleware, RequestOnlyMiddleware, RouteOptions, RouterConfig, TypedMiddleware, ZodRequest, ZodRouteHandler, createTypedRouter, createTypedRouterWithConfig, createTypedRouterWithMiddleware };
|
|
318
309
|
//# sourceMappingURL=zod-router.d.cts.map
|
package/dist/zod-router.d.ts
CHANGED
|
@@ -1,21 +1,9 @@
|
|
|
1
1
|
import express, { NextFunction, Request, Response } from "express";
|
|
2
|
-
import { ZodType
|
|
2
|
+
import { ZodType } from "zod";
|
|
3
3
|
|
|
4
4
|
//#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
5
|
|
|
6
|
+
type AnyZodType = ZodType<any, any, any> | ZodType<any, any>;
|
|
19
7
|
/**
|
|
20
8
|
* Extract route parameters from Express.js route patterns.
|
|
21
9
|
*
|
|
@@ -85,12 +73,16 @@ type RequestOnlyMiddleware<TReq extends Record<string, any>> = TypedMiddleware<T
|
|
|
85
73
|
type LocalsOnlyMiddleware<TLocals extends Record<string, any>> = TypedMiddleware<{}, TLocals>;
|
|
86
74
|
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
75
|
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
|
|
76
|
+
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
77
|
params: ExtractRouteParams<Path>;
|
|
90
|
-
body: BodySchema extends
|
|
91
|
-
|
|
78
|
+
body: BodySchema extends {
|
|
79
|
+
_output: infer O;
|
|
80
|
+
} ? O : unknown;
|
|
81
|
+
query: QuerySchema extends {
|
|
82
|
+
_output: infer O;
|
|
83
|
+
} ? O : unknown;
|
|
92
84
|
} & MiddlewareProps;
|
|
93
|
-
type ZodRouteHandler<Path extends string = string, BodySchema extends
|
|
85
|
+
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
86
|
/**
|
|
95
87
|
* Options for defining a typed route, including schemas and middleware.
|
|
96
88
|
*
|
|
@@ -100,9 +92,9 @@ type ZodRouteHandler<Path extends string = string, BodySchema extends ZodType<an
|
|
|
100
92
|
* @property querySchema - Optional Zod schema for validating the query string.
|
|
101
93
|
* @property middleware - Optional array of TypedMiddleware for this route.
|
|
102
94
|
*/
|
|
103
|
-
interface RouteOptions<BodySchema extends
|
|
104
|
-
bodySchema?: BodySchema
|
|
105
|
-
querySchema?: QuerySchema
|
|
95
|
+
interface RouteOptions<BodySchema extends AnyZodType | unknown = unknown, QuerySchema extends AnyZodType | unknown = unknown> {
|
|
96
|
+
bodySchema?: BodySchema;
|
|
97
|
+
querySchema?: QuerySchema;
|
|
106
98
|
middleware?: TypedMiddleware<any, any>[];
|
|
107
99
|
}
|
|
108
100
|
type HttpMethod = "get" | "post" | "put" | "delete" | "patch" | "options" | "head" | "all";
|
|
@@ -127,11 +119,11 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
127
119
|
*/
|
|
128
120
|
getRouter(): express.Router;
|
|
129
121
|
get<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
130
|
-
get<Path extends string, BodySchema extends
|
|
122
|
+
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
123
|
get<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
132
124
|
middleware: Middleware;
|
|
133
125
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
134
|
-
get<Path extends string, BodySchema extends
|
|
126
|
+
get<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: RouteOptions<BodySchema, QuerySchema> & {
|
|
135
127
|
middleware: [...M];
|
|
136
128
|
},
|
|
137
129
|
// Using tuple spread pattern
|
|
@@ -139,7 +131,7 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
139
131
|
// Make it readonly for type inference
|
|
140
132
|
// Make it readonly for type inference
|
|
141
133
|
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
142
|
-
post<Path extends string, BodySchema extends
|
|
134
|
+
post<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
143
135
|
bodySchema: BodySchema;
|
|
144
136
|
querySchema?: QuerySchema;
|
|
145
137
|
middleware: [...M];
|
|
@@ -147,7 +139,7 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
147
139
|
// Make it readonly for type inference
|
|
148
140
|
// Make it readonly for type inference
|
|
149
141
|
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
150
|
-
post<Path extends string, BodySchema extends
|
|
142
|
+
post<Path extends string, BodySchema extends AnyZodType, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
151
143
|
bodySchema: BodySchema;
|
|
152
144
|
middleware: [...M];
|
|
153
145
|
},
|
|
@@ -164,83 +156,83 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
164
156
|
// Make it readonly for type inference
|
|
165
157
|
// Make it readonly for type inference
|
|
166
158
|
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
167
|
-
post<Path extends string, BodySchema extends
|
|
159
|
+
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
160
|
post<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
169
|
-
put<Path extends string, BodySchema extends
|
|
161
|
+
put<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
170
162
|
bodySchema: BodySchema;
|
|
171
163
|
querySchema?: QuerySchema;
|
|
172
164
|
middleware: Middleware;
|
|
173
165
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
174
|
-
put<Path extends string, BodySchema extends
|
|
166
|
+
put<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
175
167
|
bodySchema: BodySchema;
|
|
176
168
|
middleware: Middleware;
|
|
177
169
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
178
170
|
put<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
179
171
|
middleware: Middleware;
|
|
180
172
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
181
|
-
put<Path extends string, BodySchema extends
|
|
173
|
+
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
174
|
put<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
183
|
-
patch<Path extends string, BodySchema extends
|
|
175
|
+
patch<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
184
176
|
bodySchema: BodySchema;
|
|
185
177
|
querySchema?: QuerySchema;
|
|
186
178
|
middleware: Middleware;
|
|
187
179
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
188
|
-
patch<Path extends string, BodySchema extends
|
|
180
|
+
patch<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
189
181
|
bodySchema: BodySchema;
|
|
190
182
|
middleware: Middleware;
|
|
191
183
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
192
184
|
patch<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
193
185
|
middleware: Middleware;
|
|
194
186
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
195
|
-
patch<Path extends string, BodySchema extends
|
|
187
|
+
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
188
|
patch<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
197
|
-
delete<Path extends string, QuerySchema extends
|
|
189
|
+
delete<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
198
190
|
querySchema: QuerySchema;
|
|
199
191
|
middleware: Middleware;
|
|
200
192
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
201
|
-
delete<Path extends string, QuerySchema extends
|
|
193
|
+
delete<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
202
194
|
querySchema: QuerySchema;
|
|
203
195
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
204
196
|
delete<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
205
197
|
middleware: Middleware;
|
|
206
198
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
207
199
|
delete<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
208
|
-
options<Path extends string, QuerySchema extends
|
|
200
|
+
options<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
209
201
|
querySchema: QuerySchema;
|
|
210
202
|
middleware: Middleware;
|
|
211
203
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
212
|
-
options<Path extends string, QuerySchema extends
|
|
204
|
+
options<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
213
205
|
querySchema: QuerySchema;
|
|
214
206
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
215
207
|
options<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
216
208
|
middleware: Middleware;
|
|
217
209
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
218
210
|
options<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
219
|
-
head<Path extends string, QuerySchema extends
|
|
211
|
+
head<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
220
212
|
querySchema: QuerySchema;
|
|
221
213
|
middleware: Middleware;
|
|
222
214
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
223
|
-
head<Path extends string, QuerySchema extends
|
|
215
|
+
head<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
224
216
|
querySchema: QuerySchema;
|
|
225
217
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
226
218
|
head<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
227
219
|
middleware: Middleware;
|
|
228
220
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
229
221
|
head<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
230
|
-
all<Path extends string, BodySchema extends
|
|
222
|
+
all<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
231
223
|
bodySchema: BodySchema;
|
|
232
224
|
querySchema?: QuerySchema;
|
|
233
225
|
middleware: Middleware;
|
|
234
226
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
235
|
-
all<Path extends string, BodySchema extends
|
|
227
|
+
all<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
236
228
|
bodySchema: BodySchema;
|
|
237
229
|
middleware: Middleware;
|
|
238
230
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
239
|
-
all<Path extends string, QuerySchema extends
|
|
231
|
+
all<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
240
232
|
querySchema: QuerySchema;
|
|
241
233
|
middleware: Middleware;
|
|
242
234
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
243
|
-
all<Path extends string, BodySchema extends
|
|
235
|
+
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
236
|
all<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
245
237
|
middleware: Middleware;
|
|
246
238
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
@@ -312,7 +304,6 @@ declare function createTypedRouterWithConfig<RouterMiddlewareProps extends Recor
|
|
|
312
304
|
* const router = createTypedRouterWithMiddleware(authMiddleware, loggingMiddleware);
|
|
313
305
|
*/
|
|
314
306
|
declare function createTypedRouterWithMiddleware<T extends Record<string, any>>(...middleware: TypedMiddleware<any, any>[]): TypedRouter<T>;
|
|
315
|
-
|
|
316
307
|
//#endregion
|
|
317
|
-
export { ExtractRouteParams, HttpMethod, LocalsOnlyMiddleware, RequestOnlyMiddleware, RouteOptions, RouterConfig, TypedMiddleware, ZodRequest, ZodRouteHandler, createTypedRouter, createTypedRouterWithConfig, createTypedRouterWithMiddleware };
|
|
308
|
+
export { AnyZodType, ExtractRouteParams, HttpMethod, LocalsOnlyMiddleware, RequestOnlyMiddleware, RouteOptions, RouterConfig, TypedMiddleware, ZodRequest, ZodRouteHandler, createTypedRouter, createTypedRouterWithConfig, createTypedRouterWithMiddleware };
|
|
318
309
|
//# sourceMappingURL=zod-router.d.ts.map
|
package/dist/zod-router.js
CHANGED
|
@@ -1,164 +1,2 @@
|
|
|
1
|
-
import
|
|
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{z as t}from"zod";var n=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(n,r,i)=>{try{n.body=e.parse(n.body),i()}catch(e){e instanceof t.ZodError||e&&typeof e==`object`&&`issues`in e?r.status(400).json({error:`Validation failed`,details:e.errors||e.issues}):i(e)}}}createQueryValidationMiddleware(e){return(n,r,i)=>{try{n.query=e.parse(n.query),i()}catch(e){e instanceof t.ZodError||e&&typeof e==`object`&&`issues`in e?r.status(400).json({error:`Validation failed`,details:e.errors||e.issues}):i(e)}}}};function r(){return new n}function i(e){let t=new n;return e?.errorHandler&&t.getRouter().use(e.errorHandler),t}function a(...e){let t=new n;for(let n of e)t=t.useMiddleware(n);return t}export{r as createTypedRouter,i as createTypedRouterWithConfig,a as createTypedRouterWithMiddleware};
|
|
164
2
|
//# sourceMappingURL=zod-router.js.map
|