Skip to content

Commit

Permalink
restrict returned response type
Browse files Browse the repository at this point in the history
  • Loading branch information
virgilchiriac committed Oct 23, 2023
1 parent 2b8cfd8 commit e2ec1f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Param,
Patch,
Post,
UnprocessableEntityException,
} from '@nestjs/common';
import { ApiExtraModels, ApiOperation, ApiResponse, ApiTags, getSchemaPath } from '@nestjs/swagger';
import { ApiValidationError } from '@shared/common';
Expand All @@ -17,9 +18,10 @@ import { CardUc } from '../uc';
import { ElementUc } from '../uc/element.uc';
import { SubmissionItemUc } from '../uc/submission-item.uc';
import {
AnyContentElementResponse,
CreateContentElementBodyParams,
FileElementResponse,
isFileElementResponse,
isRichTextElementResponse,
RichTextElementResponse,
SubmissionContainerUrlParams,
SubmissionItemUrlParams,
Expand Down Expand Up @@ -91,10 +93,13 @@ export class BoardSubmissionController {
@Param() urlParams: SubmissionItemUrlParams,
@Body() bodyParams: CreateContentElementBodyParams,
@CurrentUser() currentUser: ICurrentUser
): Promise<AnyContentElementResponse> {
): Promise<FileElementResponse | RichTextElementResponse> {
const { type } = bodyParams;
const element = await this.submissionItemUc.createElement(currentUser.userId, urlParams.submissionItemId, type);
const response = ContentElementResponseFactory.mapToResponse(element);
if (!isFileElementResponse(response) && !isRichTextElementResponse(response)) {
throw new UnprocessableEntityException();
}

return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ export type AnyContentElementResponse =
| RichTextElementResponse
| SubmissionContainerElementResponse
| ExternalToolElementResponse;

export const isFileElementResponse = (element: AnyContentElementResponse): element is FileElementResponse =>
element instanceof FileElementResponse;

export const isRichTextElementResponse = (element: AnyContentElementResponse): element is RichTextElementResponse =>
element instanceof RichTextElementResponse;

0 comments on commit e2ec1f3

Please sign in to comment.