@minisylar/express-typed-router 1.4.3 → 1.5.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.
@@ -1,2 +1 @@
1
- var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));const c=s(require(`express`)),l=s(require(`zod/v3`)),u=s(require(`zod/v4/core`));function d(e){return`_zod`in e}function f(e,t){return d(e)?u.parse(e,t):e.parse(t)}function p(e,t){return d(e)?u.safeParse(e,t):e.safeParse(t)}function m(e){return e instanceof l.ZodError||typeof e==`object`&&!!e&&`issues`in e&&Array.isArray(e.issues)}var h=class{router;constructor(){this.router=c.default.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=f(e,t.body),r()}catch(e){m(e)?n.status(400).json({error:`Validation failed`,details:e.errors||e.issues}):r(e)}}}createQueryValidationMiddleware(e){return(t,n,r)=>{try{let n=f(e,t.query);Object.defineProperty(t,`query`,{value:n,writable:!1,enumerable:!0,configurable:!0}),r()}catch(e){m(e)?n.status(400).json({error:`Validation failed`,details:e.errors||e.issues}):r(e)}}}};function g(){return new h}function _(e){let t=new h;return e?.errorHandler&&t.getRouter().use(e.errorHandler),t}function v(...e){let t=new h;for(let n of e)t=t.useMiddleware(n);return t}exports.createTypedRouter=g,exports.createTypedRouterWithConfig=_,exports.createTypedRouterWithMiddleware=v,exports.isZod4Schema=d,exports.isZodError=m,exports.parseSchema=f,exports.safeParseSchema=p;
2
- //# sourceMappingURL=zod-router.cjs.map
1
+ var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));const c=s(require(`express`)),l=s(require(`zod/v3`)),u=s(require(`zod/v4/core`));function d(e){return`_zod`in e}function f(e,t){return d(e)?u.parse(e,t):e.parse(t)}function p(e,t){return d(e)?u.safeParse(e,t):e.safeParse(t)}function m(e){return e instanceof l.ZodError||typeof e==`object`&&!!e&&`issues`in e&&Array.isArray(e.issues)}var h=class{router;constructor(){this.router=c.default.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=f(e,t.body),r()}catch(e){m(e)?n.status(400).json({error:`Validation failed`,details:e.errors||e.issues}):r(e)}}}createQueryValidationMiddleware(e){return(t,n,r)=>{try{let n=f(e,t.query);Object.defineProperty(t,`query`,{value:n,writable:!1,enumerable:!0,configurable:!0}),r()}catch(e){m(e)?n.status(400).json({error:`Validation failed`,details:e.errors||e.issues}):r(e)}}}};function g(){return new h}function _(e){let t=new h;return e?.errorHandler&&t.getRouter().use(e.errorHandler),t}function v(...e){let t=new h;for(let n of e)t=t.useMiddleware(n);return t}exports.createTypedRouter=g,exports.createTypedRouterWithConfig=_,exports.createTypedRouterWithMiddleware=v,exports.isZod4Schema=d,exports.isZodError=m,exports.parseSchema=f,exports.safeParseSchema=p;
@@ -94,7 +94,7 @@ type ZodRequest<Path extends string = string, BodySchema extends AnyZodType | un
94
94
  body: BodySchema extends AnyZodType ? InferOutput<BodySchema> : unknown;
95
95
  query: QuerySchema extends AnyZodType ? InferOutput<QuerySchema> : unknown;
96
96
  } & MiddlewareProps;
97
- 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>;
97
+ 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 | undefined | Promise<void | undefined> | Response | Promise<Response> | Promise<Response | undefined>;
98
98
  /**
99
99
  * Options for defining a typed route, including schemas and middleware.
100
100
  *
@@ -170,84 +170,158 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
170
170
  RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
171
171
  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>;
172
172
  post<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
173
- put<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
173
+ put<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
174
174
  bodySchema: BodySchema;
175
175
  querySchema?: QuerySchema;
176
- middleware: Middleware;
177
- }, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
178
- put<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
176
+ middleware: [...M];
177
+ }, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
178
+ // Make it readonly for type inference
179
+ // Make it readonly for type inference
180
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
181
+ put<Path extends string, BodySchema extends AnyZodType, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
179
182
  bodySchema: BodySchema;
180
- middleware: Middleware;
181
- }, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
182
- put<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
183
- middleware: Middleware;
184
- }, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
183
+ middleware: [...M];
184
+ },
185
+ // Using tuple spread pattern
186
+ handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
187
+ // Make it readonly for type inference
188
+ // Make it readonly for type inference
189
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
190
+ put<Path extends string, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
191
+ middleware: [...M];
192
+ },
193
+ // Using tuple spread pattern
194
+ handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
195
+ // Make it readonly for type inference
196
+ // Make it readonly for type inference
197
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
185
198
  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>;
186
199
  put<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
187
- patch<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
200
+ patch<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
188
201
  bodySchema: BodySchema;
189
202
  querySchema?: QuerySchema;
190
- middleware: Middleware;
191
- }, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
192
- patch<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
203
+ middleware: [...M];
204
+ }, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
205
+ // Make it readonly for type inference
206
+ // Make it readonly for type inference
207
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
208
+ patch<Path extends string, BodySchema extends AnyZodType, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
193
209
  bodySchema: BodySchema;
194
- middleware: Middleware;
195
- }, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
196
- patch<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
197
- middleware: Middleware;
198
- }, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
210
+ middleware: [...M];
211
+ },
212
+ // Using tuple spread pattern
213
+ handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
214
+ // Make it readonly for type inference
215
+ // Make it readonly for type inference
216
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
217
+ patch<Path extends string, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
218
+ middleware: [...M];
219
+ },
220
+ // Using tuple spread pattern
221
+ handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
222
+ // Make it readonly for type inference
223
+ // Make it readonly for type inference
224
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
199
225
  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>;
200
226
  patch<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
201
- delete<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
227
+ delete<Path extends string, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
202
228
  querySchema: QuerySchema;
203
- middleware: Middleware;
204
- }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
229
+ middleware: [...M];
230
+ },
231
+ // Using tuple spread pattern
232
+ handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
233
+ // Make it readonly for type inference
234
+ // Make it readonly for type inference
235
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
205
236
  delete<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
206
237
  querySchema: QuerySchema;
207
238
  }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
208
- delete<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
209
- middleware: Middleware;
210
- }, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
239
+ delete<Path extends string, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
240
+ middleware: [...M];
241
+ },
242
+ // Using tuple spread pattern
243
+ handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
244
+ // Make it readonly for type inference
245
+ // Make it readonly for type inference
246
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
211
247
  delete<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
212
- options<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
248
+ options<Path extends string, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
213
249
  querySchema: QuerySchema;
214
- middleware: Middleware;
215
- }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
250
+ middleware: [...M];
251
+ },
252
+ // Using tuple spread pattern
253
+ handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
254
+ // Make it readonly for type inference
255
+ // Make it readonly for type inference
256
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
216
257
  options<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
217
258
  querySchema: QuerySchema;
218
259
  }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
219
- options<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
220
- middleware: Middleware;
221
- }, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
260
+ options<Path extends string, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
261
+ middleware: [...M];
262
+ },
263
+ // Using tuple spread pattern
264
+ handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
265
+ // Make it readonly for type inference
266
+ // Make it readonly for type inference
267
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
222
268
  options<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
223
- head<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
269
+ head<Path extends string, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
224
270
  querySchema: QuerySchema;
225
- middleware: Middleware;
226
- }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
271
+ middleware: [...M];
272
+ },
273
+ // Using tuple spread pattern
274
+ handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
275
+ // Make it readonly for type inference
276
+ // Make it readonly for type inference
277
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
227
278
  head<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
228
279
  querySchema: QuerySchema;
229
280
  }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
230
- head<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
231
- middleware: Middleware;
232
- }, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
281
+ head<Path extends string, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
282
+ middleware: [...M];
283
+ },
284
+ // Using tuple spread pattern
285
+ handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
286
+ // Make it readonly for type inference
287
+ // Make it readonly for type inference
288
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
233
289
  head<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
234
- all<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
290
+ all<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
235
291
  bodySchema: BodySchema;
236
292
  querySchema?: QuerySchema;
237
- middleware: Middleware;
238
- }, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
239
- all<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
293
+ middleware: [...M];
294
+ }, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
295
+ // Make it readonly for type inference
296
+ // Make it readonly for type inference
297
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
298
+ all<Path extends string, BodySchema extends AnyZodType, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
240
299
  bodySchema: BodySchema;
241
- middleware: Middleware;
242
- }, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
243
- all<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
300
+ middleware: [...M];
301
+ },
302
+ // Using tuple spread pattern
303
+ handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
304
+ // Make it readonly for type inference
305
+ // Make it readonly for type inference
306
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
307
+ all<Path extends string, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
244
308
  querySchema: QuerySchema;
245
- middleware: Middleware;
246
- }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
309
+ middleware: [...M];
310
+ },
311
+ // Using tuple spread pattern
312
+ handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
313
+ // Make it readonly for type inference
314
+ // Make it readonly for type inference
315
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
247
316
  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>;
248
- all<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
249
- middleware: Middleware;
250
- }, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
317
+ all<Path extends string, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
318
+ middleware: [...M];
319
+ },
320
+ // Using tuple spread pattern
321
+ handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
322
+ // Make it readonly for type inference
323
+ // Make it readonly for type inference
324
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
251
325
  all<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
252
326
  private registerRoute;
253
327
  private createBodyValidationMiddleware;
@@ -317,5 +391,4 @@ declare function createTypedRouterWithConfig<RouterMiddlewareProps extends Recor
317
391
  */
318
392
  declare function createTypedRouterWithMiddleware<T extends Record<string, any>>(...middleware: TypedMiddleware<any, any>[]): TypedRouter<T>;
319
393
  //#endregion
320
- export { AnyZodType, ExtractRouteParams, HttpMethod, InferInput, InferOutput, LocalsOnlyMiddleware, RequestOnlyMiddleware, RouteOptions, RouterConfig, SafeParseResult, TypedMiddleware, ZodRequest, ZodRouteHandler, createTypedRouter, createTypedRouterWithConfig, createTypedRouterWithMiddleware, isZod4Schema, isZodError, parseSchema, safeParseSchema };
321
- //# sourceMappingURL=zod-router.d.cts.map
394
+ export { AnyZodType, ExtractRouteParams, HttpMethod, InferInput, InferOutput, LocalsOnlyMiddleware, RequestOnlyMiddleware, RouteOptions, RouterConfig, SafeParseResult, TypedMiddleware, ZodRequest, ZodRouteHandler, createTypedRouter, createTypedRouterWithConfig, createTypedRouterWithMiddleware, isZod4Schema, isZodError, parseSchema, safeParseSchema };
@@ -94,7 +94,7 @@ type ZodRequest<Path extends string = string, BodySchema extends AnyZodType | un
94
94
  body: BodySchema extends AnyZodType ? InferOutput<BodySchema> : unknown;
95
95
  query: QuerySchema extends AnyZodType ? InferOutput<QuerySchema> : unknown;
96
96
  } & MiddlewareProps;
