@@ -7,18 +7,6 @@ function getBSONType(property: JSONSchema): string | string[] | undefined {
77 return property . bsonType || property . type ;
88}
99
10- function isBSONObjectProperty ( property : JSONSchema ) : boolean {
11- return getBSONType ( property ) === 'object' ;
12- }
13-
14- function isBSONArrayProperty ( property : JSONSchema ) : boolean {
15- return getBSONType ( property ) === 'array' ;
16- }
17-
18- function isBSONPrimitive ( property : JSONSchema ) : boolean {
19- return ! ( isBSONArrayProperty ( property ) || isBSONObjectProperty ( property ) ) ;
20- }
21-
2210function assertIsDefined < T > (
2311 value : T ,
2412 message : string
@@ -127,28 +115,26 @@ function toTypescriptType(
127115) : string {
128116 const eachFieldDefinition = Object . entries ( properties ) . map (
129117 ( [ propertyName , schema ] ) => {
130- if ( isBSONPrimitive ( schema ) ) {
131- return `${ indentSpaces ( indent ) } ${ propertyName } ?: ${ [
132- ...uniqueTypes ( schema ) ,
133- ] . join ( ' | ' ) } `;
118+ switch ( getBSONType ( schema ) ) {
119+ case 'array' :
120+ assertIsDefined ( schema . items , 'schema.items must be defined' ) ;
121+ return `${ indentSpaces ( indent ) } ${ propertyName } ?: ${ arrayType ( [
122+ ...uniqueTypes ( schema . items ) ,
123+ ] ) } `;
124+ case 'object' :
125+ assertIsDefined (
126+ schema . properties ,
127+ 'schema.properties must be defined'
128+ ) ;
129+ return `${ indentSpaces ( indent ) } ${ propertyName } ?: ${ toTypescriptType (
130+ schema . properties as Record < string , JSONSchema > ,
131+ indent + 1
132+ ) } `;
133+ default :
134+ return `${ indentSpaces ( indent ) } ${ propertyName } ?: ${ [
135+ ...uniqueTypes ( schema ) ,
136+ ] . join ( ' | ' ) } `;
134137 }
135-
136- if ( isBSONArrayProperty ( schema ) ) {
137- assertIsDefined ( schema . items , 'schema.items must be defined' ) ;
138- return `${ indentSpaces ( indent ) } ${ propertyName } ?: ${ arrayType ( [
139- ...uniqueTypes ( schema . items ) ,
140- ] ) } `;
141- }
142-
143- if ( isBSONObjectProperty ( schema ) ) {
144- assertIsDefined ( schema . properties , 'schema.properties must be defined' ) ;
145- return `${ indentSpaces ( indent ) } ${ propertyName } ?: ${ toTypescriptType (
146- schema . properties as Record < string , JSONSchema > ,
147- indent + 1
148- ) } `;
149- }
150-
151- throw new Error ( 'We should never get here' ) ;
152138 }
153139 ) ;
154140
0 commit comments