nestjs / swagger

OpenAPI (Swagger) module for Nest framework (node.js) :earth_americas:

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Monorepo support for the Swagger module

petetnt opened this issue · comments

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

We have a NestJS [monorepo] and we would like to document the modules with @nestjs/swagger. The current documentation and implementation (https://docs.nestjs.com/openapi/introduction) only refers importing a AppModule from the root, which isn't useful in monorepo scenarios.

This feature has been asked at least twice (see #2444, #2905), both times the issues has been closed and moved to Discord as questions: Not sure if has been answered, but here's the same issues in a feature request form.

Describe the solution you'd like

Instead of building the documentation from a single AppModule, I'd like to point multiple modules towards it and optionally add adding or overwriting path for the routes, eg.

const document = SwaggerModule.createDocument([FooModule, BarModule], config, {
  deepScanRoutes: true,
});
 
// with paths
const document = SwaggerModule.createDocument([
  {
     path: 'foo',
     module: FooModule,
  }, {
     path: 'bar',
     module: BarModule,
  }],
  config,
  {
    deepScanRoutes: true,
  }
);

Alternatively as the API there is similar to the RoutesModule#routes configuration, one acceptable way could be allowing routes module as the first option

const routes: Routes =  {
     path: 'foo',
     module: FooModule,
  }, {
     path: 'bar',
     module: BarModule,
};

const document = SwaggerModule.createDocument(RoutesModule.register(routes), config, {
  deepScanRoutes: true,
});

Teachability, documentation, adoption, migration strategy

What is the motivation / use case for changing the behavior?

We can, in theory, build the required structure manually but it requires manually building and synchronizing the whole tree structures when routes are added, removed or modified, at which point it is as easy to just build the required OpenAPI-specification yourself, making @nestjs/swagger moot.

By adding this feature, we monorepo users can enjoy the benefits of @nestjs/swagger module too.

only refers importing a AppModule from the root, which isn't useful in monorepo scenarios.

Not sure if I'm following. Every app (regardless of whether it's a monorepo or not) should have a root module (the module you pass to the create() method as an argument. This has nothing to do with the structure of your repository.

This package supports monorepos and it works in the exact same way as with single-project repositories.