Skip to content

Commit 1b6ea5d

Browse files
quangkhoabrendo
authored andcommitted
Improve default sorting (temando#103)
1 parent 7f7d0e7 commit 1b6ea5d

File tree

5 files changed

+49
-25
lines changed

5 files changed

+49
-25
lines changed

src/components/Method/Method.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import './Method.scss'
1212
export default class Method extends PureComponent {
1313
render () {
1414
const { method } = this.props
15-
const { summary, description, parameters, request, responses } = method
15+
const { title, description, parameters, request, responses } = method
1616

1717
return (
1818
<ScrollableAnchor id={method.link}>
1919
<div className='method'>
20-
<h3>{summary}</h3>
20+
<h3>{title}</h3>
2121
<div className='method-body'>
2222
{description && <Description description={description} />}
2323
{parameters && this.renderParameters(parameters)}
@@ -74,5 +74,13 @@ export default class Method extends PureComponent {
7474
}
7575

7676
Method.propTypes = {
77-
method: PropTypes.object
77+
method: PropTypes.shape({
78+
type: PropTypes.string,
79+
title: PropTypes.string,
80+
link: PropTypes.string,
81+
description: PropTypes.string,
82+
parameters: PropTypes.object,
83+
request: PropTypes.object,
84+
responses: PropTypes.array
85+
})
7886
}

src/parser/open-api/v3/open-api-v3-parser.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
import refParser from 'json-schema-ref-parser'
22
import getUIReadySchema from '../schemaParser'
3-
import { sortByAlphabet, httpMethodSort } from '../../sorting'
3+
import { sortByUIMethod } from '../../sorting'
44

55
/**
66
* Construct navigation and services ready to be consumed by the UI
77
*
88
* @param {Array} tags
99
* @param {Object} paths
10-
* @param {Function} pathSortFunction
11-
* @param {Function} methodSortFunction
1210
*
1311
* @return {{navigation: [], services: []}}
1412
*/
15-
function getUINavigationAndServices (tags, paths, pathSortFunction = sortByAlphabet, methodSortFunction = httpMethodSort) {
13+
function getUINavigationAndServices (tags, paths) {
1614
const navigation = []
1715
const services = []
1816

1917
for (let i = 0; i < tags.length; i++) {
2018
const tag = tags[i]
2119
const navigationMethods = []
2220
const servicesMethods = []
23-
const pathIds = Object.keys(paths).sort(pathSortFunction)
21+
const pathIds = Object.keys(paths)
2422

2523
for (let j = 0; j < pathIds.length; j++) {
2624
const pathId = pathIds[j]
2725
const path = paths[pathId]
28-
const methodTypes = Object.keys(path).sort(methodSortFunction)
26+
const methodTypes = Object.keys(path)
2927

3028
for (let k = 0; k < methodTypes.length; k++) {
3129
const methodType = methodTypes[k]
@@ -47,8 +45,8 @@ function getUINavigationAndServices (tags, paths, pathSortFunction = sortByAlpha
4745
const uiResponses = getUIResponses(method.responses)
4846
const servicesMethod = {
4947
type: methodType,
48+
title: method.summary,
5049
link,
51-
summary: method.summary,
5250
request: uiRequest,
5351
responses: uiResponses
5452
}
@@ -68,16 +66,16 @@ function getUINavigationAndServices (tags, paths, pathSortFunction = sortByAlpha
6866

6967
navigation.push({
7068
title: tag,
71-
methods: navigationMethods
69+
methods: navigationMethods.sort(sortByUIMethod)
7270
})
7371

7472
services.push({
7573
title: tag,
76-
methods: servicesMethods
74+
methods: servicesMethods.sort(sortByUIMethod)
7775
})
7876
}
7977

80-
return { navigation, services }
78+
return {navigation, services}
8179
}
8280

8381
/**
@@ -162,9 +160,9 @@ function getUIParametersForLocation (parameters, location) {
162160
// handles this. Property should eventually be split and this won't be
163161
// necessary...
164162
if (parameter.type) {
165-
uiParameter.type = [ parameter.type ]
163+
uiParameter.type = [parameter.type]
166164
} else if (parameter.schema && parameter.schema.type) {
167-
uiParameter.type = [ parameter.schema.type ]
165+
uiParameter.type = [parameter.schema.type]
168166
}
169167

170168
if (parameter.schema && parameter.schema.default !== undefined) {
@@ -307,7 +305,7 @@ export default async function getUIReadyDefinition (openApiV3) {
307305
const tags = getTags(paths)
308306

309307
// Construction navigation and services
310-
const { navigation, services } = getUINavigationAndServices(tags, paths)
308+
const {navigation, services} = getUINavigationAndServices(tags, paths)
311309

312310
const definition = {
313311
title: info.title,

src/parser/sorting.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,32 @@ export function sortByAlphabet (str1, str2) {
3030
/**
3131
* Sort function
3232
*
33-
* @param {String} inMethod1
34-
* @param {String} inMethod2
33+
* @param {String} type1
34+
* @param {String} type2
3535
*
3636
* @return {number}
3737
*/
38-
export function httpMethodSort (inMethod1, inMethod2) {
39-
const method1 = inMethod1.toUpperCase()
40-
const method2 = inMethod2.toUpperCase()
38+
export function sortByHttpMethod (type1, type2) {
39+
const normalisedType1 = type1.toUpperCase()
40+
const normalisedType2 = type2.toUpperCase()
4141

42-
return methodWeights[method1] - methodWeights[method2]
42+
return methodWeights[normalisedType1] - methodWeights[normalisedType2]
43+
}
44+
45+
/**
46+
* Sort function
47+
*
48+
* @param {type, title} method1
49+
* @param {type, title} method2
50+
*
51+
* return {number}
52+
*/
53+
export function sortByUIMethod (method1, method2) {
54+
// Sort by method type first
55+
if (method1.type !== method2.type) {
56+
return sortByHttpMethod(method1.type, method2.type)
57+
}
58+
59+
// Then by method title
60+
return sortByAlphabet(method1.title, method2.title)
4361
}

test/parser/open-api/v3/data/outputs/accounts.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
{
2727
"type": "get",
2828
"link": "/accounts/{id}/users/get",
29-
"summary": "Get all the users of a given account",
29+
"title": "Get all the users of a given account",
3030
"parameters": {
3131
"path": [
3232
{
@@ -368,7 +368,7 @@
368368
{
369369
"type": "post",
370370
"link": "/accounts/{id}/users/post",
371-
"summary": "Create a user under the given account",
371+
"title": "Create a user under the given account",
372372
"parameters": {
373373
"path": [
374374
{

test/parser/open-api/v3/data/outputs/carrier.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
{
2222
"type": "get",
2323
"link": "/carriers/get",
24-
"summary": "Returns a list of integrations available for registration",
24+
"title": "Returns a list of integrations available for registration",
2525
"request": {
2626
"description": "Returns a list of integrations available for registration"
2727
},

0 commit comments

Comments
 (0)