NestJS Alphabetize Endpoints in SwaggerUI

Viewed 1973

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 Answers

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.

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',
      },
    });
Related