/
api-response.decorator.ts
209 lines (176 loc) 路 5.15 KB
/
api-response.decorator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { DECORATORS } from '../constants';
import {
ResponseObject,
SchemaObject,
ReferenceObject,
Type,
} from '../interfaces';
import { createMixedDecorator, getTypeIsArrayTuple } from './helpers';
import { HttpStatus } from '../common/httpStatus';
export interface ApiResponseMetadata
extends Omit<ResponseObject, 'description'> {
status?: number | 'default';
type?: Type | string | Record<string, any>;
isArray?: boolean;
description?: string;
}
export interface ApiResponseSchemaHost
extends Omit<ResponseObject, 'description'> {
schema: SchemaObject & Partial<ReferenceObject>;
status?: number;
description?: string;
}
export type ApiResponseOptions = ApiResponseMetadata | ApiResponseSchemaHost;
export function ApiResponse(options: ApiResponseOptions): any {
const [type, isArray] = getTypeIsArrayTuple(
(options as ApiResponseMetadata).type,
(options as ApiResponseMetadata).isArray
);
(options as ApiResponseMetadata).type = type;
(options as ApiResponseMetadata).isArray = isArray;
options.description = options.description ? options.description : '';
const groupedMetadata = {
[options.status || 'default']: options,
};
return createMixedDecorator(DECORATORS.API_RESPONSE, groupedMetadata);
}
export const ApiOkResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.OK,
});
export const ApiCreatedResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.CREATED,
});
export const ApiAcceptedResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.ACCEPTED,
});
export const ApiNoContentResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.NO_CONTENT,
});
export const ApiMovedPermanentlyResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.MOVED_PERMANENTLY,
});
export const ApiFoundResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.FOUND,
});
export const ApiBadRequestResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.BAD_REQUEST,
});
export const ApiUnauthorizedResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.UNAUTHORIZED,
});
export const ApiTooManyRequestsResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.TOO_MANY_REQUESTS,
});
export const ApiNotFoundResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.NOT_FOUND,
});
export const ApiInternalServerErrorResponse = (
options: ApiResponseOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.INTERNAL_SERVER_ERROR,
});
export const ApiBadGatewayResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.BAD_GATEWAY,
});
export const ApiConflictResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.CONFLICT,
});
export const ApiForbiddenResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.FORBIDDEN,
});
export const ApiGatewayTimeoutResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.GATEWAY_TIMEOUT,
});
export const ApiGoneResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.GONE,
});
export const ApiMethodNotAllowedResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.METHOD_NOT_ALLOWED,
});
export const ApiNotAcceptableResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.NOT_ACCEPTABLE,
});
export const ApiNotImplementedResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.NOT_IMPLEMENTED,
});
export const ApiPreconditionFailedResponse = (
options: ApiResponseOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.PRECONDITION_FAILED,
});
export const ApiPayloadTooLargeResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.PAYLOAD_TOO_LARGE,
});
export const ApiRequestTimeoutResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.REQUEST_TIMEOUT,
});
export const ApiServiceUnavailableResponse = (
options: ApiResponseOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.SERVICE_UNAVAILABLE,
});
export const ApiUnprocessableEntityResponse = (
options: ApiResponseOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.UNPROCESSABLE_ENTITY,
});
export const ApiUnsupportedMediaTypeResponse = (
options: ApiResponseOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.UNSUPPORTED_MEDIA_TYPE,
});
export const ApiDefaultResponse = (options: ApiResponseOptions = {}) =>
ApiResponse({
...options,
status: 'default',
});