Skip to content

Commit 0eeb52b

Browse files
authored
Merge pull request #3303 from Adrael/feature/apisSorter
Adding apisSorter options
2 parents be7b244 + 28912dd commit 0eeb52b

File tree

6 files changed

+89
-55
lines changed

6 files changed

+89
-55
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ node_modules
55
npm-debug.log*
66
.eslintcache
77
package-lock.json
8+
*.iml

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,17 @@ To use swagger-ui's bundles, you should take a look at the [source of swagger-ui
8282

8383
```javascript
8484
const ui = SwaggerUIBundle({
85-
url: "http://petstore.swagger.io/v2/swagger.json",
86-
dom_id: '#swagger-ui',
87-
presets: [
88-
SwaggerUIBundle.presets.apis,
89-
SwaggerUIStandalonePreset
90-
],
91-
plugins: [
92-
SwaggerUIBundle.plugins.DownloadUrl
93-
],
94-
layout: "StandaloneLayout"
95-
})
85+
url: "http://petstore.swagger.io/v2/swagger.json",
86+
dom_id: '#swagger-ui',
87+
presets: [
88+
SwaggerUIBundle.presets.apis,
89+
SwaggerUIStandalonePreset
90+
],
91+
plugins: [
92+
SwaggerUIBundle.plugins.DownloadUrl
93+
],
94+
layout: "StandaloneLayout"
95+
})
9696
```
9797

9898
#### OAuth2 configuration
@@ -137,6 +137,7 @@ spec | A JSON object describing the OpenAPI Specification. When used, the `url`
137137
validatorUrl | By default, Swagger-UI attempts to validate specs against swagger.io's online validator. You can use this parameter to set a different validator URL, for example for locally deployed validators ([Validator Badge](https://github.com/swagger-api/validator-badge)). Setting it to `null` will disable validation.
138138
dom_id | The id of a dom element inside which SwaggerUi will put the user interface for swagger.
139139
oauth2RedirectUrl | OAuth redirect URL
140+
tagsSorter | Apply a sort to the tag list of each API. It can be 'alpha' (sort by paths alphanumerically) or a function (see [Array.prototype.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) to learn how to write a sort function). Two tag name strings are passed to the sorter for each pass. Default is the order determined by Swagger-UI.
140141
operationsSorter | Apply a sort to the operation list of each API. It can be 'alpha' (sort by paths alphanumerically), 'method' (sort by HTTP method) or a function (see Array.prototype.sort() to know how sort function works). Default is the order returned by the server unchanged.
141142
configUrl | Configs URL
142143
parameterMacro | MUST be a function. Function to set default value to parameters. Accepts two arguments parameterMacro(operation, parameter). Operation and parameter are objects passed for context, both remain immutable

dev-helpers/index.html

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
<style>
1212
html
1313
{
14-
box-sizing: border-box;
15-
overflow: -moz-scrollbars-vertical;
16-
overflow-y: scroll;
14+
box-sizing: border-box;
15+
overflow: -moz-scrollbars-vertical;
16+
overflow-y: scroll;
1717
}
1818
*,
1919
*:before,
2020
*:after
2121
{
22-
box-sizing: inherit;
22+
box-sizing: inherit;
2323
}
2424

