@minisylar/express-typed-router 1.1.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/README.md +207 -22
- package/dist/zod-router.cjs +1 -179
- package/dist/zod-router.cjs.map +1 -1
- package/dist/zod-router.d.cts +73 -56
- package/dist/zod-router.d.ts +73 -56
- package/dist/zod-router.js +1 -154
- package/dist/zod-router.js.map +1 -1
- package/package.json +6 -5
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,10 +92,10 @@ 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
|
|
106
|
-
middleware?:
|
|
95
|
+
interface RouteOptions<BodySchema extends AnyZodType | unknown = unknown, QuerySchema extends AnyZodType | unknown = unknown> {
|
|
96
|
+
bodySchema?: BodySchema;
|
|
97
|
+
querySchema?: QuerySchema;
|
|
98
|
+
middleware?: TypedMiddleware<any, any>[];
|
|
107
99
|
}
|
|
108
100
|
type HttpMethod = "get" | "post" | "put" | "delete" | "patch" | "options" | "head" | "all";
|
|
109
101
|
declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}, RouterLocals extends Record<string, any> = {}> {
|
|
@@ -112,109 +104,135 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
|
|
|
112
104
|
/**
|
|
113
105
|
* Add typed middleware that extends the request with additional properties
|
|
114
106
|
* and/or adds properties to response.locals
|
|
115
|
-
*/
|
|
116
|
-
|
|
107
|
+
*/ /**
|
|
108
|
+
* Add typed middleware to the router.
|
|
109
|
+
* This middleware will apply to all routes defined after this call.
|
|
110
|
+
*
|
|
111
|
+
* @template TReq - Type extensions for the request object
|
|
112
|
+
* @template TLocals - Type extensions for response.locals
|
|
113
|
+
* @param middleware - The typed middleware function
|
|
114
|
+
* @returns A new router instance with updated types
|
|
115
|
+
*/
|
|
116
|
+
useMiddleware<TReq extends Record<string, any> = {}, TLocals extends Record<string, any> = {}>(middleware: TypedMiddleware<TReq, TLocals>): TypedRouter<RouterMiddlewareProps & TReq, RouterLocals & TLocals>;
|
|
117
117
|
/**
|
|
118
118
|
* Get the underlying Express router
|
|
119
119
|
*/
|
|
120
120
|
getRouter(): express.Router;
|
|
121
121
|
get<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
122
|
-
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>;
|
|
123
123
|
get<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
124
124
|
middleware: Middleware;
|
|
125
125
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
126
|
-
get<Path extends string, BodySchema extends
|
|
127
|
-
middleware:
|
|
128
|
-
},
|
|
129
|
-
|
|
126
|
+
get<Path extends string, BodySchema extends AnyZodType | unknown, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: RouteOptions<BodySchema, QuerySchema> & {
|
|
127
|
+
middleware: [...M];
|
|
128
|
+
},
|
|
129
|
+
// Using tuple spread pattern
|
|
130
|
+
handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
|
|
131
|
+
// Make it readonly for type inference
|
|
132
|
+
// Make it readonly for type inference
|
|
133
|
+
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
134
|
+
post<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
130
135
|
bodySchema: BodySchema;
|
|
131
136
|
querySchema?: QuerySchema;
|
|
132
|
-
middleware:
|
|
133
|
-
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<
|
|
134
|
-
|
|
137
|
+
middleware: [...M];
|
|
138
|
+
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
|
|
139
|
+
// Make it readonly for type inference
|
|
140
|
+
// Make it readonly for type inference
|
|
141
|
+
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
142
|
+
post<Path extends string, BodySchema extends AnyZodType, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
135
143
|
bodySchema: BodySchema;
|
|
136
|
-
middleware:
|
|
137
|
-
},
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
144
|
+
middleware: [...M];
|
|
145
|
+
},
|
|
146
|
+
// Using tuple spread pattern
|
|
147
|
+
handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
|
|
148
|
+
// Make it readonly for type inference
|
|
149
|
+
// Make it readonly for type inference
|
|
150
|
+
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
151
|
+
post<Path extends string, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
152
|
+
middleware: [...M];
|
|
153
|
+
},
|
|
154
|
+
// Using tuple spread pattern
|
|
155
|
+
handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
|
|
156
|
+
// Make it readonly for type inference
|
|
157
|
+
// Make it readonly for type inference
|
|
158
|
+
RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
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>;
|
|
142
160
|
post<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
143
|
-
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: {
|
|
144
162
|
bodySchema: BodySchema;
|
|
145
163
|
querySchema?: QuerySchema;
|
|
146
164
|
middleware: Middleware;
|
|
147
165
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
148
|
-
put<Path extends string, BodySchema extends
|
|
166
|
+
put<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
149
167
|
bodySchema: BodySchema;
|
|
150
168
|
middleware: Middleware;
|
|
151
169
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
152
170
|
put<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
153
171
|
middleware: Middleware;
|
|
154
172
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
155
|
-
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>;
|
|
156
174
|
put<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
157
|
-
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: {
|
|
158
176
|
bodySchema: BodySchema;
|
|
159
177
|
querySchema?: QuerySchema;
|
|
160
178
|
middleware: Middleware;
|
|
161
179
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
162
|
-
patch<Path extends string, BodySchema extends
|
|
180
|
+
patch<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
163
181
|
bodySchema: BodySchema;
|
|
164
182
|
middleware: Middleware;
|
|
165
183
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
166
184
|
patch<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
167
185
|
middleware: Middleware;
|
|
168
186
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
169
|
-
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>;
|
|
170
188
|
patch<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
171
|
-
delete<Path extends string, QuerySchema extends
|
|
189
|
+
delete<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
172
190
|
querySchema: QuerySchema;
|
|
173
191
|
middleware: Middleware;
|
|
174
192
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
175
|
-
delete<Path extends string, QuerySchema extends
|
|
193
|
+
delete<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
176
194
|
querySchema: QuerySchema;
|
|
177
195
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
178
196
|
delete<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
179
197
|
middleware: Middleware;
|
|
180
198
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
181
199
|
delete<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
182
|
-
options<Path extends string, QuerySchema extends
|
|
200
|
+
options<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
183
201
|
querySchema: QuerySchema;
|
|
184
202
|
middleware: Middleware;
|
|
185
203
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
186
|
-
options<Path extends string, QuerySchema extends
|
|
204
|
+
options<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
187
205
|
querySchema: QuerySchema;
|
|
188
206
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
189
207
|
options<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
190
208
|
middleware: Middleware;
|
|
191
209
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
192
210
|
options<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
193
|
-
head<Path extends string, QuerySchema extends
|
|
211
|
+
head<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
194
212
|
querySchema: QuerySchema;
|
|
195
213
|
middleware: Middleware;
|
|
196
214
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
197
|
-
head<Path extends string, QuerySchema extends
|
|
215
|
+
head<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
|
|
198
216
|
querySchema: QuerySchema;
|
|
199
217
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
200
218
|
head<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
201
219
|
middleware: Middleware;
|
|
202
220
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
203
221
|
head<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
204
|
-
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: {
|
|
205
223
|
bodySchema: BodySchema;
|
|
206
224
|
querySchema?: QuerySchema;
|
|
207
225
|
middleware: Middleware;
|
|
208
226
|
}, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
209
|
-
all<Path extends string, BodySchema extends
|
|
227
|
+
all<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
210
228
|
bodySchema: BodySchema;
|
|
211
229
|
middleware: Middleware;
|
|
212
230
|
}, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
213
|
-
all<Path extends string, QuerySchema extends
|
|
231
|
+
all<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
214
232
|
querySchema: QuerySchema;
|
|
215
233
|
middleware: Middleware;
|
|
216
234
|
}, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
217
|
-
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>;
|
|
218
236
|
all<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
|
|
219
237
|
middleware: Middleware;
|
|
220
238
|
}, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
|
|
@@ -286,7 +304,6 @@ declare function createTypedRouterWithConfig<RouterMiddlewareProps extends Recor
|
|
|
286
304
|
* const router = createTypedRouterWithMiddleware(authMiddleware, loggingMiddleware);
|
|
287
305
|
*/
|
|
288
306
|
declare function createTypedRouterWithMiddleware<T extends Record<string, any>>(...middleware: TypedMiddleware<any, any>[]): TypedRouter<T>;
|
|
289
|
-
|
|
290
307
|
//#endregion
|
|
291
|
-
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 };
|
|
292
309
|
//# sourceMappingURL=zod-router.d.ts.map
|
package/dist/zod-router.js
CHANGED
|
@@ -1,155 +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
|
-
useTypedMiddleware(middleware) {
|
|
15
|
-
this.router.use(middleware);
|
|
16
|
-
return this;
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Get the underlying Express router
|
|
20
|
-
*/
|
|
21
|
-
getRouter() {
|
|
22
|
-
return this.router;
|
|
23
|
-
}
|
|
24
|
-
get(path, optionsOrHandler, handler) {
|
|
25
|
-
return this.registerRoute("get", path, optionsOrHandler, handler);
|
|
26
|
-
}
|
|
27
|
-
post(path, optionsOrHandler, handler) {
|
|
28
|
-
return this.registerRoute("post", path, optionsOrHandler, handler);
|
|
29
|
-
}
|
|
30
|
-
put(path, optionsOrHandler, handler) {
|
|
31
|
-
return this.registerRoute("put", path, optionsOrHandler, handler);
|
|
32
|
-
}
|
|
33
|
-
patch(path, optionsOrHandler, handler) {
|
|
34
|
-
return this.registerRoute("patch", path, optionsOrHandler, handler);
|
|
35
|
-
}
|
|
36
|
-
delete(path, optionsOrHandler, handler) {
|
|
37
|
-
return this.registerRoute("delete", path, optionsOrHandler, handler);
|
|
38
|
-
}
|
|
39
|
-
options(path, optionsOrHandler, handler) {
|
|
40
|
-
return this.registerRoute("options", path, optionsOrHandler, handler);
|
|
41
|
-
}
|
|
42
|
-
head(path, optionsOrHandler, handler) {
|
|
43
|
-
return this.registerRoute("head", path, optionsOrHandler, handler);
|
|
44
|
-
}
|
|
45
|
-
all(path, optionsOrHandler, handler) {
|
|
46
|
-
return this.registerRoute("all", path, optionsOrHandler, handler);
|
|
47
|
-
}
|
|
48
|
-
registerRoute(method, path, optionsOrHandler, handler) {
|
|
49
|
-
const middlewares = [];
|
|
50
|
-
if (typeof optionsOrHandler === "object") {
|
|
51
|
-
const options = optionsOrHandler;
|
|
52
|
-
if (options.middleware) middlewares.push(...options.middleware);
|
|
53
|
-
if (options.bodySchema) middlewares.push(this.createBodyValidationMiddleware(options.bodySchema));
|
|
54
|
-
if (options.querySchema) middlewares.push(this.createQueryValidationMiddleware(options.querySchema));
|
|
55
|
-
middlewares.push(handler);
|
|
56
|
-
} else middlewares.push(optionsOrHandler);
|
|
57
|
-
this.router[method](path, ...middlewares);
|
|
58
|
-
return this;
|
|
59
|
-
}
|
|
60
|
-
createBodyValidationMiddleware(schema) {
|
|
61
|
-
return (req, res, next) => {
|
|
62
|
-
try {
|
|
63
|
-
req.body = schema.parse(req.body);
|
|
64
|
-
next();
|
|
65
|
-
} catch (error) {
|
|
66
|
-
if (error instanceof z.ZodError) res.status(400).json({
|
|
67
|
-
error: "Validation failed",
|
|
68
|
-
details: error.errors
|
|
69
|
-
});
|
|
70
|
-
else next(error);
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
createQueryValidationMiddleware(schema) {
|
|
75
|
-
return (req, res, next) => {
|
|
76
|
-
try {
|
|
77
|
-
req.query = schema.parse(req.query);
|
|
78
|
-
next();
|
|
79
|
-
} catch (error) {
|
|
80
|
-
if (error instanceof z.ZodError) res.status(400).json({
|
|
81
|
-
error: "Validation failed",
|
|
82
|
-
details: error.errors
|
|
83
|
-
});
|
|
84
|
-
else next(error);
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
/**
|
|
90
|
-
* Create a new strongly-typed Express router instance.
|
|
91
|
-
*
|
|
92
|
-
* This is the simplest way to get started with @minisylar/express-typed-router.
|
|
93
|
-
*
|
|
94
|
-
* @example
|
|
95
|
-
* import { createTypedRouter } from '@minisylar/express-typed-router';
|
|
96
|
-
*
|
|
97
|
-
* // Create a router and add a typed GET route
|
|
98
|
-
* const router = createTypedRouter();
|
|
99
|
-
* router.get('/hello/:name', (req, res) => {
|
|
100
|
-
* // req.params.name is typed as string
|
|
101
|
-
* res.json({ message: `Hello, ${req.params.name}!` });
|
|
102
|
-
* });
|
|
103
|
-
*
|
|
104
|
-
* // Use with Express
|
|
105
|
-
* import express from 'express';
|
|
106
|
-
* const app = express();
|
|
107
|
-
* app.use('/api', router.getRouter());
|
|
108
|
-
*/
|
|
109
|
-
function createTypedRouter() {
|
|
110
|
-
return new TypedRouter();
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Create a new typed router with optional configuration.
|
|
114
|
-
*
|
|
115
|
-
* Use this if you want to add a global error handler or future global options.
|
|
116
|
-
*
|
|
117
|
-
* @param config - Optional configuration for the router (e.g. error handler).
|
|
118
|
-
* @returns A new TypedRouter instance.
|
|
119
|
-
*
|
|
120
|
-
* @example
|
|
121
|
-
* import { createTypedRouterWithConfig } from '@minisylar/express-typed-router';
|
|
122
|
-
*
|
|
123
|
-
* const router = createTypedRouterWithConfig({
|
|
124
|
-
* errorHandler: (err, req, res, next) => {
|
|
125
|
-
* res.status(500).json({ error: 'Something went wrong', details: err });
|
|
126
|
-
* }
|
|
127
|
-
* });
|
|
128
|
-
*/
|
|
129
|
-
function createTypedRouterWithConfig(config) {
|
|
130
|
-
const router = new TypedRouter();
|
|
131
|
-
if (config?.errorHandler) router.getRouter().use(config.errorHandler);
|
|
132
|
-
return router;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Create a new typed router with pre-configured middleware.
|
|
136
|
-
*
|
|
137
|
-
* This is useful for setting up router-level middleware in a single call.
|
|
138
|
-
*
|
|
139
|
-
* @param middleware - One or more TypedMiddleware functions to apply to all routes.
|
|
140
|
-
* @returns A new TypedRouter instance with the middleware applied.
|
|
141
|
-
*
|
|
142
|
-
* @example
|
|
143
|
-
* import { createTypedRouterWithMiddleware } from '@minisylar/express-typed-router';
|
|
144
|
-
*
|
|
145
|
-
* const router = createTypedRouterWithMiddleware(authMiddleware, loggingMiddleware);
|
|
146
|
-
*/
|
|
147
|
-
function createTypedRouterWithMiddleware(...middleware) {
|
|
148
|
-
let router = new TypedRouter();
|
|
149
|
-
for (const mw of middleware) router = router.useTypedMiddleware(mw);
|
|
150
|
-
return router;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
//#endregion
|
|
154
|
-
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};
|
|
155
2
|
//# sourceMappingURL=zod-router.js.map
|