From 5a0020e40258933466fda125c172b1d54f1cbc07 Mon Sep 17 00:00:00 2001 From: Tianning Li Date: Tue, 13 Feb 2024 01:10:12 -0800 Subject: [PATCH] feat: add more api response --- lib/decorators/api-response.decorator.ts | 249 ++++++++--------------- lib/extra/swagger-shim.ts | 96 +++++++-- 2 files changed, 166 insertions(+), 179 deletions(-) diff --git a/lib/decorators/api-response.decorator.ts b/lib/decorators/api-response.decorator.ts index 092c0b761..b69470609 100644 --- a/lib/decorators/api-response.decorator.ts +++ b/lib/decorators/api-response.decorator.ts @@ -81,171 +81,92 @@ export function ApiResponse( }; } -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 ApiPaymentRequiredResponse = (options: ApiResponseOptions = {}) => - ApiResponse({ - ...options, - status: HttpStatus.PAYMENT_REQUIRED - }); - -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 - }); +interface HttpStatusInfo { + code: number; + functionName: string; +} -export const ApiUnsupportedMediaTypeResponse = ( - options: ApiResponseOptions = {} -) => - ApiResponse({ - ...options, - status: HttpStatus.UNSUPPORTED_MEDIA_TYPE - }); +const decorators: { + [key: string]: ( + options?: ApiResponseOptions + ) => MethodDecorator & ClassDecorator; +} = {}; + +const statusList: HttpStatusInfo[] = Object.keys(HttpStatus) + .filter((key) => !isNaN(Number(HttpStatus[key]))) + .map((key) => { + const functionName = key + .split('_') + .map( + (strToken) => + `${strToken[0].toUpperCase()}${strToken.slice(1).toLowerCase()}` + ) + .join(''); + return { + code: Number(HttpStatus[key]), + functionName: `Api${functionName}Response` + }; + }); + +statusList.forEach(({ code, functionName }) => { + decorators[functionName] = function (options: ApiResponseOptions = {}) { + return ApiResponse({ + ...options, + status: code // Convert status to number + }); + }; +}); + +export const { + ApiContinueResponse, + ApiSwitchingProtocolsResponse, + ApiProcessingResponse, + ApiEarlyhintsResponse, + ApiOkResponse, + ApiCreatedResponse, + ApiAcceptedResponse, + ApiNonAuthoritativeInformationResponse, + ApiNoContentResponse, + ApiResetContentResponse, + ApiPartialContentResponse, + ApiAmbiguousResponse, + ApiMovedPermanentlyResponse, + ApiFoundResponse, + ApiSeeOtherResponse, + ApiNotModifiedResponse, + ApiTemporaryRedirectResponse, + ApiPermanentRedirectResponse, + ApiBadRequestResponse, + ApiUnauthorizedResponse, + ApiPaymentRequiredResponse, + ApiForbiddenResponse, + ApiNotFoundResponse, + ApiMethodNotAllowedResponse, + ApiNotAcceptableResponse, + ApiProxyAuthenticationRequiredResponse, + ApiRequestTimeoutResponse, + ApiConflictResponse, + ApiGoneResponse, + ApiLengthRequiredResponse, + ApiPreconditionFailedResponse, + ApiPayloadTooLargeResponse, + ApiUriTooLongResponse, + ApiUnsupportedMediaTypeResponse, + ApiRequestedRangeNotSatisfiableResponse, + ApiExpectationFailedResponse, + ApiIAmATeapotResponse, + ApiMisdirectedResponse, + ApiUnprocessableEntityResponse, + ApiFailedDependencyResponse, + ApiPreconditionRequiredResponse, + ApiTooManyRequestsResponse, + ApiInternalServerErrorResponse, + ApiNotImplementedResponse, + ApiBadGatewayResponse, + ApiServiceUnavailableResponse, + ApiGatewayTimeoutResponse, + ApiHttpVersionNotSupportedResponse +} = decorators; export const ApiDefaultResponse = (options: ApiResponseOptions = {}) => ApiResponse({ diff --git a/lib/extra/swagger-shim.ts b/lib/extra/swagger-shim.ts index 514f9e317..bd6f568df 100644 --- a/lib/extra/swagger-shim.ts +++ b/lib/extra/swagger-shim.ts @@ -59,6 +59,18 @@ export function ApiQuery() { export function ApiResponse() { return () => {}; } +export function ApiContinueResponse() { + return () => {}; +} +export function ApiSwitchingProtocolsResponse() { + return () => {}; +} +export function ApiProcessingResponse() { + return () => {}; +} +export function ApiEarlyhintsResponse() { + return () => {}; +} export function ApiOkResponse() { return () => {}; } @@ -68,73 +80,127 @@ export function ApiCreatedResponse() { export function ApiAcceptedResponse() { return () => {}; } +export function ApiNonAuthoritativeInformationResponse() { + return () => {}; +} export function ApiNoContentResponse() { return () => {}; } +export function ApiResetContentResponse() { + return () => {}; +} +export function ApiPartialContentResponse() { + return () => {}; +} +export function ApiAmbiguousResponse() { + return () => {}; +} export function ApiMovedPermanentlyResponse() { return () => {}; } export function ApiFoundResponse() { return () => {}; } +export function ApiSeeOtherResponse() { + return () => {}; +} +export function ApiNotModifiedResponse() { + return () => {}; +} +export function ApiTemporaryRedirectResponse() { + return () => {}; +} +export function ApiPermanentRedirectResponse() { + return () => {}; +} export function ApiBadRequestResponse() { return () => {}; } export function ApiUnauthorizedResponse() { return () => {}; } -export function ApiTooManyRequestsResponse() { +export function ApiPaymentRequiredResponse() { + return () => {}; +} +export function ApiForbiddenResponse() { return () => {}; } export function ApiNotFoundResponse() { return () => {}; } -export function ApiInternalServerErrorResponse() { +export function ApiMethodNotAllowedResponse() { return () => {}; } -export function ApiBadGatewayResponse() { +export function ApiNotAcceptableResponse() { return () => {}; } -export function ApiConflictResponse() { +export function ApiProxyAuthenticationRequiredResponse() { return () => {}; } -export function ApiForbiddenResponse() { +export function ApiRequestTimeoutResponse() { return () => {}; } -export function ApiGatewayTimeoutResponse() { +export function ApiConflictResponse() { return () => {}; } export function ApiGoneResponse() { return () => {}; } -export function ApiMethodNotAllowedResponse() { +export function ApiLengthRequiredResponse() { return () => {}; } -export function ApiNotAcceptableResponse() { +export function ApiPreconditionFailedResponse() { return () => {}; } -export function ApiNotImplementedResponse() { +export function ApiPayloadTooLargeResponse() { return () => {}; } -export function ApiPreconditionFailedResponse() { +export function ApiUriTooLongResponse() { return () => {}; } -export function ApiPayloadTooLargeResponse() { +export function ApiUnsupportedMediaTypeResponse() { return () => {}; } -export function ApiPaymentRequiredResponse() { +export function ApiRequestedRangeNotSatisfiableResponse() { return () => {}; } -export function ApiRequestTimeoutResponse() { +export function ApiExpectationFailedResponse() { return () => {}; } -export function ApiServiceUnavailableResponse() { +export function ApiIAmATeapotResponse() { + return () => {}; +} +export function ApiMisdirectedResponse() { return () => {}; } export function ApiUnprocessableEntityResponse() { return () => {}; } -export function ApiUnsupportedMediaTypeResponse() { +export function ApiFailedDependencyResponse() { + return () => {}; +} +export function ApiPreconditionRequiredResponse() { + return () => {}; +} +export function ApiTooManyRequestsResponse() { + return () => {}; +} +export function ApiInternalServerErrorResponse() { + return () => {}; +} +export function ApiNotImplementedResponse() { + return () => {}; +} +export function ApiBadGatewayResponse() { + return () => {}; +} +export function ApiServiceUnavailableResponse() { + return () => {}; +} +export function ApiGatewayTimeoutResponse() { + return () => {}; +} +export function ApiHttpVersionNotSupportedResponse() { return () => {}; } export function ApiDefaultResponse() {