@@ -82,171 +82,92 @@ export function ApiResponse(
82
82
} ;
83
83
}
84
84
85
- export const ApiOkResponse = ( options : ApiResponseOptions = { } ) =>
86
- ApiResponse ( {
87
- ...options ,
88
- status : HttpStatus . OK
89
- } ) ;
90
-
91
- export const ApiCreatedResponse = ( options : ApiResponseOptions = { } ) =>
92
- ApiResponse ( {
93
- ...options ,
94
- status : HttpStatus . CREATED
95
- } ) ;
96
-
97
- export const ApiAcceptedResponse = ( options : ApiResponseOptions = { } ) =>
98
- ApiResponse ( {
99
- ...options ,
100
- status : HttpStatus . ACCEPTED
101
- } ) ;
102
-
103
- export const ApiNoContentResponse = ( options : ApiResponseOptions = { } ) =>
104
- ApiResponse ( {
105
- ...options ,
106
- status : HttpStatus . NO_CONTENT
107
- } ) ;
108
-
109
- export const ApiMovedPermanentlyResponse = ( options : ApiResponseOptions = { } ) =>
110
- ApiResponse ( {
111
- ...options ,
112
- status : HttpStatus . MOVED_PERMANENTLY
113
- } ) ;
114
-
115
- export const ApiFoundResponse = ( options : ApiResponseOptions = { } ) =>
116
- ApiResponse ( {
117
- ...options ,
118
- status : HttpStatus . FOUND
119
- } ) ;
120
-
121
- export const ApiBadRequestResponse = ( options : ApiResponseOptions = { } ) =>
122
- ApiResponse ( {
123
- ...options ,
124
- status : HttpStatus . BAD_REQUEST
125
- } ) ;
126
-
127
- export const ApiUnauthorizedResponse = ( options : ApiResponseOptions = { } ) =>
128
- ApiResponse ( {
129
- ...options ,
130
- status : HttpStatus . UNAUTHORIZED
131
- } ) ;
132
-
133
- export const ApiTooManyRequestsResponse = ( options : ApiResponseOptions = { } ) =>
134
- ApiResponse ( {
135
- ...options ,
136
- status : HttpStatus . TOO_MANY_REQUESTS
137
- } ) ;
138
-
139
- export const ApiNotFoundResponse = ( options : ApiResponseOptions = { } ) =>
140
- ApiResponse ( {
141
- ...options ,
142
- status : HttpStatus . NOT_FOUND
143
- } ) ;
144
-
145
- export const ApiInternalServerErrorResponse = (
146
- options : ApiResponseOptions = { }
147
- ) =>
148
- ApiResponse ( {
149
- ...options ,
150
- status : HttpStatus . INTERNAL_SERVER_ERROR
151
- } ) ;
152
-
153
- export const ApiBadGatewayResponse = ( options : ApiResponseOptions = { } ) =>
154
- ApiResponse ( {
155
- ...options ,
156
- status : HttpStatus . BAD_GATEWAY
157
- } ) ;
158
-
159
- export const ApiConflictResponse = ( options : ApiResponseOptions = { } ) =>
160
- ApiResponse ( {
161
- ...options ,
162
- status : HttpStatus . CONFLICT
163
- } ) ;
164
-
165
- export const ApiForbiddenResponse = ( options : ApiResponseOptions = { } ) =>
166
- ApiResponse ( {
167
- ...options ,
168
- status : HttpStatus . FORBIDDEN
169
- } ) ;
170
-
171
- export const ApiGatewayTimeoutResponse = ( options : ApiResponseOptions = { } ) =>
172
- ApiResponse ( {
173
- ...options ,
174
- status : HttpStatus . GATEWAY_TIMEOUT
175
- } ) ;
176
-
177
- export const ApiGoneResponse = ( options : ApiResponseOptions = { } ) =>
178
- ApiResponse ( {
179
- ...options ,
180
- status : HttpStatus . GONE
181
- } ) ;
182
-
183
- export const ApiMethodNotAllowedResponse = ( options : ApiResponseOptions = { } ) =>
184
- ApiResponse ( {
185
- ...options ,
186
- status : HttpStatus . METHOD_NOT_ALLOWED
187
- } ) ;
188
-
189
- export const ApiNotAcceptableResponse = ( options : ApiResponseOptions = { } ) =>
190
- ApiResponse ( {
191
- ...options ,
192
- status : HttpStatus . NOT_ACCEPTABLE
193
- } ) ;
194
-
195
- export const ApiNotImplementedResponse = ( options : ApiResponseOptions = { } ) =>
196
- ApiResponse ( {
197
- ...options ,
198
- status : HttpStatus . NOT_IMPLEMENTED
199
- } ) ;
200
-
201
- export const ApiPreconditionFailedResponse = (
202
- options : ApiResponseOptions = { }
203
- ) =>
204
- ApiResponse ( {
205
- ...options ,
206
- status : HttpStatus . PRECONDITION_FAILED
207
- } ) ;
208
-
209
- export const ApiPayloadTooLargeResponse = ( options : ApiResponseOptions = { } ) =>
210
- ApiResponse ( {
211
- ...options ,
212
- status : HttpStatus . PAYLOAD_TOO_LARGE
213
- } ) ;
214
-
215
- export const ApiPaymentRequiredResponse = ( options : ApiResponseOptions = { } ) =>
216
- ApiResponse ( {
217
- ...options ,
218
- status : HttpStatus . PAYMENT_REQUIRED
219
- } ) ;
220
-
221
- export const ApiRequestTimeoutResponse = ( options : ApiResponseOptions = { } ) =>
222
- ApiResponse ( {
223
- ...options ,
224
- status : HttpStatus . REQUEST_TIMEOUT
225
- } ) ;
226
-
227
- export const ApiServiceUnavailableResponse = (
228
- options : ApiResponseOptions = { }
229
- ) =>
230
- ApiResponse ( {
231
- ...options ,
232
- status : HttpStatus . SERVICE_UNAVAILABLE
233
- } ) ;
234
-
235
- export const ApiUnprocessableEntityResponse = (
236
- options : ApiResponseOptions = { }
237
- ) =>
238
- ApiResponse ( {
239
- ...options ,
240
- status : HttpStatus . UNPROCESSABLE_ENTITY
241
- } ) ;
85
+ interface HttpStatusInfo {
86
+ code : number ;
87
+ functionName : string ;
88
+ }
242
89
243
- export const ApiUnsupportedMediaTypeResponse = (
244
- options : ApiResponseOptions = { }
245
- ) =>
246
- ApiResponse ( {
247
- ...options ,
248
- status : HttpStatus . UNSUPPORTED_MEDIA_TYPE
249
- } ) ;
90
+ const decorators : {
91
+ [ key : string ] : (
92
+ options ?: ApiResponseOptions
93
+ ) => MethodDecorator & ClassDecorator ;
94
+ } = { } ;
95
+
96
+ const statusList : HttpStatusInfo [ ] = Object . keys ( HttpStatus )
97
+ . filter ( ( key ) => ! isNaN ( Number ( HttpStatus [ key ] ) ) )
98
+ . map ( ( key ) => {
99
+ const functionName = key
100
+ . split ( '_' )
101
+ . map (
102
+ ( strToken ) =>
103
+ `${ strToken [ 0 ] . toUpperCase ( ) } ${ strToken . slice ( 1 ) . toLowerCase ( ) } `
104
+ )
105
+ . join ( '' ) ;
106
+ return {
107
+ code : Number ( HttpStatus [ key ] ) ,
108
+ functionName : `Api${ functionName } Response`
109
+ } ;
110
+ } ) ;
111
+
112
+ statusList . forEach ( ( { code, functionName } ) => {
113
+ decorators [ functionName ] = function ( options : ApiResponseOptions = { } ) {
114
+ return ApiResponse ( {
115
+ ...options ,
116
+ status : code // Convert status to number
117
+ } ) ;
118
+ } ;
119
+ } ) ;
120
+
121
+ export const {
122
+ ApiContinueResponse,
123
+ ApiSwitchingProtocolsResponse,
124
+ ApiProcessingResponse,
125
+ ApiEarlyhintsResponse,
126
+ ApiOkResponse,
127
+ ApiCreatedResponse,
128
+ ApiAcceptedResponse,
129
+ ApiNonAuthoritativeInformationResponse,
130
+ ApiNoContentResponse,
131
+ ApiResetContentResponse,
132
+ ApiPartialContentResponse,
133
+ ApiAmbiguousResponse,
134
+ ApiMovedPermanentlyResponse,
135
+ ApiFoundResponse,
136
+ ApiSeeOtherResponse,
137
+ ApiNotModifiedResponse,
138
+ ApiTemporaryRedirectResponse,
139
+ ApiPermanentRedirectResponse,
140
+ ApiBadRequestResponse,
141
+ ApiUnauthorizedResponse,
142
+ ApiPaymentRequiredResponse,
143
+ ApiForbiddenResponse,
144
+ ApiNotFoundResponse,
145
+ ApiMethodNotAllowedResponse,
146
+ ApiNotAcceptableResponse,
147
+ ApiProxyAuthenticationRequiredResponse,
148
+ ApiRequestTimeoutResponse,
149
+ ApiConflictResponse,
150
+ ApiGoneResponse,
151
+ ApiLengthRequiredResponse,
152
+ ApiPreconditionFailedResponse,
153
+ ApiPayloadTooLargeResponse,
154
+ ApiUriTooLongResponse,
155
+ ApiUnsupportedMediaTypeResponse,
156
+ ApiRequestedRangeNotSatisfiableResponse,
157
+ ApiExpectationFailedResponse,
158
+ ApiIAmATeapotResponse,
159
+ ApiMisdirectedResponse,
160
+ ApiUnprocessableEntityResponse,
161
+ ApiFailedDependencyResponse,
162
+ ApiPreconditionRequiredResponse,
163
+ ApiTooManyRequestsResponse,
164
+ ApiInternalServerErrorResponse,
165
+ ApiNotImplementedResponse,
166
+ ApiBadGatewayResponse,
167
+ ApiServiceUnavailableResponse,
168
+ ApiGatewayTimeoutResponse,
169
+ ApiHttpVersionNotSupportedResponse
170
+ } = decorators ;
250
171
251
172
export const ApiDefaultResponse = ( options : ApiResponseOptions = { } ) =>
252
173
ApiResponse ( {
0 commit comments