Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 32x 1x 1x 1x 1x 1x 1x 1x | import axios from 'axios';
import { getAccessToken } from '@/lib/auth/token';
import { CommonErrorResponse } from '@/types/service/common';
import { API } from '../..';
import { createApiHelper } from '../lib/apiHelper';
const baseInstance = axios.create({
baseURL: process.env.NEXT_PUBLIC_API_BASE_URL,
timeout: 20000,
});
baseInstance.interceptors.request.use(async (config) => {
const token = await getAccessToken();
if (token && config.headers) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
baseInstance.interceptors.response.use(
(response) => response,
async (error) => {
const isServer = typeof window === 'undefined';
const errorResponse = new CommonErrorResponse(error.response?.data);
const status = errorResponse.status;
const originalRequest = error.config;
if (status === 401 && !originalRequest._retry) {
originalRequest._retry = true;
try {
await API.authService.refresh();
return baseInstance(originalRequest);
} catch (refreshError) {
if (isServer) {
throw refreshError;
} else {
if (window.location.pathname === '/login') {
throw errorResponse;
}
const currentPath = window.location.pathname + window.location.search;
window.location.href = `/login?error=unauthorized&path=${encodeURIComponent(currentPath)}`;
return;
}
}
}
if (status === 404) {
if (isServer) {
const { notFound } = await import('next/navigation');
notFound();
}
}
throw errorResponse;
},
);
export const baseAPI = createApiHelper(baseInstance);
|