@@ -16,12 +16,20 @@ import {
1616 isNotReferenceObject ,
1717 isReferenceObject ,
1818 pascalCase ,
19+ wordWrap ,
1920} from './utils.js' ;
2021
21- function withNullUnion ( type : string , nullable = false ) {
22+ function withNullUnion ( type : string | WriterFunction , nullable = false ) {
2223 return nullable ? Writers . unionType ( type , 'null' ) : type ;
2324}
2425
26+ function maybeWithUndefined (
27+ type : string | WriterFunction ,
28+ withUndefined : boolean ,
29+ ) {
30+ return withUndefined ? Writers . unionType ( type , 'undefined' ) : type ;
31+ }
32+
2533export function schemaToType (
2634 typesAndInterfaces : Map <
2735 string ,
@@ -30,6 +38,11 @@ export function schemaToType(
3038 parentSchema : OpenAPIV3 . SchemaObject ,
3139 propertyName : string ,
3240 schemaObject : OpenAPIV3 . SchemaObject | OpenAPIV3 . ReferenceObject ,
41+ options : {
42+ disallowUndefined ?: boolean ;
43+ booleanAsStringish ?: boolean ;
44+ integerAsStringish ?: boolean ;
45+ } = { } ,
3346) : OptionalKind < PropertySignatureStructure > {
3447 const name = `"${ propertyName } "` ;
3548 const hasQuestionToken =
@@ -159,17 +172,6 @@ export function schemaToType(
159172 // };
160173 }
161174
162- if ( schemaObject . type === 'integer' ) {
163- return {
164- name,
165- hasQuestionToken,
166- type : withNullUnion (
167- 'number' ,
168- 'nullable' in schemaObject && schemaObject . nullable ,
169- ) ,
170- } ;
171- }
172-
173175 if (
174176 'allOf' in schemaObject ||
175177 'oneOf' in schemaObject ||
@@ -264,25 +266,50 @@ export function schemaToType(
264266 } ) ,
265267 ! options . disallowUndefined && hasQuestionToken ,
266268 ) ,
269+ docs,
267270 } ;
268271 }
269272
270- if ( schemaObject . type === 'string' && 'enum' in schemaObject ) {
273+ if ( schemaObject . type === 'integer' || schemaObject . type === 'number' ) {
271274 return {
272275 name,
273276 hasQuestionToken,
277+ type : withNullUnion (
278+ // eslint-disable-next-line no-template-curly-in-string
279+ options . integerAsStringish ? '`${number}`' : 'number' ,
280+ schemaObject . nullable ,
281+ ) ,
274282 docs,
275283 } ;
284+ }
276285
277- type:
286+ if ( schemaObject . type === 'boolean' ) {
287+ return {
288+ name,
289+ hasQuestionToken,
290+ type : withNullUnion (
291+ options . booleanAsStringish
292+ ? Writers . unionType ( '"true"' , '"false"' )
293+ : 'boolean' ,
294+ schemaObject . nullable ,
295+ ) ,
278296 docs,
279297 } ;
298+ }
299+
300+ if ( schemaObject . type === 'string' && 'enum' in schemaObject ) {
301+ return {
302+ name,
303+ hasQuestionToken,
304+ type : maybeWithUndefined (
280305 schemaObject . enum . length === 1
281306 ? JSON . stringify ( schemaObject . enum [ 0 ] )
282307 : Writers . unionType (
283308 // @ts -expect-error
284309 ...schemaObject . enum . map ( ( e ) => JSON . stringify ( e ) ) ,
285310 ) ,
311+ ! options . disallowUndefined && hasQuestionToken ,
312+ ) ,
286313 docs,
287314 } ;
288315 }
@@ -315,6 +342,11 @@ export function registerTypesFromSchema(
315342 typesFile : SourceFile ,
316343 schemaName : string ,
317344 schemaObject : OpenAPIV3 . SchemaObject | OpenAPIV3 . ReferenceObject ,
345+ options : {
346+ disallowUndefined ?: boolean ;
347+ booleanAsStringish ?: boolean ;
348+ integerAsStringish ?: boolean ;
349+ } = { } ,
318350) {
319351 // deal with refs
320352 if ( '$ref' in schemaObject ) {
@@ -470,7 +502,10 @@ export function registerTypesFromSchema(
470502 const typeAlias = typesFile . addTypeAlias ( {
471503 name : pascalCase ( schemaName ) ,
472504 isExported : true ,
473- type : withNullUnion ( 'number' , schemaObject . nullable ) ,
505+ type : withNullUnion (
506+ options ?. integerAsStringish ? '`${number}`' : schemaObject . type ,
507+ schemaObject . nullable ,
508+ ) ,
474509 } ) ;
475510
476511 if ( schemaObject . description ) {
@@ -487,7 +522,12 @@ export function registerTypesFromSchema(
487522 const typeAlias = typesFile . addTypeAlias ( {
488523 name : pascalCase ( schemaName ) ,
489524 isExported : true ,
490- type : withNullUnion ( 'boolean' , schemaObject . nullable ) ,
525+ type : withNullUnion (
526+ options ?. booleanAsStringish
527+ ? Writers . unionType ( '"true"' , '"false"' )
528+ : schemaObject . type ,
529+ schemaObject . nullable ,
530+ ) ,
491531 } ) ;
492532
493533 if ( schemaObject . description ) {
0 commit comments