97
- 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>;
97
+ 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 | undefined | Promise<void | undefined> | Response | Promise<Response> | Promise<Response | undefined>;
98
98
  /**
99
99
  * Options for defining a typed route, including schemas and middleware.
100
100
  *
@@ -170,84 +170,158 @@ declare class TypedRouter<RouterMiddlewareProps extends Record<string, any> = {}
170
170
  RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
171
171
  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>;
172
172
  post<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
173
- put<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
173
+ put<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
174
174
  bodySchema: BodySchema;
175
175
  querySchema?: QuerySchema;
176
- middleware: Middleware;
177
- }, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
178
- put<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
176
+ middleware: [...M];
177
+ }, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
178
+ // Make it readonly for type inference
179
+ // Make it readonly for type inference
180
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
181
+ put<Path extends string, BodySchema extends AnyZodType, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
179
182
  bodySchema: BodySchema;
180
- middleware: Middleware;
181
- }, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
182
- put<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
183
- middleware: Middleware;
184
- }, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
183
+ middleware: [...M];
184
+ },
185
+ // Using tuple spread pattern
186
+ handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
187
+ // Make it readonly for type inference
188
+ // Make it readonly for type inference
189
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
190
+ put<Path extends string, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
191
+ middleware: [...M];
192
+ },
193
+ // Using tuple spread pattern
194
+ handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
195
+ // Make it readonly for type inference
196
+ // Make it readonly for type inference
197
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
185
198
  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>;
186
199
  put<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
187
- patch<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
200
+ patch<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
188
201
  bodySchema: BodySchema;
189
202
  querySchema?: QuerySchema;
190
- middleware: Middleware;
191
- }, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
192
- patch<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
203
+ middleware: [...M];
204
+ }, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
205
+ // Make it readonly for type inference
206
+ // Make it readonly for type inference
207
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
208
+ patch<Path extends string, BodySchema extends AnyZodType, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
193
209
  bodySchema: BodySchema;
194
- middleware: Middleware;
195
- }, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
196
- patch<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
197
- middleware: Middleware;
198
- }, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
210
+ middleware: [...M];
211
+ },
212
+ // Using tuple spread pattern
213
+ handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
214
+ // Make it readonly for type inference
215
+ // Make it readonly for type inference
216
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
217
+ patch<Path extends string, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
218
+ middleware: [...M];
219
+ },
220
+ // Using tuple spread pattern
221
+ handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
222
+ // Make it readonly for type inference
223
+ // Make it readonly for type inference
224
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
199
225
  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>;
200
226
  patch<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
201
- delete<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
227
+ delete<Path extends string, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
202
228
  querySchema: QuerySchema;
203
- middleware: Middleware;
204
- }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
229
+ middleware: [...M];
230
+ },
231
+ // Using tuple spread pattern
232
+ handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
233
+ // Make it readonly for type inference
234
+ // Make it readonly for type inference
235
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
205
236
  delete<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
206
237
  querySchema: QuerySchema;
207
238
  }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
208
- delete<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
209
- middleware: Middleware;
210
- }, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
239
+ delete<Path extends string, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
240
+ middleware: [...M];
241
+ },
242
+ // Using tuple spread pattern
243
+ handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
244
+ // Make it readonly for type inference
245
+ // Make it readonly for type inference
246
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
211
247
  delete<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
212
- options<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
248
+ options<Path extends string, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
213
249
  querySchema: QuerySchema;
214
- middleware: Middleware;
215
- }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
250
+ middleware: [...M];
251
+ },
252
+ // Using tuple spread pattern
253
+ handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
254
+ // Make it readonly for type inference
255
+ // Make it readonly for type inference
256
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
216
257
  options<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
217
258
  querySchema: QuerySchema;
218
259
  }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
219
- options<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
220
- middleware: Middleware;
221
- }, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
260
+ options<Path extends string, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
261
+ middleware: [...M];
262
+ },
263
+ // Using tuple spread pattern
264
+ handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
265
+ // Make it readonly for type inference
266
+ // Make it readonly for type inference
267
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
222
268
  options<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
223
- head<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
269
+ head<Path extends string, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
224
270
  querySchema: QuerySchema;
225
- middleware: Middleware;
226
- }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
271
+ middleware: [...M];
272
+ },
273
+ // Using tuple spread pattern
274
+ handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
275
+ // Make it readonly for type inference
276
+ // Make it readonly for type inference
277
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
227
278
  head<Path extends string, QuerySchema extends AnyZodType | unknown>(path: Path, options: {
228
279
  querySchema: QuerySchema;
229
280
  }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
230
- head<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
231
- middleware: Middleware;
232
- }, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
281
+ head<Path extends string, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
282
+ middleware: [...M];
283
+ },
284
+ // Using tuple spread pattern
285
+ handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
286
+ // Make it readonly for type inference
287
+ // Make it readonly for type inference
288
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
233
289
  head<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
234
- all<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
290
+ all<Path extends string, BodySchema extends AnyZodType, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
235
291
  bodySchema: BodySchema;
236
292
  querySchema?: QuerySchema;
237
- middleware: Middleware;
238
- }, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
239
- all<Path extends string, BodySchema extends AnyZodType, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
293
+ middleware: [...M];
294
+ }, handler: ZodRouteHandler<Path, BodySchema, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
295
+ // Make it readonly for type inference
296
+ // Make it readonly for type inference
297
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
298
+ all<Path extends string, BodySchema extends AnyZodType, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
240
299
  bodySchema: BodySchema;
241
- middleware: Middleware;
242
- }, handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
243
- all<Path extends string, QuerySchema extends AnyZodType | unknown, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
300
+ middleware: [...M];
301
+ },
302
+ // Using tuple spread pattern
303
+ handler: ZodRouteHandler<Path, BodySchema, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
304
+ // Make it readonly for type inference
305
+ // Make it readonly for type inference
306
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
307
+ all<Path extends string, QuerySchema extends AnyZodType | unknown, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
244
308
  querySchema: QuerySchema;
245
- middleware: Middleware;
246
- }, handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
309
+ middleware: [...M];
310
+ },
311
+ // Using tuple spread pattern
312
+ handler: ZodRouteHandler<Path, unknown, QuerySchema, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
313
+ // Make it readonly for type inference
314
+ // Make it readonly for type inference
315
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
247
316
  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>;
248
- all<Path extends string, Middleware extends readonly TypedMiddleware<any, any>[]>(path: Path, options: {
249
- middleware: Middleware;
250
- }, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<Middleware>, RouterLocals & InferMiddlewareLocals<Middleware>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
317
+ all<Path extends string, M extends TypedMiddleware<any, any>[]>(path: Path, options: {
318
+ middleware: [...M];
319
+ },
320
+ // Using tuple spread pattern
321
+ handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>,
322
+ // Make it readonly for type inference
323
+ // Make it readonly for type inference
324
+ RouterLocals & InferMiddlewareLocals<readonly [...M]>>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
251
325
  all<Path extends string>(path: Path, handler: ZodRouteHandler<Path, unknown, unknown, RouterMiddlewareProps, RouterLocals>): TypedRouter<RouterMiddlewareProps, RouterLocals>;
252
326
  private registerRoute;
253
327
  private createBodyValidationMiddleware;
@@ -317,5 +391,4 @@ declare function createTypedRouterWithConfig<RouterMiddlewareProps extends Recor
317
391
  */
318
392
  declare function createTypedRouterWithMiddleware<T extends Record<string, any>>(...middleware: TypedMiddleware<any, any>[]): TypedRouter<T>;
319
393
  //#endregion
320
- export { AnyZodType, ExtractRouteParams, HttpMethod, InferInput, InferOutput, LocalsOnlyMiddleware, RequestOnlyMiddleware, RouteOptions, RouterConfig, SafeParseResult, TypedMiddleware, ZodRequest, ZodRouteHandler, createTypedRouter, createTypedRouterWithConfig, createTypedRouterWithMiddleware, isZod4Schema, isZodError, parseSchema, safeParseSchema };
321
- //# sourceMappingURL=zod-router.d.ts.map
394
+ export { AnyZodType, ExtractRouteParams, HttpMethod, InferInput, InferOutput, LocalsOnlyMiddleware, RequestOnlyMiddleware, RouteOptions, RouterConfig, SafeParseResult, TypedMiddleware, ZodRequest, ZodRouteHandler, createTypedRouter, createTypedRouterWithConfig, createTypedRouterWithMiddleware, isZod4Schema, isZodError, parseSchema, safeParseSchema };
@@ -1,2 +1 @@
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{let n=i(e,t.query);Object.defineProperty(t,`query`,{value:n,writable:!1,enumerable:!0,configurable:!0}),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};
2
- //# sourceMappingURL=zod-router.js.map
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{let n=i(e,t.query);Object.defineProperty(t,`query`,{value:n,writable:!1,enumerable:!0,configurable:!0}),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};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minisylar/express-typed-router",
3
- "version": "1.4.3",
3
+ "version": "1.5.0",
4
4
  "description": "A strongly-typed Express router with Zod validation and automatic type inference for params, body, query, and middleware",
5
5
  "type": "module",
6
6
  "main": "dist/zod-router.cjs",
@@ -54,18 +54,18 @@
54
54
  "packageManager": "pnpm@10.11.0",
55
55
  "peerDependencies": {
56
56
  "express": "^4.18.0 || ^5.0.0",
57
- "zod": "^3.25.0"
57
+ "zod": "^3.25.0 || ^4.0.0"
58
58
  },
59
59
  "devDependencies": {
60
60
  "@semantic-release/changelog": "^6.0.3",
61
61
  "@semantic-release/git": "^10.0.1",
62
62
  "@types/express": "^5.0.3",
63
- "conventional-changelog-conventionalcommits": "^9.0.0",
63
+ "conventional-changelog-conventionalcommits": "^9.1.0",
64
64
  "rimraf": "^6.0.1",
65
- "semantic-release": "^24.2.5",
66
- "tsdown": "^0.12.7",
65
+ "semantic-release": "^24.2.7",
66
+ "tsdown": "^0.14.0",
67
67
  "typescript": "^5.8.3",
68
- "zod": "^3.25.56"
68
+ "zod": "^3.25.76"
69
69
  },
