From 64c8410cc6987cf7b858f06cad74b0aceb53bd07 Mon Sep 17 00:00:00 2001 From: Eonseok Jeon <121864459+eonseok-jeon@users.noreply.github.com> Date: Sat, 7 Sep 2024 14:22:54 +0900 Subject: [PATCH] =?UTF-8?q?[Refactor]=20token=20=ED=95=84=EC=9A=94?= =?UTF-8?q?=EC=97=86=EB=8A=94=20fetch=20custom=20hook=20=EB=A7=8C=EB=93=A4?= =?UTF-8?q?=EA=B8=B0=20(#445)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 기존 axios instace -> fetch instance로 대체 * refactor: 기존 코드 fetch instance로 변경 * refactor: headers type 제한 * feat: instace option method type 제한 --- src/common/apis/getRecruitingInfo.ts | 2 +- src/common/apis/instance.ts | 33 +++++++++++++++++++------- src/common/components/Input/apis.ts | 35 +++++++++++++++++----------- src/views/PasswordPage/apis.ts | 15 +++++++----- src/views/SignInPage/apis.ts | 13 +++++++---- src/views/SignupPage/apis.ts | 19 ++++++++------- 6 files changed, 76 insertions(+), 41 deletions(-) diff --git a/src/common/apis/getRecruitingInfo.ts b/src/common/apis/getRecruitingInfo.ts index a7859ca3..ad2ed290 100644 --- a/src/common/apis/getRecruitingInfo.ts +++ b/src/common/apis/getRecruitingInfo.ts @@ -1,7 +1,7 @@ import instance from '@apis/instance'; export const getRecruitingInfo = async () => { - const res = await instance.get('/recruiting-season/latest'); + const res = await instance('/recruiting-season/latest', { method: 'GET' }); return res; }; diff --git a/src/common/apis/instance.ts b/src/common/apis/instance.ts index 7948729b..3dc78203 100644 --- a/src/common/apis/instance.ts +++ b/src/common/apis/instance.ts @@ -1,10 +1,27 @@ -import axios from 'axios'; - -const instance = axios.create({ - baseURL: import.meta.env.VITE_BASE_URL, - headers: { - 'Content-Type': 'application/json', - }, -}); +const baseURL = import.meta.env.VITE_BASE_URL; + +type StandardHeaders = 'Content-Type' | 'Authorization' | 'Accept' | 'Cache-Control' | 'User-Agent'; +type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'; + +interface FetchOptions extends RequestInit { + method?: RequestMethod; + headers?: Record; +} + +const instance = async (url: string, options: FetchOptions = {}) => { + const response = await fetch(`${baseURL}${url}`, { + headers: { + 'Content-Type': 'application/json', + ...options.headers, + }, + ...options, + }); + + if (!response.ok) { + throw new Error('network response가 도착하지 않았어요'); + } + + return response.json(); +}; export default instance; diff --git a/src/common/components/Input/apis.ts b/src/common/components/Input/apis.ts index d90e0190..67730417 100644 --- a/src/common/components/Input/apis.ts +++ b/src/common/components/Input/apis.ts @@ -4,31 +4,40 @@ import { CheckUserRequest } from './types'; export const checkUser = async (userInfo: CheckUserRequest) => { const { email, name, season, group } = userInfo; - const res = await instance.post('/recruiting-auth/check/user', { - email, - name, - season, - group, + const res = await instance('/recruiting-auth/check/user', { + method: 'POST', + body: JSON.stringify({ + email, + name, + season, + group, + }), }); return res; }; export const sendVerificationCode = async (email: string, season: number, group: string, isSignup: boolean) => { - const res = await instance.post('/recruiting-auth/verify/send', { - email, - season, - group, - isSignup, + const res = await instance('/recruiting-auth/verify/send', { + method: 'POST', + body: JSON.stringify({ + email, + season, + group, + isSignup, + }), }); return res; }; export const checkVerificationCode = async (email: string, code: string) => { - const res = await instance.post('/recruiting-auth/verify/email', { - email, - code, + const res = await instance('/recruiting-auth/verify/email', { + method: 'POST', + body: JSON.stringify({ + email, + code, + }), }); return res; diff --git a/src/views/PasswordPage/apis.ts b/src/views/PasswordPage/apis.ts index ae637b87..df138c15 100644 --- a/src/views/PasswordPage/apis.ts +++ b/src/views/PasswordPage/apis.ts @@ -4,12 +4,15 @@ import type { PasswordRequest } from './types'; export const sendPasswordChange = async (userInfo: PasswordRequest) => { const { email, season, group, password, passwordCheck } = userInfo; - const res = await instance.post('/recruiting-auth/change/password', { - email, - season, - group, - password, - passwordCheck, + const res = await instance('/recruiting-auth/change/password', { + method: 'POST', + body: JSON.stringify({ + email, + season, + group, + password, + passwordCheck, + }), }); return res; diff --git a/src/views/SignInPage/apis.ts b/src/views/SignInPage/apis.ts index 18dc093a..0cf22390 100644 --- a/src/views/SignInPage/apis.ts +++ b/src/views/SignInPage/apis.ts @@ -4,11 +4,14 @@ import type { SignInRequest } from './types'; export const sendSignIn = async (userInfo: SignInRequest) => { const { email, season, group, password } = userInfo; - const res = await instance.post('/recruiting-auth/login', { - email, - season, - group, - password, + const res = await instance('/recruiting-auth/login', { + method: 'POST', + body: JSON.stringify({ + email, + season, + group, + password, + }), }); return res; diff --git a/src/views/SignupPage/apis.ts b/src/views/SignupPage/apis.ts index acae49bc..fa38bde1 100644 --- a/src/views/SignupPage/apis.ts +++ b/src/views/SignupPage/apis.ts @@ -4,14 +4,17 @@ import type { SignUpRequest } from './types'; export const sendSignUp = async (userInfo: SignUpRequest) => { const { email, password, passwordCheck, name, phone, season, group } = userInfo; - const res = await instance.post('/recruiting-auth/signup', { - email, - password, - passwordCheck, - name, - phone, - season, - group, + const res = await instance('/recruiting-auth/signup', { + method: 'POST', + body: JSON.stringify({ + email, + password, + passwordCheck, + name, + phone, + season, + group, + }), }); return res;