@@ -37,7 +37,14 @@ function capitalize(str: string): string {
37
37
}
38
38
39
39
function camelCase ( name : string ) : string {
40
- return name . replace ( / ( - | _ | \. | \s ) + \w / g, letter => letter . toUpperCase ( ) . replace ( / [ ^ 0 - 9 a - z ] / gi, '' ) ) ;
40
+ return name . replace (
41
+ / ( - | _ | \. | \s ) + \w / g,
42
+ ( letter ) : string => letter . toUpperCase ( ) . replace ( / [ ^ 0 - 9 a - z ] / gi, '' )
43
+ ) ;
44
+ }
45
+
46
+ function sanitize ( name : string ) : string {
47
+ return name . includes ( '-' ) ? `'${ name } '` : name ;
41
48
}
42
49
43
50
function parse ( spec : Swagger2 , options : Swagger2Options = { } ) : string {
@@ -88,7 +95,7 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
88
95
}
89
96
90
97
if ( Array . isArray ( value . oneOf ) ) {
91
- return value . oneOf . map ( def => getType ( def , '' ) ) . join ( ' | ' ) ;
98
+ return value . oneOf . map ( ( def ) : string => getType ( def , '' ) ) . join ( ' | ' ) ;
92
99
}
93
100
94
101
if ( value . properties ) {
@@ -114,15 +121,17 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
114
121
115
122
// Include allOf, if specified
116
123
if ( Array . isArray ( allOf ) ) {
117
- allOf . forEach ( item => {
118
- // Add “implements“ if this references other items
119
- if ( item . $ref ) {
120
- const [ refName ] = getRef ( item . $ref ) ;
121
- includes . push ( refName ) ;
122
- } else if ( item . properties ) {
123
- allProperties = { ...allProperties , ...item . properties } ;
124
+ allOf . forEach (
125
+ ( item ) : void => {
126
+ // Add “implements“ if this references other items
127
+ if ( item . $ref ) {
128
+ const [ refName ] = getRef ( item . $ref ) ;
129
+ includes . push ( refName ) ;
130
+ } else if ( item . properties ) {
131
+ allProperties = { ...allProperties , ...item . properties } ;
132
+ }
124
133
}
125
- } ) ;
134
+ ) ;
126
135
}
127
136
128
137
// If nothing’s here, let’s skip this one.
@@ -140,26 +149,28 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
140
149
output . push ( `export interface ${ shouldCamelCase ? camelCase ( ID ) : ID } ${ isExtending } {` ) ;
141
150
142
151
// Populate interface
143
- Object . entries ( allProperties ) . forEach ( ( [ key , value ] ) => {
144
- const optional = ! Array . isArray ( required ) || required . indexOf ( key ) === - 1 ;
145
- const formattedKey = shouldCamelCase ? camelCase ( key ) : key ;
146
- const name = `${ formattedKey } ${ optional ? '?' : '' } ` ;
147
- const newID = `${ ID } ${ capitalize ( formattedKey ) } ` ;
148
- const interfaceType = getType ( value , newID ) ;
149
-
150
- if ( typeof value . description === 'string' ) {
151
- // Print out descriptions as comments, but only if there’s something there (.*)
152
- output . push ( `// ${ value . description . replace ( / \n $ / , '' ) . replace ( / \n / g, '\n// ' ) } ` ) ;
153
- }
152
+ Object . entries ( allProperties ) . forEach (
153
+ ( [ key , value ] ) : void => {
154
+ const optional = ! Array . isArray ( required ) || required . indexOf ( key ) === - 1 ;
155
+ const formattedKey = shouldCamelCase ? camelCase ( key ) : key ;
156
+ const name = `${ sanitize ( formattedKey ) } ${ optional ? '?' : '' } ` ;
157
+ const newID = `${ ID } ${ capitalize ( formattedKey ) } ` ;
158
+ const interfaceType = getType ( value , newID ) ;
159
+
160
+ if ( typeof value . description === 'string' ) {
161
+ // Print out descriptions as comments, but only if there’s something there (.*)
162
+ output . push ( `// ${ value . description . replace ( / \n $ / , '' ) . replace ( / \n / g, '\n// ' ) } ` ) ;
163
+ }
154
164
155
- // Handle enums in the same definition
156
- if ( Array . isArray ( value . enum ) ) {
157
- output . push ( `${ name } : ${ value . enum . map ( option => JSON . stringify ( option ) ) . join ( ' | ' ) } ;` ) ;
158
- return ;
159
- }
165
+ // Handle enums in the same definition
166
+ if ( Array . isArray ( value . enum ) ) {
167
+ output . push ( `${ name } : ${ value . enum . map ( option => JSON . stringify ( option ) ) . join ( ' | ' ) } ;` ) ;
168
+ return ;
169
+ }
160
170
161
- output . push ( `${ name } : ${ interfaceType } ;` ) ;
162
- } ) ;
171
+ output . push ( `${ name } : ${ interfaceType } ;` ) ;
172
+ }
173
+ ) ;
163
174
164
175
if ( additionalProperties ) {
165
176
if ( ( additionalProperties as boolean ) === true ) {
@@ -177,12 +188,14 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
177
188
}
178
189
179
190
// Begin parsing top-level entries
180
- Object . entries ( definitions ) . forEach ( entry => {
181
- // Ignore top-level array definitions
182
- if ( entry [ 1 ] . type === 'object' ) {
183
- queue . push ( entry ) ;
191
+ Object . entries ( definitions ) . forEach (
192
+ ( entry ) : void => {
193
+ // Ignore top-level array definitions
194
+ if ( entry [ 1 ] . type === 'object' ) {
195
+ queue . push ( entry ) ;
196
+ }
184
197
}
185
- } ) ;
198
+ ) ;
186
199
queue . sort ( ( a , b ) => a [ 0 ] . localeCompare ( b [ 0 ] ) ) ;
187
200
while ( queue . length > 0 ) {
188
201
buildNextInterface ( ) ;
0 commit comments