Skip to content

Commit

Permalink
feat: change sort option
Browse files Browse the repository at this point in the history
  • Loading branch information
wahyubucil committed Nov 1, 2023
1 parent 4f36d9e commit 700b134
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/patch-nest-swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface Type<T = any> extends Function {

interface Options {
/** @default default */
sort?: 'default' | 'alpha' | 'localeCompare'
schemasSort?: 'default' | 'alpha' | 'localeCompare'
}

interface Modules {
Expand All @@ -31,7 +31,7 @@ export function patchNestjsSwagger(
options: Options = {},
modules: Modules = {},
) {
const { sort = 'default' } = options
const { schemasSort = 'default' } = options
const {
schemaObjectFactoryModule = require('@nestjs/swagger/dist/services/schema-object-factory'),
swaggerScannerModule = require('@nestjs/swagger/dist/swagger-scanner'),
Expand Down Expand Up @@ -82,15 +82,15 @@ export function patchNestjsSwagger(
...doc.components?.schemas,
}

if (sort === 'alpha') {
if (schemasSort === 'alpha') {
const sortedSchemas = Object.entries(schemas).sort(([aKey], [bKey]) => {
if (aKey < bKey) return -1
if (aKey > bKey) return 1
return 0
})

openAPIObject.components.schemas = Object.fromEntries(sortedSchemas)
} else if (sort === 'localeCompare') {
} else if (schemasSort === 'localeCompare') {
const sortedSchemas = Object.entries(schemas).sort(([aKey], [bKey]) =>
aKey.localeCompare(bKey),
)
Expand Down

0 comments on commit 700b134

Please sign in to comment.