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

[Refactor] token 필요없는 fetch custom hook 만들기 #445

Merged
merged 4 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
31 changes: 23 additions & 8 deletions src/common/apis/instance.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
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';

interface FetchOptions extends RequestInit {
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