2525
body {
@@ -34,7 +34,7 @@
3434
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="position:absolute;width:0;height:0">
3535
<defs>
3636
<symbol viewBox="0 0 20 20" id="unlocked">
37-
<path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V6h2v-.801C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8z"></path>
37+
<path d="M15.8 8H14V5.6C14 2.703 12.665 1 10 1 7.334 1 6 2.703 6 5.6V6h2v-.801C8 3.754 8.797 3 10 3c1.203 0 2 .754 2 2.199V8H4c-.553 0-1 .646-1 1.199V17c0 .549.428 1.139.951 1.307l1.197.387C5.672 18.861 6.55 19 7.1 19h5.8c.549 0 1.428-.139 1.951-.307l1.196-.387c.524-.167.953-.757.953-1.306V9.199C17 8.646 16.352 8 15.8 8z"></path>
3838
</symbol>
3939

4040
<symbol viewBox="0 0 20 20" id="locked">
@@ -70,34 +70,34 @@
7070
<script src="./swagger-ui-bundle.js"> </script>
7171
<script src="./swagger-ui-standalone-preset.js"> </script>
7272
<script>
73-
window.onload = function() {
74-
window["SwaggerUIBundle"] = window["swagger-ui-bundle"]
75-
window["SwaggerUIStandalonePreset"] = window["swagger-ui-standalone-preset"]
76-
// Build a system
77-
const ui = SwaggerUIBundle({
78-
url: "http://petstore.swagger.io/v2/swagger.json",
79-
dom_id: '#swagger-ui',
80-
presets: [
81-
SwaggerUIBundle.presets.apis,
82-
SwaggerUIStandalonePreset
83-
],
84-
plugins: [
85-
SwaggerUIBundle.plugins.DownloadUrl
86-
],
87-
layout: "StandaloneLayout"
88-
})
89-
90-
window.ui = ui
91-
92-
ui.initOAuth({
93-
clientId: "your-client-id",
94-
clientSecret: "your-client-secret-if-required",
95-
realm: "your-realms",
96-
appName: "your-app-name",
97-
scopeSeparator: " ",
98-
additionalQueryStringParams: {}
99-
})
100-
}
73+
window.onload = function() {
74+
window["SwaggerUIBundle"] = window["swagger-ui-bundle"]
75+
window["SwaggerUIStandalonePreset"] = window["swagger-ui-standalone-preset"]
76+
// Build a system
77+
const ui = SwaggerUIBundle({
78+
url: "http://petstore.swagger.io/v2/swagger.json",
79+
dom_id: '#swagger-ui',
80+
presets: [
81+
SwaggerUIBundle.presets.apis,
82+
SwaggerUIStandalonePreset
83+
],
84+
plugins: [
85+
SwaggerUIBundle.plugins.DownloadUrl
86+
],
87+
layout: "StandaloneLayout"
88+
})
89+
90+
window.ui = ui
91+
92+
ui.initOAuth({
93+
clientId: "your-client-id",
94+
clientSecret: "your-client-secret-if-required",
95+
realm: "your-realms",
96+
appName: "your-app-name",
97+
scopeSeparator: " ",
98+
additionalQueryStringParams: {}
99+
})
100+
}
101101
</script>
102102
</body>
103103

src/core/index.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,31 @@ import ApisPreset from "core/presets/apis"
66
import * as AllPlugins from "core/plugins/all"
77
import { parseSeach, filterConfigs } from "core/utils"
88

9-
const CONFIGS = [ "url", "urls", "urls.primaryName", "spec", "validatorUrl", "onComplete", "onFailure", "authorizations", "docExpansion", "maxDisplayedTags", "filter",
10-
"apisSorter", "operationsSorter", "supportedSubmitMethods", "dom_id", "defaultModelRendering", "oauth2RedirectUrl",
11-
"showRequestHeaders", "custom", "modelPropertyMacro", "parameterMacro", "displayOperationId" , "displayRequestDuration"]
9+
const CONFIGS = [
10+
"url",
11+
"urls",
12+
"urls.primaryName",
13+
"spec",
14+
"validatorUrl",
15+
"onComplete",
16+
"onFailure",
17+
"authorizations",
18+
"docExpansion",
19+
"tagsSorter",
20+
"maxDisplayedTags",
21+
"filter",
22+
"operationsSorter",
23+
"supportedSubmitMethods",
24+
"dom_id",
25+
"defaultModelRendering",
26+
"oauth2RedirectUrl",
27+
"showRequestHeaders",
28+
"custom",
29+
"modelPropertyMacro",
30+
"parameterMacro",
31+
"displayOperationId",
32+
"displayRequestDuration",
33+
]
1234

1335
// eslint-disable-next-line no-undef
1436
const { GIT_DIRTY, GIT_COMMIT, PACKAGE_VERSION, HOSTNAME, BUILD_TIME } = buildInfo

src/core/plugins/spec/selectors.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,22 @@ export const operationsWithTags = createSelector(
200200
}
201201
)
202202

203-
export const taggedOperations = ( state ) =>( { getConfigs } ) => {
204-
let { operationsSorter }= getConfigs()
205-
206-
return operationsWithTags(state).map((ops, tag) => {
207-
let sortFn = typeof operationsSorter === "function" ? operationsSorter
208-
: sorters.operationsSorter[operationsSorter]
209-
let operations = !sortFn ? ops : ops.sort(sortFn)
203+
export const taggedOperations = (state) => ({ getConfigs }) => {
204+
let { tagsSorter, operationsSorter } = getConfigs()
205+
return operationsWithTags(state)
206+
.sortBy(
207+
(val, key) => key, // get the name of the tag to be passed to the sorter
208+
(tagA, tagB) => {
209+
let sortFn = (typeof tagsSorter === "function" ? tagsSorter : sorters.tagsSorter[ tagsSorter ])
210+
return (!sortFn ? null : sortFn(tagA, tagB))
211+
}
212+
)
213+
.map((ops, tag) => {
214+
let sortFn = (typeof operationsSorter === "function" ? operationsSorter : sorters.operationsSorter[ operationsSorter ])
215+
let operations = (!sortFn ? ops : ops.sort(sortFn))
210216

211-
return Map({tagDetails: tagDetails(state, tag), operations: operations})})
217+
return Map({ tagDetails: tagDetails(state, tag), operations: operations })
218+
})
212219
}
213220

214221
export const responses = createSelector(

src/core/utils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ export const sorters = {
574574
operationsSorter: {
575575
alpha: (a, b) => a.get("path").localeCompare(b.get("path")),
576576
method: (a, b) => a.get("method").localeCompare(b.get("method"))
577+
},
578+
tagsSorter: {
579+
alpha: (a, b) => a.localeCompare(b)
577580
}
578581
}
579582

0 commit comments

Comments
 (0)