fix: the first function call creates multiple axios instances (#3273)

This commit is contained in:
TianHua Liu 2025-04-07 16:50:53 +08:00 committed by GitHub
parent c894a15d13
commit 49c81f6201
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,13 +3,9 @@ import { getClashInfo } from "./cmds";
import { invoke } from "@tauri-apps/api/core"; import { invoke } from "@tauri-apps/api/core";
import { useLockFn } from "ahooks"; import { useLockFn } from "ahooks";
let axiosIns: AxiosInstance = null!; let instancePromise: Promise<AxiosInstance> = null!;
/// initialize some information
/// enable force update axiosIns
export const getAxios = async (force: boolean = false) => {
if (axiosIns && !force) return axiosIns;
async function getInstancePromise() {
let server = ""; let server = "";
let secret = ""; let secret = "";
@ -26,13 +22,22 @@ export const getAxios = async (force: boolean = false) => {
if (info?.secret) secret = info?.secret; if (info?.secret) secret = info?.secret;
} catch {} } catch {}
axiosIns = axios.create({ const axiosIns = axios.create({
baseURL: `http://${server}`, baseURL: `http://${server}`,
headers: secret ? { Authorization: `Bearer ${secret}` } : {}, headers: secret ? { Authorization: `Bearer ${secret}` } : {},
timeout: 15000, timeout: 15000,
}); });
axiosIns.interceptors.response.use((r) => r.data); axiosIns.interceptors.response.use((r) => r.data);
return axiosIns; return axiosIns;
}
/// initialize some information
/// enable force update axiosIns
export const getAxios = async (force: boolean = false) => {
if (!instancePromise || force) {
instancePromise = getInstancePromise();
}
return instancePromise;
}; };
/// Get Version /// Get Version