mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-04-16 23:40:32 +08:00
refactor: reorganize imports and remove unused axios instance code
This commit is contained in:
parent
a8fe5a71f7
commit
204bfa36e5
@ -1,11 +1,11 @@
|
|||||||
import {
|
import {
|
||||||
LocationOnOutlined,
|
LocationOnOutlined,
|
||||||
RefreshOutlined,
|
RefreshOutlined,
|
||||||
VisibilityOutlined,
|
|
||||||
VisibilityOffOutlined,
|
VisibilityOffOutlined,
|
||||||
|
VisibilityOutlined,
|
||||||
} from "@mui/icons-material";
|
} from "@mui/icons-material";
|
||||||
import { Box, Typography, Button, Skeleton, IconButton } from "@mui/material";
|
import { Box, Button, IconButton, Skeleton, Typography } from "@mui/material";
|
||||||
import { useState, useEffect, useCallback, memo } from "react";
|
import { memo, useCallback, useEffect, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
import { getIpInfo } from "@/services/api";
|
import { getIpInfo } from "@/services/api";
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import { useEffect } from "react";
|
|||||||
import { mutate } from "swr";
|
import { mutate } from "swr";
|
||||||
|
|
||||||
import { useListen } from "@/hooks/use-listen";
|
import { useListen } from "@/hooks/use-listen";
|
||||||
import { getAxios } from "@/services/api";
|
|
||||||
|
|
||||||
export const useLayoutEvents = (
|
export const useLayoutEvents = (
|
||||||
handleNotice: (payload: [string, string]) => void,
|
handleNotice: (payload: [string, string]) => void,
|
||||||
@ -39,7 +38,6 @@ export const useLayoutEvents = (
|
|||||||
|
|
||||||
register(
|
register(
|
||||||
addListener("verge://refresh-clash-config", async () => {
|
addListener("verge://refresh-clash-config", async () => {
|
||||||
await getAxios(true);
|
|
||||||
mutate("getProxies");
|
mutate("getProxies");
|
||||||
mutate("getVersion");
|
mutate("getVersion");
|
||||||
mutate("getClashConfig");
|
mutate("getClashConfig");
|
||||||
|
|||||||
@ -1,44 +1,4 @@
|
|||||||
import { fetch } from "@tauri-apps/plugin-http";
|
import { fetch } from "@tauri-apps/plugin-http";
|
||||||
import axios, { AxiosInstance } from "axios";
|
|
||||||
|
|
||||||
import { getClashInfo } from "./cmds";
|
|
||||||
|
|
||||||
let instancePromise: Promise<AxiosInstance> = null!;
|
|
||||||
|
|
||||||
async function getInstancePromise() {
|
|
||||||
let server = "";
|
|
||||||
let secret = "";
|
|
||||||
|
|
||||||
try {
|
|
||||||
const info = await getClashInfo();
|
|
||||||
|
|
||||||
if (info?.server) {
|
|
||||||
server = info.server;
|
|
||||||
|
|
||||||
// compatible width `external-controller`
|
|
||||||
if (server.startsWith(":")) server = `127.0.0.1${server}`;
|
|
||||||
else if (/^\d+$/.test(server)) server = `127.0.0.1:${server}`;
|
|
||||||
}
|
|
||||||
if (info?.secret) secret = info?.secret;
|
|
||||||
} catch {}
|
|
||||||
|
|
||||||
const axiosIns = axios.create({
|
|
||||||
baseURL: `http://${server}`,
|
|
||||||
headers: secret ? { Authorization: `Bearer ${secret}` } : {},
|
|
||||||
timeout: 15000,
|
|
||||||
});
|
|
||||||
axiosIns.interceptors.response.use((r) => r.data);
|
|
||||||
return axiosIns;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// initialize some information
|
|
||||||
/// enable force update axiosIns
|
|
||||||
export const getAxios = async (force: boolean = false) => {
|
|
||||||
if (!instancePromise || force) {
|
|
||||||
instancePromise = getInstancePromise();
|
|
||||||
}
|
|
||||||
return instancePromise;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Get current IP and geolocation information (refactored IP detection with service-specific mappings)
|
// Get current IP and geolocation information (refactored IP detection with service-specific mappings)
|
||||||
interface IpInfo {
|
interface IpInfo {
|
||||||
@ -213,6 +173,7 @@ export const getIpInfo = async (): Promise<IpInfo> => {
|
|||||||
timeoutId = setTimeout(() => {
|
timeoutId = setTimeout(() => {
|
||||||
timeoutController.abort();
|
timeoutController.abort();
|
||||||
}, service.timeout || serviceTimeout);
|
}, service.timeout || serviceTimeout);
|
||||||
|
console.debug("Fetching IP information...");
|
||||||
|
|
||||||
const response = await fetch(service.url, {
|
const response = await fetch(service.url, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user