Skip to content

Commit 7c2712b

Browse files
committed
provided option to sort endpoints by method
1 parent 6618c09 commit 7c2712b

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

docs/api.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ <h2> Attributes</h2>
8787
<td class="gray"><span class='bold dark-gray'>Allowed:<span class='blue'> true, false</span></span> To list APIs sorted by tags</td>
8888
<td class='default-col'>false</td>
8989
</tr>
90+
<tr>
91+
<td class="attr-col mono bold right">sort-endpoints-by </td>
92+
<td class="gray"><span class='bold dark-gray'>Allowed:<span class='blue'> path, method</span></span> Sort endpoints within each tags by path or method </td>
93+
<td class='default-col'>path</td>
94+
</tr>
9095
<tr>
9196
<td class="mono bold right">heading-text </td>
9297
<td class="gray">Heading Text on top-left corner </td>

package.json

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,13 @@
3232
"lit-element": "2.2.1",
3333
"lit-html": "1.0.0",
3434
"marked": "^0.8.0",
35-
<<<<<<< HEAD
36-
"snyk": "^1.278.0",
3735
"swagger2openapi": "^5.3.2"
38-
=======
39-
"swagger2openapi": "^5.3.2",
40-
"snyk": "^1.278.2"
41-
>>>>>>> 8e79a50b5783960d72b314a10590f8bd5e97908e
4236
},
4337
"scripts": {
4438
"build": "NODE_ENV=production webpack",
4539
"serve": "webpack-dev-server --mode=development",
4640
"lint": "./node_modules/eslint/bin/eslint.js ./src/**",
47-
"lint-fix": "./node_modules/eslint/bin/eslint.js --fix ./src/**",
48-
"snyk-protect": "snyk protect",
49-
"prepare": "yarn run snyk-protect"
41+
"lint-fix": "./node_modules/eslint/bin/eslint.js --fix ./src/**"
5042
},
5143
"devDependencies": {
5244
"@babel/core": "^7.8.0",
@@ -73,5 +65,5 @@
7365
"webpack-cli": "^3.3.10",
7466
"webpack-dev-server": "^3.10.1"
7567
},
76-
"snyk": true
68+
"snyk": false
7769
}

src/rapidoc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export default class RapiDoc extends LitElement {
3838
// Spec
3939
specUrl: { type: String, attribute: 'spec-url' },
4040
sortTags: { type: String, attribute: 'sort-tags' },
41+
sortEndpointsBy: { type: String, attribute: 'sort-endpoints-by' },
4142
specFile: { type: String, attribute: false },
4243

4344
// UI Layouts
@@ -100,6 +101,7 @@ export default class RapiDoc extends LitElement {
100101
if (!this.apiKeyName) { this.apiKeyName = ''; }
101102
if (!this.apiKeyValue) { this.apiKeyValue = ''; }
102103
if (!this.sortTags || !'true false'.includes(this.sortTags)) { this.sortTags = 'false'; }
104+
if (!this.sortEndpointsBy || !'method path'.includes(this.sortEndpointsBy)) { this.sortEndpointsBy = 'path'; }
103105
}
104106

105107
// Cleanup
@@ -684,6 +686,7 @@ export default class RapiDoc extends LitElement {
684686
const spec = await ProcessSpec(
685687
specUrl,
686688
this.sortTags === 'true',
689+
this.getAttribute('sort-endpoints-by'),
687690
this.getAttribute('api-key-name'),
688691
this.getAttribute('api-key-location'),
689692
this.getAttribute('api-key-value'),

src/utils/spec-parser.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import JsonRefs from 'json-refs';
33
import converter from 'swagger2openapi';
44

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 = '') {
66
let jsonParsedSpec;
77
let convertedSpec;
88
let resolvedRefSpec;
@@ -42,7 +42,7 @@ export default async function ProcessSpec(specUrl, sortTags = false, attrApiKey
4242
// const pathGroups = groupByPaths(jsonParsedSpec);
4343

4444
// Tags
45-
const tags = groupByTags(jsonParsedSpec, sortTags);
45+
const tags = groupByTags(jsonParsedSpec, sortTags, sortEndpointsBy);
4646

4747
// Security Scheme
4848
const securitySchemes = [];
@@ -125,8 +125,8 @@ function groupByPaths(openApiSpec) {
125125
}
126126
*/
127127

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
130130
const tags = openApiSpec.tags && Array.isArray(openApiSpec.tags)
131131
? openApiSpec.tags.map((v) => ({
132132
show: true,
@@ -242,12 +242,21 @@ function groupByTags(openApiSpec, sortTags = false) {
242242
}); // End of Methods
243243
}
244244

245-
// sort paths within each tags;
245+
// sort paths by methods or path within each tags;
246246
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;
253262
}

0 commit comments

Comments
 (0)