@@ -7,18 +7,6 @@ function getBSONType(property: JSONSchema): string | string[] | undefined {
7
7
return property . bsonType || property . type ;
8
8
}
9
9
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
-
22
10
function assertIsDefined < T > (
23
11
value : T ,
24
12
message : string
@@ -127,28 +115,26 @@ function toTypescriptType(
127
115
) : string {
128
116
const eachFieldDefinition = Object . entries ( properties ) . map (
129
117
( [ 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 ( ' | ' ) } `;
134
137
}
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' ) ;
152
138
}
153
139
) ;
154
140
0 commit comments