-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
57fe9f1
commit b759c95
Showing
4 changed files
with
74 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useAxiosInterceptor } from '@/app/_hooks/useAxiosInterceptor' | ||
|
||
interface Props { | ||
children: React.ReactNode | ||
} | ||
|
||
const AxiosProvider = ({ children }: Props) => { | ||
useAxiosInterceptor() | ||
|
||
return <>{children}</> | ||
} | ||
|
||
export default AxiosProvider |
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 @@ | ||
'use client' | ||
|
||
import { useAxiosInterceptor } from '@/app/_hooks/useAxiosInterceptor' | ||
|
||
import AxiosProvider from './AxiosInterceptorProvider' | ||
import QueryClientProvider from './QueryClientProvider' | ||
|
||
export default function CoreProvider({ children }: { children: React.ReactNode }) { | ||
useAxiosInterceptor() | ||
|
||
return ( | ||
<AxiosProvider> | ||
<QueryClientProvider>{children}</QueryClientProvider> | ||
</AxiosProvider> | ||
) | ||
} |
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,41 @@ | ||
import { useEffect } from 'react' | ||
|
||
import { type AxiosError, type AxiosResponse, type InternalAxiosRequestConfig } from 'axios' | ||
|
||
import { axiosInstance } from '../_service/core/api' | ||
import { getAccessToken } from '../_utils/token' | ||
|
||
export const useAxiosInterceptor = () => { | ||
const requestHandler = (config: InternalAxiosRequestConfig) => { | ||
const accessToken = getAccessToken() | ||
|
||
if (accessToken !== null) { | ||
// eslint-disable-next-line no-param-reassign | ||
config.headers.Authorization = `${accessToken}` | ||
} | ||
|
||
return config | ||
} | ||
|
||
const requestErrorHandler = (error: AxiosError) => { | ||
return Promise.reject(error) | ||
} | ||
|
||
const responseHandler = (response: AxiosResponse) => { | ||
return response | ||
} | ||
|
||
const responseErrorHandler = (error: any) => { | ||
return Promise.reject(error) | ||
} | ||
|
||
const requestInterceptor = axiosInstance.interceptors.request.use(requestHandler, requestErrorHandler) | ||
const responseInterceptor = axiosInstance.interceptors.response.use(responseHandler, responseErrorHandler) | ||
|
||
useEffect(() => { | ||
return () => { | ||
axiosInstance.interceptors.request.eject(requestInterceptor) | ||
axiosInstance.interceptors.request.eject(responseInterceptor) | ||
} | ||
}, [requestInterceptor, responseInterceptor]) | ||
} |