Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);

like image 336
PeS Avatar asked Jan 24 '20 19:01

PeS


1 Answers

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.

like image 161
Jay McDoniel Avatar answered Sep 27 '22 21:09

Jay McDoniel