gscRequest.ts
3.24 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import type { Request } from "glsk-toolkit";
import type { QiankunProps } from "vite-plugin-qiankun/dist/helper";
import { qiankunWindow } from "vite-plugin-qiankun/dist/helper";
/**
* GSC Request 管理器
* 用于管理从基座获取的 Request 实例
*/
class GscRequestManager {
private getGscRequestFn: (() => Promise<Request>) | null = null;
private initPromise: null | Promise<Request> = null;
private requestInstance: null | Request = null;
/**
* 销毁管理器
*/
destroy() {
this.requestInstance = null;
this.getGscRequestFn = null;
this.initPromise = null;
}
/**
* 获取 GSC Request 实例
* @returns Promise<Request> Request 实例
*/
async getRequest(): Promise<Request> {
// 如果已有实例,直接返回
if (this.requestInstance) {
return this.requestInstance;
}
// 如果有获取函数,尝试获取
if (this.getGscRequestFn) {
// 如果正在初始化,等待初始化完成
if (this.initPromise) {
return this.initPromise;
}
// 创建初始化 Promise
this.initPromise = this.getGscRequestFn().then((instance) => {
this.requestInstance = instance;
this.initPromise = null;
return instance;
});
return this.initPromise;
}
throw new Error(
"[GSC Request] Request 未初始化,请确保在 qiankun 环境中运行并已调用 init 方法",
);
}
/**
* 初始化 GSC Request(在子应用 render 函数中调用)
* @param props qiankun 传递的 props
*/
async init(props: QiankunProps) {
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
console.warn("[GSC Request] 非 qiankun 环境,无法获取 Request 实例");
return;
}
if (!props.getGscRequest) {
console.warn("[GSC Request] props 中未找到 getGscRequest 函数");
return;
}
this.getGscRequestFn = props.getGscRequest;
try {
if (this.getGscRequestFn) {
this.requestInstance = await this.getGscRequestFn();
}
} catch (error) {
console.error("[GSC Request] 实例初始化失败:", error);
throw error;
}
}
/**
* 检查 Request 是否已初始化
*/
isInitialized(): boolean {
return this.requestInstance !== null;
}
}
// 导出单例
export const gscRequestManager = new GscRequestManager();
/**
* GSC POST 请求封装(类似基座的 gscPost)
* @param url 请求地址
* @param data 请求数据
* @param config 请求配置
* @returns Promise<any> 响应数据
*/
export async function gscPost(
url: string,
data: any,
config: any = {},
): Promise<any> {
const request = await gscRequestManager.getRequest();
// Request 实例有 post 方法,但类型定义可能不完整,使用类型断言
return (request as any).post(url, data, config);
}
/**
* GSC GET 请求封装
* @param url 请求地址
* @param config 请求配置
* @returns Promise<any> 响应数据
*/
export async function gscGet(url: string, config: any = {}): Promise<any> {
const request = await gscRequestManager.getRequest();
// Request 实例有 get 方法,但类型定义可能不完整,使用类型断言
return (request as any).get(url, config);
}
/**
* 直接获取 Request 实例(高级用法)
* @returns Promise<Request> Request 实例
*/
export async function getGscRequest(): Promise<Request> {
return gscRequestManager.getRequest();
}