Skip to content

Commit

Permalink
✨ add axios interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
wook-hyung committed Dec 15, 2023
1 parent 57fe9f1 commit b759c95
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
13 changes: 13 additions & 0 deletions app/_components/providers/AxiosInterceptorProvider.tsx
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
16 changes: 16 additions & 0 deletions app/_components/providers/CoreProvider.tsx
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>
)
}
8 changes: 4 additions & 4 deletions app/_components/providers/QueryClientProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import { type PropsWithChildren, useState } from 'react'

import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { QueryClient, QueryClientProvider as TanstackQueryProvider } from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'

export default function Providers({ children }: PropsWithChildren) {
export default function QueryClientProvider({ children }: PropsWithChildren) {
const [queryClient] = useState(
() =>
new QueryClient({
Expand All @@ -21,9 +21,9 @@ export default function Providers({ children }: PropsWithChildren) {
)

return (
<QueryClientProvider client={queryClient}>
<TanstackQueryProvider client={queryClient}>
<ReactQueryDevtools />
{children}
</QueryClientProvider>
</TanstackQueryProvider>
)
}
41 changes: 41 additions & 0 deletions app/_hooks/useAxiosInterceptor.ts
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])
}

0 comments on commit b759c95

Please sign in to comment.