-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbase.ts
82 lines (66 loc) · 2.59 KB
/
base.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import AxiosWrapper from '@gravity-ui/axios-wrapper';
import type {AxiosWrapperOptions} from '@gravity-ui/axios-wrapper';
import axiosRetry from 'axios-retry';
import {backend as BACKEND} from '../../store';
import {DEV_ENABLE_TRACING_FOR_ALL_REQUESTS} from '../../utils/constants';
import {isRedirectToAuth} from '../../utils/response';
import {settingsManager} from '../settings';
export type AxiosOptions = {
concurrentId?: string;
signal?: AbortSignal;
withRetries?: boolean;
};
export class BaseYdbAPI extends AxiosWrapper {
DEFAULT_RETRIES_COUNT = 0;
constructor(options?: AxiosWrapperOptions) {
super(options);
axiosRetry(this._axios, {
retries: this.DEFAULT_RETRIES_COUNT,
retryDelay: axiosRetry.exponentialDelay,
});
// Make possible manually enable tracing for all requests
// For development purposes
this._axios.interceptors.request.use(function (config) {
const enableTracing = settingsManager.readUserSettingsValue(
DEV_ENABLE_TRACING_FOR_ALL_REQUESTS,
);
if (enableTracing) {
config.headers['X-Want-Trace'] = 1;
}
return config;
});
// Add traceId to response if it exists
this._axios.interceptors.response.use(function (response) {
if (
response.data &&
response.data instanceof Object &&
!Array.isArray(response.data) &&
response.headers['traceresponse']
) {
const traceId = response.headers['traceresponse'].split('-')[1];
response.data = {
...response.data,
_meta: {...response.data._meta, traceId},
};
}
return response;
});
// Interceptor to process OIDC auth
this._axios.interceptors.response.use(null, function (error) {
const response = error.response;
// OIDC proxy returns 401 response with authUrl in it
// authUrl - external auth service link, after successful auth additional cookies will be appended
// that will allow access to clusters where OIDC proxy is a balancer
if (isRedirectToAuth(response)) {
window.location.assign(response.data.authUrl);
}
return Promise.reject(error);
});
}
getPath(path: string) {
return `${BACKEND ?? ''}${path}`;
}
prepareArrayRequestParam(arr: (string | number)[]) {
return arr.join(',');
}
}