1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-25 08:19:40 +02:00
portainer/app/portainer/services/axios.ts
Oscar Zhou 2059a9e064
feat(adminmonitor): redirect to timeout page if admin is not created in 5 mins [EE-2691] (#6688)
This PR solves the issue that the Portainer instance will be always accessible in certain cases, like `restart: always` setting with docker run, even if the administrator is not created in the first 5 minutes. 
The solution is that the user will be redirected to a timeout page when any actions, such as refresh the page and click button, are made after administrator initialisation window(5 minutes) timeout.
2022-04-05 16:29:57 +12:00

72 lines
1.9 KiB
TypeScript

import axiosOrigin, { AxiosError, AxiosRequestConfig } from 'axios';
import { loadProgressBar } from 'axios-progress-bar';
import 'axios-progress-bar/dist/nprogress.css';
import PortainerError from '@/portainer/error';
import { get as localStorageGet } from '@/portainer/hooks/useLocalStorage';
import {
portainerAgentManagerOperation,
portainerAgentTargetHeader,
} from './http-request.helper';
const axios = axiosOrigin.create({ baseURL: 'api' });
loadProgressBar(undefined, axios);
export default axios;
axios.interceptors.request.use(async (config) => {
const newConfig = { headers: config.headers || {}, ...config };
const jwt = localStorageGet('JWT', '');
if (jwt) {
newConfig.headers.Authorization = `Bearer ${jwt}`;
}
return newConfig;
});
export function agentInterceptor(config: AxiosRequestConfig) {
if (!config.url || !config.url.includes('/docker/')) {
return config;
}
const newConfig = { headers: config.headers || {}, ...config };
const target = portainerAgentTargetHeader();
if (target) {
newConfig.headers['X-PortainerAgent-Target'] = target;
}
if (portainerAgentManagerOperation()) {
newConfig.headers['X-PortainerAgent-ManagerOperation'] = '1';
}
return newConfig;
}
axios.interceptors.request.use(agentInterceptor);
export function parseAxiosError(
err: Error,
msg = '',
parseError = defaultErrorParser
) {
let resultErr = err;
let resultMsg = msg;
if ('isAxiosError' in err) {
const { error, details } = parseError(err as AxiosError);
resultErr = error;
resultMsg = msg ? `${msg}: ${details}` : details;
}
return new PortainerError(resultMsg, resultErr);
}
function defaultErrorParser(axiosError: AxiosError) {
const message = axiosError.response?.data.message;
const details = axiosError.response?.data.details || message;
const error = new Error(message);
return { error, details };
}