Skip to content

Commit

Permalink
Refactor: axios 공용 인터셉터 추출
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdchanii committed Dec 19, 2024
1 parent dc1a9ce commit 8faf5dc
Showing 1 changed file with 21 additions and 56 deletions.
77 changes: 21 additions & 56 deletions utils/axios.ts
Original file line number Diff line number Diff line change
@@ -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 ?? '/';
Expand All @@ -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<ErrorPayload>(
error: unknown
Expand All @@ -80,3 +29,19 @@ export {
instanceInAgenda,
isAxiosError,
};

function setConfig<T>(config: AxiosRequestConfig<T>) {
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);
}

0 comments on commit 8faf5dc

Please sign in to comment.