diff --git a/utils/axios.ts b/utils/axios.ts index 82c952520..1576f7156 100644 --- a/utils/axios.ts +++ b/utils/axios.ts @@ -1,4 +1,4 @@ -import axios, { AxiosError } from 'axios'; +import axios, { AxiosError, AxiosRequestConfig } from 'axios'; const baseURL = `${process.env.NEXT_PUBLIC_SERVER_ENDPOINT}`; const manageBaseURL = process.env.NEXT_PUBLIC_MANAGE_SERVER_ENDPOINT ?? '/'; @@ -11,61 +11,10 @@ const instanceInManage = axios.create({ baseURL: manageBaseURL }); const instanceInPartyManage = axios.create({ baseURL: managePartyBaseURL }); const instanceInAgenda = axios.create({ baseURL: agendaBaseURL }); -instance.interceptors.request.use( - function setConfig(config) { - config.headers = { - 'Content-Type': 'application/json', - Authorization: `Bearer ${localStorage.getItem('42gg-token')}`, - }; - config.withCredentials = true; - return config; - }, - function getError(error) { - return Promise.reject(error); - } -); - -instanceInManage.interceptors.request.use( - function setConfig(config) { - config.headers = { - 'Content-Type': 'application/json', - Authorization: `Bearer ${localStorage.getItem('42gg-token')}`, - }; - config.withCredentials = true; - return config; - }, - function getError(error) { - return Promise.reject(error); - } -); - -instanceInPartyManage.interceptors.request.use( - function setConfig(config) { - config.headers = { - 'Content-Type': 'application/json', - Authorization: `Bearer ${localStorage.getItem('42gg-token')}`, - }; - config.withCredentials = true; - return config; - }, - function getError(error) { - return Promise.reject(error); - } -); - -instanceInAgenda.interceptors.request.use( - function setConfig(config) { - config.headers = { - 'Content-Type': 'application/json', - Authorization: `Bearer ${localStorage.getItem('42gg-token')}`, - }; - config.withCredentials = true; - return config; - }, - function getError(error) { - return Promise.reject(error); - } -); +instance.interceptors.request.use(setConfig, getError); +instanceInManage.interceptors.request.use(setConfig, getError); +instanceInPartyManage.interceptors.request.use(setConfig, getError); +instanceInAgenda.interceptors.request.use(setConfig, getError); function isAxiosError( error: unknown @@ -80,3 +29,19 @@ export { instanceInAgenda, isAxiosError, }; + +function setConfig(config: AxiosRequestConfig) { + return { + ...config, + headers: { + ...config.headers, + 'Content-Type': 'application/json', + Authorization: `Bearer ${localStorage.getItem('42gg-token') ?? ''}`, + withCredentials: true, + }, + }; +} + +function getError(error: unknown) { + return Promise.reject(error); +}