Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(): Updated types for Specific ApiResponse decorator options to omit status #2877

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 58 additions & 27 deletions lib/decorators/api-response.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export interface ApiResponseSchemaHost
}

export type ApiResponseOptions = ApiResponseMetadata | ApiResponseSchemaHost;
export type ApiResponseNoStatusOptions =
| Omit<ApiResponseMetadata, 'status'>
| Omit<ApiResponseSchemaHost, 'status'>;

export function ApiResponse(
options: ApiResponseOptions,
Expand Down Expand Up @@ -81,173 +84,201 @@ export function ApiResponse(
};
}

export const ApiOkResponse = (options: ApiResponseOptions = {}) =>
export const ApiOkResponse = (options: ApiResponseNoStatusOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.OK
});

export const ApiCreatedResponse = (options: ApiResponseOptions = {}) =>
export const ApiCreatedResponse = (options: ApiResponseNoStatusOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.CREATED
});

export const ApiAcceptedResponse = (options: ApiResponseOptions = {}) =>
export const ApiAcceptedResponse = (options: ApiResponseNoStatusOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.ACCEPTED
});

export const ApiNoContentResponse = (options: ApiResponseOptions = {}) =>
export const ApiNoContentResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.NO_CONTENT
});

export const ApiMovedPermanentlyResponse = (options: ApiResponseOptions = {}) =>
export const ApiMovedPermanentlyResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.MOVED_PERMANENTLY
});

export const ApiFoundResponse = (options: ApiResponseOptions = {}) =>
export const ApiFoundResponse = (options: ApiResponseNoStatusOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.FOUND
});

export const ApiBadRequestResponse = (options: ApiResponseOptions = {}) =>
export const ApiBadRequestResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.BAD_REQUEST
});

export const ApiUnauthorizedResponse = (options: ApiResponseOptions = {}) =>
export const ApiUnauthorizedResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.UNAUTHORIZED
});

export const ApiTooManyRequestsResponse = (options: ApiResponseOptions = {}) =>
export const ApiTooManyRequestsResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.TOO_MANY_REQUESTS
});

export const ApiNotFoundResponse = (options: ApiResponseOptions = {}) =>
export const ApiNotFoundResponse = (options: ApiResponseNoStatusOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.NOT_FOUND
});

export const ApiInternalServerErrorResponse = (
options: ApiResponseOptions = {}
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.INTERNAL_SERVER_ERROR
});

export const ApiBadGatewayResponse = (options: ApiResponseOptions = {}) =>
export const ApiBadGatewayResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.BAD_GATEWAY
});

export const ApiConflictResponse = (options: ApiResponseOptions = {}) =>
export const ApiConflictResponse = (options: ApiResponseNoStatusOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.CONFLICT
});

export const ApiForbiddenResponse = (options: ApiResponseOptions = {}) =>
export const ApiForbiddenResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.FORBIDDEN
});

export const ApiGatewayTimeoutResponse = (options: ApiResponseOptions = {}) =>
export const ApiGatewayTimeoutResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.GATEWAY_TIMEOUT
});

export const ApiGoneResponse = (options: ApiResponseOptions = {}) =>
export const ApiGoneResponse = (options: ApiResponseNoStatusOptions = {}) =>
ApiResponse({
...options,
status: HttpStatus.GONE
});

export const ApiMethodNotAllowedResponse = (options: ApiResponseOptions = {}) =>
export const ApiMethodNotAllowedResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.METHOD_NOT_ALLOWED
});

export const ApiNotAcceptableResponse = (options: ApiResponseOptions = {}) =>
export const ApiNotAcceptableResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.NOT_ACCEPTABLE
});

export const ApiNotImplementedResponse = (options: ApiResponseOptions = {}) =>
export const ApiNotImplementedResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.NOT_IMPLEMENTED
});

export const ApiPreconditionFailedResponse = (
options: ApiResponseOptions = {}
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.PRECONDITION_FAILED
});

export const ApiPayloadTooLargeResponse = (options: ApiResponseOptions = {}) =>
export const ApiPayloadTooLargeResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.PAYLOAD_TOO_LARGE
});

export const ApiPaymentRequiredResponse = (options: ApiResponseOptions = {}) =>
export const ApiPaymentRequiredResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.PAYMENT_REQUIRED
});

export const ApiRequestTimeoutResponse = (options: ApiResponseOptions = {}) =>
export const ApiRequestTimeoutResponse = (
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.REQUEST_TIMEOUT
});

export const ApiServiceUnavailableResponse = (
options: ApiResponseOptions = {}
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.SERVICE_UNAVAILABLE
});

export const ApiUnprocessableEntityResponse = (
options: ApiResponseOptions = {}
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.UNPROCESSABLE_ENTITY
});

export const ApiUnsupportedMediaTypeResponse = (
options: ApiResponseOptions = {}
options: ApiResponseNoStatusOptions = {}
) =>
ApiResponse({
...options,
status: HttpStatus.UNSUPPORTED_MEDIA_TYPE
});

