Skip to content

Commit

Permalink
✨ feat : privateApi config
Browse files Browse the repository at this point in the history
  • Loading branch information
seondal committed Apr 2, 2024
1 parent ec4a423 commit b566199
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 18 deletions.
18 changes: 8 additions & 10 deletions src/apis/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PoseTalkResponse,
RegisterResponse,
} from '.';
import privateApi from './config/privateApi';
import publicApi from './config/publicApi';
import { KAKAO_REDIRECT_URI } from '@/constants/env';

Expand All @@ -24,7 +25,7 @@ export const getPoseFeed = async (
tags: string,
pageNumber: number
) =>
await publicApi.get<PoseFeedResponse>(`/pose`, {
await privateApi.get<PoseFeedResponse>(`/pose`, {
params: {
frameCount,
pageNumber,
Expand Down Expand Up @@ -57,20 +58,17 @@ export const patchDeleteAccount = (
withdrawalReason,
});

export const postBookmark = (accesstoken: string, poseId: number) =>
publicApi.post(`/bookmark`, null, {
export const postBookmark = (poseId: number) =>
privateApi.post(`/bookmark`, null, {
params: { poseId },
headers: { Authorization: `Bearer ${accesstoken}` },
});

export const deleteBookmark = (accesstoken: string, poseId: number) =>
publicApi.delete(`/bookmark`, {
export const deleteBookmark = (poseId: number) =>
privateApi.delete(`/bookmark`, {
params: { poseId },
headers: { Authorization: `Bearer ${accesstoken}` },
});

export const getBookmarkFeed = (accesstoken: string, pageNumber: number) =>
publicApi.get<PoseFeedContents>('/bookmark/feed', {
headers: { Authorization: `Bearer ${accesstoken}` },
export const getBookmarkFeed = (pageNumber: number) =>
privateApi.get<PoseFeedContents>('/bookmark/feed', {
params: { pageNumber, pageSize: 10 },
});
34 changes: 34 additions & 0 deletions src/apis/config/privateApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import axios from 'axios';

import { CustomInstance } from './type';
import { BASE_API_URL } from '@/constants/env';

function getAccesstoken() {
if (typeof window !== 'undefined') {
const item = localStorage.getItem('accesstoken');
return item;
}
}

const privateApi: CustomInstance = axios.create({
baseURL: `${BASE_API_URL}/api`,
withCredentials: true,
});

privateApi.interceptors.response.use((response) => response.data);
privateApi.interceptors.request.use(
(config) => {
const accessToken = getAccesstoken();

if (accessToken) {
config.headers.Authorization = `Bearer ${accessToken}`;
}

return config;
},
(error) => {
return Promise.reject(error);
}
);

export default privateApi;
2 changes: 1 addition & 1 deletion src/apis/config/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ export interface CustomInstance extends AxiosInstance {
patch<T>(...params: Parameters<AxiosInstance['patch']>): Promise<T>;
}

export type ErrorStatus = 400 | 401 | 403 | 412 | 500;
export type ErrorStatus = 400 | 401 | 403 | 412 | 500 | 415;
7 changes: 4 additions & 3 deletions src/apis/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export const usePoseTalkQuery = (options?: UseQueryOptions<PoseTalkResponse>) =>
export const usePoseFeedQuery = (
{ peopleCount, frameCount, tags }: FilterState,
options?: UseInfiniteQueryOptions<PoseFeedResponse>
) =>
useSuspenseInfiniteQuery<PoseFeedResponse>(
) => {
return useSuspenseInfiniteQuery<PoseFeedResponse>(
['poseFeed', peopleCount, frameCount, tags],
({ pageParam = 0 }) => getPoseFeed(peopleCount, frameCount, tags.join(','), pageParam),
{
Expand All @@ -52,14 +52,15 @@ export const usePoseFeedQuery = (
...options,
}
);
};

export const useBookmarkFeedQuery = (
accesstoken: string,
options?: UseInfiniteQueryOptions<PoseFeedContents>
) =>
useSuspenseInfiniteQuery<PoseFeedContents>(
['bookmarkFeed'],
({ pageParam = 0 }) => getBookmarkFeed(accesstoken, pageParam),
({ pageParam = 0 }) => getBookmarkFeed(pageParam),
{
getNextPageParam: (lastPage) => {
return lastPage.last ? undefined : lastPage.number + 1;
Expand Down
8 changes: 7 additions & 1 deletion src/app/(Main)/mypose/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import { QueryAsyncBoundary } from '@suspensive/react-query';
import LoginSection from './components/LoginSection';
import MyposeTab from './components/MyposeTab';
import { RejectedFallback } from '@/components/ErrorBoundary';
import { Loading } from '@/components/Loading';
import { StrictPropsWithChildren } from '@/types';

export default function Layout({ children }: StrictPropsWithChildren) {
return (
<>
<LoginSection />
<MyposeTab />
<QueryAsyncBoundary rejectedFallback={RejectedFallback}>{children}</QueryAsyncBoundary>
<QueryAsyncBoundary
rejectedFallback={RejectedFallback}
pendingFallback={<Loading className="h-[calc(100dvh-178px)]" />}
>
{children}
</QueryAsyncBoundary>
</>
);
}
2 changes: 1 addition & 1 deletion src/app/auth/logout/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export default function Page() {
useEffect(() => {
if (token) {
patchLogout(token.accessToken, token.refreshToken).then((response) => {
console.log(response);
alert('로그아웃 성공!');
localStorage.removeItem('accesstoken');
});
}
clearUser();
Expand Down
1 change: 1 addition & 0 deletions src/app/auth/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default function Page() {
if (code) {
getRegister(code).then((response) => {
setUser(response);
localStorage.setItem('accesstoken', response.token.accessToken);
alert(`로그인에 성공했어요!`);
router.back();
});
Expand Down
4 changes: 2 additions & 2 deletions src/components/Feed/BookmarkButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export default function BookmarkButton({ poseId, isMarked }: BookmarkButtonI) {
return;
}
if (marked) {
deleteBookmark(token?.accessToken, poseId).then(() => {
deleteBookmark(poseId).then(() => {
setMarked(false);
});
} else {
postBookmark(token?.accessToken, poseId).then(() => {
postBookmark(poseId).then(() => {
setMarked(true);
});
}
Expand Down

0 comments on commit b566199

Please sign in to comment.