Skip to content

Commit

Permalink
[Refactor] token 필요없는 fetch custom hook 만들기 (#445)
Browse files Browse the repository at this point in the history
* feat: 기존 axios instace -> fetch instance로 대체

* refactor: 기존 코드 fetch instance로 변경

* refactor: headers type 제한

* feat: instace option method type 제한
  • Loading branch information
eonseok-jeon authored Sep 7, 2024
1 parent 77532a9 commit 64c8410
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/common/apis/getRecruitingInfo.ts
Original file line number Diff line number Diff line change
@@ -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;
};
33 changes: 25 additions & 8 deletions src/common/apis/instance.ts
Original file line number Diff line number Diff line change
@@ -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<StandardHeaders, string>;
}

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;
35 changes: 22 additions & 13 deletions src/common/components/Input/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 9 additions & 6 deletions src/views/PasswordPage/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 8 additions & 5 deletions src/views/SignInPage/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 11 additions & 8 deletions src/views/SignupPage/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 64c8410

Please sign in to comment.