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

feat: TypedAxios 모듈 생성 #674

Merged
merged 8 commits into from
Apr 28, 2023
Merged

feat: TypedAxios 모듈 생성 #674

merged 8 commits into from
Apr 28, 2023

Conversation

Tekiter
Copy link
Member

@Tekiter Tekiter commented Apr 27, 2023

🤫 쉿, 나한테만 말해줘요. 이슈넘버

🧐 어떤 것을 변경했어요~?

서버의 타입을 한번 더 검증하는 typedAxios.ts 모듈을 추가했어요. createEndpoint 를 통해서 선언적으로 서버에서 오는 값의 타입을 zod로 검증할 수 있어요.

현재는 예시를 위해서 일부 endpoint 에만 새로운 변경 사항을 적용했어요. 차차 다른 endpoint 들에도 적용할 예정이에요.

🤔 그렇다면, 어떻게 구현했어요~?

제너릭을 적절하게 사용해서, serverResponse 에 들어간 Zod Schema 를 넣으면 타입 추론이 되도록 했어요. .request 함수 호출시 axios 호출 후 zod 타입 검증까지 진행해요.

❤️‍🔥 당신이 생각하는 PR포인트, 내겐 매력포인트.

📸 스크린샷, 없으면 이것 참,, 섭섭한데요?

@Tekiter Tekiter requested a review from NamJwong April 27, 2023 14:54
@Tekiter Tekiter self-assigned this Apr 27, 2023
@github-actions
Copy link

github-actions bot commented Apr 27, 2023

🚀 프리뷰 배포 확인하기 🚀

https://051cba11.sopt-internal-dev.pages.dev

@github-actions
Copy link

github-actions bot commented Apr 27, 2023

✨✨ 스토리북으로 확인하기 ✨✨

api/typedAxios.ts Outdated Show resolved Hide resolved
Comment on lines +35 to +38
const zodError = String(res.error).slice(0, 1000) + '\n...';
const message = `서버 타입 검증에 실패했습니다. (${axiosConfig.method} ${axiosConfig.url})\n${zodError}`;
console.error(zodError);
throw new Error(message);
Copy link
Member

Choose a reason for hiding this comment

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

디버깅 하기에 정말 좋겠네요 👍👍👍

};
}

export type GetResponseType<T> = T extends Endpoint<infer R, never> ? R : never;
Copy link
Member

Choose a reason for hiding this comment

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

TS 고수님 ,, 혹시 <infer R, never>에서 왜 never로 한 건지 알려주실 수 있나요? 🥺

Copy link
Member Author

Choose a reason for hiding this comment

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

R 을 infer 해 오는것만 중요하고, 두번째 제너릭 인자는 필요가 없기 때문에 never를 줬어요! (사실 unknown[] 로 해도 작동해요.)

Copy link
Member

@NamJwong NamJwong left a comment

Choose a reason for hiding this comment

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

최최고고👍

@Tekiter Tekiter added this pull request to the merge queue Apr 28, 2023
Merged via the queue into main with commit c8fef26 Apr 28, 2023
@Tekiter Tekiter deleted the refactor-api-layer branch May 12, 2023 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants