@@ -61,19 +61,28 @@ export function toSchemaType(
61
61
schema ?:
62
62
| OpenAPI . ReferenceObject
63
63
| OpenAPI . SchemaObject ,
64
+ coerceToString ?: boolean ,
64
65
) : string | undefined {
65
66
if ( schema === undefined ) return undefined ;
66
67
if ( "$ref" in schema ) return pascalCase ( schema . $ref . split ( "/" ) . pop ( ) ! ) ;
67
68
68
69
if ( "nullable" in schema && schema . nullable !== undefined ) {
69
- const type = toSchemaType ( document , { ...schema , nullable : undefined } ) ;
70
+ const type = toSchemaType (
71
+ document ,
72
+ { ...schema , nullable : undefined } ,
73
+ coerceToString ,
74
+ ) ;
70
75
if ( type !== undefined ) return `${ type } |null` ;
71
76
return "null" ;
72
77
}
73
78
74
79
if ( schema . not !== undefined ) {
75
- const type = toSchemaType ( document , { ...schema , not : undefined } ) ;
76
- const exclude = toSchemaType ( document , schema . not ) ;
80
+ const type = toSchemaType (
81
+ document ,
82
+ { ...schema , not : undefined } ,
83
+ coerceToString ,
84
+ ) ;
85
+ const exclude = toSchemaType ( document , schema . not , coerceToString ) ;
77
86
if ( type !== undefined && exclude !== undefined ) {
78
87
return `Exclude<${ type } , ${ exclude } >` ;
79
88
}
@@ -85,12 +94,13 @@ export function toSchemaType(
85
94
const type = toSchemaType ( document , {
86
95
...schema ,
87
96
additionalProperties : undefined ,
88
- } ) ;
97
+ } , coerceToString ) ;
89
98
let additionalProperties ;
90
99
if ( schema . additionalProperties !== true ) {
91
100
additionalProperties = toSchemaType (
92
101
document ,
93
102
schema . additionalProperties ,
103
+ coerceToString ,
94
104
) ;
95
105
}
96
106
if ( type !== undefined ) {
@@ -101,14 +111,14 @@ export function toSchemaType(
101
111
102
112
if ( schema . allOf ) {
103
113
return schema . allOf
104
- . map ( ( schema ) => toSchemaType ( document , schema ) )
114
+ . map ( ( schema ) => toSchemaType ( document , schema , coerceToString ) )
105
115
. filter ( Boolean )
106
116
. join ( "&" ) ;
107
117
}
108
118
109
119
if ( schema . oneOf ) {
110
120
return schema . oneOf
111
- . map ( ( schema ) => toSchemaType ( document , schema ) )
121
+ . map ( ( schema ) => toSchemaType ( document , schema , coerceToString ) )
112
122
. map ( ( type , _ , types ) => toSafeUnionString ( type , types ) )
113
123
. filter ( Boolean )
114
124
. join ( "|" ) ;
@@ -129,7 +139,7 @@ export function toSchemaType(
129
139
}
130
140
131
141
return schema . anyOf
132
- . map ( ( schema ) => toSchemaType ( document , schema ) )
142
+ . map ( ( schema ) => toSchemaType ( document , schema , coerceToString ) )
133
143
. map ( ( type , _ , types ) => toSafeUnionString ( type , types ) )
134
144
. filter ( Boolean )
135
145
. join ( "|" ) ;
@@ -141,11 +151,13 @@ export function toSchemaType(
141
151
142
152
switch ( schema . type ) {
143
153
case "boolean" :
154
+ if ( coerceToString ) return "`${boolean}`" ;
144
155
return "boolean" ;
145
156
case "string" :
146
157
return "string" ;
147
158
case "number" :
148
159
case "integer" :
160
+ if ( coerceToString ) return "`${number}`" ;
149
161
return "number" ;
150
162
case "object" : {
151
163
if ( "properties" in schema && schema . properties !== undefined ) {
@@ -157,19 +169,24 @@ export function toSchemaType(
157
169
. map ( ( [ property , type ] ) =>
158
170
`${ escapeObjectKey ( property ) } ${
159
171
schema . required ?. includes ( property ) ? "" : "?"
160
- } :${ toSchemaType ( document , type ) } `
172
+ } :${ toSchemaType ( document , type , coerceToString ) } `
161
173
)
162
174
. join ( ";" )
163
175
} }`;
164
176
}
177
+
178
+ if ( coerceToString ) return "Record<string, string>" ;
165
179
return "Record<string, unknown>" ;
166
180
}
167
181
case "array" : {
168
- const items = toSchemaType ( document , schema . items ) ;
182
+ const items = toSchemaType ( document , schema . items , coerceToString ) ;
169
183
if ( items !== undefined ) return `(${ items } )[]` ;
184
+
185
+ if ( coerceToString ) return "string[]" ;
170
186
return "unknown[]" ;
171
187
}
172
188
case "null" :
189
+ if ( coerceToString ) return "`${null}`" ;
173
190
return "null" ;
174
191
}
175
192
@@ -300,25 +317,43 @@ export function createRequestBodyType(
300
317
document : OpenAPI . Document ,
301
318
contentType : string ,
302
319
schema ?: OpenAPI . SchemaObject | OpenAPI . ReferenceObject ,
320
+ options ?: Options ,
303
321
) : string {
304
322
let type = "BodyInit" ;
305
323
306
324
switch ( contentType ) {
307
- case "application/json" :
325
+ case "application/json" : {
308
326
type = `JSONString<${ toSchemaType ( document , schema ) ?? "unknown" } >` ;
309
327
break ;
310
- case "text/plain" :
328
+ }
329
+ case "text/plain" : {
311
330
type = "string" ;
312
331
break ;
313
- case "multipart/form-data" :
332
+ }
333
+ case "multipart/form-data" : {
314
334
type = "FormData" ;
315
335
break ;
316
- case "application/x-www-form-urlencoded" :
317
- type = "URLSearchParams" ;
336
+ }
337
+ case "application/x-www-form-urlencoded" : {
338
+ const schemaType = toSchemaType ( document , schema , true ) ;
339
+ if ( schemaType !== undefined ) {
340
+ const types = [ `URLSearchParamsString<${ schemaType } >` ] ;
341
+
342
+ // TODO: We don't yet support URLSearchParams with the --experimental-urlsearchparams flag
343
+ if ( ! options ?. experimentalURLSearchParams ) {
344
+ types . push ( `URLSearchParams<${ schemaType } >` ) ;
345
+ }
346
+
347
+ return `(${ types . join ( "|" ) } )` ;
348
+ } else {
349
+ type = `URLSearchParams` ;
350
+ }
318
351
break ;
319
- case "application/octet-stream" :
352
+ }
353
+ case "application/octet-stream" : {
320
354
type = "ReadableStream | Blob | BufferSource" ;
321
355
break ;
356
+ }
322
357
}
323
358
324
359
return type ;
@@ -385,7 +420,6 @@ export function toTemplateString(
385
420
document : OpenAPI . Document ,
386
421
pattern : string ,
387
422
parameters : ParameterObjectMap ,
388
- options : Options ,
389
423
) : string {
390
424
let patternTemplateString = pattern ;
391
425
let urlSearchParamsOptional = true ;
@@ -397,7 +431,9 @@ export function toTemplateString(
397
431
urlSearchParamsOptional = false ;
398
432
}
399
433
400
- const types = [ toSchemaType ( document , parameter . schema ) ?? "string" ] ;
434
+ const types = [
435
+ toSchemaType ( document , parameter . schema , true ) ?? "string" ,
436
+ ] ;
401
437
if ( parameter . allowEmptyValue === true ) types . push ( "true" ) ;
402
438
urlSearchParamsRecord . push (
403
439
`${ escapeObjectKey ( parameter . name ) } ${ ! parameter . required ? "?" : "" } : ${
@@ -414,15 +450,17 @@ export function toTemplateString(
414
450
) ;
415
451
}
416
452
417
- const URLSearchParams = urlSearchParamsRecord . length > 0
418
- ? options . experimentalURLSearchParams
419
- ? `\${URLSearchParamsString<{${ urlSearchParamsRecord . join ( ";" ) } }>}`
420
- : urlSearchParamsOptional
421
- ? '${"" | `?${string}`}'
422
- : "?${string}"
453
+ const urlSearchParamsType = urlSearchParamsRecord . length > 0
454
+ ? `URLSearchParamsString<{${ urlSearchParamsRecord . join ( ";" ) } }>`
455
+ : undefined ;
456
+
457
+ const urlSearchParams = urlSearchParamsType
458
+ ? urlSearchParamsOptional
459
+ ? `\${\`?\${${ urlSearchParamsType } }\` | ""}`
460
+ : `?\${${ urlSearchParamsType } }`
423
461
: "" ;
424
462
425
- return `${ patternTemplateString } ${ URLSearchParams } ` ;
463
+ return `${ patternTemplateString } ${ urlSearchParams } ` ;
426
464
}
427
465
428
466
export function toHeadersInitType (
@@ -569,7 +607,7 @@ export function addOperationObject(
569
607
doc . tags . push ( { tagName : "summary" , text : operation . summary . trim ( ) } ) ;
570
608
}
571
609
572
- const path = toTemplateString ( document , pattern , parameters , options ) ;
610
+ const path = toTemplateString ( document , pattern , parameters ) ;
573
611
574
612
const inputs = [ ] ;
575
613
0 commit comments