@@ -3,20 +3,22 @@ import {
3
3
ParameterLocation ,
4
4
ParameterType ,
5
5
RegexParameterType ,
6
- RouteParameter ,
6
+ HeaderParameter ,
7
+ QueryParameter ,
8
+ PathParameter ,
7
9
StringParameterType ,
10
+ LegacyParameter ,
8
11
} from './types'
9
- import { z , ZodObject } from 'zod'
12
+ import { z } from 'zod'
10
13
import { isSpecificZodType , legacyTypeIntoZod } from './zod/utils'
11
14
import { extendZodWithOpenApi } from '@asteasolutions/zod-to-openapi'
12
- import { ZodType } from 'zod'
13
15
14
16
if ( z . string ( ) . openapi === undefined ) {
15
17
// console.log('zod extension applied')
16
18
extendZodWithOpenApi ( z )
17
19
}
18
20
19
- export function convertParams ( field : any , params : any ) : ZodType {
21
+ export function convertParams ( field : any , params : any ) : z . ZodType {
20
22
params = params || { }
21
23
if ( params . required === false )
22
24
// @ts -ignore
@@ -60,7 +62,7 @@ export class Obj {
60
62
}
61
63
}
62
64
63
- export class Num {
65
+ export const Num : LegacyParameter < z . ZodNumber > = class Num {
64
66
static generator = true
65
67
66
68
constructor ( params ?: ParameterType ) {
@@ -71,9 +73,9 @@ export class Num {
71
73
type : 'number' ,
72
74
} )
73
75
}
74
- }
76
+ } as unknown as LegacyParameter < z . ZodNumber >
75
77
76
- export class Int {
78
+ export const Int : LegacyParameter < z . ZodNumber > = class Int {
77
79
static generator = true
78
80
79
81
constructor ( params ?: ParameterType ) {
@@ -84,17 +86,17 @@ export class Int {
84
86
type : 'integer' ,
85
87
} )
86
88
}
87
- }
89
+ } as unknown as LegacyParameter < z . ZodNumber >
88
90
89
- export class Str {
91
+ export const Str : LegacyParameter < z . ZodString > = class Str {
90
92
static generator = true
91
93
92
94
constructor ( params ?: StringParameterType ) {
93
95
return convertParams ( z . string ( ) , params )
94
96
}
95
- }
97
+ } as unknown as LegacyParameter < z . ZodString >
96
98
97
- export class DateTime {
99
+ export const DateTime : LegacyParameter < z . ZodString > = class DateTime {
98
100
static generator = true
99
101
100
102
constructor ( params ?: ParameterType ) {
@@ -105,9 +107,9 @@ export class DateTime {
105
107
params
106
108
)
107
109
}
108
- }
110
+ } as unknown as LegacyParameter < z . ZodString >
109
111
110
- export class Regex {
112
+ export const Regex : LegacyParameter < z . ZodString > = class Regex {
111
113
static generator = true
112
114
113
115
constructor ( params : RegexParameterType ) {
@@ -117,25 +119,25 @@ export class Regex {
117
119
params
118
120
)
119
121
}
120
- }
122
+ } as unknown as LegacyParameter < z . ZodString >
121
123
122
- export class Email {
124
+ export const Email : LegacyParameter < z . ZodString > = class Email {
123
125
static generator = true
124
126
125
127
constructor ( params ?: ParameterType ) {
126
128
return convertParams ( z . string ( ) . email ( ) , params )
127
129
}
128
- }
130
+ } as unknown as LegacyParameter < z . ZodString >
129
131
130
- export class Uuid {
132
+ export const Uuid : LegacyParameter < z . ZodString > = class Uuid {
131
133
static generator = true
132
134
133
135
constructor ( params ?: ParameterType ) {
134
136
return convertParams ( z . string ( ) . uuid ( ) , params )
135
137
}
136
- }
138
+ } as unknown as LegacyParameter < z . ZodString >
137
139
138
- export class Hostname {
140
+ export const Hostname : LegacyParameter < z . ZodString > = class Hostname {
139
141
static generator = true
140
142
141
143
constructor ( params ?: ParameterType ) {
@@ -148,33 +150,33 @@ export class Hostname {
148
150
params
149
151
)
150
152
}
151
- }
153
+ } as unknown as LegacyParameter < z . ZodString >
152
154
153
- export class Ipv4 {
155
+ export const Ipv4 : LegacyParameter < z . ZodString > = class Ipv4 {
154
156
static generator = true
155
157
156
158
constructor ( params ?: ParameterType ) {
157
159
return convertParams ( z . coerce . string ( ) . ip ( { version : 'v4' } ) , params )
158
160
}
159
- }
161
+ } as unknown as LegacyParameter < z . ZodString >
160
162
161
- export class Ipv6 {
163
+ export const Ipv6 : LegacyParameter < z . ZodString > = class Ipv6 {
162
164
static generator = true
163
165
164
166
constructor ( params ?: ParameterType ) {
165
167
return convertParams ( z . string ( ) . ip ( { version : 'v6' } ) , params )
166
168
}
167
- }
169
+ } as unknown as LegacyParameter < z . ZodString >
168
170
169
- export class DateOnly {
171
+ export const DateOnly : LegacyParameter < z . ZodString > = class DateOnly {
170
172
static generator = true
171
173
172
174
constructor ( params ?: ParameterType ) {
173
175
return convertParams ( z . coerce . date ( ) , params )
174
176
}
175
- }
177
+ } as unknown as LegacyParameter < z . ZodString >
176
178
177
- export class Bool {
179
+ export const Bool : LegacyParameter < z . ZodBoolean > = class Bool {
178
180
static generator = true
179
181
180
182
constructor ( params ?: ParameterType ) {
@@ -188,7 +190,7 @@ export class Bool {
188
190
type : 'boolean' ,
189
191
} )
190
192
}
191
- }
193
+ } as unknown as LegacyParameter < z . ZodBoolean >
192
194
193
195
export class Enumeration {
194
196
static generator = true
@@ -202,7 +204,7 @@ export class Enumeration {
202
204
203
205
const originalKeys : [ string , ...string [ ] ] = Object . keys ( values ) as [
204
206
string ,
205
- ...string [ ]
207
+ ...string [ ] ,
206
208
]
207
209
208
210
if ( params . enumCaseSensitive === false ) {
@@ -215,7 +217,7 @@ export class Enumeration {
215
217
216
218
const keys : [ string , ...string [ ] ] = Object . keys ( values ) as [
217
219
string ,
218
- ...string [ ]
220
+ ...string [ ] ,
219
221
]
220
222
221
223
let field
@@ -239,32 +241,95 @@ export class Enumeration {
239
241
}
240
242
}
241
243
244
+ export function Query < Z extends z . ZodType > ( type : Z ) : QueryParameter < Z >
245
+ export function Query < Z extends z . ZodType > (
246
+ type : Z ,
247
+ params : ParameterLocation & { required : false }
248
+ ) : QueryParameter < z . ZodOptional < Z > >
249
+ export function Query < Z extends z . ZodType > (
250
+ type : Z ,
251
+ params : ParameterLocation
252
+ ) : QueryParameter < Z >
253
+ export function Query < Z extends z . ZodType > (
254
+ type : [ Z ]
255
+ ) : QueryParameter < z . ZodArray < Z > >
256
+ export function Query < Z extends z . ZodType > (
257
+ type : [ Z ] ,
258
+ params : ParameterLocation & { required : false }
259
+ ) : QueryParameter < z . ZodOptional < z . ZodArray < Z > > >
260
+ export function Query < Z extends z . ZodType > (
261
+ type : [ Z ] ,
262
+ params : ParameterLocation
263
+ ) : QueryParameter < z . ZodArray < Z > >
264
+ export function Query ( type : any ) : QueryParameter
265
+ export function Query ( type : any , params : ParameterLocation ) : QueryParameter
242
266
export function Query (
243
267
type : any ,
244
268
params : ParameterLocation = { }
245
- ) : RouteParameter {
269
+ ) : QueryParameter {
246
270
return {
247
271
name : params . name ,
248
272
location : 'query' ,
249
273
type : legacyTypeIntoZod ( type , params ) ,
250
274
}
251
275
}
252
276
253
- export function Path (
254
- type : any ,
255
- params : ParameterLocation = { }
256
- ) : RouteParameter {
277
+ export function Path < Z extends z . ZodType > ( type : Z ) : PathParameter < Z >
278
+ export function Path < Z extends z . ZodType > (
279
+ type : Z ,
280
+ params : ParameterLocation & { required : false }
281
+ ) : PathParameter < z . ZodOptional < Z > >
282
+ export function Path < Z extends z . ZodType > (
283
+ type : Z ,
284
+ params : ParameterLocation
285
+ ) : PathParameter < Z >
286
+ export function Path < Z extends z . ZodType > (
287
+ type : [ Z ]
288
+ ) : PathParameter < z . ZodArray < Z > >
289
+ export function Path < Z extends z . ZodType > (
290
+ type : [ Z ] ,
291
+ params : ParameterLocation & { required : false }
292
+ ) : PathParameter < z . ZodOptional < z . ZodArray < Z > > >
293
+ export function Path < Z extends z . ZodType > (
294
+ type : [ Z ] ,
295
+ params : ParameterLocation
296
+ ) : PathParameter < z . ZodArray < Z > >
297
+ export function Path ( type : any ) : PathParameter
298
+ export function Path ( type : any , params : ParameterLocation ) : PathParameter
299
+ export function Path ( type : any , params : ParameterLocation = { } ) : PathParameter {
257
300
return {
258
301
name : params . name ,
259
302
location : 'params' ,
260
303
type : legacyTypeIntoZod ( type , params ) ,
261
304
}
262
305
}
263
306
307
+ export function Header < Z extends z . ZodType > ( type : Z ) : HeaderParameter < Z >
308
+ export function Header < Z extends z . ZodType > (
309
+ type : Z ,
310
+ params : ParameterLocation & { required : false }
311
+ ) : HeaderParameter < z . ZodOptional < Z > >
312
+ export function Header < Z extends z . ZodType > (
313
+ type : Z ,
314
+ params : ParameterLocation
315
+ ) : HeaderParameter < Z >
316
+ export function Header < Z extends z . ZodType > (
317
+ type : [ Z ]
318
+ ) : HeaderParameter < z . ZodArray < Z > >
319
+ export function Header < Z extends z . ZodType > (
320
+ type : [ Z ] ,
321
+ params : ParameterLocation & { required : false }
322
+ ) : HeaderParameter < z . ZodOptional < z . ZodArray < Z > > >
323
+ export function Header < Z extends z . ZodType > (
324
+ type : [ Z ] ,
325
+ params : ParameterLocation
326
+ ) : HeaderParameter < z . ZodArray < Z > >
327
+ export function Header ( type : any ) : HeaderParameter
328
+ export function Header ( type : any , params : ParameterLocation ) : HeaderParameter
264
329
export function Header (
265
330
type : any ,
266
331
params : ParameterLocation = { }
267
- ) : RouteParameter {
332
+ ) : HeaderParameter {
268
333
return {
269
334
name : params . name ,
270
335
location : 'headers' ,
@@ -296,7 +361,7 @@ export function extractParameter(
296
361
297
362
export function extractQueryParameters (
298
363
request : Request ,
299
- schema ?: ZodObject < any >
364
+ schema ?: z . ZodObject < any >
300
365
) : Record < string , any > | null {
301
366
const { searchParams } = new URL ( request . url )
302
367
0 commit comments