70
70
  "engines": {
71
71
  "node": ">=18.0.0"
@@ -1 +0,0 @@
1
- {"version":3,"file":"zod-router.cjs","names":["schema: AnyZodType","schema: T","data: unknown","error: unknown","z3","middleware: TypedMiddleware<TReq, TLocals>","path: string","optionsOrHandler: any","handler?: any","method: HttpMethod","middlewares: any[]","req: Request","res: Response","next: NextFunction","config?: RouterConfig"],"sources":["../src/zod-router.ts"],"sourcesContent":["/**\n * @packageDocumentation\n * @module @minisylar/express-typed-router\n *\n * @title @minisylar/express-typed-router\n *\n * A strongly-typed Express router with Zod validation and automatic type inference for params, body, query, and middleware.\n *\n * @example\n * // Example 1: Basic usage with router-level middleware\n * const router = createTypedRouter()\n * .useMiddleware(timestampMiddleware)\n * .useMiddleware(requestIdMiddleware)\n *\n * router.get('/posts/:postId', (req, res) => {\n * const { postId } = req.params // Typed as { postId: string }\n * const { timestamp, requestId } = req // Both properties are now typed correctly!\n * res.json({ postId, timestamp, requestId })\n * })\n *\n * @example\n * // Example 2: Per-route middleware with automatic type inference\n * router.post(\n * '/posts',\n * {\n * bodySchema: CreatePostSchema,\n * middleware: [timestampMiddleware, requestIdMiddleware] as const\n * },\n * (req, res) => {\n * const { title, content, tags } = req.body // From schema validation\n * const { timestamp, requestId } = req // From middleware - should be automatically typed!\n * res.json({ title, content, tags, timestamp, requestId })\n * }\n * )\n *\n * @example\n * // Example 3: Mixed middleware\n * const router = createTypedRouter().useMiddleware(requestIdMiddleware)\n * router.get(\n * '/posts/:postId',\n * {\n * middleware: [authMiddleware] as const\n * },\n * (req, res) => {\n * const { postId } = req.params\n * const { requestId } = req // From router-level middleware\n * const { userId, hasPermission } = req // From per-route middleware\n * res.json({ postId, requestId, userId, hasPermission })\n * }\n * )\n *\n * @example\n * // Example 4: Using factory with pre-configured middleware\n * const router = createTypedRouterWithMiddleware(timestampMiddleware, requestIdMiddleware)\n * router.get('/simple/:id', (req, res) => {\n * const { id } = req.params\n * const { timestamp, requestId } = req // Already available from factory setup!\n * res.json({ id, timestamp, requestId })\n * })\n *\n * @example\n * // Example 5: Demonstrating all HTTP methods\n * const router = createTypedRouter().useMiddleware(requestIdMiddleware)\n *\n * // GET with query validation\n * router.get('/posts', { querySchema: QuerySchema }, (req, res) => {\n * const { limit, offset } = req.query // Typed from schema\n * const { requestId } = req // From router middleware\n * res.json({ posts: [], limit, offset, requestId })\n * })\n *\n * // POST with body validation and middleware\n * router.post(\n * '/posts',\n * {\n * bodySchema: CreatePostSchema,\n * middleware: [timestampMiddleware] as const\n * },\n * (req, res) => {\n * const { title, content } = req.body // From body schema\n * const { requestId, timestamp } = req // From middleware\n * res.json({ id: 'new-post', title, content, requestId, timestamp })\n * }\n * )\n *\n * // PUT for full updates\n * router.put(\n * '/posts/:postId',\n * {\n * bodySchema: CreatePostSchema,\n * middleware: [authMiddleware] as const\n * },\n * (req, res) => {\n * const { postId } = req.params\n * const { title, content } = req.body\n * const { requestId, userId, hasPermission } = req\n * res.json({ postId, title, content, requestId, userId, hasPermission })\n * }\n * )\n *\n * // PATCH for partial updates\n * router.patch('/posts/:postId', { bodySchema: UpdatePostSchema }, (req, res) => {\n * const { postId } = req.params\n * const updates = req.body // Partial update object\n * const { requestId } = req\n * res.json({ postId, updates, requestId })\n * })\n *\n * // DELETE\n * router.delete('/posts/:postId', (req, res) => {\n * const { postId } = req.params\n * const { requestId } = req\n * res.json({ deleted: postId, requestId })\n * })\n *\n * // OPTIONS for CORS preflight\n * router.options('/posts/*', (req, res) => {\n * res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,PATCH,DELETE')\n * res.header('Access-Control-Allow-Headers', 'Content-Type')\n * res.status(200).end()\n * })\n *\n * // HEAD for metadata only\n * router.head('/posts/:postId', (req, res) => {\n * const { postId } = req.params\n * res.header('X-Post-ID', postId)\n * res.status(200).end()\n * })\n *\n * // ALL method for catch-all routes\n * router.all('/debug/*', (req, res) => {\n * const { requestId } = req\n * res.json({\n * method: req.method,\n * path: req.path,\n * requestId\n * })\n * })\n */\n/* eslint-disable @typescript-eslint/no-empty-object-type */\nimport express, {\n type Request,\n type Response,\n type NextFunction,\n} from \"express\";\n\n// Import from versioned subpaths as recommended by Zod docs\nimport * as z3 from \"zod/v3\";\nimport * as z4 from \"zod/v4/core\";\n\n// Compatibility layer for Zod v3/v4 following official recommendations\nexport type AnyZodType = z3.ZodTypeAny | z4.$ZodType;\n\n// Type guard to detect Zod 4 schemas at runtime\nexport function isZod4Schema(schema: AnyZodType): schema is z4.$ZodType {\n return \"_zod\" in schema;\n}\n\n// Helper type to extract output type from Zod v3, v4, drizzle-zod, or fallback to unknown\nexport type InferOutput<T> = T extends z4.$ZodType\n ? z4.output<T>\n : T extends z3.ZodTypeAny\n ? z3.output<T>\n : T extends { _output: infer O }\n ? O\n : T extends { _type: infer O }\n ? O\n : unknown;\n\n// Helper type to extract input type from Zod v3, v4, drizzle-zod, or fallback to unknown\nexport type InferInput<T> = T extends z4.$ZodType\n ? z4.input<T>\n : T extends z3.ZodTypeAny\n ? z3.input<T>\n : T extends { _input: infer I }\n ? I\n : T extends { _type: infer I }\n ? I\n : unknown;\n\n// Unified parsing functions that work with both Zod 3 and 4\nexport function parseSchema<T extends AnyZodType>(\n schema: T,\n data: unknown\n): InferOutput<T> {\n if (isZod4Schema(schema)) {\n return z4.parse(schema, data) as InferOutput<T>;\n } else {\n return (schema as z3.ZodTypeAny).parse(data) as InferOutput<T>;\n }\n}\n\n// Safe parse result type that works with both versions\nexport type SafeParseResult<T extends AnyZodType> = T extends z4.$ZodType\n ? ReturnType<typeof z4.safeParse<T>>\n : T extends z3.ZodTypeAny\n ? z3.SafeParseReturnType<z3.input<T>, z3.output<T>>\n : never;\n\nexport function safeParseSchema<T extends AnyZodType>(\n schema: T,\n data: unknown\n): SafeParseResult<T> {\n if (isZod4Schema(schema)) {\n return z4.safeParse(schema, data) as SafeParseResult<T>;\n } else {\n return (schema as z3.ZodTypeAny).safeParse(data) as SafeParseResult<T>;\n }\n}\n\n// Error detection helper\nexport function isZodError(error: unknown): error is z3.ZodError {\n return (\n error instanceof z3.ZodError ||\n (error !== null &&\n typeof error === \"object\" &&\n \"issues\" in error &&\n Array.isArray((error as any).issues))\n );\n}\n\n/**\n * Extract route parameters from Express.js route patterns.\n *\n * Supports all Express.js routing patterns:\n * - Named parameters: /users/:userId → { userId: string }\n * - Multiple parameters: /users/:userId/books/:bookId → { userId: string; bookId: string }\n * - Parameters with separators: /flights/:from-:to → { from: string; to: string }\n * - Dot notation: /plantae/:genus.:species → { genus: string; species: string }\n * - Regex constraints: /user/:id(\\\\d+) → { id: string }\n * - Optional parameters: /posts/:year/:month? → { year: string; month?: string }\n * - Wildcard parameters: /files/* → { \"0\": string }\n * - Multiple wildcards: /a/star/b/star → { \"0\": string; \"1\": string }\n */\nexport type ExtractRouteParams<Path extends string> = string extends Path\n ? Record<string, string>\n : ExtractParams<Path>;\n\n/**\n * Main parameter extraction logic - enhanced for Express 5 support with recursion depth limit\n */\ntype ExtractParams<Path extends string> =\n // Handle Express 5 braces for optional segments: {/:param} or {/path/:param}\n Path extends `${infer Before}{${infer OptionalContent}}${infer After}`\n ? ExtractOptionalSegment<OptionalContent> &\n ExtractParams<`${Before}${After}`>\n : // Handle named parameters :paramName\n Path extends `${infer _Before}:${infer Rest}`\n ? ExtractSingleParam<Rest> & ExtractParams<RemoveFirstParam<Path>>\n : // Handle wildcards *\n Path extends `${infer _Before}*${infer After}`\n ? {\n [K in CountWildcards<_Before, \"0\">]: string;\n } & ExtractParams<After>\n : // No more parameters\n {};\n\n/**\n * Extract parameters from Express 5 optional segments in braces\n * Handles patterns like {/:param}, {.:ext}, {/optional/:param}\n */\ntype ExtractOptionalSegment<Content extends string> =\n // Handle optional parameter patterns like /:param\n Content extends `/:${infer Rest}`\n ? ExtractOptionalParam<Rest>\n : Content extends `.:${infer Rest}`\n ? ExtractOptionalParam<Rest>\n : Content extends `${infer _Path}:${infer Rest}`\n ? ExtractOptionalParam<Rest>\n : {};\n\n/**\n * Extract a single optional parameter from brace content\n */\ntype ExtractOptionalParam<Rest extends string> =\n Rest extends `${infer ParamName}/${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}-${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}.${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}`\n ? { [K in ParamName]?: string }\n : {};\n\n/**\n * Extract a single parameter name from the rest of the path\n * Enhanced to handle Express 5 patterns and optional parameters correctly\n * Special handling for consecutive parameters like :from-:to\n * Order matters: regex constraints must be handled before repeating parameters\n */\ntype ExtractSingleParam<Rest extends string> =\n // Handle regex constraints FIRST (before +, *, ?) to avoid conflicts\n Rest extends `${infer ParamName}(${infer _Constraint})${infer _After}`\n ? { [K in ParamName]: string } // Handle consecutive parameters with separators first: param-:nextParam\n : Rest extends `${infer ParamName}-:${infer _NextParam}`\n ? { [K in ParamName]: string }\n : Rest extends `${infer ParamName}.:${infer _NextParam}`\n ? { [K in ParamName]: string }\n : // Handle optional parameters followed by delimiters (before regular delimiters)\n Rest extends `${infer ParamName}?/${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}?-${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}?.${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}?#${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}?:${infer _After}`\n ? { [K in ParamName]?: string }\n : // Then handle regular delimiters (after optional parameter patterns)\n Rest extends `${infer ParamName}/${infer _After}`\n ? { [K in ParamName]: string }\n : Rest extends `${infer ParamName}-${infer _After}`\n ? { [K in ParamName]: string }\n : Rest extends `${infer ParamName}.${infer _After}`\n ? { [K in ParamName]: string }\n : Rest extends `${infer ParamName}#${infer _After}`\n ? { [K in ParamName]: string }\n : Rest extends `${infer ParamName}:${infer _After}`\n ? { [K in ParamName]: string }\n : // Handle Express 5 repeating parameters (after regular delimiters)\n Rest extends `${infer ParamName}+${infer _After}`\n ? { [K in ParamName]: string[] }\n : Rest extends `${infer ParamName}*${infer _After}`\n ? { [K in ParamName]?: string[] }\n : // Handle optional parameters with ? (Express 4) - only at the end of a segment\n Rest extends `${infer ParamName}?${infer _After}`\n ? { [K in ParamName]?: string } // Parameter at absolute end of string\n : Rest extends string\n ? Rest extends \"\"\n ? {}\n : Rest extends `${infer ParamName}?`\n ? { [K in ParamName]?: string } // ParamName here doesn't include the ?\n : Rest extends `${infer ParamName}+`\n ? { [K in ParamName]: string[] }\n : Rest extends `${infer ParamName}*`\n ? { [K in ParamName]?: string[] }\n : { [K in Rest]: string }\n : {};\n\n/**\n * Remove the first parameter from path to continue parsing\n * Enhanced to handle Express 5 patterns and optional parameters\n * Handles patterns like :from-:to by removing just :from and keeping -:to\n * Order matters: regex constraints must be handled before repeating parameters\n */\ntype RemoveFirstParam<Path extends string> =\n Path extends `${infer Before}:${infer Rest}`\n ? // Handle regex constraints FIRST (before +, *, ?) to avoid conflicts\n Rest extends `${infer _ParamName}(${infer _Constraint})${infer After}`\n ? `${Before}${After}` // Handle consecutive parameters: :param-:nextParam -> -:nextParam\n : Rest extends `${infer _ParamName}-:${infer After}`\n ? `${Before}-:${After}`\n : Rest extends `${infer _ParamName}.:${infer After}`\n ? `${Before}.:${After}`\n : // Handle optional parameters followed by delimiters (before regular delimiters)\n Rest extends `${infer _ParamName}?/${infer After}`\n ? `${Before}/${After}`\n : Rest extends `${infer _ParamName}?-${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}?.${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}?#${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}?:${infer After}`\n ? `${Before}:${After}`\n : // Handle regular separators (after optional parameter patterns)\n Rest extends `${infer _ParamName}/${infer After}`\n ? `${Before}/${After}`\n : Rest extends `${infer _ParamName}-${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}.${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}#${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}:${infer After}`\n ? `${Before}:${After}`\n : // Handle Express 5 repeating parameters (after regular separators)\n Rest extends `${infer _ParamName}+${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}*${infer After}`\n ? `${Before}${After}`\n : // Handle optional parameters with ?\n Rest extends `${infer _ParamName}?${infer After}`\n ? `${Before}${After}`\n : Before\n : Path;\n\n/**\n * Count wildcards to assign proper numeric indices with recursion depth limit\n */\ntype CountWildcards<\n Path extends string,\n Count extends string = \"0\"\n> = Path extends `${infer _Before}*${infer Rest}`\n ? CountWildcards<Rest, IncrementWildcard<Count>>\n : Count;\n\n/**\n * Helper type to increment wildcard count as string\n */\ntype IncrementWildcard<T extends string> = T extends \"0\"\n ? \"1\"\n : T extends \"1\"\n ? \"2\"\n : T extends \"2\"\n ? \"3\"\n : T extends \"3\"\n ? \"4\"\n : T extends \"4\"\n ? \"5\"\n : T extends \"5\"\n ? \"6\"\n : T extends \"6\"\n ? \"7\"\n : T extends \"7\"\n ? \"8\"\n : T extends \"8\"\n ? \"9\"\n : \"10\"; // Reasonable limit for wildcards\n\n/**\n * Express middleware that adds custom properties to the request object and/or response locals.\n *\n * @template TReq - The shape of the properties added to the request object.\n * @template TLocals - The shape of the properties added to response.locals.\n * @param req - The Express request object, extended with TReq.\n * @param res - The Express response object with typed locals.\n * @param next - The next middleware function.\n */\nexport type TypedMiddleware<\n TReq extends Record<string, any> = {},\n TLocals extends Record<string, any> = {}\n> = (\n req: Request & TReq,\n res: Response<any, TLocals>,\n next: NextFunction\n) => void | Promise<void>;\n\n/**\n * Simplified TypedMiddleware for request-only extensions (backward compatibility)\n */\nexport type RequestOnlyMiddleware<TReq extends Record<string, any>> =\n TypedMiddleware<TReq, {}>;\n\n/**\n * Simplified TypedMiddleware for response locals-only extensions\n */\nexport type LocalsOnlyMiddleware<TLocals extends Record<string, any>> =\n TypedMiddleware<{}, TLocals>;\n\n// Utility type to infer props from middleware array (no recursion depth limit)\ntype InferMiddlewareProps<T extends readonly TypedMiddleware<any, any>[]> =\n T extends readonly [infer First, ...infer Rest]\n ? First extends TypedMiddleware<infer FirstReq, any>\n ? Rest extends readonly TypedMiddleware<any, any>[]\n ? FirstReq & InferMiddlewareProps<Rest>\n : FirstReq\n : {}\n : {};\n\n// Utility type to infer locals from middleware array (no recursion depth limit)\ntype InferMiddlewareLocals<T extends readonly TypedMiddleware<any, any>[]> =\n T extends readonly [infer First, ...infer Rest]\n ? First extends TypedMiddleware<any, infer FirstLocals>\n ? Rest extends readonly TypedMiddleware<any, any>[]\n ? FirstLocals & InferMiddlewareLocals<Rest>\n : FirstLocals\n : {}\n : {};\n\n// Enhanced Request type with proper inference\nexport type ZodRequest<\n Path extends string = string,\n BodySchema extends AnyZodType | unknown = unknown,\n QuerySchema extends AnyZodType | unknown = unknown,\n MiddlewareProps extends Record<string, any> = {}\n> = Omit<Request, \"params\" | \"query\" | \"body\"> & {\n params: ExtractRouteParams<Path>;\n body: BodySchema extends AnyZodType ? InferOutput<BodySchema> : unknown;\n query: QuerySchema extends AnyZodType ? InferOutput<QuerySchema> : unknown;\n} & MiddlewareProps;\n\n// Route handler type\nexport type ZodRouteHandler<\n Path extends string = string,\n BodySchema extends AnyZodType | unknown = unknown,\n QuerySchema extends AnyZodType | unknown = unknown,\n MiddlewareProps extends Record<string, any> = {},\n ResponseLocals extends Record<string, any> = {}\n> = (\n req: ZodRequest<Path, BodySchema, QuerySchema, MiddlewareProps>,\n res: Response<any, ResponseLocals>,\n next?: NextFunction\n) => void | Promise<void> | Response | Promise<Response>;\n\n/**\n * Options for defining a typed route, including schemas and middleware.\n *\n * @template BodySchema - Zod schema for request body validation.\n * @template QuerySchema - Zod schema for query parameter validation.\n * @property bodySchema - Optional Zod schema for validating the request body.\n * @property querySchema - Optional Zod schema for validating the query string.\n * @property middleware - Optional array of TypedMiddleware for this route.\n */\nexport interface RouteOptions<\n BodySchema extends AnyZodType | unknown = unknown,\n QuerySchema extends AnyZodType | unknown = unknown\n> {\n bodySchema?: BodySchema;\n querySchema?: QuerySchema;\n middleware?: TypedMiddleware<any, any>[];\n}\n\n// HTTP methods\nexport type HttpMethod =\n | \"get\"\n | \"post\"\n | \"put\"\n | \"delete\"\n | \"patch\"\n | \"options\"\n | \"head\"\n | \"all\";\n\n// Main typed router class\nclass TypedRouter<\n RouterMiddlewareProps extends Record<string, any> = {},\n RouterLocals extends Record<string, any> = {}\n> {\n private router: express.Router;\n\n constructor() {\n this.router = express.Router();\n }\n /**\n * Add typed middleware that extends the request with additional properties\n * and/or adds properties to response.locals\n */ /**\n * Add typed middleware to the router.\n * This middleware will apply to all routes defined after this call.\n *\n * @template TReq - Type extensions for the request object\n * @template TLocals - Type extensions for response.locals\n * @param middleware - The typed middleware function\n * @returns A new router instance with updated types\n */\n useMiddleware<\n TReq extends Record<string, any> = {},\n TLocals extends Record<string, any> = {}\n >(\n middleware: TypedMiddleware<TReq, TLocals>\n ): TypedRouter<RouterMiddlewareProps & TReq, RouterLocals & TLocals> {\n this.router.use(middleware as any);\n return this as any;\n }\n /**\n * Get the underlying Express router\n */\n getRouter(): express.Router {\n return this.router;\n }\n // Method overloads for GET requests with automatic middleware type inference\n get<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n get<\n Path extends string,\n BodySchema extends AnyZodType | unknown,\n QuerySchema extends AnyZodType | unknown\n >(\n path: Path,\n options: RouteOptions<BodySchema, QuerySchema>,\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Special overload for middleware type inference\n get<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n // Combined overload for body/query schema + middleware\n get<\n Path extends string,\n BodySchema extends AnyZodType | unknown,\n QuerySchema extends AnyZodType | unknown,\n M extends TypedMiddleware<any, any>[] // Using array type for JS compatibility\n >(\n path: Path,\n options: RouteOptions<BodySchema, QuerySchema> & { middleware: [...M] }, // Using tuple spread pattern\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>, // Make it readonly for type inference\n RouterLocals & InferMiddlewareLocals<readonly [...M]>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n // Implementation\n get(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"get\", path, optionsOrHandler, handler);\n } // Combined overload for body/query schema + middleware (most specific first)\n post<\n Path extends string,\n BodySchema extends AnyZodType,\n QuerySchema extends AnyZodType | unknown,\n M extends TypedMiddleware<any, any>[] // Using array type for JS compatibility\n >(\n path: Path,\n options: {\n bodySchema: BodySchema;\n querySchema?: QuerySchema;\n middleware: [...M]; // Using tuple spread pattern\n },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>, // Make it readonly for type inference\n RouterLocals & InferMiddlewareLocals<readonly [...M]>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Body schema only + middleware\n post<\n Path extends string,\n BodySchema extends AnyZodType,\n M extends TypedMiddleware<any, any>[] // Using array type for JS compatibility\n >(\n path: Path,\n options: { bodySchema: BodySchema; middleware: [...M] }, // Using tuple spread pattern\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>, // Make it readonly for type inference\n RouterLocals & InferMiddlewareLocals<readonly [...M]>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Middleware only\n post<\n Path extends string,\n M extends TypedMiddleware<any, any>[] // Using array type for JS compatibility\n >(\n path: Path,\n options: { middleware: [...M] }, // Using tuple spread pattern\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>, // Make it readonly for type inference\n RouterLocals & InferMiddlewareLocals<readonly [...M]>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Body + Query schema without middleware\n post<\n Path extends string,\n BodySchema extends AnyZodType | unknown,\n QuerySchema extends AnyZodType | unknown\n >(\n path: Path,\n options: RouteOptions<BodySchema, QuerySchema>,\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Just handler, no options\n post<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n post(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"post\", path, optionsOrHandler, handler);\n }\n // PUT method with all the same overloads as POST\n put<\n Path extends string,\n BodySchema extends AnyZodType,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: {\n bodySchema: BodySchema;\n querySchema?: QuerySchema;\n middleware: Middleware;\n },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n put<\n Path extends string,\n BodySchema extends AnyZodType,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { bodySchema: BodySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n put<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n put<\n Path extends string,\n BodySchema extends AnyZodType | unknown,\n QuerySchema extends AnyZodType | unknown\n >(\n path: Path,\n options: RouteOptions<BodySchema, QuerySchema>,\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n put<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n put(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"put\", path, optionsOrHandler, handler);\n }\n // PATCH method with all the same overloads as POST\n patch<\n Path extends string,\n BodySchema extends AnyZodType,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: {\n bodySchema: BodySchema;\n querySchema?: QuerySchema;\n middleware: Middleware;\n },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n patch<\n Path extends string,\n BodySchema extends AnyZodType,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { bodySchema: BodySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n patch<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n patch<\n Path extends string,\n BodySchema extends AnyZodType | unknown,\n QuerySchema extends AnyZodType | unknown\n >(\n path: Path,\n options: RouteOptions<BodySchema, QuerySchema>,\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n patch<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n patch(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"patch\", path, optionsOrHandler, handler);\n } // DELETE method (typically no body, but can have query params and middleware)\n // Most specific first: query schema + middleware\n delete<\n Path extends string,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { querySchema: QuerySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Query schema only\n delete<Path extends string, QuerySchema extends AnyZodType | unknown>(\n path: Path,\n options: { querySchema: QuerySchema },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Middleware only\n delete<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Basic overload with no options\n delete<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n delete(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"delete\", path, optionsOrHandler, handler);\n } // OPTIONS method (typically no body, used for CORS preflight)\n // Most specific first: query schema + middleware\n options<\n Path extends string,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { querySchema: QuerySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Query schema only\n options<Path extends string, QuerySchema extends AnyZodType | unknown>(\n path: Path,\n options: { querySchema: QuerySchema },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Middleware only\n options<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Basic overload with no options\n options<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n options(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"options\", path, optionsOrHandler, handler);\n } // HEAD method (like GET but only returns headers)\n // Most specific first: query schema + middleware\n head<\n Path extends string,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { querySchema: QuerySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Query schema only\n head<Path extends string, QuerySchema extends AnyZodType | unknown>(\n path: Path,\n options: { querySchema: QuerySchema },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Middleware only\n head<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Basic overload with no options\n head<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n head(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"head\", path, optionsOrHandler, handler);\n } // ALL method (matches all HTTP methods)\n // Most specific first: body + query + middleware\n all<\n Path extends string,\n BodySchema extends AnyZodType,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: {\n bodySchema: BodySchema;\n querySchema?: QuerySchema;\n middleware: Middleware;\n },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Body schema + middleware (no query)\n all<\n Path extends string,\n BodySchema extends AnyZodType,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { bodySchema: BodySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Query schema + middleware (no body)\n all<\n Path extends string,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { querySchema: QuerySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Body + query schemas (no middleware)\n all<\n Path extends string,\n BodySchema extends AnyZodType | unknown,\n QuerySchema extends AnyZodType | unknown\n >(\n path: Path,\n options: RouteOptions<BodySchema, QuerySchema>,\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Middleware only (no schemas)\n all<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Basic overload with no options\n all<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n all(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"all\", path, optionsOrHandler, handler);\n }\n // Helper method to register routes\n private registerRoute(\n method: HttpMethod,\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n const middlewares: any[] = [];\n\n if (typeof optionsOrHandler === \"object\") {\n const options = optionsOrHandler as RouteOptions<any, any>;\n\n // Add per-route middleware first\n if (options.middleware) {\n middlewares.push(...options.middleware);\n }\n\n // Add schema validation middleware\n if (options.bodySchema) {\n middlewares.push(\n this.createBodyValidationMiddleware(options.bodySchema)\n );\n }\n if (options.querySchema) {\n middlewares.push(\n this.createQueryValidationMiddleware(options.querySchema)\n );\n }\n\n // Add the main handler\n middlewares.push(handler);\n } else {\n // Direct handler without options\n middlewares.push(optionsOrHandler);\n }\n\n // Register with Express router\n (this.router as any)[method](path, ...middlewares);\n\n return this;\n }\n private createBodyValidationMiddleware(schema: AnyZodType) {\n return (req: Request, res: Response, next: NextFunction) => {\n try {\n req.body = parseSchema(schema, req.body);\n next();\n } catch (error) {\n // Check for ZodError using our unified helper\n if (isZodError(error)) {\n res.status(400).json({\n error: \"Validation failed\",\n details: (error as any).errors || (error as any).issues,\n });\n } else {\n next(error);\n }\n }\n };\n }\n private createQueryValidationMiddleware(schema: AnyZodType) {\n return (req: Request, res: Response, next: NextFunction) => {\n try {\n const validatedQuery = parseSchema(schema, req.query);\n // Use Object.defineProperty to properly set the read-only query property\n Object.defineProperty(req, \"query\", {\n value: validatedQuery,\n writable: false,\n enumerable: true,\n configurable: true,\n });\n next();\n } catch (error) {\n // Check for ZodError using our unified helper\n if (isZodError(error)) {\n res.status(400).json({\n error: \"Validation failed\",\n details: (error as any).errors || (error as any).issues,\n });\n } else {\n next(error);\n }\n }\n };\n }\n}\n\n/**\n * Create a new strongly-typed Express router instance.\n *\n * This is the simplest way to get started with @minisylar/express-typed-router.\n *\n * @example\n * import { createTypedRouter } from '@minisylar/express-typed-router';\n *\n * // Create a router and add a typed GET route\n * const router = createTypedRouter();\n * router.get('/hello/:name', (req, res) => {\n * // req.params.name is typed as string\n * res.json({ message: `Hello, ${req.params.name}!` });\n * });\n *\n * // Use with Express\n * import express from 'express';\n * const app = express();\n * app.use('/api', router.getRouter());\n */\nexport function createTypedRouter<\n RouterMiddlewareProps extends Record<string, any> = {},\n RouterLocals extends Record<string, any> = {}\n>(): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return new TypedRouter<RouterMiddlewareProps, RouterLocals>();\n}\n\n// Option 2: Factory with optional configuration\n\n/**\n * Configuration options for createTypedRouterWithConfig.\n *\n * @property validateInput - (Future) Whether to enable global input validation.\n * @property errorHandler - Optional global error handler middleware for the router.\n */\nexport interface RouterConfig {\n validateInput?: boolean;\n errorHandler?: (\n error: any,\n req: Request,\n res: Response,\n next: NextFunction\n ) => void;\n}\n\n/**\n * Create a new typed router with optional configuration.\n *\n * Use this if you want to add a global error handler or future global options.\n *\n * @param config - Optional configuration for the router (e.g. error handler).\n * @returns A new TypedRouter instance.\n *\n * @example\n * import { createTypedRouterWithConfig } from '@minisylar/express-typed-router';\n *\n * const router = createTypedRouterWithConfig({\n * errorHandler: (err, req, res, next) => {\n * res.status(500).json({ error: 'Something went wrong', details: err });\n * }\n * });\n */\nexport function createTypedRouterWithConfig<\n RouterMiddlewareProps extends Record<string, any> = {},\n RouterLocals extends Record<string, any> = {}\n>(config?: RouterConfig): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n const router = new TypedRouter<RouterMiddlewareProps, RouterLocals>();\n if (config?.errorHandler) {\n router.getRouter().use(config.errorHandler);\n }\n return router;\n}\n\n// Option 3: Factory with pre-configured middleware\n\n/**\n * Create a new typed router with pre-configured middleware.\n *\n * This is useful for setting up router-level middleware in a single call.\n *\n * @param middleware - One or more TypedMiddleware functions to apply to all routes.\n * @returns A new TypedRouter instance with the middleware applied.\n *\n * @example\n * import { createTypedRouterWithMiddleware } from '@minisylar/express-typed-router';\n *\n * const router = createTypedRouterWithMiddleware(authMiddleware, loggingMiddleware);\n */\nexport function createTypedRouterWithMiddleware<T extends Record<string, any>>(\n ...middleware: TypedMiddleware<any, any>[]\n): TypedRouter<T> {\n let router = new TypedRouter() as any;\n for (const mw of middleware) {\n router = router.useMiddleware(mw);\n }\n return router;\n}\n"],"mappings":"+iBA0JA,SAAgB,EAAaA,EAA2C,CACtE,MAAO,SAAU,CAClB,CAyBD,SAAgB,EACdC,EACAC,EACgB,CAId,MAHE,GAAa,EAAO,CACf,EAAG,MAAM,EAAQ,EAAK,CAEtB,EAA0B,MAAM,EAAK,AAE/C,CASD,SAAgB,EACdD,EACAC,EACoB,CAIlB,MAHE,GAAa,EAAO,CACf,EAAG,UAAU,EAAQ,EAAK,CAE1B,EAA0B,UAAU,EAAK,AAEnD,CAGD,SAAgB,EAAWC,EAAsC,CAC/D,OACE,aAAiBC,EAAG,iBAEX,GAAU,YADlB,GAEC,WAAY,GACZ,MAAM,QAAS,EAAc,OAAO,AAEzC,CAoTD,IAAM,EAAN,KAGE,CACA,OAEA,aAAc,CACZ,KAAK,OAAS,EAAA,QAAQ,QAAQ,AAC/B,CAaD,cAIEC,EACmE,CAEnE,MADA,MAAK,OAAO,IAAI,EAAkB,CAC3B,IACR,CAID,WAA4B,CAC1B,OAAO,KAAK,MACb,CA8DD,IACEC,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,MAAO,EAAM,EAAkB,EAAQ,AAClE,CAoFD,KACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,OAAQ,EAAM,EAAkB,EAAQ,AACnE,CAgFD,IACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,MAAO,EAAM,EAAkB,EAAQ,AAClE,CAgFD,MACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,QAAS,EAAM,EAAkB,EAAQ,AACpE,CA0DD,OACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,SAAU,EAAM,EAAkB,EAAQ,AACrE,CA0DD,QACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,UAAW,EAAM,EAAkB,EAAQ,AACtE,CA0DD,KACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,OAAQ,EAAM,EAAkB,EAAQ,AACnE,CAqGD,IACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,MAAO,EAAM,EAAkB,EAAQ,AAClE,CAED,cACEC,EACAH,EACAC,EACAC,EACkD,CAClD,IAAME,EAAqB,CAAE,EAE7B,UAAW,GAAqB,SAAU,CACxC,IAAM,EAAU,EAoBhB,AAjBI,EAAQ,YACV,EAAY,KAAK,GAAG,EAAQ,WAAW,CAIrC,EAAQ,YACV,EAAY,KACV,KAAK,+BAA+B,EAAQ,WAAW,CACxD,CAEC,EAAQ,aACV,EAAY,KACV,KAAK,gCAAgC,EAAQ,YAAY,CAC1D,CAIH,EAAY,KAAK,EAAQ,AAC1B,MAEC,EAAY,KAAK,EAAiB,CAMpC,MAFC,MAAK,OAAe,GAAQ,EAAM,GAAG,EAAY,CAE3C,IACR,CACD,+BAAuCV,EAAoB,CACzD,MAAO,CAACW,EAAcC,EAAeC,IAAuB,CAC1D,GAAI,CAEF,AADA,EAAI,KAAO,EAAY,EAAQ,EAAI,KAAK,CACxC,GAAM,AACP,OAAQ,EAAO,CAEd,AAAI,EAAW,EAAM,CACnB,EAAI,OAAO,IAAI,CAAC,KAAK,CACnB,MAAO,oBACP,QAAU,EAAc,QAAW,EAAc,MAClD,EAAC,CAEF,EAAK,EAAM,AAEd,CACF,CACF,CACD,gCAAwCb,EAAoB,CAC1D,MAAO,CAACW,EAAcC,EAAeC,IAAuB,CAC1D,GAAI,CACF,IAAM,EAAiB,EAAY,EAAQ,EAAI,MAAM,CAQrD,AANA,OAAO,eAAe,EAAK,QAAS,CAClC,MAAO,EACP,UAAU,EACV,YAAY,EACZ,cAAc,CACf,EAAC,CACF,GAAM,AACP,OAAQ,EAAO,CAEd,AAAI,EAAW,EAAM,CACnB,EAAI,OAAO,IAAI,CAAC,KAAK,CACnB,MAAO,oBACP,QAAU,EAAc,QAAW,EAAc,MAClD,EAAC,CAEF,EAAK,EAAM,AAEd,CACF,CACF,CACF,EAsBD,SAAgB,GAGsC,CACpD,OAAO,IAAI,CACZ,CAqCD,SAAgB,EAGdC,EAAyE,CACzE,IAAM,EAAS,IAAI,EAInB,OAHI,GAAQ,cACV,EAAO,WAAW,CAAC,IAAI,EAAO,aAAa,CAEtC,CACR,CAiBD,SAAgB,EACd,GAAG,EACa,CAChB,IAAI,EAAS,IAAI,EACjB,IAAK,IAAM,KAAM,EACf,EAAS,EAAO,cAAc,EAAG,CAEnC,OAAO,CACR"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"zod-router.js","names":["schema: AnyZodType","schema: T","data: unknown","error: unknown","middleware: TypedMiddleware<TReq, TLocals>","path: string","optionsOrHandler: any","handler?: any","method: HttpMethod","middlewares: any[]","req: Request","res: Response","next: NextFunction","config?: RouterConfig"],"sources":["../src/zod-router.ts"],"sourcesContent":["/**\n * @packageDocumentation\n * @module @minisylar/express-typed-router\n *\n * @title @minisylar/express-typed-router\n *\n * A strongly-typed Express router with Zod validation and automatic type inference for params, body, query, and middleware.\n *\n * @example\n * // Example 1: Basic usage with router-level middleware\n * const router = createTypedRouter()\n * .useMiddleware(timestampMiddleware)\n * .useMiddleware(requestIdMiddleware)\n *\n * router.get('/posts/:postId', (req, res) => {\n * const { postId } = req.params // Typed as { postId: string }\n * const { timestamp, requestId } = req // Both properties are now typed correctly!\n * res.json({ postId, timestamp, requestId })\n * })\n *\n * @example\n * // Example 2: Per-route middleware with automatic type inference\n * router.post(\n * '/posts',\n * {\n * bodySchema: CreatePostSchema,\n * middleware: [timestampMiddleware, requestIdMiddleware] as const\n * },\n * (req, res) => {\n * const { title, content, tags } = req.body // From schema validation\n * const { timestamp, requestId } = req // From middleware - should be automatically typed!\n * res.json({ title, content, tags, timestamp, requestId })\n * }\n * )\n *\n * @example\n * // Example 3: Mixed middleware\n * const router = createTypedRouter().useMiddleware(requestIdMiddleware)\n * router.get(\n * '/posts/:postId',\n * {\n * middleware: [authMiddleware] as const\n * },\n * (req, res) => {\n * const { postId } = req.params\n * const { requestId } = req // From router-level middleware\n * const { userId, hasPermission } = req // From per-route middleware\n * res.json({ postId, requestId, userId, hasPermission })\n * }\n * )\n *\n * @example\n * // Example 4: Using factory with pre-configured middleware\n * const router = createTypedRouterWithMiddleware(timestampMiddleware, requestIdMiddleware)\n * router.get('/simple/:id', (req, res) => {\n * const { id } = req.params\n * const { timestamp, requestId } = req // Already available from factory setup!\n * res.json({ id, timestamp, requestId })\n * })\n *\n * @example\n * // Example 5: Demonstrating all HTTP methods\n * const router = createTypedRouter().useMiddleware(requestIdMiddleware)\n *\n * // GET with query validation\n * router.get('/posts', { querySchema: QuerySchema }, (req, res) => {\n * const { limit, offset } = req.query // Typed from schema\n * const { requestId } = req // From router middleware\n * res.json({ posts: [], limit, offset, requestId })\n * })\n *\n * // POST with body validation and middleware\n * router.post(\n * '/posts',\n * {\n * bodySchema: CreatePostSchema,\n * middleware: [timestampMiddleware] as const\n * },\n * (req, res) => {\n * const { title, content } = req.body // From body schema\n * const { requestId, timestamp } = req // From middleware\n * res.json({ id: 'new-post', title, content, requestId, timestamp })\n * }\n * )\n *\n * // PUT for full updates\n * router.put(\n * '/posts/:postId',\n * {\n * bodySchema: CreatePostSchema,\n * middleware: [authMiddleware] as const\n * },\n * (req, res) => {\n * const { postId } = req.params\n * const { title, content } = req.body\n * const { requestId, userId, hasPermission } = req\n * res.json({ postId, title, content, requestId, userId, hasPermission })\n * }\n * )\n *\n * // PATCH for partial updates\n * router.patch('/posts/:postId', { bodySchema: UpdatePostSchema }, (req, res) => {\n * const { postId } = req.params\n * const updates = req.body // Partial update object\n * const { requestId } = req\n * res.json({ postId, updates, requestId })\n * })\n *\n * // DELETE\n * router.delete('/posts/:postId', (req, res) => {\n * const { postId } = req.params\n * const { requestId } = req\n * res.json({ deleted: postId, requestId })\n * })\n *\n * // OPTIONS for CORS preflight\n * router.options('/posts/*', (req, res) => {\n * res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,PATCH,DELETE')\n * res.header('Access-Control-Allow-Headers', 'Content-Type')\n * res.status(200).end()\n * })\n *\n * // HEAD for metadata only\n * router.head('/posts/:postId', (req, res) => {\n * const { postId } = req.params\n * res.header('X-Post-ID', postId)\n * res.status(200).end()\n * })\n *\n * // ALL method for catch-all routes\n * router.all('/debug/*', (req, res) => {\n * const { requestId } = req\n * res.json({\n * method: req.method,\n * path: req.path,\n * requestId\n * })\n * })\n */\n/* eslint-disable @typescript-eslint/no-empty-object-type */\nimport express, {\n type Request,\n type Response,\n type NextFunction,\n} from \"express\";\n\n// Import from versioned subpaths as recommended by Zod docs\nimport * as z3 from \"zod/v3\";\nimport * as z4 from \"zod/v4/core\";\n\n// Compatibility layer for Zod v3/v4 following official recommendations\nexport type AnyZodType = z3.ZodTypeAny | z4.$ZodType;\n\n// Type guard to detect Zod 4 schemas at runtime\nexport function isZod4Schema(schema: AnyZodType): schema is z4.$ZodType {\n return \"_zod\" in schema;\n}\n\n// Helper type to extract output type from Zod v3, v4, drizzle-zod, or fallback to unknown\nexport type InferOutput<T> = T extends z4.$ZodType\n ? z4.output<T>\n : T extends z3.ZodTypeAny\n ? z3.output<T>\n : T extends { _output: infer O }\n ? O\n : T extends { _type: infer O }\n ? O\n : unknown;\n\n// Helper type to extract input type from Zod v3, v4, drizzle-zod, or fallback to unknown\nexport type InferInput<T> = T extends z4.$ZodType\n ? z4.input<T>\n : T extends z3.ZodTypeAny\n ? z3.input<T>\n : T extends { _input: infer I }\n ? I\n : T extends { _type: infer I }\n ? I\n : unknown;\n\n// Unified parsing functions that work with both Zod 3 and 4\nexport function parseSchema<T extends AnyZodType>(\n schema: T,\n data: unknown\n): InferOutput<T> {\n if (isZod4Schema(schema)) {\n return z4.parse(schema, data) as InferOutput<T>;\n } else {\n return (schema as z3.ZodTypeAny).parse(data) as InferOutput<T>;\n }\n}\n\n// Safe parse result type that works with both versions\nexport type SafeParseResult<T extends AnyZodType> = T extends z4.$ZodType\n ? ReturnType<typeof z4.safeParse<T>>\n : T extends z3.ZodTypeAny\n ? z3.SafeParseReturnType<z3.input<T>, z3.output<T>>\n : never;\n\nexport function safeParseSchema<T extends AnyZodType>(\n schema: T,\n data: unknown\n): SafeParseResult<T> {\n if (isZod4Schema(schema)) {\n return z4.safeParse(schema, data) as SafeParseResult<T>;\n } else {\n return (schema as z3.ZodTypeAny).safeParse(data) as SafeParseResult<T>;\n }\n}\n\n// Error detection helper\nexport function isZodError(error: unknown): error is z3.ZodError {\n return (\n error instanceof z3.ZodError ||\n (error !== null &&\n typeof error === \"object\" &&\n \"issues\" in error &&\n Array.isArray((error as any).issues))\n );\n}\n\n/**\n * Extract route parameters from Express.js route patterns.\n *\n * Supports all Express.js routing patterns:\n * - Named parameters: /users/:userId → { userId: string }\n * - Multiple parameters: /users/:userId/books/:bookId → { userId: string; bookId: string }\n * - Parameters with separators: /flights/:from-:to → { from: string; to: string }\n * - Dot notation: /plantae/:genus.:species → { genus: string; species: string }\n * - Regex constraints: /user/:id(\\\\d+) → { id: string }\n * - Optional parameters: /posts/:year/:month? → { year: string; month?: string }\n * - Wildcard parameters: /files/* → { \"0\": string }\n * - Multiple wildcards: /a/star/b/star → { \"0\": string; \"1\": string }\n */\nexport type ExtractRouteParams<Path extends string> = string extends Path\n ? Record<string, string>\n : ExtractParams<Path>;\n\n/**\n * Main parameter extraction logic - enhanced for Express 5 support with recursion depth limit\n */\ntype ExtractParams<Path extends string> =\n // Handle Express 5 braces for optional segments: {/:param} or {/path/:param}\n Path extends `${infer Before}{${infer OptionalContent}}${infer After}`\n ? ExtractOptionalSegment<OptionalContent> &\n ExtractParams<`${Before}${After}`>\n : // Handle named parameters :paramName\n Path extends `${infer _Before}:${infer Rest}`\n ? ExtractSingleParam<Rest> & ExtractParams<RemoveFirstParam<Path>>\n : // Handle wildcards *\n Path extends `${infer _Before}*${infer After}`\n ? {\n [K in CountWildcards<_Before, \"0\">]: string;\n } & ExtractParams<After>\n : // No more parameters\n {};\n\n/**\n * Extract parameters from Express 5 optional segments in braces\n * Handles patterns like {/:param}, {.:ext}, {/optional/:param}\n */\ntype ExtractOptionalSegment<Content extends string> =\n // Handle optional parameter patterns like /:param\n Content extends `/:${infer Rest}`\n ? ExtractOptionalParam<Rest>\n : Content extends `.:${infer Rest}`\n ? ExtractOptionalParam<Rest>\n : Content extends `${infer _Path}:${infer Rest}`\n ? ExtractOptionalParam<Rest>\n : {};\n\n/**\n * Extract a single optional parameter from brace content\n */\ntype ExtractOptionalParam<Rest extends string> =\n Rest extends `${infer ParamName}/${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}-${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}.${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}`\n ? { [K in ParamName]?: string }\n : {};\n\n/**\n * Extract a single parameter name from the rest of the path\n * Enhanced to handle Express 5 patterns and optional parameters correctly\n * Special handling for consecutive parameters like :from-:to\n * Order matters: regex constraints must be handled before repeating parameters\n */\ntype ExtractSingleParam<Rest extends string> =\n // Handle regex constraints FIRST (before +, *, ?) to avoid conflicts\n Rest extends `${infer ParamName}(${infer _Constraint})${infer _After}`\n ? { [K in ParamName]: string } // Handle consecutive parameters with separators first: param-:nextParam\n : Rest extends `${infer ParamName}-:${infer _NextParam}`\n ? { [K in ParamName]: string }\n : Rest extends `${infer ParamName}.:${infer _NextParam}`\n ? { [K in ParamName]: string }\n : // Handle optional parameters followed by delimiters (before regular delimiters)\n Rest extends `${infer ParamName}?/${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}?-${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}?.${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}?#${infer _After}`\n ? { [K in ParamName]?: string }\n : Rest extends `${infer ParamName}?:${infer _After}`\n ? { [K in ParamName]?: string }\n : // Then handle regular delimiters (after optional parameter patterns)\n Rest extends `${infer ParamName}/${infer _After}`\n ? { [K in ParamName]: string }\n : Rest extends `${infer ParamName}-${infer _After}`\n ? { [K in ParamName]: string }\n : Rest extends `${infer ParamName}.${infer _After}`\n ? { [K in ParamName]: string }\n : Rest extends `${infer ParamName}#${infer _After}`\n ? { [K in ParamName]: string }\n : Rest extends `${infer ParamName}:${infer _After}`\n ? { [K in ParamName]: string }\n : // Handle Express 5 repeating parameters (after regular delimiters)\n Rest extends `${infer ParamName}+${infer _After}`\n ? { [K in ParamName]: string[] }\n : Rest extends `${infer ParamName}*${infer _After}`\n ? { [K in ParamName]?: string[] }\n : // Handle optional parameters with ? (Express 4) - only at the end of a segment\n Rest extends `${infer ParamName}?${infer _After}`\n ? { [K in ParamName]?: string } // Parameter at absolute end of string\n : Rest extends string\n ? Rest extends \"\"\n ? {}\n : Rest extends `${infer ParamName}?`\n ? { [K in ParamName]?: string } // ParamName here doesn't include the ?\n : Rest extends `${infer ParamName}+`\n ? { [K in ParamName]: string[] }\n : Rest extends `${infer ParamName}*`\n ? { [K in ParamName]?: string[] }\n : { [K in Rest]: string }\n : {};\n\n/**\n * Remove the first parameter from path to continue parsing\n * Enhanced to handle Express 5 patterns and optional parameters\n * Handles patterns like :from-:to by removing just :from and keeping -:to\n * Order matters: regex constraints must be handled before repeating parameters\n */\ntype RemoveFirstParam<Path extends string> =\n Path extends `${infer Before}:${infer Rest}`\n ? // Handle regex constraints FIRST (before +, *, ?) to avoid conflicts\n Rest extends `${infer _ParamName}(${infer _Constraint})${infer After}`\n ? `${Before}${After}` // Handle consecutive parameters: :param-:nextParam -> -:nextParam\n : Rest extends `${infer _ParamName}-:${infer After}`\n ? `${Before}-:${After}`\n : Rest extends `${infer _ParamName}.:${infer After}`\n ? `${Before}.:${After}`\n : // Handle optional parameters followed by delimiters (before regular delimiters)\n Rest extends `${infer _ParamName}?/${infer After}`\n ? `${Before}/${After}`\n : Rest extends `${infer _ParamName}?-${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}?.${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}?#${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}?:${infer After}`\n ? `${Before}:${After}`\n : // Handle regular separators (after optional parameter patterns)\n Rest extends `${infer _ParamName}/${infer After}`\n ? `${Before}/${After}`\n : Rest extends `${infer _ParamName}-${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}.${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}#${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}:${infer After}`\n ? `${Before}:${After}`\n : // Handle Express 5 repeating parameters (after regular separators)\n Rest extends `${infer _ParamName}+${infer After}`\n ? `${Before}${After}`\n : Rest extends `${infer _ParamName}*${infer After}`\n ? `${Before}${After}`\n : // Handle optional parameters with ?\n Rest extends `${infer _ParamName}?${infer After}`\n ? `${Before}${After}`\n : Before\n : Path;\n\n/**\n * Count wildcards to assign proper numeric indices with recursion depth limit\n */\ntype CountWildcards<\n Path extends string,\n Count extends string = \"0\"\n> = Path extends `${infer _Before}*${infer Rest}`\n ? CountWildcards<Rest, IncrementWildcard<Count>>\n : Count;\n\n/**\n * Helper type to increment wildcard count as string\n */\ntype IncrementWildcard<T extends string> = T extends \"0\"\n ? \"1\"\n : T extends \"1\"\n ? \"2\"\n : T extends \"2\"\n ? \"3\"\n : T extends \"3\"\n ? \"4\"\n : T extends \"4\"\n ? \"5\"\n : T extends \"5\"\n ? \"6\"\n : T extends \"6\"\n ? \"7\"\n : T extends \"7\"\n ? \"8\"\n : T extends \"8\"\n ? \"9\"\n : \"10\"; // Reasonable limit for wildcards\n\n/**\n * Express middleware that adds custom properties to the request object and/or response locals.\n *\n * @template TReq - The shape of the properties added to the request object.\n * @template TLocals - The shape of the properties added to response.locals.\n * @param req - The Express request object, extended with TReq.\n * @param res - The Express response object with typed locals.\n * @param next - The next middleware function.\n */\nexport type TypedMiddleware<\n TReq extends Record<string, any> = {},\n TLocals extends Record<string, any> = {}\n> = (\n req: Request & TReq,\n res: Response<any, TLocals>,\n next: NextFunction\n) => void | Promise<void>;\n\n/**\n * Simplified TypedMiddleware for request-only extensions (backward compatibility)\n */\nexport type RequestOnlyMiddleware<TReq extends Record<string, any>> =\n TypedMiddleware<TReq, {}>;\n\n/**\n * Simplified TypedMiddleware for response locals-only extensions\n */\nexport type LocalsOnlyMiddleware<TLocals extends Record<string, any>> =\n TypedMiddleware<{}, TLocals>;\n\n// Utility type to infer props from middleware array (no recursion depth limit)\ntype InferMiddlewareProps<T extends readonly TypedMiddleware<any, any>[]> =\n T extends readonly [infer First, ...infer Rest]\n ? First extends TypedMiddleware<infer FirstReq, any>\n ? Rest extends readonly TypedMiddleware<any, any>[]\n ? FirstReq & InferMiddlewareProps<Rest>\n : FirstReq\n : {}\n : {};\n\n// Utility type to infer locals from middleware array (no recursion depth limit)\ntype InferMiddlewareLocals<T extends readonly TypedMiddleware<any, any>[]> =\n T extends readonly [infer First, ...infer Rest]\n ? First extends TypedMiddleware<any, infer FirstLocals>\n ? Rest extends readonly TypedMiddleware<any, any>[]\n ? FirstLocals & InferMiddlewareLocals<Rest>\n : FirstLocals\n : {}\n : {};\n\n// Enhanced Request type with proper inference\nexport type ZodRequest<\n Path extends string = string,\n BodySchema extends AnyZodType | unknown = unknown,\n QuerySchema extends AnyZodType | unknown = unknown,\n MiddlewareProps extends Record<string, any> = {}\n> = Omit<Request, \"params\" | \"query\" | \"body\"> & {\n params: ExtractRouteParams<Path>;\n body: BodySchema extends AnyZodType ? InferOutput<BodySchema> : unknown;\n query: QuerySchema extends AnyZodType ? InferOutput<QuerySchema> : unknown;\n} & MiddlewareProps;\n\n// Route handler type\nexport type ZodRouteHandler<\n Path extends string = string,\n BodySchema extends AnyZodType | unknown = unknown,\n QuerySchema extends AnyZodType | unknown = unknown,\n MiddlewareProps extends Record<string, any> = {},\n ResponseLocals extends Record<string, any> = {}\n> = (\n req: ZodRequest<Path, BodySchema, QuerySchema, MiddlewareProps>,\n res: Response<any, ResponseLocals>,\n next?: NextFunction\n) => void | Promise<void> | Response | Promise<Response>;\n\n/**\n * Options for defining a typed route, including schemas and middleware.\n *\n * @template BodySchema - Zod schema for request body validation.\n * @template QuerySchema - Zod schema for query parameter validation.\n * @property bodySchema - Optional Zod schema for validating the request body.\n * @property querySchema - Optional Zod schema for validating the query string.\n * @property middleware - Optional array of TypedMiddleware for this route.\n */\nexport interface RouteOptions<\n BodySchema extends AnyZodType | unknown = unknown,\n QuerySchema extends AnyZodType | unknown = unknown\n> {\n bodySchema?: BodySchema;\n querySchema?: QuerySchema;\n middleware?: TypedMiddleware<any, any>[];\n}\n\n// HTTP methods\nexport type HttpMethod =\n | \"get\"\n | \"post\"\n | \"put\"\n | \"delete\"\n | \"patch\"\n | \"options\"\n | \"head\"\n | \"all\";\n\n// Main typed router class\nclass TypedRouter<\n RouterMiddlewareProps extends Record<string, any> = {},\n RouterLocals extends Record<string, any> = {}\n> {\n private router: express.Router;\n\n constructor() {\n this.router = express.Router();\n }\n /**\n * Add typed middleware that extends the request with additional properties\n * and/or adds properties to response.locals\n */ /**\n * Add typed middleware to the router.\n * This middleware will apply to all routes defined after this call.\n *\n * @template TReq - Type extensions for the request object\n * @template TLocals - Type extensions for response.locals\n * @param middleware - The typed middleware function\n * @returns A new router instance with updated types\n */\n useMiddleware<\n TReq extends Record<string, any> = {},\n TLocals extends Record<string, any> = {}\n >(\n middleware: TypedMiddleware<TReq, TLocals>\n ): TypedRouter<RouterMiddlewareProps & TReq, RouterLocals & TLocals> {\n this.router.use(middleware as any);\n return this as any;\n }\n /**\n * Get the underlying Express router\n */\n getRouter(): express.Router {\n return this.router;\n }\n // Method overloads for GET requests with automatic middleware type inference\n get<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n get<\n Path extends string,\n BodySchema extends AnyZodType | unknown,\n QuerySchema extends AnyZodType | unknown\n >(\n path: Path,\n options: RouteOptions<BodySchema, QuerySchema>,\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Special overload for middleware type inference\n get<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n // Combined overload for body/query schema + middleware\n get<\n Path extends string,\n BodySchema extends AnyZodType | unknown,\n QuerySchema extends AnyZodType | unknown,\n M extends TypedMiddleware<any, any>[] // Using array type for JS compatibility\n >(\n path: Path,\n options: RouteOptions<BodySchema, QuerySchema> & { middleware: [...M] }, // Using tuple spread pattern\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>, // Make it readonly for type inference\n RouterLocals & InferMiddlewareLocals<readonly [...M]>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n // Implementation\n get(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"get\", path, optionsOrHandler, handler);\n } // Combined overload for body/query schema + middleware (most specific first)\n post<\n Path extends string,\n BodySchema extends AnyZodType,\n QuerySchema extends AnyZodType | unknown,\n M extends TypedMiddleware<any, any>[] // Using array type for JS compatibility\n >(\n path: Path,\n options: {\n bodySchema: BodySchema;\n querySchema?: QuerySchema;\n middleware: [...M]; // Using tuple spread pattern\n },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>, // Make it readonly for type inference\n RouterLocals & InferMiddlewareLocals<readonly [...M]>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Body schema only + middleware\n post<\n Path extends string,\n BodySchema extends AnyZodType,\n M extends TypedMiddleware<any, any>[] // Using array type for JS compatibility\n >(\n path: Path,\n options: { bodySchema: BodySchema; middleware: [...M] }, // Using tuple spread pattern\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>, // Make it readonly for type inference\n RouterLocals & InferMiddlewareLocals<readonly [...M]>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Middleware only\n post<\n Path extends string,\n M extends TypedMiddleware<any, any>[] // Using array type for JS compatibility\n >(\n path: Path,\n options: { middleware: [...M] }, // Using tuple spread pattern\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<readonly [...M]>, // Make it readonly for type inference\n RouterLocals & InferMiddlewareLocals<readonly [...M]>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Body + Query schema without middleware\n post<\n Path extends string,\n BodySchema extends AnyZodType | unknown,\n QuerySchema extends AnyZodType | unknown\n >(\n path: Path,\n options: RouteOptions<BodySchema, QuerySchema>,\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Just handler, no options\n post<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n post(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"post\", path, optionsOrHandler, handler);\n }\n // PUT method with all the same overloads as POST\n put<\n Path extends string,\n BodySchema extends AnyZodType,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: {\n bodySchema: BodySchema;\n querySchema?: QuerySchema;\n middleware: Middleware;\n },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n put<\n Path extends string,\n BodySchema extends AnyZodType,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { bodySchema: BodySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n put<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n put<\n Path extends string,\n BodySchema extends AnyZodType | unknown,\n QuerySchema extends AnyZodType | unknown\n >(\n path: Path,\n options: RouteOptions<BodySchema, QuerySchema>,\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n put<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n put(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"put\", path, optionsOrHandler, handler);\n }\n // PATCH method with all the same overloads as POST\n patch<\n Path extends string,\n BodySchema extends AnyZodType,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: {\n bodySchema: BodySchema;\n querySchema?: QuerySchema;\n middleware: Middleware;\n },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n patch<\n Path extends string,\n BodySchema extends AnyZodType,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { bodySchema: BodySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n patch<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n patch<\n Path extends string,\n BodySchema extends AnyZodType | unknown,\n QuerySchema extends AnyZodType | unknown\n >(\n path: Path,\n options: RouteOptions<BodySchema, QuerySchema>,\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n patch<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n patch(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"patch\", path, optionsOrHandler, handler);\n } // DELETE method (typically no body, but can have query params and middleware)\n // Most specific first: query schema + middleware\n delete<\n Path extends string,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { querySchema: QuerySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Query schema only\n delete<Path extends string, QuerySchema extends AnyZodType | unknown>(\n path: Path,\n options: { querySchema: QuerySchema },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Middleware only\n delete<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Basic overload with no options\n delete<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n delete(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"delete\", path, optionsOrHandler, handler);\n } // OPTIONS method (typically no body, used for CORS preflight)\n // Most specific first: query schema + middleware\n options<\n Path extends string,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { querySchema: QuerySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Query schema only\n options<Path extends string, QuerySchema extends AnyZodType | unknown>(\n path: Path,\n options: { querySchema: QuerySchema },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Middleware only\n options<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Basic overload with no options\n options<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n options(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"options\", path, optionsOrHandler, handler);\n } // HEAD method (like GET but only returns headers)\n // Most specific first: query schema + middleware\n head<\n Path extends string,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { querySchema: QuerySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Query schema only\n head<Path extends string, QuerySchema extends AnyZodType | unknown>(\n path: Path,\n options: { querySchema: QuerySchema },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Middleware only\n head<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Basic overload with no options\n head<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n head(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"head\", path, optionsOrHandler, handler);\n } // ALL method (matches all HTTP methods)\n // Most specific first: body + query + middleware\n all<\n Path extends string,\n BodySchema extends AnyZodType,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: {\n bodySchema: BodySchema;\n querySchema?: QuerySchema;\n middleware: Middleware;\n },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Body schema + middleware (no query)\n all<\n Path extends string,\n BodySchema extends AnyZodType,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { bodySchema: BodySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Query schema + middleware (no body)\n all<\n Path extends string,\n QuerySchema extends AnyZodType | unknown,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { querySchema: QuerySchema; middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n QuerySchema,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Body + query schemas (no middleware)\n all<\n Path extends string,\n BodySchema extends AnyZodType | unknown,\n QuerySchema extends AnyZodType | unknown\n >(\n path: Path,\n options: RouteOptions<BodySchema, QuerySchema>,\n handler: ZodRouteHandler<\n Path,\n BodySchema,\n QuerySchema,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Middleware only (no schemas)\n all<\n Path extends string,\n Middleware extends readonly TypedMiddleware<any, any>[]\n >(\n path: Path,\n options: { middleware: Middleware },\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps & InferMiddlewareProps<Middleware>,\n RouterLocals & InferMiddlewareLocals<Middleware>\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n\n // Basic overload with no options\n all<Path extends string>(\n path: Path,\n handler: ZodRouteHandler<\n Path,\n unknown,\n unknown,\n RouterMiddlewareProps,\n RouterLocals\n >\n ): TypedRouter<RouterMiddlewareProps, RouterLocals>;\n all(\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return this.registerRoute(\"all\", path, optionsOrHandler, handler);\n }\n // Helper method to register routes\n private registerRoute(\n method: HttpMethod,\n path: string,\n optionsOrHandler: any,\n handler?: any\n ): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n const middlewares: any[] = [];\n\n if (typeof optionsOrHandler === \"object\") {\n const options = optionsOrHandler as RouteOptions<any, any>;\n\n // Add per-route middleware first\n if (options.middleware) {\n middlewares.push(...options.middleware);\n }\n\n // Add schema validation middleware\n if (options.bodySchema) {\n middlewares.push(\n this.createBodyValidationMiddleware(options.bodySchema)\n );\n }\n if (options.querySchema) {\n middlewares.push(\n this.createQueryValidationMiddleware(options.querySchema)\n );\n }\n\n // Add the main handler\n middlewares.push(handler);\n } else {\n // Direct handler without options\n middlewares.push(optionsOrHandler);\n }\n\n // Register with Express router\n (this.router as any)[method](path, ...middlewares);\n\n return this;\n }\n private createBodyValidationMiddleware(schema: AnyZodType) {\n return (req: Request, res: Response, next: NextFunction) => {\n try {\n req.body = parseSchema(schema, req.body);\n next();\n } catch (error) {\n // Check for ZodError using our unified helper\n if (isZodError(error)) {\n res.status(400).json({\n error: \"Validation failed\",\n details: (error as any).errors || (error as any).issues,\n });\n } else {\n next(error);\n }\n }\n };\n }\n private createQueryValidationMiddleware(schema: AnyZodType) {\n return (req: Request, res: Response, next: NextFunction) => {\n try {\n const validatedQuery = parseSchema(schema, req.query);\n // Use Object.defineProperty to properly set the read-only query property\n Object.defineProperty(req, \"query\", {\n value: validatedQuery,\n writable: false,\n enumerable: true,\n configurable: true,\n });\n next();\n } catch (error) {\n // Check for ZodError using our unified helper\n if (isZodError(error)) {\n res.status(400).json({\n error: \"Validation failed\",\n details: (error as any).errors || (error as any).issues,\n });\n } else {\n next(error);\n }\n }\n };\n }\n}\n\n/**\n * Create a new strongly-typed Express router instance.\n *\n * This is the simplest way to get started with @minisylar/express-typed-router.\n *\n * @example\n * import { createTypedRouter } from '@minisylar/express-typed-router';\n *\n * // Create a router and add a typed GET route\n * const router = createTypedRouter();\n * router.get('/hello/:name', (req, res) => {\n * // req.params.name is typed as string\n * res.json({ message: `Hello, ${req.params.name}!` });\n * });\n *\n * // Use with Express\n * import express from 'express';\n * const app = express();\n * app.use('/api', router.getRouter());\n */\nexport function createTypedRouter<\n RouterMiddlewareProps extends Record<string, any> = {},\n RouterLocals extends Record<string, any> = {}\n>(): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n return new TypedRouter<RouterMiddlewareProps, RouterLocals>();\n}\n\n// Option 2: Factory with optional configuration\n\n/**\n * Configuration options for createTypedRouterWithConfig.\n *\n * @property validateInput - (Future) Whether to enable global input validation.\n * @property errorHandler - Optional global error handler middleware for the router.\n */\nexport interface RouterConfig {\n validateInput?: boolean;\n errorHandler?: (\n error: any,\n req: Request,\n res: Response,\n next: NextFunction\n ) => void;\n}\n\n/**\n * Create a new typed router with optional configuration.\n *\n * Use this if you want to add a global error handler or future global options.\n *\n * @param config - Optional configuration for the router (e.g. error handler).\n * @returns A new TypedRouter instance.\n *\n * @example\n * import { createTypedRouterWithConfig } from '@minisylar/express-typed-router';\n *\n * const router = createTypedRouterWithConfig({\n * errorHandler: (err, req, res, next) => {\n * res.status(500).json({ error: 'Something went wrong', details: err });\n * }\n * });\n */\nexport function createTypedRouterWithConfig<\n RouterMiddlewareProps extends Record<string, any> = {},\n RouterLocals extends Record<string, any> = {}\n>(config?: RouterConfig): TypedRouter<RouterMiddlewareProps, RouterLocals> {\n const router = new TypedRouter<RouterMiddlewareProps, RouterLocals>();\n if (config?.errorHandler) {\n router.getRouter().use(config.errorHandler);\n }\n return router;\n}\n\n// Option 3: Factory with pre-configured middleware\n\n/**\n * Create a new typed router with pre-configured middleware.\n *\n * This is useful for setting up router-level middleware in a single call.\n *\n * @param middleware - One or more TypedMiddleware functions to apply to all routes.\n * @returns A new TypedRouter instance with the middleware applied.\n *\n * @example\n * import { createTypedRouterWithMiddleware } from '@minisylar/express-typed-router';\n *\n * const router = createTypedRouterWithMiddleware(authMiddleware, loggingMiddleware);\n */\nexport function createTypedRouterWithMiddleware<T extends Record<string, any>>(\n ...middleware: TypedMiddleware<any, any>[]\n): TypedRouter<T> {\n let router = new TypedRouter() as any;\n for (const mw of middleware) {\n router = router.useMiddleware(mw);\n }\n return router;\n}\n"],"mappings":"8EA0JA,SAAgB,EAAaA,EAA2C,CACtE,MAAO,SAAU,CAClB,CAyBD,SAAgB,EACdC,EACAC,EACgB,CAId,MAHE,GAAa,EAAO,CACf,EAAG,MAAM,EAAQ,EAAK,CAEtB,EAA0B,MAAM,EAAK,AAE/C,CASD,SAAgB,EACdD,EACAC,EACoB,CAIlB,MAHE,GAAa,EAAO,CACf,EAAG,UAAU,EAAQ,EAAK,CAE1B,EAA0B,UAAU,EAAK,AAEnD,CAGD,SAAgB,EAAWC,EAAsC,CAC/D,OACE,aAAiB,EAAG,iBAEX,GAAU,YADlB,GAEC,WAAY,GACZ,MAAM,QAAS,EAAc,OAAO,AAEzC,CAoTD,IAAM,EAAN,KAGE,CACA,OAEA,aAAc,CACZ,KAAK,OAAS,EAAQ,QAAQ,AAC/B,CAaD,cAIEC,EACmE,CAEnE,MADA,MAAK,OAAO,IAAI,EAAkB,CAC3B,IACR,CAID,WAA4B,CAC1B,OAAO,KAAK,MACb,CA8DD,IACEC,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,MAAO,EAAM,EAAkB,EAAQ,AAClE,CAoFD,KACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,OAAQ,EAAM,EAAkB,EAAQ,AACnE,CAgFD,IACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,MAAO,EAAM,EAAkB,EAAQ,AAClE,CAgFD,MACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,QAAS,EAAM,EAAkB,EAAQ,AACpE,CA0DD,OACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,SAAU,EAAM,EAAkB,EAAQ,AACrE,CA0DD,QACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,UAAW,EAAM,EAAkB,EAAQ,AACtE,CA0DD,KACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,OAAQ,EAAM,EAAkB,EAAQ,AACnE,CAqGD,IACEF,EACAC,EACAC,EACkD,CAClD,MAAO,MAAK,cAAc,MAAO,EAAM,EAAkB,EAAQ,AAClE,CAED,cACEC,EACAH,EACAC,EACAC,EACkD,CAClD,IAAME,EAAqB,CAAE,EAE7B,UAAW,GAAqB,SAAU,CACxC,IAAM,EAAU,EAoBhB,AAjBI,EAAQ,YACV,EAAY,KAAK,GAAG,EAAQ,WAAW,CAIrC,EAAQ,YACV,EAAY,KACV,KAAK,+BAA+B,EAAQ,WAAW,CACxD,CAEC,EAAQ,aACV,EAAY,KACV,KAAK,gCAAgC,EAAQ,YAAY,CAC1D,CAIH,EAAY,KAAK,EAAQ,AAC1B,MAEC,EAAY,KAAK,EAAiB,CAMpC,MAFC,MAAK,OAAe,GAAQ,EAAM,GAAG,EAAY,CAE3C,IACR,CACD,+BAAuCT,EAAoB,CACzD,MAAO,CAACU,EAAcC,EAAeC,IAAuB,CAC1D,GAAI,CAEF,AADA,EAAI,KAAO,EAAY,EAAQ,EAAI,KAAK,CACxC,GAAM,AACP,OAAQ,EAAO,CAEd,AAAI,EAAW,EAAM,CACnB,EAAI,OAAO,IAAI,CAAC,KAAK,CACnB,MAAO,oBACP,QAAU,EAAc,QAAW,EAAc,MAClD,EAAC,CAEF,EAAK,EAAM,AAEd,CACF,CACF,CACD,gCAAwCZ,EAAoB,CAC1D,MAAO,CAACU,EAAcC,EAAeC,IAAuB,CAC1D,GAAI,CACF,IAAM,EAAiB,EAAY,EAAQ,EAAI,MAAM,CAQrD,AANA,OAAO,eAAe,EAAK,QAAS,CAClC,MAAO,EACP,UAAU,EACV,YAAY,EACZ,cAAc,CACf,EAAC,CACF,GAAM,AACP,OAAQ,EAAO,CAEd,AAAI,EAAW,EAAM,CACnB,EAAI,OAAO,IAAI,CAAC,KAAK,CACnB,MAAO,oBACP,QAAU,EAAc,QAAW,EAAc,MAClD,EAAC,CAEF,EAAK,EAAM,AAEd,CACF,CACF,CACF,EAsBD,SAAgB,GAGsC,CACpD,OAAO,IAAI,CACZ,CAqCD,SAAgB,EAGdC,EAAyE,CACzE,IAAM,EAAS,IAAI,EAInB,OAHI,GAAQ,cACV,EAAO,WAAW,CAAC,IAAI,EAAO,aAAa,CAEtC,CACR,CAiBD,SAAgB,EACd,GAAG,EACa,CAChB,IAAI,EAAS,IAAI,EACjB,IAAK,IAAM,KAAM,EACf,EAAS,EAAO,cAAc,EAAG,CAEnC,OAAO,CACR"}