Skip to content

Commit

Permalink
✨ feat : refresh token 관련 api 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
codefug committed Jul 20, 2024
1 parent 665a2be commit 4921433
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
61 changes: 50 additions & 11 deletions src/shared/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { AxiosError } from "axios";
import { instance } from "./axios";
import { instanceWithoutInterceptors } from "./axios";
import {
GetCommentsProps,
GetDatumProps,
GetProductProps,
PostAuthRefreshToken,
SignInRequestData,
SignInResponseData,
SignUpRequestData,
Expand All @@ -12,6 +13,7 @@ import {
SpecificProductData,
TotalProductsData,
} from "./type";
import Cookies from "js-cookie";

// 나중에 전부 axios로 바꾸기
export async function getDatum({
Expand All @@ -27,8 +29,10 @@ export async function getDatum({
orderBy,
});
if (keyword) searchParams.set("keyword", keyword);
const response = await instance(`/products?`, { params: searchParams });
return response.data as TotalProductsData;
const response = await instanceWithoutInterceptors(`/products?`, {
params: searchParams,
});
return response.data;
} catch (error) {
console.error(error);
alert(error);
Expand All @@ -38,7 +42,9 @@ export async function getDatum({

export const getProduct = async ({ productId }: GetProductProps) => {
try {
const response = await instance(`/products/${productId}`);
const response = await instanceWithoutInterceptors(
`/products/${productId}`
);
const data: SpecificProductData = response.data;
return data;
} catch (error) {
Expand All @@ -56,9 +62,12 @@ export const getComments = async ({
const searchParams = new URLSearchParams({
limit: limit.toString(),
});
const response = await instance(`/products/${productId}/comments`, {
params: searchParams,
});
const response = await instanceWithoutInterceptors(
`/products/${productId}/comments`,
{
params: searchParams,
}
);
const data: SpecificCommentsData = response.data;
return data;
} catch (error) {
Expand All @@ -75,7 +84,7 @@ export const postSignUp = async ({
passwordConfirmation,
}: SignUpRequestData) => {
try {
const response = await instance.post(`/auth/signUp`, {
const response = await instanceWithoutInterceptors.post(`/auth/signUp`, {
email,
nickname,
password,
Expand All @@ -84,21 +93,51 @@ export const postSignUp = async ({
const data: SignUpResponseData = response.data;
return data;
} catch (error) {
console.error(error);
alert(error);
if (error instanceof AxiosError) {
console.log(error.response);
alert(error.response?.data?.message);
throw new Error();
}
throw new Error();
}
};

export const postSignIn = async ({ email, password }: SignInRequestData) => {
try {
const response = await instance.post(`/auth/signIn`, { email, password });
const response = await instanceWithoutInterceptors.post(`/auth/signIn`, {
email,
password,
});
const data: SignInResponseData = response.data;
return data;
} catch (error) {
if (error instanceof AxiosError) {
console.log(error.response);
alert(error.response?.data?.message);
throw new Error();
}
throw new Error();
}
};

export const postAuthRefreshToken = async () => {
try {
const refreshToken = Cookies.get("refreshToken");
if (refreshToken === undefined) {
throw new Error("refreshToken이 없습니다.");
}
const response = await instanceWithoutInterceptors.post(
`/auth/refresh-token`,
{ refreshToken }
);
const data: PostAuthRefreshToken = response.data;
return data;
} catch (error) {
if (error instanceof AxiosError) {
console.log(error.response);
alert(error.response?.data?.message);
throw new Error();
}
throw new Error();
}
};
4 changes: 4 additions & 0 deletions src/shared/api/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,7 @@ export interface SignInResponseData {
createdAt: string;
};
}

export interface PostAuthRefreshToken {
accessToken: string;
}

0 comments on commit 4921433

Please sign in to comment.