feat: 修改后端apiclient生成逻辑
fix: 修复debugger获取flag失败的问题 refactor: 重新编写debugger前后端逻辑
This commit is contained in:
@@ -14,6 +14,7 @@ import {
|
||||
OscilloscopeApiClient,
|
||||
DebuggerClient,
|
||||
} from "@/APIClient";
|
||||
import axios, { type AxiosInstance } from "axios";
|
||||
|
||||
// 支持的客户端类型联合类型
|
||||
type SupportedClient =
|
||||
@@ -108,13 +109,27 @@ export class AuthManager {
|
||||
};
|
||||
}
|
||||
|
||||
// 私有方法:创建带认证的Axios实例
|
||||
private static createAuthenticatedAxiosInstance(): AxiosInstance | null {
|
||||
const token = AuthManager.getToken();
|
||||
if (!token) return null;
|
||||
|
||||
const instance = axios.create();
|
||||
instance.interceptors.request.use(config => {
|
||||
config.headers = config.headers || {};
|
||||
(config.headers as any)["Authorization"] = `Bearer ${token}`;
|
||||
return config;
|
||||
});
|
||||
return instance;
|
||||
}
|
||||
|
||||
// 通用的创建已认证客户端的方法(使用泛型)
|
||||
public static createAuthenticatedClient<T extends SupportedClient>(
|
||||
ClientClass: new (baseUrl?: string, http?: any) => T,
|
||||
ClientClass: new (baseUrl?: string, instance?: AxiosInstance) => T,
|
||||
): T {
|
||||
const customHttp = AuthManager.createAuthenticatedHttp();
|
||||
return customHttp
|
||||
? new ClientClass(undefined, customHttp)
|
||||
const axiosInstance = AuthManager.createAuthenticatedAxiosInstance();
|
||||
return axiosInstance
|
||||
? new ClientClass(undefined, axiosInstance)
|
||||
: new ClientClass();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user