Skip to content

Commit

Permalink
Merge pull request #2836 from lit26/feature/add-more-apiresponse
Browse files Browse the repository at this point in the history
feat: add more shorthand api response decorator
  • Loading branch information
kamilmysliwiec authored Jul 1, 2024
2 parents 91a40a3 + 2358cae commit 91a7c76
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 179 deletions.
249 changes: 85 additions & 164 deletions lib/decorators/api-response.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,171 +82,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({
Expand Down
Loading

0 comments on commit 91a7c76

Please sign in to comment.