- Implement `releaseHeaders.js` to manage release-related HTTP headers. - Update requests to include runtime-based release headers. - Extend i18n phrases across multiple locales for release runtime and configuration labels. - Enhance unit tests for header building and API availability error handling.
72 lines
2.2 KiB
JavaScript
72 lines
2.2 KiB
JavaScript
import axios from "axios";
|
|
import { enqueueRequest } from "@/services/requestQueue.js";
|
|
import { buildCurrentReleaseHeaders, rewriteReleaseApiUrl } from "@/services/releaseTimeline.js";
|
|
|
|
let requestInterceptorId = null;
|
|
|
|
const isQueueBypassed = (config) => config?.__skipRequestQueue === true;
|
|
|
|
const isAbsoluteUrl = (url) => /^https?:\/\//i.test(url || "");
|
|
|
|
const combineBaseUrlAndPath = (baseURL, url) => {
|
|
if (!url) {
|
|
return baseURL || "";
|
|
}
|
|
|
|
if (isAbsoluteUrl(url) || !baseURL) {
|
|
return url;
|
|
}
|
|
|
|
return `${String(baseURL).replace(/\/+$/, "")}/${String(url).replace(/^\/+/, "")}`;
|
|
};
|
|
|
|
export const installAxiosRequestQueue = () => {
|
|
if (!axios?.interceptors?.request) {
|
|
return;
|
|
}
|
|
|
|
if (requestInterceptorId !== null) {
|
|
return;
|
|
}
|
|
|
|
requestInterceptorId = axios.interceptors.request.use((config) => {
|
|
if (config?.__skipReleaseApiRewrite !== true && config?.url) {
|
|
config.url = rewriteReleaseApiUrl(config.url);
|
|
}
|
|
if (config?.__skipReleaseApiRewrite !== true && config?.baseURL) {
|
|
config.baseURL = rewriteReleaseApiUrl(config.baseURL);
|
|
}
|
|
if (config?.__skipReleaseApiRewrite !== true) {
|
|
config.headers = {
|
|
...buildCurrentReleaseHeaders(),
|
|
...(config.headers || {}),
|
|
};
|
|
}
|
|
|
|
if (isQueueBypassed(config) || config?.__queueAdapterWrapped) {
|
|
return config;
|
|
}
|
|
|
|
const resolvedAdapter = axios.getAdapter(config?.adapter ?? axios.defaults.adapter);
|
|
config.__queueAdapterWrapped = true;
|
|
config.adapter = (adapterConfig) =>
|
|
enqueueRequest(() => resolvedAdapter(adapterConfig), {
|
|
method: adapterConfig?.method ?? config?.method,
|
|
url: combineBaseUrlAndPath(adapterConfig?.baseURL ?? config?.baseURL, adapterConfig?.url ?? config?.url),
|
|
requestData: {
|
|
params: adapterConfig?.params ?? config?.params ?? null,
|
|
data: adapterConfig?.data ?? config?.data ?? null,
|
|
headers: adapterConfig?.headers ?? config?.headers ?? null,
|
|
},
|
|
});
|
|
return config;
|
|
});
|
|
};
|
|
|
|
export const __resetAxiosRequestQueueInstallerForTests = () => {
|
|
if (requestInterceptorId !== null && axios?.interceptors?.request) {
|
|
axios.interceptors.request.eject(requestInterceptorId);
|
|
}
|
|
requestInterceptorId = null;
|
|
};
|