简体   繁体   English

@nestjs/swagger:如何添加 API URL?

[英]@nestjs/swagger: How to add API URL?

I'm using @nestjs/swagger.我正在使用@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.在我可以使用setHost('URL HERE')添加 API url 之前,但现在它不起作用。 So I want to add api url to which will be sent the requests from the Swagger UI.所以我想添加 api url,将从 Swagger UI 发送请求。 I'm beginner so sorry if the question is not very smart Thanks!如果问题不是很聪明,我是初学者很抱歉谢谢!

Looks like you are using @nestjs/swagger version 4***.看起来您正在使用 @nestjs/swagger 版本 4***。 setHost was removed in version 4*** but was available in version 3***. setHost 在版本 4*** 中被删除,但在版本 3*** 中可用。 If it works before I guess the version of @nestjs/swagger was updated in your project.如果它在我猜测 @nestjs/swagger 的版本在您的项目中更新之前有效。

As you can see here正如你在这里看到的

If you're currently using @nestjs/swagger@3.*, note the following breaking/API changes in version 4.0.如果您当前正在使用 @nestjs/swagger@3.*,请注意 4.0 版中的以下重大/API 更改。

The following decorators have been changed/renamed:以下装饰器已更改/重命名:

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

DocumentBuilder breaking changes (updated method signatures): DocumentBuilder 重大更改(更新的方法签名):

  • addTag添加标签
  • addBearerAuth添加承载身份验证
  • addOAuth2添加OAuth2
  • setContactEmail is now setContact setContactEmail 现在是 setContact
  • setHost has been removed setHost 已被删除
  • setSchemes has been removed (use the addServer instead, eg, addServer('http://')) setSchemes 已被删除(使用 addServer 代替,例如 addServer('http://'))

The following methods have been added:添加了以下方法:

  • addServer添加服务器
  • addApiKey添加APIKey
  • 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 添加到 Swagger UI 页面,您可以通过添加.addServer(API_URL) API_URL can be either something like /api or https://example.com/api . API_URL 可以是/apihttps://example.com/api And it can be added multiple times.并且可以多次添加。 If you need for example URL for local usage, development, and production environments.如果您需要例如本地使用、开发和生产环境的 URL。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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