feat: 添加VO类型的文档生成

master
luoer 2023-08-04 17:46:43 +08:00
parent 1819a1a8df
commit 6bdc589a6f
6 changed files with 28 additions and 10 deletions

View File

@ -7,6 +7,7 @@
{ {
"name": "@nestjs/swagger", "name": "@nestjs/swagger",
"options": { "options": {
"dtoFileNameSuffix": [".vo.ts", ".dto.ts", ".entity.ts"],
"introspectComments": true "introspectComments": true
} }
} }

View File

@ -47,6 +47,7 @@ export function addResponseWrapper(doc: OpenAPIObject) {
properties: { properties: {
data: schema, data: schema,
}, },
required: ['data'],
}, },
], ],
}; };

View File

@ -1,21 +1,22 @@
import { Body, Controller, HttpCode, HttpStatus, Post } from '@nestjs/common'; import { Body, Controller, HttpCode, HttpStatus, Post } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { ApiTags } from '@nestjs/swagger';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
import { AuthUserDto } from './dto/auth-user.dto'; import { AuthUserDto } from './dto/auth-user.dto';
import { Public } from './jwt'; import { Public } from './jwt';
import { LoginedUserVo } from './vo/logined-user.vo';
@ApiTags('auth') @ApiTags('auth')
@Controller('auth') @Controller('auth')
export class AuthController { export class AuthController {
constructor(private authService: AuthService) {} constructor(private authService: AuthService) {}
@Public() /**
*
*/
@Post('login') @Post('login')
@Public()
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@ApiResponse({ status: HttpStatus.UNAUTHORIZED, description: '账号或密码错误' }) login(@Body() user: AuthUserDto): Promise<LoginedUserVo> {
@ApiResponse({ description: '登录成功' })
@ApiOperation({ description: '账号登录', operationId: 'login' })
login(@Body() user: AuthUserDto) {
return this.authService.signIn(user); return this.authService.signIn(user);
} }
} }

View File

@ -2,6 +2,7 @@ import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt'; import { JwtService } from '@nestjs/jwt';
import { UserService } from '../user'; import { UserService } from '../user';
import { AuthUserDto } from './dto/auth-user.dto'; import { AuthUserDto } from './dto/auth-user.dto';
import { LoginedUserVo } from './vo/logined-user.vo';
@Injectable() @Injectable()
export class AuthService { export class AuthService {
@ -9,13 +10,17 @@ export class AuthService {
async signIn(authUserDto: AuthUserDto) { async signIn(authUserDto: AuthUserDto) {
const user = await this.userService.findByUsername(authUserDto.username); const user = await this.userService.findByUsername(authUserDto.username);
const { password, ...result } = user;
if (!user) { if (!user) {
throw new UnauthorizedException('用户名不存在'); throw new UnauthorizedException('用户名不存在');
} }
if (password !== authUserDto.password) { if (user.password !== authUserDto.password) {
throw new UnauthorizedException('密码错误'); throw new UnauthorizedException('密码错误');
} }
return this.jwtService.signAsync(result); const loginedUser = new LoginedUserVo();
for (const key in loginedUser) {
loginedUser[key] = user[key];
}
loginedUser.token = await this.jwtService.signAsync({ id: user.id, username: user.username });
return loginedUser;
} }
} }

View File

@ -0,0 +1,10 @@
import { User } from '@/modules/user';
import { OmitType } from '@nestjs/swagger';
export class LoginedUserVo extends OmitType(User, ['password'] as const) {
/**
* 访
* @example 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MjIsInVzZXJuYW1lIjoianVldGFuIiwiaWF0IjoxNjkxMTM5MjI3LCJleHAiOjE2OTExOTkyMjd9.6z7f-xfsHABbsyg401o2boKeqNQ1epPDYfEdavIcfYc'
*/
token: string;
}

View File

@ -61,7 +61,7 @@ export class UserService extends BaseService {
* *
*/ */
findByUsername(username: string) { findByUsername(username: string) {
return this.userRepository.findOneOrFail({ where: { username } }); return this.userRepository.findOne({ where: { username } });
} }
/** /**