-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from team-nabi/NABI-77
🎉 Nabi 77 fetch 관련 설정
- Loading branch information
Showing
14 changed files
with
1,104 additions
and
752 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
NEXT_PUBLIC_API_ADDRESS=http://localhost:3000 | ||
NEXT_PUBLIC_API_MOCKING=enabled # or disabled | ||
CHROMATIC_TOKEN=your-token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use client' | ||
|
||
import React, { useEffect } from 'react' | ||
// import { useApiClient } from '@/contexts/FetchContext' | ||
import { getTest } from '@/services/test/test' | ||
|
||
// async function getTestValue() { | ||
// const res = await getTest() | ||
// const data = await res.json() | ||
// return data | ||
// } | ||
|
||
export default function TestBlock() { | ||
// const data = await getTestValue() | ||
// console.log(data.message) | ||
|
||
useEffect(() => { | ||
async function fetchData() { | ||
const data = await getTest() | ||
console.log(await data.json()) | ||
} | ||
fetchData() | ||
}, []) | ||
|
||
return <div>{'index '}</div> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const ApiEndPoint = { | ||
login: () => '/login', | ||
test: () => '/test', | ||
} as const | ||
|
||
export default ApiEndPoint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import { assertValue } from '@/utils/assertValue' | ||
|
||
export const Environment = { | ||
apiAddress: () => process.env.NEXT_PUBLIC_API_ADDRESS, | ||
apiMocking: () => | ||
process.env.NEXT_PUBLIC_API_MOCKING === 'enabled' ? 'enabled' : 'disabled', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'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 ( | ||
<FetchContext.Provider value={contextValue}> | ||
{children} | ||
</FetchContext.Provider> | ||
) | ||
} | ||
|
||
export const useApiClient = () => { | ||
const context = useContext(FetchContext) | ||
if (!context) { | ||
throw new Error('useApiClient must be used within a FetchProvider') | ||
} | ||
return context | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use client' | ||
|
||
import { useState, type PropsWithChildren, useEffect } from 'react' | ||
import { Environment } from '@/config/environment' | ||
|
||
const isMockingMode = Environment.apiMocking() === 'enabled' | ||
|
||
const MSWComponent = ({ children }: PropsWithChildren) => { | ||
const [mswReady, setMSWReady] = useState(() => !isMockingMode) | ||
|
||
useEffect(() => { | ||
const init = async () => { | ||
if (isMockingMode) { | ||
const initMocks = await import('../lib/msw/initMockApi').then( | ||
(res) => res.initMockApi, | ||
) | ||
await initMocks() | ||
setMSWReady(true) | ||
} | ||
} | ||
|
||
if (!mswReady) { | ||
init() | ||
} | ||
}, [mswReady]) | ||
|
||
if (!mswReady) { | ||
return null | ||
} | ||
|
||
return <>{children}</> | ||
} | ||
|
||
export default MSWComponent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { Environment } from '@/config/environment' | ||
|
||
class FetchAPI { | ||
private baseURL: string | ||
private headers: { [key: string]: string } | ||
|
||
private static instance: FetchAPI | ||
|
||
private constructor() { | ||
this.baseURL = Environment.apiAddress() ?? '' | ||
this.headers = { | ||
'Content-Type': 'application/json', | ||
} | ||
} | ||
|
||
public static getInstance(): FetchAPI { | ||
if (!FetchAPI.instance) { | ||
FetchAPI.instance = new FetchAPI() | ||
} | ||
return FetchAPI.instance | ||
} | ||
|
||
public setBaseURL(url: string): void { | ||
this.baseURL = url | ||
} | ||
|
||
public setDefaultHeader(key: string, value: string): void { | ||
this.headers[key] = value | ||
} | ||
|
||
public async get( | ||
endpoint: string, | ||
nextInit: RequestInit = {}, | ||
customHeaders: { [key: string]: string } = {}, | ||
): Promise<any> { | ||
const response = await fetch(`${this.baseURL}${endpoint}`, { | ||
method: 'GET', | ||
headers: { ...this.headers, ...customHeaders }, | ||
...nextInit, | ||
}) | ||
return response | ||
} | ||
|
||
public async post( | ||
endpoint: string, | ||
body: any, | ||
nextInit: RequestInit = {}, | ||
customHeaders: { [key: string]: string } = {}, | ||
): Promise<any> { | ||
const response = await fetch(`${this.baseURL}${endpoint}`, { | ||
method: 'POST', | ||
headers: { ...this.headers, ...customHeaders }, | ||
body: JSON.stringify(body), | ||
...nextInit, | ||
}) | ||
return response | ||
} | ||
|
||
public async put( | ||
endpoint: string, | ||
body: any, | ||
nextInit: RequestInit = {}, | ||
customHeaders: { [key: string]: string } = {}, | ||
): Promise<any> { | ||
const response = await fetch(`${this.baseURL}${endpoint}`, { | ||
method: 'PUT', | ||
headers: { ...this.headers, ...customHeaders }, | ||
body: JSON.stringify(body), | ||
...nextInit, | ||
}) | ||
return response | ||
} | ||
|
||
public async delete( | ||
endpoint: string, | ||
nextInit: RequestInit = {}, | ||
customHeaders: { [key: string]: string } = {}, | ||
): Promise<any> { | ||
const response = await fetch(`${this.baseURL}${endpoint}`, { | ||
method: 'DELETE', | ||
headers: { ...this.headers, ...customHeaders }, | ||
...nextInit, | ||
}) | ||
return response | ||
} | ||
} | ||
|
||
export default FetchAPI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import ApiEndPoint from '@/config/apiEndPoint' | ||
import { Environment } from '@/config/environment' | ||
|
||
const postLogin = async () => { | ||
const response = await fetch(Environment.apiAddress() + ApiEndPoint.login(), { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}) | ||
const data = await response.json() | ||
return data | ||
} | ||
|
||
export { postLogin } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import ApiEndPoint from '@/config/apiEndPoint' | ||
import FetchAPI from '@/lib/fetchAPI' | ||
|
||
const getTest = async () => { | ||
const api = FetchAPI.getInstance() | ||
const response = await api.get( | ||
ApiEndPoint.test(), | ||
{ next: { revalidate: 10 } }, | ||
{ | ||
'Content-Type': 'application/json', | ||
}, | ||
) | ||
return response | ||
} | ||
|
||
export { getTest } |