Skip to content

Commit f3e24d3

Browse files
committed
refactoring - exracted groupByTag into its own function
1 parent fffe1f3 commit f3e24d3

File tree

1 file changed

+58
-49
lines changed

1 file changed

+58
-49
lines changed

src/utils/spec-parser.js

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,18 @@
1+
/* eslint-disable no-use-before-define */
12
import JsonRefs from 'json-refs';
23
import converter from 'swagger2openapi';
34

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;
115

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) {
408
41-
const openApiSpec = jsonParsedSpec;
9+
}
10+
*/
11+
12+
function groupByTags(openApiSpec) {
4213
const methods = ['get', 'put', 'post', 'delete', 'patch', 'options', 'head'];
4314
const tags = [];
44-
let totalPathCount = 0;
15+
4516
// For each path find the tag and push it into the corrosponding tag
4617
for (const path in openApiSpec.paths) {
4718
const commonParams = openApiSpec.paths[path].parameters;
@@ -144,17 +115,58 @@ export default async function ProcessSpec(specUrl) {
144115
commonSummary: commonPathProp.summary,
145116
commonDescription: commonPathProp.description,
146117
});
147-
totalPathCount++;
148118
}
149119
}); // End of Methods
150120
}
151121

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+
152163
let securitySchemes = {};
153164
let servers = [];
165+
const tags = groupByTags(jsonParsedSpec);
154166

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) => {
158170
const tempUrl = v.url.trim().toLowerCase();
159171
if (v.url && tempUrl.substr(0, 4) !== 'http') {
160172
if (tempUrl.substr(0, 2) === '//') {
@@ -165,19 +177,16 @@ export default async function ProcessSpec(specUrl) {
165177
}
166178
});
167179
} else {
168-
openApiSpec.servers = [{ url: window.location.origin }];
180+
jsonParsedSpec.servers = [{ url: window.location.origin }];
169181
}
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;
173183
const parsedSpec = {
174-
info: openApiSpec.info,
184+
info: jsonParsedSpec.info,
175185
tags,
176-
externalDocs: openApiSpec.externalDocs,
186+
externalDocs: jsonParsedSpec.externalDocs,
177187
securitySchemes,
178188
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
181190
};
182191
return parsedSpec;
183192
}

0 commit comments

Comments
 (0)