1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-08-07 14:55:27 +02:00

chore(deps): upgrade axios [EE-6488] (#10885)

Co-authored-by: Matt Hook <hookenz@gmail.com>
This commit is contained in:
Chaim Lev-Ari 2024-01-02 13:26:54 +07:00 committed by GitHub
parent 4c226d7a17
commit a1519ba737
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 116 additions and 57 deletions

View file

@ -1,4 +1,4 @@
import axiosOrigin, { AxiosError, AxiosRequestConfig } from 'axios';
import axiosOrigin, { AxiosError, InternalAxiosRequestConfig } from 'axios';
import { setupCache } from 'axios-cache-adapter';
import { loadProgressBar } from 'axios-progress-bar';
@ -18,14 +18,18 @@ export const cache = setupCache({
exclude: {
query: false, // include urls with query params
methods: ['put', 'patch', 'delete'],
filter: (req: AxiosRequestConfig) => {
filter: (req: InternalAxiosRequestConfig) => {
// exclude caching get requests unless the path contains 'kubernetes'
if (!req.url?.includes('kubernetes') && req.method === 'get') {
return true;
}
// exclude caching get with yaml accept header
if (req.headers?.Accept.includes('application/yaml')) {
const acceptHeader = req.headers?.Accept;
if (
acceptHeader &&
typeof acceptHeader === 'string' &&
acceptHeader.includes('application/yaml')
) {
return true;
}
@ -59,12 +63,12 @@ export default axios;
export const agentTargetHeader = 'X-PortainerAgent-Target';
export function agentInterceptor(config: AxiosRequestConfig) {
export function agentInterceptor(config: InternalAxiosRequestConfig) {
if (!config.url || !config.url.includes('/docker/')) {
return config;
}
const newConfig = { headers: config.headers || {}, ...config };
const newConfig = { ...config };
const target = portainerAgentTargetHeader();
if (target) {
newConfig.headers[agentTargetHeader] = target;
@ -135,16 +139,40 @@ export function parseAxiosError(
return new PortainerError(resultMsg, resultErr);
}
export function defaultErrorParser(axiosError: AxiosError) {
const message = axiosError.response?.data.message || '';
const details = axiosError.response?.data.details || message;
const error = new Error(message);
type DefaultAxiosErrorType = {
message: string;
details?: string;
};
export function defaultErrorParser(axiosError: AxiosError<unknown>) {
if (isDefaultResponse(axiosError.response?.data)) {
const message = axiosError.response?.data.message || '';
const details = axiosError.response?.data.details || message;
const error = new Error(message);
return { error, details };
}
const details = axiosError.response?.data
? axiosError.response?.data.toString()
: '';
const error = new Error('Axios error');
return { error, details };
}
export function isAxiosError<
ResponseType = { message: string; details: string },
>(error: unknown): error is AxiosError<ResponseType> {
export function isDefaultResponse(
data: unknown
): data is DefaultAxiosErrorType {
return (
!!data &&
typeof data === 'object' &&
'message' in data &&
typeof data.message === 'string'
);
}
export function isAxiosError<ResponseType>(
error: unknown
): error is AxiosError<ResponseType> {
return axiosOrigin.isAxiosError(error);
}

View file

@ -1,4 +1,4 @@
import { AxiosRequestConfig, AxiosResponse } from 'axios';
import { InternalAxiosRequestConfig, AxiosResponse } from 'axios';
import { IHttpResponse } from 'angular';
import axios from './axios';
@ -26,12 +26,12 @@ export function csrfTokenReaderInterceptorAngular(
return config;
}
export function csrfInterceptor(config: AxiosRequestConfig) {
export function csrfInterceptor(config: InternalAxiosRequestConfig) {
if (!csrfToken) {
return config;
}
const newConfig = { headers: config.headers || {}, ...config };
const newConfig = { ...config };
newConfig.headers['X-CSRF-Token'] = csrfToken;
return newConfig;
}