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

chore: api 에러 처리 관심사 분리 #69

Merged
merged 9 commits into from
Jan 9, 2025
Merged
21 changes: 2 additions & 19 deletions src/api/ImageApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import axiosInstance from '@/lib/axiosInstance';
import sessionStorage from './storage/sessionStorage';
import { ImageRequest, ImageResponse } from './type/Image';

/*** 이미지 업로드
Expand All @@ -11,23 +10,7 @@ export const postImagesUpload = async (
// create formdata
const formData = new FormData();
formData.append('image', data.file);

const URL = `/images/upload`;
console.log('POST - postImagesUpload(): ', URL);

try {
const res = await axiosInstance.post(URL, formData);

if (res.status === 200 || res.status === 201) {
const resData = res.data as ImageResponse;
sessionStorage.setItem(`postImagesUpload`, resData);
return resData;
} else {
throw new Error(
`Failed to postImagesUpload() res.status: ${res.status}, res.data: ${res.data}`
);
}
} catch (error) {
throw error;
}
const res = await axiosInstance.post(URL, formData);
return res.data;
};
79 changes: 9 additions & 70 deletions src/api/authApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import axiosInstance from '@/lib/axiosInstance';
import localStorage from '@/api/storage/localStorage';
import sessionStorage from '@/api/storage/sessionStorage';

import {
SignUpRequest,
SignInRequest,
Expand All @@ -14,47 +13,17 @@ export const postSignUp = async (
data: SignUpRequest
): Promise<AuthResponse> => {
const URL = `/auth/signUp`;
console.log('POST - postAuthSignUp(): ', URL);

try {
const res = await axiosInstance.post(URL, data);

if (res.status === 200 || res.status === 201) {
const resData = res.data as AuthResponse;
localStorage.setItem(`postAuthSignUp`, resData);
return resData;
} else {
throw new Error(
`Failed to postAuthSignUp() res.status: ${res.status}, res.data: ${res.data}`
);
}
} catch (error) {
throw error;
}
const res = await axiosInstance.post(URL, data);
return res.data;
};

/*** '로그인' 요청 ***/
export const postSignIn = async (
data: SignInRequest
): Promise<AuthResponse> => {
const URL = `auth/signIn`;
console.log('POST - postAuthSignIn(): ', URL);

try {
const res = await axiosInstance.post(URL, data);

if (res.status === 200 || res.status === 201) {
const resData = res.data as AuthResponse;
sessionStorage.setItem(`postAuthSignIn`, resData);
return resData;
} else {
throw new Error(
`Failed to postAuthSignIn() res.status: ${res.status}, res.data: ${res.data}`
);
}
} catch (error) {
throw error;
}
const res = await axiosInstance.post(URL, data);
return res.data;
};

/*** '간편 회원가입' 요청 ***/
Expand All @@ -63,23 +32,8 @@ export const postSignUpProvider = async (
data: SocialSignUpRequest
): Promise<AuthResponse> => {
const URL = `/auth/signUp/${provider}`;
console.log('POST - postAuthSignUpProvider(): ', URL);

try {
const res = await axiosInstance.post(URL, data);

if (res.status === 200 || res.status === 201) {
const resData = res.data as AuthResponse;
localStorage.setItem(`postAuthSignUpProvider`, resData);
return resData;
} else {
throw new Error(
`Failed to postAuthSignUpProvider() res.status: ${res.status}, res.data: ${res.data}`
);
}
} catch (error) {
throw error;
}
const res = await axiosInstance.post(URL, data);
return res.data;
};

/*** '간편 로그인' 요청 ***/
Expand All @@ -88,21 +42,6 @@ export const postSignInProvider = async (
data: SocialSignInRequest
): Promise<AuthResponse> => {
const URL = `auth/signIn/${provider}`;
console.log('POST - postAuthSignInProvider(): ', URL);

try {
const res = await axiosInstance.post(URL, data);

if (res.status === 200 || res.status === 201) {
const resData = res.data as AuthResponse;
sessionStorage.setItem(`postAuthSignInProvider`, resData);
return resData;
} else {
throw new Error(
`Failed to postAuthSignInProvider() res.status: ${res.status}, res.data: ${res.data}`
);
}
} catch (error) {
throw error;
}
const res = await axiosInstance.post(URL, data);
return res.data;
};
20 changes: 2 additions & 18 deletions src/api/categoryApi.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
import axiosInstance from '@/lib/axiosInstance';
import sessionStorage from '@/api/storage/sessionStorage';
import { CategoryResponse } from '@/api/type/Category';

/*** 상품 카테고리 조회 ***/
export const getCategories = async (): Promise<CategoryResponse[]> => {
const URL = `/categories`;
console.log('GET - getCategories(): ', URL);

try {
const res = await axiosInstance.get(URL);

if (res.status === 200 || res.status === 201) {
const resData = res.data as CategoryResponse[];
sessionStorage.setItem(`getCategories`, resData);
return resData;
} else {
throw new Error(
`Failed to getCategories() res.status: ${res.status}, res.data: ${res.data}`
);
}
} catch (error) {
throw error;
}
const res = await axiosInstance.get(URL);
return res.data;
};
39 changes: 4 additions & 35 deletions src/api/followApi.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,20 @@
import axiosInstance from '@/lib/axiosInstance';
import sessionStorage from '@/api/storage/sessionStorage';
import { FollowRequest, FollowResponse } from './type/Follow';

/*** 유저 팔로우 ***/
export const postFollow = async (
data: FollowRequest
): Promise<FollowResponse> => {
const URL = `/follow`;
console.log('POST - postFollow(): ', URL);

try {
const res = await axiosInstance.post(URL, data);

if (res.status === 200 || res.status === 201) {
const resData = res.data as FollowResponse;
sessionStorage.setItem(`postFollow`, resData);
return resData;
} else {
throw new Error(
`Failed to postFollow() res.status: ${res.status}, res.data: ${res.data}`
);
}
} catch (error) {
throw error;
}
const res = await axiosInstance.post(URL, data);
return res.data;
};

/*** 유저 언팔로우 ***/
export const deleteFollow = async (
data: FollowRequest
): Promise<FollowResponse> => {
const URL = `/follow`;
console.log('DELETE - deleteFollow(): ', URL);

try {
const res = await axiosInstance.delete(URL, { data });

if (res.status === 200 || res.status === 201) {
const resData = res.data as FollowResponse;
sessionStorage.setItem(`deleteFollow`, resData);
return resData;
} else {
throw new Error(
`Failed to deleteFollow() res.status: ${res.status}, res.data: ${res.data}`
);
}
} catch (error) {
throw error;
}
const res = await axiosInstance.delete(URL, { data });
return res.data;
};
20 changes: 2 additions & 18 deletions src/api/oauthApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import axiosInstance from '@/lib/axiosInstance';
import localStorage from './storage/localStorage';
import { OauthRequest, OauthResponse } from './type/Oauth';

/*** '간편 로그인 App 등록/수정' 요청
Expand All @@ -15,21 +14,6 @@ export const postOauthApps = async (
data: OauthRequest
): Promise<OauthResponse> => {
const URL = `/oauthApps`;
console.log('POST - postOauthApps(): ', URL);

try {
const res = await axiosInstance.post(URL, data);

if (res.status === 200 || res.status === 201) {
const resdata = res.data as OauthResponse;
localStorage.setItem(`postOauthApps`, resdata);
return resdata;
} else {
throw new Error(
`Failed to postOauthApps() res.status: ${res.status}, res.data: ${res.data}`
);
}
} catch (error) {
throw error;
}
const res = await axiosInstance.post(URL, data);
return res.data;
};
Loading
Loading