export const ApiDefaultResponse = (options: ApiResponseOptions = {}) =>
export const ApiDefaultResponse = (options: ApiResponseNoStatusOptions = {}) =>
ApiResponse({
...options,
status: 'default'
Expand Down
154 changes: 154 additions & 0 deletions test/decorators/api-response.decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { Controller, Get, HttpStatus, Param } from '@nestjs/common';
import { DECORATORS } from '../../lib/constants';
import {
ApiAcceptedResponse,
ApiBadGatewayResponse,
ApiBadRequestResponse,
ApiConflictResponse,
ApiCreatedResponse,
ApiDefaultResponse,
ApiForbiddenResponse,
ApiFoundResponse,
ApiGatewayTimeoutResponse,
ApiGoneResponse,
ApiInternalServerErrorResponse,
ApiMethodNotAllowedResponse,
ApiMovedPermanentlyResponse,
ApiNoContentResponse,
ApiNotAcceptableResponse,
ApiNotFoundResponse,
ApiNotImplementedResponse,
ApiOkResponse,
ApiPayloadTooLargeResponse,
ApiPaymentRequiredResponse,
ApiPreconditionFailedResponse,
ApiRequestTimeoutResponse,
ApiResponse,
ApiServiceUnavailableResponse,
ApiTooManyRequestsResponse,
ApiUnauthorizedResponse,
ApiUnprocessableEntityResponse,
ApiUnsupportedMediaTypeResponse
} from '../../lib/decorators';

describe('ApiResponse', () => {
describe('when applied on the method level', () => {
@Controller('tests/:testId')
class TestAppController {
@Get()
@ApiResponse({ status: 204 })
public get(@Param('testId') testId: string): string {
return testId;
}
}

it('should attach metadata to a given method', () => {
const controller = new TestAppController();
expect(
Reflect.hasMetadata(DECORATORS.API_RESPONSE, controller.get)
).toBeTruthy();
expect(
Reflect.getMetadata(DECORATORS.API_RESPONSE, controller.get)
).toEqual({
'204': { description: '', isArray: undefined, type: undefined }
});
});

it.each([
{ decorator: ApiOkResponse, status: HttpStatus.OK },
{ decorator: ApiCreatedResponse, status: HttpStatus.CREATED },
{ decorator: ApiAcceptedResponse, status: HttpStatus.ACCEPTED },
{ decorator: ApiNoContentResponse, status: HttpStatus.NO_CONTENT },
{
decorator: ApiMovedPermanentlyResponse,
status: HttpStatus.MOVED_PERMANENTLY
},
{ decorator: ApiFoundResponse, status: HttpStatus.FOUND },
{ decorator: ApiBadRequestResponse, status: HttpStatus.BAD_REQUEST },
{ decorator: ApiUnauthorizedResponse, status: HttpStatus.UNAUTHORIZED },
{
decorator: ApiTooManyRequestsResponse,
status: HttpStatus.TOO_MANY_REQUESTS
},
{ decorator: ApiNotFoundResponse, status: HttpStatus.NOT_FOUND },
{
decorator: ApiInternalServerErrorResponse,
status: HttpStatus.INTERNAL_SERVER_ERROR
},
{ decorator: ApiBadGatewayResponse, status: HttpStatus.BAD_GATEWAY },
{ decorator: ApiConflictResponse, status: HttpStatus.CONFLICT },
{ decorator: ApiForbiddenResponse, status: HttpStatus.FORBIDDEN },
{
decorator: ApiGatewayTimeoutResponse,
status: HttpStatus.GATEWAY_TIMEOUT
},
{ decorator: ApiGoneResponse, status: HttpStatus.GONE },
{
decorator: ApiMethodNotAllowedResponse,
status: HttpStatus.METHOD_NOT_ALLOWED
},
{
decorator: ApiNotAcceptableResponse,
status: HttpStatus.NOT_ACCEPTABLE
},
{
decorator: ApiNotImplementedResponse,
status: HttpStatus.NOT_IMPLEMENTED
},
{
decorator: ApiPreconditionFailedResponse,
status: HttpStatus.PRECONDITION_FAILED
},
{
decorator: ApiPayloadTooLargeResponse,
status: HttpStatus.PAYLOAD_TOO_LARGE
},
{
decorator: ApiPaymentRequiredResponse,
status: HttpStatus.PAYMENT_REQUIRED
},
{
decorator: ApiRequestTimeoutResponse,
status: HttpStatus.REQUEST_TIMEOUT
},
{
decorator: ApiServiceUnavailableResponse,
status: HttpStatus.SERVICE_UNAVAILABLE
},
{
decorator: ApiUnprocessableEntityResponse,
status: HttpStatus.UNPROCESSABLE_ENTITY
},
{
decorator: ApiUnsupportedMediaTypeResponse,
status: HttpStatus.UNSUPPORTED_MEDIA_TYPE
},
{ decorator: ApiDefaultResponse, status: 'default' }
] as const)(
'should not allow to override status of $decorator.name[$status]',
({ decorator, status }) => {
@Controller('tests/:testId')
class TestAppController {
@Get()
@decorator({
// @ts-expect-error -- Should error if user tries to override status
status: 2010
})
public get(@Param('testId') testId: string): string {
return testId;
}
}

const controller = new TestAppController();
expect(
Reflect.hasMetadata(DECORATORS.API_RESPONSE, controller.get)
).toBeTruthy();
expect(
Reflect.getMetadata(DECORATORS.API_RESPONSE, controller.get)
).toEqual({
[status]: { description: '', isArray: undefined, type: undefined }
});
}
);
});
});