Skip to content

Commit

Permalink
sort categories by title (except core)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s committed Jan 9, 2024
1 parent 79dd749 commit 54cf821
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
14 changes: 10 additions & 4 deletions core/base-service/openapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,13 @@ function addGlobalProperties(endpoints) {
return paths
}

function services2openapi(services) {
function sortPaths(obj) {
const entries = Object.entries(obj)
entries.sort((a, b) => a[1].get.summary.localeCompare(b[1].get.summary))
return Object.fromEntries(entries)
}

function services2openapi(services, sort) {
const paths = {}
for (const service of services) {
if (service.openApi) {
Expand All @@ -221,10 +227,10 @@ function services2openapi(services) {
}
}
}
return paths
return sort ? sortPaths(paths) : paths
}

function category2openapi(category, services) {
function category2openapi({ category, services, sort = false }) {
const spec = {
openapi: '3.0.0',
info: {
Expand Down Expand Up @@ -334,7 +340,7 @@ function category2openapi(category, services) {
},
},
},
paths: services2openapi(services),
paths: services2openapi(services, sort),
}

return spec
Expand Down
11 changes: 7 additions & 4 deletions core/base-service/openapi.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,13 @@ describe('category2openapi', function () {
it('generates an Open API spec', function () {
expect(
clean(
category2openapi({ name: 'build' }, [
OpenApiService.getDefinition(),
LegacyService.getDefinition(),
]),
category2openapi({
category: { name: 'build' },
services: [
OpenApiService.getDefinition(),
LegacyService.getDefinition(),
],
}),
),
).to.deep.equal(expected)
})
Expand Down
8 changes: 6 additions & 2 deletions scripts/export-openapi-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function writeSpec(filename, spec) {

writeSpec(
path.join(specsPath, `${category.id}.yaml`),
category2openapi(category, services),
category2openapi({ category, services, sort: true }),
)
}

Expand All @@ -44,6 +44,10 @@ function writeSpec(filename, spec) {
)
writeSpec(
path.join(specsPath, '1core.yaml'),
category2openapi({ name: 'Core' }, coreServices),
category2openapi({
category: { name: 'Core' },
services: coreServices,
sort: false,
}),
)
})()

0 comments on commit 54cf821

Please sign in to comment.