Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prefix url in Swagger module

I am using nest js and swagger as documentation but Swagger module ignores setGlobalPrefix(). in my environemnt the api prefix is API_PREFIX=/api/v2 , I have no problem when testing it with postman cause the endpoints/url does work which is http://localhost:5000/api/v2/user/profile

but the swagger cant get the /api/v2 prefix , the swagger request url is http://localhost:5000/user/profile which is wrong.

Any idea ? Thank you for any help.

settings

```const SWAGGER_PREFIX = '/docs';

async function bootstrap(): Promise<void> {
  const app = await NestFactory.create(AppModule);

  if (!process.env.SWAGGER_ENABLE || process.env.SWAGGER_ENABLE === '1') {
    // eslint-disable-next-line @typescript-eslint/no-use-before-define
    createSwagger(app);
  }

  app.use(bodyParser.json());
  app.use(helmet());
  app.use(
    cors({
      origin: process.env.API_CORS || '*'
    })
  );

  app.setGlobalPrefix(process.env.API_PREFIX || API_DEFAULT_PREFIX);

  const logInterceptor = app.select(CommonModule).get(LogInterceptor);
  app.useGlobalInterceptors(logInterceptor);

  await app.listen(process.env.API_PORT || API_DEFAULT_PORT);
}

function createSwagger(app: INestApplication) {
  const version = require('../package.json').version || '';

  const options = new DocumentBuilder()
    .setTitle(SWAGGER_TITLE)
    .setDescription(SWAGGER_DESCRIPTION)
    .setVersion(version)
    .setBasePath(process.env.API_PREFIX || API_DEFAULT_PREFIX)
    .addBearerAuth()
    .build();

  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup(SWAGGER_PREFIX, app, document);[![enter image description here][1]][1]


//swagger - the request url is http://localhost:5000/user/profile
which is supposed to be  http://localhost:5000/api/v2/user/profile

2 Answers

are you sure that this line

  app.setGlobalPrefix(process.env.API_PREFIX || API_DEFAULT_PREFIX);

and this line, evaluates the same

.setBasePath(process.env.API_PREFIX || API_DEFAULT_PREFIX)
like image 65
Moazzam Arif Avatar answered Sep 02 '25 16:09

Moazzam Arif


You should create the swagger (call to createSwagger in function bootstrap) after having set the global prefix of your api (call to setGlobalPrefix in function bootstrap). It will then find the api prefix on its own a prepend any request with it.

async function bootstrap(): Promise<void> {

    const app = await NestFactory.create(ApplicationModule);

    app.setGlobalPrefix(process.env.API_PREFIX || API_DEFAULT_PREFIX);

    if (!process.env.SWAGGER_ENABLE || process.env.SWAGGER_ENABLE === 'true') {
        createSwagger(app);
    }

    await app.listen(process.env.API_PORT || API_DEFAULT_PORT);
}

function createSwagger(app: INestApplication) {

    const version = require('../package.json').version || '';

    const options = new DocumentBuilder()
        .setTitle(SWAGGER_TITLE)
        .setDescription(SWAGGER_DESCRIPTION)
        .setVersion(version)
        .build();

    const document = SwaggerModule.createDocument(app, options);
    SwaggerModule.setup(SWAGGER_PREFIX, app, document);
}
like image 20
Guillaume Everarts Avatar answered Sep 02 '25 14:09

Guillaume Everarts



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!