From 9ff4b43c5b14fb2faaa4a79d9d49c4c66b379a85 Mon Sep 17 00:00:00 2001 From: oaoong Date: Wed, 1 Nov 2023 22:37:10 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20fetch=20class=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../(routes)/(home)/components/TestBlock.tsx | 1 - src/app/layout.tsx | 25 ++++++++---------- src/contexts/FetchContext.tsx | 26 ------------------- src/lib/fetchAPI.tsx | 4 +-- src/services/apiClient.ts | 8 ++++++ src/services/test/test.ts | 5 ++-- 6 files changed, 22 insertions(+), 47 deletions(-) delete mode 100644 src/contexts/FetchContext.tsx create mode 100644 src/services/apiClient.ts diff --git a/src/app/(root)/(routes)/(home)/components/TestBlock.tsx b/src/app/(root)/(routes)/(home)/components/TestBlock.tsx index 59cd8801..cb336899 100644 --- a/src/app/(root)/(routes)/(home)/components/TestBlock.tsx +++ b/src/app/(root)/(routes)/(home)/components/TestBlock.tsx @@ -1,7 +1,6 @@ 'use client' import React, { useEffect } from 'react' -// import { useApiClient } from '@/contexts/FetchContext' import { getTest } from '@/services/test/test' // async function getTestValue() { diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 235bbfba..c0add4dc 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,7 +1,6 @@ import { Suspense } from 'react' import type { Metadata } from 'next' import Header from '@/components/domain/Header' -import { FetchProvider } from '@/contexts/FetchContext' import MSWWrapper from '@/contexts/MSWWrapper' import TanstackQueryContext from '@/contexts/TanstackQueryContext' import ThemeProviderContext from '@/contexts/ThemeProviderContext' @@ -24,19 +23,17 @@ export default function RootLayout({ - - - - loading...}> -
-
- {children} - {authModal} -
-
-
-
-
+ + + loading...}> +
+
+ {children} + {authModal} +
+
+
+
diff --git a/src/contexts/FetchContext.tsx b/src/contexts/FetchContext.tsx deleted file mode 100644 index e265b6c0..00000000 --- a/src/contexts/FetchContext.tsx +++ /dev/null @@ -1,26 +0,0 @@ -'use client' - -import React, { ReactNode, createContext, useContext, useMemo } from 'react' -import FetchAPI from '@/lib/fetchAPI' - -const FetchContext = createContext<{ api: FetchAPI }>({ api: {} as FetchAPI }) - -export const FetchProvider = ({ children }: { children: ReactNode }) => { - const api = FetchAPI.getInstance() - - const contextValue = useMemo(() => ({ api }), [api]) - - return ( - - {children} - - ) -} - -export const useApiClient = () => { - const context = useContext(FetchContext) - if (!context) { - throw new Error('useApiClient must be used within a FetchProvider') - } - return context -} diff --git a/src/lib/fetchAPI.tsx b/src/lib/fetchAPI.tsx index 744c3a51..0d053daf 100644 --- a/src/lib/fetchAPI.tsx +++ b/src/lib/fetchAPI.tsx @@ -1,5 +1,3 @@ -import { Environment } from '@/config/environment' - class FetchAPI { private baseURL: string private headers: { [key: string]: string } @@ -7,7 +5,7 @@ class FetchAPI { private static instance: FetchAPI private constructor() { - this.baseURL = Environment.apiAddress() ?? '' + this.baseURL = '' this.headers = { 'Content-Type': 'application/json', } diff --git a/src/services/apiClient.ts b/src/services/apiClient.ts new file mode 100644 index 00000000..ad838650 --- /dev/null +++ b/src/services/apiClient.ts @@ -0,0 +1,8 @@ +import { Environment } from '@/config/environment' +import FetchAPI from '@/lib/fetchAPI' + +const apiClient = FetchAPI.getInstance() +apiClient.setDefaultHeader('Content-Type', 'application/json') +apiClient.setBaseURL(Environment.apiAddress() ?? '') + +export default apiClient diff --git a/src/services/test/test.ts b/src/services/test/test.ts index 849d6135..6d82ebcd 100644 --- a/src/services/test/test.ts +++ b/src/services/test/test.ts @@ -1,9 +1,8 @@ import ApiEndPoint from '@/config/apiEndPoint' -import FetchAPI from '@/lib/fetchAPI' +import apiClient from '../apiClient' const getTest = async () => { - const api = FetchAPI.getInstance() - const response = await api.get( + const response = await apiClient.get( ApiEndPoint.test(), { next: { revalidate: 10 } }, {