-
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.
π§ feat: api ν΅μ μν axios instance μ€μ μΆκ° #1
- Loading branch information
1 parent
20d57a8
commit 5cbb712
Showing
2 changed files
with
99 additions
and
0 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,76 @@ | ||
import { getAccessToken } from '@/utils/auth'; | ||
import axios, { AxiosInstance } from 'axios'; | ||
|
||
/** | ||
* Axios μΈμ€ν΄μ€μ μΈν°μ ν°λ₯Ό μ€μ νλ ν¨μ | ||
* | ||
* @function setInterceptors | ||
* @param {AxiosInstance} instance - μΈν°μ ν°λ₯Ό μ€μ ν Axios μΈμ€ν΄μ€ | ||
* @returns {AxiosInstance} μΈν°μ ν°κ° μ€μ λ Axios μΈμ€ν΄μ€ | ||
* | ||
* @description | ||
* μ΄ ν¨μλ μμ² μΈν°μ ν°λ₯Ό μ€μ νμ¬ κ° API μμ²μ μΈμ¦ ν ν°μ μΆκ°ν©λλ€. | ||
* ν΄λΌμ΄μΈνΈ μ¬μ΄λμμλ§ ν ν°μ μΆκ°νλ©°, μλ² μ¬μ΄λ λ λλ§ μμλ ν ν°μ μΆκ°νμ§ μμ΅λλ€. | ||
*/ | ||
function setInterceptors(instance: AxiosInstance) { | ||
instance.interceptors.request.use( | ||
(config) => { | ||
if (typeof window !== 'undefined' && config.headers) { | ||
config.headers.Authorization = `Bearer ${getAccessToken()}`; | ||
} | ||
return config; | ||
}, | ||
(error) => { | ||
return Promise.reject(error); | ||
}, | ||
); | ||
|
||
return instance; | ||
} | ||
|
||
/** | ||
* μΈμ¦μ΄ νμν API μμ²μ μν Axios μΈμ€ν΄μ€λ₯Ό μμ±νλ ν¨μ | ||
* | ||
* @function createInstance | ||
* @returns {AxiosInstance} μΈν°μ ν°κ° μ€μ λ Axios μΈμ€ν΄μ€ | ||
* | ||
* @description | ||
* μ΄ ν¨μλ κΈ°λ³Έ URLμ΄ μ€μ λ Axios μΈμ€ν΄μ€λ₯Ό μμ±νκ³ , | ||
* setInterceptors ν¨μλ₯Ό ν΅ν΄ μΈμ¦ ν ν°μ μλμΌλ‘ μΆκ°νλ μΈν°μ ν°λ₯Ό μ€μ ν©λλ€. | ||
*/ | ||
function createInstance() { | ||
const instance = axios.create({ | ||
baseURL: process.env.NEXT_PUBLIC_API_URL, | ||
}); | ||
return setInterceptors(instance); | ||
} | ||
|
||
/** | ||
* μΈμ¦μ΄ νμνμ§ μμ API μμ²μ μν Axios μΈμ€ν΄μ€λ₯Ό μμ±νλ ν¨μ | ||
* | ||
* @function createInstanceWithoutAuth | ||
* @returns {AxiosInstance} κΈ°λ³Έ μ€μ λ§ λ Axios μΈμ€ν΄μ€ | ||
* | ||
* @description | ||
* μ΄ ν¨μλ κΈ°λ³Έ URLλ§ μ€μ λ Axios μΈμ€ν΄μ€λ₯Ό μμ±ν©λλ€. | ||
* μΈμ¦ ν ν°μ΄ νμνμ§ μμ API μμ²(μ: λ‘κ·ΈμΈ, νμκ°μ λ±)μ μ¬μ©λ©λλ€. | ||
*/ | ||
// Todo: .env νμΌμμ νκ²½λ³μλ‘ api μ£Όμλ₯Ό κ°μ Έμ€κ³ μμ§λ§, μλ² μ£Όμκ° λμ€λ©΄ env νμΌ μμ΄λ μ¬μ© κ°λ₯νκ² μμ ν΄μΌ ν¨. | ||
function createInstanceWithoutAuth() { | ||
const instance = axios.create({ | ||
baseURL: process.env.NEXT_PUBLIC_API_URL, | ||
}); | ||
return instance; | ||
} | ||
|
||
/** | ||
* μΈμ¦μ΄ νμν API μμ²μ μ¬μ©ν Axios μΈμ€ν΄μ€ | ||
* @const {AxiosInstance} | ||
*/ | ||
export const api = createInstance(); | ||
|
||
/** | ||
* μΈμ¦μ΄ νμνμ§ μμ API μμ²μ μ¬μ©ν Axios μΈμ€ν΄μ€ | ||
* @const {AxiosInstance} | ||
*/ | ||
export const apiWithoutAuth = createInstanceWithoutAuth(); |
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,23 @@ | ||
export const setAccessToken = (token: string | null) => { | ||
localStorage.setItem('authorization', token || ''); | ||
}; | ||
|
||
/** | ||
* λ‘컬 μ€ν 리μ§μμ μΈμ¦ ν ν°μ κ°μ Έμ€λ ν¨μ | ||
* | ||
* @function getAccessToken | ||
* @returns {string | null} μ μ₯λ μΈμ¦ ν ν° λλ null | ||
* | ||
* @description | ||
* μ΄ ν¨μλ λ‘컬 μ€ν 리μ§μμ 'authorization' ν€λ‘ μ μ₯λ μΈμ¦ ν ν°μ λ°νν©λλ€. | ||
* ν ν°μ΄ μλ κ²½μ° nullμ λ°νν©λλ€. | ||
*/ | ||
// @utils/auth.ts | ||
export const getAccessToken = () => { | ||
return localStorage.getItem('authorization'); | ||
}; | ||
|
||
|
||
export const deleteAccessToken = () => { | ||
localStorage.removeItem('authorization'); | ||
}; |