This repository has been archived by the owner on Dec 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pref: swagger docs * feat: Dockerfile自定义mysql主机名和端口
- Loading branch information
Showing
34 changed files
with
309 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,96 @@ | ||
export class ResOp { | ||
readonly data: any; | ||
readonly statusCode: number; | ||
readonly message: string; | ||
import { applyDecorators, Type } from '@nestjs/common'; | ||
import { | ||
ApiExtraModels, | ||
ApiProperty, | ||
ApiResponse, | ||
ApiResponseSchemaHost, | ||
getSchemaPath, | ||
} from '@nestjs/swagger'; | ||
|
||
constructor(code: number, data?: any, message = 'success') { | ||
this.statusCode = code; | ||
class EmptyClass {} | ||
|
||
export class ResponseDto<T> { | ||
data: T; | ||
|
||
@ApiProperty({ default: 200 }) | ||
statusCode: number; | ||
|
||
@ApiProperty({ default: 'success' }) | ||
message: string; | ||
|
||
constructor(statusCode: number, data?: any, message = 'success') { | ||
this.statusCode = statusCode; | ||
this.data = data; | ||
this.message = message; | ||
} | ||
|
||
static success(data?: any) { | ||
return new ResOp(200, data); | ||
return new ResponseDto(200, data); | ||
} | ||
} | ||
|
||
export class Pagination { | ||
export class PaginatedDto<TData> { | ||
@ApiProperty() | ||
total: number; | ||
page: number; | ||
size: number; | ||
} | ||
|
||
export class PageResult<T> { | ||
list?: Array<T>; | ||
pagination: Pagination; | ||
@ApiProperty() | ||
limit: number; | ||
|
||
@ApiProperty() | ||
offset: number; | ||
|
||
results: TData[]; | ||
} | ||
|
||
export const CustomApiResponse = < | ||
DataDto extends Type<any>, | ||
WrapperDataDto extends Type<unknown>, | ||
>( | ||
dataDto: DataDto, | ||
wrapperDataDto: WrapperDataDto, | ||
dataType = 'object', | ||
options?: Partial<ApiResponseSchemaHost>, | ||
) => | ||
applyDecorators( | ||
ApiExtraModels(wrapperDataDto, dataDto), | ||
ApiResponse({ | ||
status: 200, | ||
schema: { | ||
allOf: [ | ||
{ $ref: getSchemaPath(wrapperDataDto) }, | ||
{ | ||
properties: { | ||
data: { | ||
type: dataType, | ||
[dataType === 'object' ? '$ref' : 'items']: | ||
dataType === 'object' | ||
? getSchemaPath(dataDto) | ||
: { $ref: getSchemaPath(dataDto) }, | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
...options, | ||
}), | ||
); | ||
|
||
export const ApiOkResponseData = <DataDto extends Type<any>>( | ||
dataDto?: DataDto, | ||
dataType: 'object' | 'array' = 'object', | ||
options?: Partial<ApiResponseSchemaHost>, | ||
) => CustomApiResponse(dataDto ?? EmptyClass, ResponseDto, dataType, options); | ||
|
||
export const ApiCreatedResponseData = <DataDto extends Type<any>>( | ||
dataDto?: DataDto, | ||
dataType: 'object' | 'array' = 'object', | ||
options?: Partial<ApiResponseSchemaHost>, | ||
) => | ||
CustomApiResponse(dataDto ?? EmptyClass, ResponseDto, dataType, { | ||
status: 201, | ||
...options, | ||
}); | ||
|
||
export const ApiOkResponsePaginated = <DataDto extends Type<any>>( | ||
dataDto: DataDto, | ||
) => CustomApiResponse(dataDto, PaginatedDto); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.