PeS
PeS

Reputation: 4039

Nestjs Swagger - Publishing different API docs on separate routes

I am building an app that has public API and internal one. I would like to publish docs for these to different routes. I thought this would be accomplished by adding only certain tags to document (addTag) but after further reading and experiments it does not do the job.

The docs always contain everything, all documented endpoints from all modules.

Is this even possible? If so, how?

I don't believe code is necessary but FWIW:

const pubOptions = new DocumentBuilder()
    .setTitle('Pub API Docs')
    .setDescription('Blah blah API documentation')
    .setVersion(p.version)
    .addBearerAuth()
    .addTag('public-app')
    .build();
const document = SwaggerModule.createDocument(app, pubOptions);
SwaggerModule.setup('public-api', app, document);

const internalOptions = new DocumentBuilder()
    .setTitle('Internal API Docs')
    .setDescription('Blah blah API documentation')
    .setVersion(p.version)
    .addBearerAuth()
    .addTag('internal')
    .build();
const iDocument = SwaggerModule.createDocument(app, internalOptions);
SwaggerModule.setup('internal-api', app, iDocument);

Upvotes: 9

Views: 11477

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70101

You need to tell the SwaggerModule.createDocument function what modules to include in the swagger that is to be composed. Here are the related docs. As a third parameter, you can pass in an object with an include property that contains an array of modules. These modules are the ones related to the swagger document that is about to be built.

Upvotes: 11

Related Questions