feat: 优化文件不存在时的返回格式
parent
876154256c
commit
1819a1a8df
|
|
@ -1,7 +1,7 @@
|
||||||
import { BaseController } from '@/common/base';
|
import { BaseController } from '@/common/base';
|
||||||
import { Respond, RespondType } from '@/common/response';
|
import { Respond, RespondType } from '@/common/response';
|
||||||
import { Body, Controller, Delete, Get, Param, Patch, Post, Query, Version } from '@nestjs/common';
|
import { Body, Controller, Delete, Get, Param, Patch, Post, Query, ParseIntPipe } from '@nestjs/common';
|
||||||
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
|
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
|
||||||
import { Create{{upcaseName name}}Dto } from './dto/create-{{fileName name}}.dto';
|
import { Create{{upcaseName name}}Dto } from './dto/create-{{fileName name}}.dto';
|
||||||
import { Find{{upcaseName name}}Dto } from './dto/find-{{fileName name}}.dto';
|
import { Find{{upcaseName name}}Dto } from './dto/find-{{fileName name}}.dto';
|
||||||
import { Update{{upcaseName name}}Dto } from './dto/update-{{fileName name}}.dto';
|
import { Update{{upcaseName name}}Dto } from './dto/update-{{fileName name}}.dto';
|
||||||
|
|
@ -15,29 +15,45 @@ export class {{upcaseName name}}Controller extends BaseController {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增{{cnName}}
|
||||||
|
*/
|
||||||
@Post()
|
@Post()
|
||||||
@ApiOperation({ description: '创建{{cnName}}', operationId: 'add{{upcaseName name}}' })
|
add{{upcaseName name}}(@Body() create{{upcaseName name}}Dto: Create{{upcaseName name}}Dto) {
|
||||||
create(@Body() create{{upcaseName name}}Dto: Create{{upcaseName name}}Dto) {
|
|
||||||
return this.{{lowcaseName name}}Service.create(create{{upcaseName name}}Dto);
|
return this.{{lowcaseName name}}Service.create(create{{upcaseName name}}Dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据分页/过滤参数查询{{cnName}}
|
||||||
|
*/
|
||||||
@Get()
|
@Get()
|
||||||
@Respond(RespondType.PAGINATION)
|
@Respond(RespondType.PAGINATION)
|
||||||
@ApiOkResponse({ isArray: true, type: {{upcaseName name}} })
|
@ApiOkResponse({ isArray: true, type: {{upcaseName name}} })
|
||||||
@ApiOperation({ description: '批量查询{{cnName}}', operationId: 'get{{upcaseName name}}s' })
|
get{{upcaseName name}}s(@Query() query: Find{{upcaseName name}}Dto) {
|
||||||
async findMany(@Query() query: Find{{upcaseName name}}Dto) {
|
|
||||||
return this.{{lowcaseName name}}Service.findMany(query);
|
return this.{{lowcaseName name}}Service.findMany(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据ID查询{{cnName}}
|
||||||
|
*/
|
||||||
|
@Get(':id')
|
||||||
|
get{{upcaseName name}}(@Param('id', ParseIntPipe) id: number): Promise<{{upcaseName name}}> {
|
||||||
|
return this.{{lowcaseName name}}Service.findOne(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据ID更新{{cnName}}
|
||||||
|
*/
|
||||||
@Patch(':id')
|
@Patch(':id')
|
||||||
@ApiOperation({ description: '更新{{cnName}}', operationId: 'update{{upcaseName name}}' })
|
update{{upcaseName name}}(@Param('id', ParseIntPipe) id: number, @Body() update{{upcaseName name}}Dto: Update{{upcaseName name}}Dto) {
|
||||||
update(@Param('id') id: number, @Body() update{{upcaseName name}}Dto: Update{{upcaseName name}}Dto) {
|
|
||||||
return this.{{lowcaseName name}}Service.update(+id, update{{upcaseName name}}Dto);
|
return this.{{lowcaseName name}}Service.update(+id, update{{upcaseName name}}Dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据ID删除{{cnName}}
|
||||||
|
*/
|
||||||
@Delete(':id')
|
@Delete(':id')
|
||||||
@ApiOperation({ description: '删除{{cnName}}', operationId: 'delete{{upcaseName name}}' })
|
del{{upcaseName name}}(@Param('id', ParseIntPipe) id: number) {
|
||||||
remove(@Param('id') id: number) {
|
|
||||||
return this.{{lowcaseName name}}Service.remove(+id);
|
return this.{{lowcaseName name}}Service.remove(+id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ import { LoggerService } from '../logger';
|
||||||
export class AllExecptionFilter implements ExceptionFilter {
|
export class AllExecptionFilter implements ExceptionFilter {
|
||||||
constructor(private logger: LoggerService) {}
|
constructor(private logger: LoggerService) {}
|
||||||
|
|
||||||
catch(exception: Error, host: ArgumentsHost) {
|
catch(exception: any, host: ArgumentsHost) {
|
||||||
const ctx = host.switchToHttp();
|
const ctx = host.switchToHttp();
|
||||||
const request = ctx.getRequest<Request>();
|
const request = ctx.getRequest<Request>();
|
||||||
const response = ctx.getResponse<_Response>();
|
const response = ctx.getResponse<_Response>();
|
||||||
|
|
@ -16,6 +16,23 @@ export class AllExecptionFilter implements ExceptionFilter {
|
||||||
const code = ResponseCode.UNKNOWN_ERROR;
|
const code = ResponseCode.UNKNOWN_ERROR;
|
||||||
console.trace(exception);
|
console.trace(exception);
|
||||||
this.logger.error(exception, `${request.method} ${request.url}`);
|
this.logger.error(exception, `${request.method} ${request.url}`);
|
||||||
response.status(HttpStatus.INTERNAL_SERVER_ERROR).json(Response.create({ code, message, data: null }));
|
|
||||||
|
// 静态文件路径不存在的异常
|
||||||
|
if (exception.code === 'ENOENT') {
|
||||||
|
return response.status(exception.status).json(
|
||||||
|
Response.create({
|
||||||
|
code: ResponseCode.ERROR,
|
||||||
|
message: '访问的路径不存在',
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 其他异常
|
||||||
|
response.status(HttpStatus.INTERNAL_SERVER_ERROR).json(
|
||||||
|
Response.create({
|
||||||
|
code,
|
||||||
|
message,
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ export class Response<T = any> {
|
||||||
* @example 1
|
* @example 1
|
||||||
*/
|
*/
|
||||||
@ApiProperty({})
|
@ApiProperty({})
|
||||||
data: T;
|
data?: T;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 响应元数据
|
* 响应元数据
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,16 @@ export const ServeStaticModule = _ServeStaticModule.forRootAsync({
|
||||||
{
|
{
|
||||||
rootPath: config.uploadDir,
|
rootPath: config.uploadDir,
|
||||||
serveRoot: config.uploadPrefix,
|
serveRoot: config.uploadPrefix,
|
||||||
|
serveStaticOptions: {
|
||||||
|
fallthrough: false,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
rootPath: config.staticDir,
|
rootPath: config.staticDir,
|
||||||
|
serveRoot: config.staticPrefix,
|
||||||
|
serveStaticOptions: {
|
||||||
|
fallthrough: false,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ export const initSwagger = (app: INestApplication) => {
|
||||||
.build();
|
.build();
|
||||||
const options: SwaggerDocumentOptions = {
|
const options: SwaggerDocumentOptions = {
|
||||||
operationIdFactory(controllerKey, methodKey) {
|
operationIdFactory(controllerKey, methodKey) {
|
||||||
return `${controllerKey}_${methodKey}`;
|
return `${methodKey}`;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const document = addResponseWrapper(SwaggerModule.createDocument(app, docConfig, options));
|
const document = addResponseWrapper(SwaggerModule.createDocument(app, docConfig, options));
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,13 @@ export function addResponseWrapper(doc: OpenAPIObject) {
|
||||||
for (const status of Object.keys(responses)) {
|
for (const status of Object.keys(responses)) {
|
||||||
const json = responses[status].content?.['application/json'];
|
const json = responses[status].content?.['application/json'];
|
||||||
if (!json) {
|
if (!json) {
|
||||||
|
responses[status].content = {
|
||||||
|
'application/json': {
|
||||||
|
schema: {
|
||||||
|
$ref: '#/components/schemas/Response',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const schema = json.schema;
|
const schema = json.schema;
|
||||||
|
|
|
||||||
|
|
@ -122,6 +122,14 @@ export class ConfigService {
|
||||||
return this.config.get('STATIC_DIR', './content/static');
|
return this.config.get('STATIC_DIR', './content/static');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 静态文件URL前缀
|
||||||
|
* @default '/''
|
||||||
|
*/
|
||||||
|
get staticPrefix(): string {
|
||||||
|
return this.config.get('STATIC_PREFIX', '/');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认页码
|
* 默认页码
|
||||||
* @default 1
|
* @default 1
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
import { BaseController } from '@/common/base';
|
import { BaseController } from '@/common/base';
|
||||||
import { Respond, RespondType } from '@/common/response';
|
import { Respond, RespondType } from '@/common/response';
|
||||||
import { Body, Controller, Delete, Get, Param, Patch, Post, Query, Version } from '@nestjs/common';
|
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, Query } from '@nestjs/common';
|
||||||
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
|
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
|
||||||
import { CreateUserDto } from './dto/create-user.dto';
|
import { CreateUserDto } from './dto/create-user.dto';
|
||||||
import { FindUserDto } from './dto/find-user.dto';
|
import { FindUserDto } from './dto/find-user.dto';
|
||||||
import { UpdateUserDto } from './dto/update-user.dto';
|
import { UpdateUserDto } from './dto/update-user.dto';
|
||||||
import { User } from './entities/user.entity';
|
|
||||||
import { UserService } from './user.service';
|
import { UserService } from './user.service';
|
||||||
|
import { User } from './entities/user.entity';
|
||||||
|
|
||||||
@ApiTags('user')
|
@ApiTags('user')
|
||||||
@Controller('users')
|
@Controller('users')
|
||||||
|
|
@ -15,36 +15,45 @@ export class UserController extends BaseController {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 新增用户
|
||||||
|
*/
|
||||||
@Post()
|
@Post()
|
||||||
@ApiOperation({ description: '创建用户', operationId: 'addUser' })
|
addUser(@Body() createUserDto: CreateUserDto) {
|
||||||
create(@Body() createUserDto: CreateUserDto) {
|
|
||||||
return this.userService.create(createUserDto);
|
return this.userService.create(createUserDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页/条件查询用户
|
||||||
|
*/
|
||||||
@Get()
|
@Get()
|
||||||
@Respond(RespondType.PAGINATION)
|
@Respond(RespondType.PAGINATION)
|
||||||
@ApiOkResponse({ isArray: true, type: User })
|
@ApiOkResponse({ type: User, isArray: true })
|
||||||
@ApiOperation({ description: '批量查询用户', operationId: 'getUsers' })
|
async getUsers(@Query() query: FindUserDto) {
|
||||||
async findMany(@Query() query: FindUserDto) {
|
|
||||||
return this.userService.findMany(query);
|
return this.userService.findMany(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据ID查询用户
|
||||||
|
*/
|
||||||
@Get(':id')
|
@Get(':id')
|
||||||
@Version('2')
|
getUser(@Param('id', ParseIntPipe) id: number): Promise<User> {
|
||||||
@ApiOperation({ deprecated: true, description: '查询用户', operationId: 'getUserv2' })
|
return this.userService.findOne(id);
|
||||||
findOne(@Param('id') id: number) {
|
|
||||||
return this.userService.findOne(+id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据ID更新用户
|
||||||
|
*/
|
||||||
@Patch(':id')
|
@Patch(':id')
|
||||||
@ApiOperation({ description: '更新用户', operationId: 'updateUser' })
|
updateUser(@Param('id') id: number, @Body() updateUserDto: UpdateUserDto) {
|
||||||
update(@Param('id') id: number, @Body() updateUserDto: UpdateUserDto) {
|
|
||||||
return this.userService.update(+id, updateUserDto);
|
return this.userService.update(+id, updateUserDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据ID删除用户
|
||||||
|
*/
|
||||||
@Delete(':id')
|
@Delete(':id')
|
||||||
@ApiOperation({ description: '删除用户', operationId: 'deleteUser' })
|
delUser(@Param('id', ParseIntPipe) id: number) {
|
||||||
remove(@Param('id') id: number) {
|
|
||||||
return this.userService.remove(+id);
|
return this.userService.remove(+id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue