简体   繁体   中英

@nestjs/swagger: How to add API URL?

I'm using @nestjs/swagger. Here is the simple example how I'm using it


    const document = SwaggerModule.createDocument(
      app,
      new DocumentBuilder()
        .setTitle('API')
        .setDescription('API')
        .setVersion('1.0')
        .addBearerAuth()
        .build(),
    );

    SwaggerModule.setup('/api/doc', app, document);

Before I could add API url using setHost('URL HERE') but now it is not working. So I want to add api url to which will be sent the requests from the Swagger UI. I'm beginner so sorry if the question is not very smart Thanks!

Looks like you are using @nestjs/swagger version 4***. setHost was removed in version 4*** but was available in version 3***. If it works before I guess the version of @nestjs/swagger was updated in your project.

As you can see here

If you're currently using @nestjs/swagger@3.*, note the following breaking/API changes in version 4.0.

The following decorators have been changed/renamed:

  • @ApiModelProperty is now @ApiProperty
  • @ApiModelPropertyOptional is now @ApiPropertyOptional
  • @ApiResponseModelProperty is now @ApiResponseProperty
  • @ApiImplicitQuery is now @ApiQuery
  • @ApiImplicitParam is now @ApiParam
  • @ApiImplicitBody is now @ApiBody
  • @ApiImplicitHeader is now @ApiHeader
  • @ApiOperation({ title: 'test' }) is now @ApiOperation({ summary: 'test' })
  • @ApiUseTags is now @ApiTags

DocumentBuilder breaking changes (updated method signatures):

  • addTag
  • addBearerAuth
  • addOAuth2
  • setContactEmail is now setContact
  • setHost has been removed
  • setSchemes has been removed (use the addServer instead, eg, addServer('http://'))

The following methods have been added:

  • addServer
  • addApiKey
  • addBasicAuth
  • addSecurity
  • addSecurityRequirements

So if you need to add the API URL to you Swagger UI page, you can do that by adding .addServer(API_URL) . API_URL can be either something like /api or https://example.com/api . And it can be added multiple times. If you need for example URL for local usage, development, and production environments.

The full example will look like

const document = SwaggerModule.createDocument(
      app,
      new DocumentBuilder()
        .setTitle('API')
        .setDescription('API')
        .setVersion('1.0')
        .addBearerAuth()
        .addServer(API_URL)
        .build(),
    );

    SwaggerModule.setup(swaggerPath, app, document);

i think you might be missing the 'includes' param of where the controller is. try to add

   const document = SwaggerModule.createDocument(
  app,
  new DocumentBuilder()
    .setTitle('API')
    .setDescription('API')
    .setVersion('1.0')
    .addBearerAuth()
    .build(),
    { incliude: [ModuleWhereControllerIs]  }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM