|
2 | 2 | import JsonRefs from 'json-refs';
|
3 | 3 | import converter from 'swagger2openapi';
|
4 | 4 |
|
5 |
| -export default async function ProcessSpec(specUrl, sortTags = false, attrApiKey = '', attrApiKeyLocation = '', attrApiKeyValue = '') { |
| 5 | +export default async function ProcessSpec(specUrl, sortTags = false, sortEndpointsBy, attrApiKey = '', attrApiKeyLocation = '', attrApiKeyValue = '') { |
6 | 6 | let jsonParsedSpec;
|
7 | 7 | let convertedSpec;
|
8 | 8 | let resolvedRefSpec;
|
@@ -42,7 +42,7 @@ export default async function ProcessSpec(specUrl, sortTags = false, attrApiKey
|
42 | 42 | // const pathGroups = groupByPaths(jsonParsedSpec);
|
43 | 43 |
|
44 | 44 | // Tags
|
45 |
| - const tags = groupByTags(jsonParsedSpec, sortTags); |
| 45 | + const tags = groupByTags(jsonParsedSpec, sortTags, sortEndpointsBy); |
46 | 46 |
|
47 | 47 | // Security Scheme
|
48 | 48 | const securitySchemes = [];
|
@@ -125,8 +125,8 @@ function groupByPaths(openApiSpec) {
|
125 | 125 | }
|
126 | 126 | */
|
127 | 127 |
|
128 |
| -function groupByTags(openApiSpec, sortTags = false) { |
129 |
| - const methods = ['get', 'put', 'post', 'delete', 'patch', 'head']; |
| 128 | +function groupByTags(openApiSpec, sortTags = false, sortEndpointsBy) { |
| 129 | + const methods = ['get', 'put', 'post', 'delete', 'patch', 'head']; // this is also used for ordering endpoints by methods |
130 | 130 | const tags = openApiSpec.tags && Array.isArray(openApiSpec.tags)
|
131 | 131 | ? openApiSpec.tags.map((v) => ({
|
132 | 132 | show: true,
|
@@ -242,12 +242,21 @@ function groupByTags(openApiSpec, sortTags = false) {
|
242 | 242 | }); // End of Methods
|
243 | 243 | }
|
244 | 244 |
|
245 |
| - // sort paths within each tags; |
| 245 | + // sort paths by methods or path within each tags; |
246 | 246 | const tagsWithSortedPaths = tags.filter((v) => v.paths && v.paths.length > 0);
|
247 |
| - tagsWithSortedPaths.forEach((v) => { |
248 |
| - if (v.paths) { |
249 |
| - v.paths.sort((a, b) => (a.path < b.path ? -1 : (a.path > b.path ? 1 : 0))); |
250 |
| - } |
251 |
| - }); |
252 |
| - return sortTags ? tagsWithSortedPaths.sort((a, b) => (a.name < b.name ? -1 : (a.name > b.name ? 1 : 0))) : tagsWithSortedPaths; |
| 247 | + if (sortEndpointsBy === 'method') { |
| 248 | + tagsWithSortedPaths.forEach((v) => { |
| 249 | + if (v.paths) { |
| 250 | + // v.paths.sort((a, b) => a.method.localeCompare(b.method)); |
| 251 | + v.paths.sort((a, b) => methods.indexOf(a.method).toString().localeCompare(methods.indexOf(b.method))); |
| 252 | + } |
| 253 | + }); |
| 254 | + } else { |
| 255 | + tagsWithSortedPaths.forEach((v) => { |
| 256 | + if (v.paths) { |
| 257 | + v.paths.sort((a, b) => a.path.localeCompare(b.path)); |
| 258 | + } |
| 259 | + }); |
| 260 | + } |
| 261 | + return sortTags ? tagsWithSortedPaths.sort((a, b) => a.name.localeCompare(b.name)) : tagsWithSortedPaths; |
253 | 262 | }
|
0 commit comments