1
+ /* eslint-disable no-use-before-define */
1
2
import JsonRefs from 'json-refs' ;
2
3
import converter from 'swagger2openapi' ;
3
4
4
- export default async function ProcessSpec ( specUrl ) {
5
- let jsonParsedSpec ;
6
- let convertedSpec ;
7
- let resolvedRefSpec ;
8
- let resolveOptions ;
9
- let specLocation = '' ; let
10
- url ;
11
5
12
- const convertOptions = { patch : true , warnOnly : true } ;
13
- try {
14
- // JsonRefs cant load yaml files, so first use converter
15
- if ( typeof specUrl === 'string' ) {
16
- // resolvedRefSpec = await JsonRefs.resolveRefsAt(specUrl, resolveOptions);
17
- convertedSpec = await converter . convertUrl ( specUrl , convertOptions ) ;
18
- specLocation = convertedSpec . source . trim ( ) ;
19
- if ( specLocation . startsWith ( '/' ) ) {
20
- url = new URL ( `.${ specLocation } ` , window . location . href ) ;
21
- specLocation = url . pathname ;
22
- }
23
- } else {
24
- // resolvedRefSpec = await JsonRefs.resolveRefs(specUrl, resolveOptions);
25
- convertedSpec = await converter . convertObj ( specUrl , convertOptions ) ;
26
- url = new URL ( window . location . href ) ;
27
- specLocation = url . pathname ;
28
- }
29
- // convertedSpec = await converter.convertObj(resolvedRefSpec.resolved, convertOptions);
30
- resolveOptions = {
31
- resolveCirculars : false ,
32
- location : specLocation , // location is important to specify to resolve relative external file references when using JsonRefs.resolveRefs() which takes an JSON object
33
- } ;
34
- resolvedRefSpec = await JsonRefs . resolveRefs ( convertedSpec . openapi , resolveOptions ) ;
35
- // jsonParsedSpec = convertedSpec.openapi;
36
- jsonParsedSpec = resolvedRefSpec . resolved ;
37
- } catch ( err ) {
38
- console . info ( '%c There was an issue while parsing the spec %o ' , 'color:orangered' , err ) ; // eslint-disable-line no-console
39
- }
6
+ /*
7
+ async function groupByPath(openApiSpec) {
40
8
41
- const openApiSpec = jsonParsedSpec ;
9
+ }
10
+ */
11
+
12
+ function groupByTags ( openApiSpec ) {
42
13
const methods = [ 'get' , 'put' , 'post' , 'delete' , 'patch' , 'options' , 'head' ] ;
43
14
const tags = [ ] ;
44
- let totalPathCount = 0 ;
15
+
45
16
// For each path find the tag and push it into the corrosponding tag
46
17
for ( const path in openApiSpec . paths ) {
47
18
const commonParams = openApiSpec . paths [ path ] . parameters ;
@@ -144,17 +115,58 @@ export default async function ProcessSpec(specUrl) {
144
115
commonSummary : commonPathProp . summary ,
145
116
commonDescription : commonPathProp . description ,
146
117
} ) ;
147
- totalPathCount ++ ;
148
118
}
149
119
} ) ; // End of Methods
150
120
}
151
121
122
+ tags . sort ( ( a , b ) => ( a . name < b . name ? - 1 : ( a . name > b . name ? 1 : 0 ) ) ) ;
123
+ return tags ;
124
+ }
125
+
126
+ export default async function ProcessSpec ( specUrl ) {
127
+ let jsonParsedSpec ;
128
+ let convertedSpec ;
129
+ let resolvedRefSpec ;
130
+ let resolveOptions ;
131
+ let specLocation = '' ;
132
+ let url ;
133
+
134
+ const convertOptions = { patch : true , warnOnly : true } ;
135
+ try {
136
+ // JsonRefs cant load yaml files, so first use converter
137
+ if ( typeof specUrl === 'string' ) {
138
+ // resolvedRefSpec = await JsonRefs.resolveRefsAt(specUrl, resolveOptions);
139
+ convertedSpec = await converter . convertUrl ( specUrl , convertOptions ) ;
140
+ specLocation = convertedSpec . source . trim ( ) ;
141
+ if ( specLocation . startsWith ( '/' ) ) {
142
+ url = new URL ( `.${ specLocation } ` , window . location . href ) ;
143
+ specLocation = url . pathname ;
144
+ }
145
+ } else {
146
+ // resolvedRefSpec = await JsonRefs.resolveRefs(specUrl, resolveOptions);
147
+ convertedSpec = await converter . convertObj ( specUrl , convertOptions ) ;
148
+ url = new URL ( window . location . href ) ;
149
+ specLocation = url . pathname ;
150
+ }
151
+ // convertedSpec = await converter.convertObj(resolvedRefSpec.resolved, convertOptions);
152
+ resolveOptions = {
153
+ resolveCirculars : false ,
154
+ location : specLocation , // location is important to specify to resolve relative external file references when using JsonRefs.resolveRefs() which takes an JSON object
155
+ } ;
156
+ resolvedRefSpec = await JsonRefs . resolveRefs ( convertedSpec . openapi , resolveOptions ) ;
157
+ // jsonParsedSpec = convertedSpec.openapi;
158
+ jsonParsedSpec = resolvedRefSpec . resolved ;
159
+ } catch ( err ) {
160
+ console . info ( '%c There was an issue while parsing the spec %o ' , 'color:orangered' , err ) ; // eslint-disable-line no-console
161
+ }
162
+
152
163
let securitySchemes = { } ;
153
164
let servers = [ ] ;
165
+ const tags = groupByTags ( jsonParsedSpec ) ;
154
166
155
- securitySchemes = ( openApiSpec . components ? openApiSpec . components . securitySchemes : { } ) ;
156
- if ( openApiSpec . servers ) {
157
- openApiSpec . servers . map ( ( v ) => {
167
+ securitySchemes = ( jsonParsedSpec . components ? jsonParsedSpec . components . securitySchemes : { } ) ;
168
+ if ( jsonParsedSpec . servers ) {
169
+ jsonParsedSpec . servers . map ( ( v ) => {
158
170
const tempUrl = v . url . trim ( ) . toLowerCase ( ) ;
159
171
if ( v . url && tempUrl . substr ( 0 , 4 ) !== 'http' ) {
160
172
if ( tempUrl . substr ( 0 , 2 ) === '//' ) {
@@ -165,19 +177,16 @@ export default async function ProcessSpec(specUrl) {
165
177
}
166
178
} ) ;
167
179
} else {
168
- openApiSpec . servers = [ { url : window . location . origin } ] ;
180
+ jsonParsedSpec . servers = [ { url : window . location . origin } ] ;
169
181
}
170
- servers = openApiSpec . servers ;
171
-
172
- tags . sort ( ( a , b ) => ( a . name < b . name ? - 1 : ( a . name > b . name ? 1 : 0 ) ) ) ;
182
+ servers = jsonParsedSpec . servers ;
173
183
const parsedSpec = {
174
- info : openApiSpec . info ,
184
+ info : jsonParsedSpec . info ,
175
185
tags,
176
- externalDocs : openApiSpec . externalDocs ,
186
+ externalDocs : jsonParsedSpec . externalDocs ,
177
187
securitySchemes,
178
188
servers, // In swagger 2, its generated from schemes, host and basePath properties
179
- basePath : openApiSpec . basePath , // Only available in swagger V2
180
- totalPathCount,
189
+ basePath : jsonParsedSpec . basePath , // Only available in swagger V2
181
190
} ;
182
191
return parsedSpec ;
183
192
}
0 commit comments