Skip to content

Commit

Permalink
fix: api 타입 수정 (#409)
Browse files Browse the repository at this point in the history
- main page 스터디 목록
- my study page 스터디 목록
-> 타입 충돌 오류 수정
  • Loading branch information
nan-noo authored Oct 11, 2022
1 parent 0540c58 commit 240e02e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion frontend/src/api/my-study/typeChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const checkMyStudy = (data: unknown): MyStudy => {
id: checkType(data.id, isNumber),
title: checkType(data.title, isString),
startDate: checkType(data.startDate, isDateYMD),
endDate: checkType(data.endDate, isDateYMD),
endDate: checkType(data.endDate, isDateYMD, true),
studyStatus: checkType(data.studyStatus, isStudyStatus),
tags: checkType(data.tags, isArray).map(tag => checkMyStudyTag(tag)),
owner: checkMember(data.owner),
Expand Down
52 changes: 50 additions & 2 deletions frontend/src/api/studies/typeChecker.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,57 @@
import { AxiosError } from 'axios';

import { arrayOfAll, checkType, hasOwnProperties, isArray, isBoolean, isObject } from '@utils';
import {
arrayOfAll,
checkType,
hasOwnProperties,
isArray,
isBoolean,
isNumber,
isObject,
isRecruitmentStatus,
isString,
} from '@utils';

import type { Study, Tag } from '@custom-types';

import { type ApiStudies } from '@api/studies';
import { checkStudy } from '@api/study/typeChecker';

type MainStudyTag = Pick<Tag, 'id' | 'name'>;
type MainStudyTagKeys = keyof MainStudyTag;

const arrayOfAllMainStudyTagKeys = arrayOfAll<MainStudyTagKeys>();

const checkMainStudyTag = (data: unknown): MainStudyTag => {
if (!isObject(data)) throw new AxiosError(`Main-Tag does not have correct type: object`);

const keys = arrayOfAllMainStudyTagKeys(['id', 'name']);
if (!hasOwnProperties(data, keys)) throw new AxiosError('Main-Tag does not have some properties');

return {
id: checkType(data.id, isNumber),
name: checkType(data.name, isString),
};
};

type StudyKeys = keyof Study;

const arrayOfAllStudyKeys = arrayOfAll<StudyKeys>();

const checkStudy = (data: unknown): Study => {
if (!isObject(data)) throw new AxiosError(`Main-Study does not have correct type: object`);

const keys = arrayOfAllStudyKeys(['id', 'excerpt', 'recruitmentStatus', 'tags', 'thumbnail', 'title']);
if (!hasOwnProperties(data, keys)) throw new AxiosError('Main-Study does not have some properties');

return {
id: checkType(data.id, isNumber),
title: checkType(data.title, isString),
excerpt: checkType(data.title, isString),
thumbnail: checkType(data.title, isString),
tags: checkType(data.tags, isArray).map(tag => checkMainStudyTag(tag)),
recruitmentStatus: checkType(data.recruitmentStatus, isRecruitmentStatus),
};
};

type StudiesKeys = keyof ApiStudies['get']['responseData'];

Expand Down

0 comments on commit 240e02e

Please sign in to comment.