Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Axios 공용 인터셉터 추출 #1579

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}