NestJS Alphabetize Endpoints in SwaggerUI

6.6k views Asked by At

This SO answer shows that SwaggerUi will sort endpoints alphabetically if it is passed apisSorter : "alpha" when instantiated. In NestJS the config options are passed in the SwaggerModule.createDocument. I cannot see where in the config eg here I can pass this.

2

There are 2 answers

1
Jay McDoniel On BEST ANSWER

You can pass it as the fourth parameter to the SwaggerModule.setup method like so:

const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('docs', app, document, {
    swaggerOptions: {
      tagsSorter: 'alpha',
      operationsSorter: 'alpha',
    },
  });

swaggerOptions is untyped which is why you just have to know what you're passing. Found the answer in the discord server so hopefully that link doesn't expire.

2
volf On

To anyone trying @midopa's solution for FastifySwagger, pass the tagsSorter and operationsSorter values to uiConfig instead of swaggerOptions.

    const doc = SwaggerModule.createDocument(app, config);
    SwaggerModule.setup('docs', app, doc, {
    uiConfig: {
      tagsSorter: 'alpha',
      operationsSorter: 'alpha',
      },
    });

NOTE: This is for @nestjs/swagger version 6 or less. For v7 or above, swaggerOptions will work just fine.