【NestJS】swagger快速接入

9 篇文章 2 订阅
6 篇文章 0 订阅

1.安装swagger

$ npm install --save @nestjs/swagger swagger-ui-express


//如果使用fastify,则必须安装fastify-swagger而不是swagger-ui-express:
$ npm install --save @nestjs/swagger fastify-swagger

2.Bootstrap启动配置

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const options = new DocumentBuilder()
    .setTitle('测试接口')
    .setDescription('接口文档')
    .setVersion('1.0')
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api-docs', app, document);

  await app.listen(3000);
}
bootstrap();

3.DTO示例

import { ApiProperty } from "@nestjs/swagger";

/** */
export class LoginDto {
    @ApiProperty({ 
        required: true,
        description:'登录名'
    })
    readonly loginId: string;
    @ApiProperty({ 
        required: true,
        description:'密码'
    })
    readonly pwd: string;
}

// export interface SchemaObject {
//     nullable?: boolean;
//     discriminator?: DiscriminatorObject;
//     readOnly?: boolean;
//     writeOnly?: boolean;
//     xml?: XmlObject;
//     externalDocs?: ExternalDocumentationObject;
//     example?: any;
//     examples?: any[];
//     deprecated?: boolean;
//     type?: string;
//     allOf?: (SchemaObject | ReferenceObject)[];
//     oneOf?: (SchemaObject | ReferenceObject)[];
//     anyOf?: (SchemaObject | ReferenceObject)[];
//     not?: SchemaObject | ReferenceObject;
//     items?: SchemaObject | ReferenceObject;
//     properties?: Record<string, SchemaObject | ReferenceObject>;
//     additionalProperties?: SchemaObject | ReferenceObject | boolean;
//     description?: string;
//     format?: string;
//     default?: any;
//     title?: string;
//     multipleOf?: number;
//     maximum?: number;
//     exclusiveMaximum?: boolean;
//     minimum?: number;
//     exclusiveMinimum?: boolean;
//     maxLength?: number;
//     minLength?: number;
//     pattern?: string;
//     maxItems?: number;
//     minItems?: number;
//     uniqueItems?: boolean;
//     maxProperties?: number;
//     minProperties?: number;
//     required?: string[];
//     enum?: any[];
// }

 

4.控制器示例


import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common'
import { ApiTags, ApiOperation, ApiQuery, ApiParam } from '@nestjs/swagger';
import { LoginDto } from '../../models/dto/customer.dto'

// import { Request } from 'express'

@Controller('customer')
@ApiTags('用户模块')
export class CustomerController {

    @Post('login')
    @ApiOperation({ summary: '登录' })
    // login(@Req() request: Request): boolean {
    login(@Body() reqBody: LoginDto): boolean {
        // console.log('query', query)
        console.log('reqBody', reqBody)

        // const query = request.query
        const loginId = reqBody.loginId
        const pwd = reqBody.pwd

        if (pwd === '123') {
            return true
        }

        return false
    }

    @Get('/getUserName/:id')
    @ApiOperation({ summary: '获取用户名' })
    @ApiParam({ name: 'id', description: '用户id', required: false })
    getUserName(@Param('id') id: string): string {
        return "这个是用户名" + id
    }

    @Get('getUserName2')
    @ApiOperation({ summary: '获取用户名' })
    @ApiQuery({ name: 'id', description: '用户id', required: false })
    getUserName2(@Query('id') id: string): string {
        return "这个是用户名" + id
    }


}

 

5.实际效果

npm rn start

http://localhost:3000/api-docs/

 

 

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员查理

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值