@@ -37,9 +37,8 @@ function capitalize(str: string): string {
37
37
}
38
38
39
39
function camelCase ( name : string ) : string {
40
- return name . replace (
41
- / ( - | _ | \. | \s ) + \w / g,
42
- ( letter ) : string => letter . toUpperCase ( ) . replace ( / [ ^ 0 - 9 a - z ] / gi, '' )
40
+ return name . replace ( / ( - | _ | \. | \s ) + \w / g, ( letter ) : string =>
41
+ letter . toUpperCase ( ) . replace ( / [ ^ 0 - 9 a - z ] / gi, '' )
43
42
) ;
44
43
}
45
44
@@ -68,6 +67,8 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
68
67
function getType ( definition : Swagger2Definition , nestedName : string ) : string {
69
68
const { $ref, items, type, ...value } = definition ;
70
69
70
+ const nextInterface = camelCase ( nestedName ) ; // if this becomes an interface, it’ll need to be camelCased
71
+
71
72
const DEFAULT_TYPE = 'any' ;
72
73
73
74
if ( $ref ) {
@@ -90,8 +91,9 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
90
91
if ( TYPES [ items . type ] ) {
91
92
return `${ TYPES [ items . type ] } []` ;
92
93
}
93
- queue . push ( [ nestedName , items ] ) ;
94
- return `${ nestedName } []` ;
94
+ // If this is an array of items, let’s add it to the stack for later
95
+ queue . push ( [ nextInterface , items ] ) ;
96
+ return `${ nextInterface } []` ;
95
97
}
96
98
97
99
if ( Array . isArray ( value . oneOf ) ) {
@@ -100,8 +102,8 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
100
102
101
103
if ( value . properties ) {
102
104
// If this is a nested object, let’s add it to the stack for later
103
- queue . push ( [ nestedName , { $ref, items, type, ...value } ] ) ;
104
- return nestedName ;
105
+ queue . push ( [ nextInterface , { $ref, items, type, ...value } ] ) ;
106
+ return nextInterface ;
105
107
}
106
108
107
109
if ( type ) {
@@ -121,17 +123,15 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
121
123
122
124
// Include allOf, if specified
123
125
if ( Array . isArray ( allOf ) ) {
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
- }
126
+ allOf . forEach ( ( item ) : void => {
127
+ // Add “implements“ if this references other items
128
+ if ( item . $ref ) {
129
+ const [ refName ] = getRef ( item . $ref ) ;
130
+ includes . push ( refName ) ;
131
+ } else if ( item . properties ) {
132
+ allProperties = { ...allProperties , ...item . properties } ;
133
133
}
134
- ) ;
134
+ } ) ;
135
135
}
136
136
137
137
// If nothing’s here, let’s skip this one.
@@ -149,28 +149,26 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
149
149
output . push ( `export interface ${ shouldCamelCase ? camelCase ( ID ) : ID } ${ isExtending } {` ) ;
150
150
151
151
// Populate interface
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
- }
164
-
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
- }
152
+ Object . entries ( allProperties ) . forEach ( ( [ key , value ] ) : void => {
153
+ const optional = ! Array . isArray ( required ) || required . indexOf ( key ) === - 1 ;
154
+ const formattedKey = shouldCamelCase ? camelCase ( key ) : key ;
155
+ const name = `${ sanitize ( formattedKey ) } ${ optional ? '?' : '' } ` ;
156
+ const newID = `${ ID } ${ capitalize ( formattedKey ) } ` ;
157
+ const interfaceType = getType ( value , newID ) ;
158
+
159
+ if ( typeof value . description === 'string' ) {
160
+ // Print out descriptions as comments, but only if there’s something there (.*)
161
+ output . push ( `// ${ value . description . replace ( / \n $ / , '' ) . replace ( / \n / g, '\n// ' ) } ` ) ;
162
+ }
170
163
171
- output . push ( `${ name } : ${ interfaceType } ;` ) ;
164
+ // Handle enums in the same definition
165
+ if ( Array . isArray ( value . enum ) ) {
166
+ output . push ( `${ name } : ${ value . enum . map ( option => JSON . stringify ( option ) ) . join ( ' | ' ) } ;` ) ;
167
+ return ;
172
168
}
173
- ) ;
169
+
170
+ output . push ( `${ name } : ${ interfaceType } ;` ) ;
171
+ } ) ;
174
172
175
173
if ( additionalProperties ) {
176
174
if ( ( additionalProperties as boolean ) === true ) {
@@ -188,14 +186,12 @@ function parse(spec: Swagger2, options: Swagger2Options = {}): string {
188
186
}
189
187
190
188
// Begin parsing top-level entries
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
- }
189
+ Object . entries ( definitions ) . forEach ( ( entry ) : void => {
190
+ // Ignore top-level array definitions
191
+ if ( entry [ 1 ] . type === 'object' ) {
192
+ queue . push ( entry ) ;
197
193
}
198
- ) ;
194
+ } ) ;
199
195
queue . sort ( ( a , b ) => a [ 0 ] . localeCompare ( b [ 0 ] ) ) ;
200
196
while ( queue . length > 0 ) {
201
197
buildNextInterface ( ) ;
0 commit comments