init
This commit is contained in:
30
src/common/dto/api-response.dto.ts
Normal file
30
src/common/dto/api-response.dto.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class ApiResponseDto<T> {
|
||||
@ApiProperty({ description: '请求是否成功' })
|
||||
success: boolean;
|
||||
|
||||
@ApiProperty({ description: '响应数据', nullable: true })
|
||||
data: T | null;
|
||||
|
||||
@ApiProperty({ description: '错误信息', nullable: true })
|
||||
message: string | null;
|
||||
|
||||
@ApiProperty({ description: '响应时间戳' })
|
||||
timestamp: Date;
|
||||
|
||||
constructor(success: boolean, data: T | null, message: string | null = null) {
|
||||
this.success = success;
|
||||
this.data = data;
|
||||
this.message = message;
|
||||
this.timestamp = new Date();
|
||||
}
|
||||
|
||||
static success<T>(data: T): ApiResponseDto<T> {
|
||||
return new ApiResponseDto(true, data, null);
|
||||
}
|
||||
|
||||
static error<T>(message: string): ApiResponseDto<T | null> {
|
||||
return new ApiResponseDto<T | null>(false, null, message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user