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

[FE] issue387: 서버 응답 런타임에 타입 확인 #401

Merged
merged 31 commits into from
Oct 10, 2022

Conversation

nan-noo
Copy link
Collaborator

@nan-noo nan-noo commented Oct 7, 2022

요약

서버 응답이 타입대로 오지 않을 때 런타임 에러가 발생해서 화면이 하얗게 변하는 문제
-> axios api 응답값 반환 전에 타입을 체크해서 응답값이 제대로 오지 않을 시 처리를 통해 하얗게 변하는 것을 방지했습니다.

세부사항

api 폴더의 각 도메인마다 typeChecker.ts 파일을 추가했습니다.

예시

// api 파일 - index.ts
export const postLogin = async ({ code }: ApiLogin['post']['variables']) => {
  const response = await axiosInstance.post<
    ApiLogin['post']['responseData'],
    AxiosResponse<ApiLogin['post']['responseData']>,
    ApiLogin['post']['variables']
  >(`/api/auth/login?code=${code}`);

// type check 추가
  return checkLogin(response.data);
};
// type checker 파일 - typeChecker.ts
type LoginKeys = keyof ApiLogin['post']['responseData'];

export const checkLogin = (data: unknown): ApiRefreshToken['get']['responseData'] => {
  if (!isObject(data)) throw new AxiosError(`Token does not have correct type: object`);

  const keys: Array<LoginKeys> = ['accessToken', 'expiredTime'];
  if (!hasOwnProperties(data, keys)) throw new AxiosError('StudyReviews does not have some properties');

  return {
    accessToken: checkType(data.accessToken, isString),
    expiredTime: checkType(data.expiredTime, isNumber),
  };
};
// utils/typeChecker.ts
export const isNull = (value: unknown): value is null => value === null;

export const isNullOrUndefined = (value: unknown): value is null | undefined => value === undefined || value === null;

export const isBoolean = (value: unknown): value is boolean => typeof value === 'boolean';

...

// isNull, isNumber등의 조건 함수 참/거짓 반환값에 따라 value를 return하는 함수
const checkType = <T>(value: unknown, isTypeFn: (value: unknown) => value is T): T => {
  if (!isTypeFn(value)) {
    console.error(`${isTypeFn} ${value} does not have correct type`);
    throw new AxiosError(`${value} does not have correct type`);
  }

  return value;
};

export default checkType;

close #387

@nan-noo nan-noo added 🚀 feature New feature or request 😁 frontend New frontend feature labels Oct 7, 2022
@nan-noo nan-noo requested a review from airman5573 October 7, 2022 04:58
@nan-noo nan-noo self-assigned this Oct 7, 2022
@nan-noo nan-noo linked an issue Oct 7, 2022 that may be closed by this pull request
Copy link
Collaborator

@airman5573 airman5573 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

태태 코드 짜느라 수고하셨어요.
리뷰 확인해주세요 :D

frontend/src/api/auth/typeChecker.ts Outdated Show resolved Hide resolved
frontend/src/api/axiosInstance.ts Outdated Show resolved Hide resolved
frontend/src/utils/typeChecker.ts Outdated Show resolved Hide resolved
frontend/src/utils/typeChecker.ts Outdated Show resolved Hide resolved
frontend/src/utils/typeChecker.ts Outdated Show resolved Hide resolved
frontend/src/api/study/typeChecker.ts Outdated Show resolved Hide resolved
frontend/src/api/tags/typeChecker.ts Outdated Show resolved Hide resolved
frontend/src/api/tags/typeChecker.ts Show resolved Hide resolved
frontend/src/custom-types/index.ts Show resolved Hide resolved
frontend/src/mocks/handlers/detailStudyHandlers.ts Outdated Show resolved Hide resolved
@nan-noo nan-noo requested a review from airman5573 October 10, 2022 10:23
- USER_ROLE
- CATEGORY_NAME
- STUDY_STATUS
isNullOrUndefined -> isNull
@airman5573 airman5573 merged commit 984a67a into develop Oct 10, 2022
@nan-noo nan-noo deleted the feat/387-api-runtime-check branch October 10, 2022 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚀 feature New feature or request 😁 frontend New frontend feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FE] 런타임에 응답 데이터 타입 체크하도록 개선
2 participants