@lark-apaas/miaoda-core 0.0.1-alpha.2 → 0.0.5-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/components/AppContainer/utils/childApi.js +1 -13
- package/lib/types/iframe-events.d.ts +0 -11
- package/package.json +3 -1
- package/lib/components/AppContainer/api-proxy/core.d.ts +0 -188
- package/lib/components/AppContainer/api-proxy/core.js +0 -290
- package/lib/components/AppContainer/utils/api-panel.d.ts +0 -38
- package/lib/components/AppContainer/utils/api-panel.js +0 -84
@@ -1,5 +1,4 @@
|
|
1
1
|
import { normalizeBasePath } from "../../../utils/utils.js";
|
2
|
-
import { api_delete, api_get, api_head, api_options, api_patch, api_post, api_put, getLogJson, getOpenApiJson } from "./api-panel.js";
|
3
2
|
async function getRoutes() {
|
4
3
|
let routes = [
|
5
4
|
{
|
@@ -16,17 +15,6 @@ async function getRoutes() {
|
|
16
15
|
return routes;
|
17
16
|
}
|
18
17
|
const childApi = {
|
19
|
-
getRoutes
|
20
|
-
apiProxy: {
|
21
|
-
api_get: api_get,
|
22
|
-
api_post: api_post,
|
23
|
-
api_put: api_put,
|
24
|
-
api_delete: api_delete,
|
25
|
-
api_patch: api_patch,
|
26
|
-
api_head: api_head,
|
27
|
-
api_options: api_options,
|
28
|
-
getOpenApiJson: getOpenApiJson,
|
29
|
-
getLogJson: getLogJson
|
30
|
-
}
|
18
|
+
getRoutes
|
31
19
|
};
|
32
20
|
export { childApi };
|
@@ -47,15 +47,4 @@ export interface ParentApi {
|
|
47
47
|
}
|
48
48
|
export interface ChildApi {
|
49
49
|
getRoutes: () => Promise<any[]>;
|
50
|
-
apiProxy: {
|
51
|
-
api_get: (url: string, config?: any) => Promise<any>;
|
52
|
-
api_post: (url: string, data?: any, config?: any) => Promise<any>;
|
53
|
-
api_put: (url: string, data?: any, config?: any) => Promise<any>;
|
54
|
-
api_delete: (url: string, config?: any) => Promise<any>;
|
55
|
-
api_patch: (url: string, data?: any, config?: any) => Promise<any>;
|
56
|
-
api_head: (url: string, config?: any) => Promise<any>;
|
57
|
-
api_options: (url: string, config?: any) => Promise<any>;
|
58
|
-
getOpenApiJson: () => Promise<any>;
|
59
|
-
getLogJson: () => Promise<any>;
|
60
|
-
};
|
61
50
|
}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@lark-apaas/miaoda-core",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.5-beta.2",
|
4
4
|
"types": "./lib/index.d.ts",
|
5
5
|
"main": "./lib/index.js",
|
6
6
|
"files": [
|
@@ -70,6 +70,8 @@
|
|
70
70
|
"test": "echo 0",
|
71
71
|
"lint": "eslint src --ext .js,.jsx,.ts,.tsx",
|
72
72
|
"lint:fix": "eslint src --ext .js,.jsx,.ts,.tsx --fix",
|
73
|
+
"postinstall": "yes | npx shadcn@latest add --all -o -y -c \"$INIT_CWD\"",
|
74
|
+
"setup": "echo 'setup'",
|
73
75
|
"prepublishOnly": "npm run build && node scripts/replace-workspace-alias.js"
|
74
76
|
},
|
75
77
|
"dependencies": {
|
@@ -1,188 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* HTTP 请求方法类型
|
3
|
-
*/
|
4
|
-
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS';
|
5
|
-
/**
|
6
|
-
* 请求配置接口
|
7
|
-
*/
|
8
|
-
export interface RequestConfig {
|
9
|
-
/** 请求 URL */
|
10
|
-
url: string;
|
11
|
-
/** 请求方法 */
|
12
|
-
method?: HttpMethod;
|
13
|
-
/** 请求头 */
|
14
|
-
headers?: Record<string, string>;
|
15
|
-
/** 请求参数(用于 GET 请求的查询参数) */
|
16
|
-
params?: Record<string, any>;
|
17
|
-
/** 请求体数据 */
|
18
|
-
data?: any;
|
19
|
-
/** 超时时间(毫秒) */
|
20
|
-
timeout?: number;
|
21
|
-
/** 响应类型 */
|
22
|
-
responseType?: 'json' | 'text' | 'blob' | 'arrayBuffer' | 'formData';
|
23
|
-
/** 是否携带凭证 */
|
24
|
-
credentials?: RequestCredentials;
|
25
|
-
/** AbortSignal 用于取消请求 */
|
26
|
-
signal?: AbortSignal;
|
27
|
-
}
|
28
|
-
/**
|
29
|
-
* 响应数据接口
|
30
|
-
*/
|
31
|
-
export interface ApiResponse<T = any> {
|
32
|
-
/** 响应数据 */
|
33
|
-
data: T;
|
34
|
-
/** HTTP 状态码 */
|
35
|
-
status: number;
|
36
|
-
/** 状态文本 */
|
37
|
-
statusText: string;
|
38
|
-
/** 响应头 */
|
39
|
-
headers: Headers;
|
40
|
-
/** 原始响应对象 */
|
41
|
-
response: Response;
|
42
|
-
/** 请求配置 */
|
43
|
-
config: RequestConfig;
|
44
|
-
}
|
45
|
-
/**
|
46
|
-
* API 错误类
|
47
|
-
*/
|
48
|
-
export declare class ApiError extends Error {
|
49
|
-
status?: number;
|
50
|
-
statusText?: string;
|
51
|
-
response?: Response;
|
52
|
-
config?: RequestConfig;
|
53
|
-
code?: string;
|
54
|
-
constructor(message: string, config?: RequestConfig, response?: Response, code?: string);
|
55
|
-
}
|
56
|
-
/**
|
57
|
-
* 请求拦截器类型
|
58
|
-
*/
|
59
|
-
export type RequestInterceptor = (config: RequestConfig) => RequestConfig | Promise<RequestConfig>;
|
60
|
-
/**
|
61
|
-
* 响应拦截器类型
|
62
|
-
*/
|
63
|
-
export type ResponseInterceptor = <T = any>(response: ApiResponse<T>) => ApiResponse<T> | Promise<ApiResponse<T>>;
|
64
|
-
/**
|
65
|
-
* 错误拦截器类型
|
66
|
-
*/
|
67
|
-
export type ErrorInterceptor = (error: ApiError) => any;
|
68
|
-
/**
|
69
|
-
* 通用接口测试 API 代理类
|
70
|
-
*
|
71
|
-
* 特性:
|
72
|
-
* - 支持所有 RESTful 请求方法 (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS)
|
73
|
-
* - 完善的错误处理和边界检查
|
74
|
-
* - 请求/响应拦截器
|
75
|
-
* - 超时控制和请求取消
|
76
|
-
* - 支持多种响应类型
|
77
|
-
*/
|
78
|
-
declare class ApiProxy {
|
79
|
-
/** 默认配置 */
|
80
|
-
private defaultConfig;
|
81
|
-
/** 请求拦截器队列 */
|
82
|
-
private requestInterceptors;
|
83
|
-
/** 响应拦截器队列 */
|
84
|
-
private responseInterceptors;
|
85
|
-
/** 错误拦截器队列 */
|
86
|
-
private errorInterceptors;
|
87
|
-
/** 活跃的请求控制器 Map */
|
88
|
-
private activeRequests;
|
89
|
-
/**
|
90
|
-
* 构造函数
|
91
|
-
* @param baseConfig 基础配置
|
92
|
-
*/
|
93
|
-
constructor(baseConfig?: Partial<RequestConfig>);
|
94
|
-
/**
|
95
|
-
* 添加请求拦截器
|
96
|
-
*/
|
97
|
-
useRequestInterceptor(interceptor: RequestInterceptor): void;
|
98
|
-
/**
|
99
|
-
* 添加响应拦截器
|
100
|
-
*/
|
101
|
-
useResponseInterceptor(interceptor: ResponseInterceptor): void;
|
102
|
-
/**
|
103
|
-
* 添加错误拦截器
|
104
|
-
*/
|
105
|
-
useErrorInterceptor(interceptor: ErrorInterceptor): void;
|
106
|
-
/**
|
107
|
-
* 验证 URL 格式
|
108
|
-
*/
|
109
|
-
private validateUrl;
|
110
|
-
/**
|
111
|
-
* 构建完整 URL (包含查询参数)
|
112
|
-
*/
|
113
|
-
private buildUrl;
|
114
|
-
/**
|
115
|
-
* 序列化请求体
|
116
|
-
*/
|
117
|
-
private serializeData;
|
118
|
-
/**
|
119
|
-
* 解析响应数据
|
120
|
-
*/
|
121
|
-
private parseResponse;
|
122
|
-
/**
|
123
|
-
* 执行请求拦截器
|
124
|
-
*/
|
125
|
-
private runRequestInterceptors;
|
126
|
-
/**
|
127
|
-
* 执行响应拦截器
|
128
|
-
*/
|
129
|
-
private runResponseInterceptors;
|
130
|
-
/**
|
131
|
-
* 执行错误拦截器
|
132
|
-
*/
|
133
|
-
private runErrorInterceptors;
|
134
|
-
/**
|
135
|
-
* 生成请求唯一键
|
136
|
-
*/
|
137
|
-
private generateRequestKey;
|
138
|
-
/**
|
139
|
-
* 核心请求方法
|
140
|
-
*/
|
141
|
-
request<T = any>(config: RequestConfig): Promise<ApiResponse<T>>;
|
142
|
-
/**
|
143
|
-
* 执行实际请求
|
144
|
-
*/
|
145
|
-
private executeRequest;
|
146
|
-
/**
|
147
|
-
* GET 请求
|
148
|
-
*/
|
149
|
-
get<T = any>(url: string, config?: Omit<RequestConfig, 'url' | 'method' | 'data'>): Promise<ApiResponse<T>>;
|
150
|
-
/**
|
151
|
-
* POST 请求
|
152
|
-
*/
|
153
|
-
post<T = any>(url: string, data?: any, config?: Omit<RequestConfig, 'url' | 'method' | 'data'>): Promise<ApiResponse<T>>;
|
154
|
-
/**
|
155
|
-
* PUT 请求
|
156
|
-
*/
|
157
|
-
put<T = any>(url: string, data?: any, config?: Omit<RequestConfig, 'url' | 'method' | 'data'>): Promise<ApiResponse<T>>;
|
158
|
-
/**
|
159
|
-
* DELETE 请求
|
160
|
-
*/
|
161
|
-
delete<T = any>(url: string, config?: Omit<RequestConfig, 'url' | 'method' | 'data'>): Promise<ApiResponse<T>>;
|
162
|
-
/**
|
163
|
-
* PATCH 请求
|
164
|
-
*/
|
165
|
-
patch<T = any>(url: string, data?: any, config?: Omit<RequestConfig, 'url' | 'method' | 'data'>): Promise<ApiResponse<T>>;
|
166
|
-
/**
|
167
|
-
* HEAD 请求
|
168
|
-
*/
|
169
|
-
head<T = any>(url: string, config?: Omit<RequestConfig, 'url' | 'method' | 'data'>): Promise<ApiResponse<T>>;
|
170
|
-
/**
|
171
|
-
* OPTIONS 请求
|
172
|
-
*/
|
173
|
-
options<T = any>(url: string, config?: Omit<RequestConfig, 'url' | 'method' | 'data'>): Promise<ApiResponse<T>>;
|
174
|
-
/**
|
175
|
-
* 取消指定的请求
|
176
|
-
*/
|
177
|
-
cancelRequest(requestKey: string): void;
|
178
|
-
/**
|
179
|
-
* 取消所有活跃请求
|
180
|
-
*/
|
181
|
-
cancelAllRequests(): void;
|
182
|
-
/**
|
183
|
-
* 获取活跃请求数量
|
184
|
-
*/
|
185
|
-
getActiveRequestCount(): number;
|
186
|
-
}
|
187
|
-
declare const apiProxy: ApiProxy;
|
188
|
-
export default apiProxy;
|
@@ -1,290 +0,0 @@
|
|
1
|
-
class ApiError extends Error {
|
2
|
-
status;
|
3
|
-
statusText;
|
4
|
-
response;
|
5
|
-
config;
|
6
|
-
code;
|
7
|
-
constructor(message, config, response, code){
|
8
|
-
super(message);
|
9
|
-
this.name = 'ApiError';
|
10
|
-
this.config = config;
|
11
|
-
this.response = response;
|
12
|
-
this.status = response?.status;
|
13
|
-
this.statusText = response?.statusText;
|
14
|
-
this.code = code;
|
15
|
-
Object.setPrototypeOf(this, ApiError.prototype);
|
16
|
-
}
|
17
|
-
}
|
18
|
-
class ApiProxy {
|
19
|
-
defaultConfig = {
|
20
|
-
timeout: 30000,
|
21
|
-
responseType: 'json',
|
22
|
-
credentials: 'same-origin',
|
23
|
-
headers: {
|
24
|
-
'Content-Type': 'application/json',
|
25
|
-
'X-Suda-Csrf-Token': window.csrfToken || ''
|
26
|
-
}
|
27
|
-
};
|
28
|
-
requestInterceptors = [];
|
29
|
-
responseInterceptors = [];
|
30
|
-
errorInterceptors = [];
|
31
|
-
activeRequests = new Map();
|
32
|
-
constructor(baseConfig){
|
33
|
-
if (baseConfig) this.defaultConfig = {
|
34
|
-
...this.defaultConfig,
|
35
|
-
...baseConfig
|
36
|
-
};
|
37
|
-
}
|
38
|
-
useRequestInterceptor(interceptor) {
|
39
|
-
if ('function' != typeof interceptor) throw new TypeError('Request interceptor must be a function');
|
40
|
-
this.requestInterceptors.push(interceptor);
|
41
|
-
}
|
42
|
-
useResponseInterceptor(interceptor) {
|
43
|
-
if ('function' != typeof interceptor) throw new TypeError('Response interceptor must be a function');
|
44
|
-
this.responseInterceptors.push(interceptor);
|
45
|
-
}
|
46
|
-
useErrorInterceptor(interceptor) {
|
47
|
-
if ('function' != typeof interceptor) throw new TypeError('Error interceptor must be a function');
|
48
|
-
this.errorInterceptors.push(interceptor);
|
49
|
-
}
|
50
|
-
validateUrl(url) {
|
51
|
-
if (!url || 'string' != typeof url) return false;
|
52
|
-
if (url.startsWith('/') || url.startsWith('./') || url.startsWith('../')) return true;
|
53
|
-
try {
|
54
|
-
new URL(url);
|
55
|
-
return true;
|
56
|
-
} catch {
|
57
|
-
return false;
|
58
|
-
}
|
59
|
-
}
|
60
|
-
buildUrl(url, params) {
|
61
|
-
if (!params || 0 === Object.keys(params).length) return url;
|
62
|
-
try {
|
63
|
-
const urlObj = new URL(url, window.location.origin);
|
64
|
-
Object.entries(params).forEach(([key, value])=>{
|
65
|
-
if (null != value) urlObj.searchParams.append(key, String(value));
|
66
|
-
});
|
67
|
-
return urlObj.toString();
|
68
|
-
} catch (error) {
|
69
|
-
console.error('Failed to build URL:', error);
|
70
|
-
return url;
|
71
|
-
}
|
72
|
-
}
|
73
|
-
serializeData(data, contentType) {
|
74
|
-
if (null == data) return null;
|
75
|
-
if (data instanceof FormData) return data;
|
76
|
-
if (data instanceof Blob || data instanceof File) return data;
|
77
|
-
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) return data;
|
78
|
-
if (data instanceof URLSearchParams) return data;
|
79
|
-
const type = contentType?.toLowerCase() || '';
|
80
|
-
if (type.includes('application/json')) try {
|
81
|
-
return JSON.stringify(data);
|
82
|
-
} catch (error) {
|
83
|
-
throw new ApiError('Failed to serialize JSON data', void 0, void 0, 'SERIALIZATION_ERROR');
|
84
|
-
}
|
85
|
-
if (type.includes('application/x-www-form-urlencoded')) {
|
86
|
-
if ('object' == typeof data) return new URLSearchParams(data).toString();
|
87
|
-
return String(data);
|
88
|
-
}
|
89
|
-
if ('object' == typeof data) try {
|
90
|
-
return JSON.stringify(data);
|
91
|
-
} catch (error) {
|
92
|
-
throw new ApiError('Failed to serialize data', void 0, void 0, 'SERIALIZATION_ERROR');
|
93
|
-
}
|
94
|
-
return String(data);
|
95
|
-
}
|
96
|
-
async parseResponse(response, responseType) {
|
97
|
-
try {
|
98
|
-
switch(responseType){
|
99
|
-
case 'json':
|
100
|
-
const text = await response.text();
|
101
|
-
return text ? JSON.parse(text) : null;
|
102
|
-
case 'text':
|
103
|
-
return await response.text();
|
104
|
-
case 'blob':
|
105
|
-
return await response.blob();
|
106
|
-
case 'arrayBuffer':
|
107
|
-
return await response.arrayBuffer();
|
108
|
-
case 'formData':
|
109
|
-
return await response.formData();
|
110
|
-
default:
|
111
|
-
return await response.text();
|
112
|
-
}
|
113
|
-
} catch (error) {
|
114
|
-
throw new ApiError(`Failed to parse response as ${responseType}`, void 0, response, 'PARSE_ERROR');
|
115
|
-
}
|
116
|
-
}
|
117
|
-
async runRequestInterceptors(config) {
|
118
|
-
let modifiedConfig = {
|
119
|
-
...config
|
120
|
-
};
|
121
|
-
for (const interceptor of this.requestInterceptors)try {
|
122
|
-
modifiedConfig = await interceptor(modifiedConfig);
|
123
|
-
} catch (error) {
|
124
|
-
console.error('Request interceptor error:', error);
|
125
|
-
throw new ApiError('Request interceptor failed', modifiedConfig, void 0, 'INTERCEPTOR_ERROR');
|
126
|
-
}
|
127
|
-
return modifiedConfig;
|
128
|
-
}
|
129
|
-
async runResponseInterceptors(response) {
|
130
|
-
let modifiedResponse = response;
|
131
|
-
for (const interceptor of this.responseInterceptors)try {
|
132
|
-
modifiedResponse = await interceptor(modifiedResponse);
|
133
|
-
} catch (error) {
|
134
|
-
console.error('Response interceptor error:', error);
|
135
|
-
}
|
136
|
-
return modifiedResponse;
|
137
|
-
}
|
138
|
-
async runErrorInterceptors(error) {
|
139
|
-
for (const interceptor of this.errorInterceptors)try {
|
140
|
-
const result = await interceptor(error);
|
141
|
-
if (void 0 !== result) return result;
|
142
|
-
} catch (err) {
|
143
|
-
console.error('Error interceptor failed:', err);
|
144
|
-
}
|
145
|
-
throw error;
|
146
|
-
}
|
147
|
-
generateRequestKey(config) {
|
148
|
-
return `${config.method || 'GET'}_${config.url}_${Date.now()}_${Math.random()}`;
|
149
|
-
}
|
150
|
-
async request(config) {
|
151
|
-
if (!config || 'object' != typeof config) throw new ApiError('Request config is required and must be an object', config, void 0, 'INVALID_CONFIG');
|
152
|
-
if (!config.url) throw new ApiError('Request URL is required', config, void 0, 'MISSING_URL');
|
153
|
-
if (!this.validateUrl(config.url)) throw new ApiError('Invalid URL format', config, void 0, 'INVALID_URL');
|
154
|
-
const mergedConfig = {
|
155
|
-
...this.defaultConfig,
|
156
|
-
...config,
|
157
|
-
headers: {
|
158
|
-
...this.defaultConfig.headers,
|
159
|
-
...config.headers
|
160
|
-
}
|
161
|
-
};
|
162
|
-
const finalConfig = await this.runRequestInterceptors(mergedConfig);
|
163
|
-
const requestKey = this.generateRequestKey(finalConfig);
|
164
|
-
try {
|
165
|
-
const response = await this.executeRequest(finalConfig, requestKey);
|
166
|
-
return response;
|
167
|
-
} catch (error) {
|
168
|
-
const apiError = error instanceof ApiError ? error : new ApiError(error instanceof Error ? error.message : 'Unknown error', finalConfig, void 0, 'REQUEST_FAILED');
|
169
|
-
return await this.runErrorInterceptors(apiError);
|
170
|
-
}
|
171
|
-
}
|
172
|
-
async executeRequest(config, requestKey) {
|
173
|
-
let abortController;
|
174
|
-
if (config.signal) {
|
175
|
-
abortController = new AbortController();
|
176
|
-
config.signal.addEventListener('abort', ()=>abortController.abort());
|
177
|
-
} else abortController = new AbortController();
|
178
|
-
this.activeRequests.set(requestKey, abortController);
|
179
|
-
let timeoutId = null;
|
180
|
-
if (config.timeout && config.timeout > 0) timeoutId = setTimeout(()=>{
|
181
|
-
abortController.abort();
|
182
|
-
}, config.timeout);
|
183
|
-
try {
|
184
|
-
const fullUrl = this.buildUrl(config.url, config.params);
|
185
|
-
const fetchOptions = {
|
186
|
-
method: config.method || 'GET',
|
187
|
-
headers: config.headers,
|
188
|
-
credentials: config.credentials,
|
189
|
-
signal: abortController.signal
|
190
|
-
};
|
191
|
-
const methodsWithoutBody = [
|
192
|
-
'GET',
|
193
|
-
'HEAD'
|
194
|
-
];
|
195
|
-
if (config.data && !methodsWithoutBody.includes(config.method || 'GET')) fetchOptions.body = this.serializeData(config.data, config.headers?.['Content-Type']);
|
196
|
-
const response = await fetch(fullUrl, fetchOptions);
|
197
|
-
if (timeoutId) clearTimeout(timeoutId);
|
198
|
-
this.activeRequests.delete(requestKey);
|
199
|
-
if (!response.ok) throw new ApiError(`HTTP Error: ${response.status} ${response.statusText}`, config, response, 'HTTP_ERROR');
|
200
|
-
const data = await this.parseResponse(response, config.responseType || 'json');
|
201
|
-
const apiResponse = {
|
202
|
-
data,
|
203
|
-
status: response.status,
|
204
|
-
statusText: response.statusText,
|
205
|
-
headers: response.headers,
|
206
|
-
response,
|
207
|
-
config
|
208
|
-
};
|
209
|
-
return await this.runResponseInterceptors(apiResponse);
|
210
|
-
} catch (error) {
|
211
|
-
if (timeoutId) clearTimeout(timeoutId);
|
212
|
-
this.activeRequests.delete(requestKey);
|
213
|
-
if (error instanceof Error && 'AbortError' === error.name) throw new ApiError(config.timeout ? 'Request timeout' : 'Request cancelled', config, void 0, 'ABORT_ERROR');
|
214
|
-
if (error instanceof TypeError) throw new ApiError('Network error or CORS issue', config, void 0, 'NETWORK_ERROR');
|
215
|
-
if (error instanceof ApiError) throw error;
|
216
|
-
throw new ApiError(error instanceof Error ? error.message : 'Unknown error', config, void 0, 'UNKNOWN_ERROR');
|
217
|
-
}
|
218
|
-
}
|
219
|
-
async get(url, config) {
|
220
|
-
return this.request({
|
221
|
-
...config,
|
222
|
-
url,
|
223
|
-
method: 'GET'
|
224
|
-
});
|
225
|
-
}
|
226
|
-
async post(url, data, config) {
|
227
|
-
return this.request({
|
228
|
-
...config,
|
229
|
-
url,
|
230
|
-
method: 'POST',
|
231
|
-
data
|
232
|
-
});
|
233
|
-
}
|
234
|
-
async put(url, data, config) {
|
235
|
-
return this.request({
|
236
|
-
...config,
|
237
|
-
url,
|
238
|
-
method: 'PUT',
|
239
|
-
data
|
240
|
-
});
|
241
|
-
}
|
242
|
-
async delete(url, config) {
|
243
|
-
return this.request({
|
244
|
-
...config,
|
245
|
-
url,
|
246
|
-
method: 'DELETE'
|
247
|
-
});
|
248
|
-
}
|
249
|
-
async patch(url, data, config) {
|
250
|
-
return this.request({
|
251
|
-
...config,
|
252
|
-
url,
|
253
|
-
method: 'PATCH',
|
254
|
-
data
|
255
|
-
});
|
256
|
-
}
|
257
|
-
async head(url, config) {
|
258
|
-
return this.request({
|
259
|
-
...config,
|
260
|
-
url,
|
261
|
-
method: 'HEAD'
|
262
|
-
});
|
263
|
-
}
|
264
|
-
async options(url, config) {
|
265
|
-
return this.request({
|
266
|
-
...config,
|
267
|
-
url,
|
268
|
-
method: 'OPTIONS'
|
269
|
-
});
|
270
|
-
}
|
271
|
-
cancelRequest(requestKey) {
|
272
|
-
const controller = this.activeRequests.get(requestKey);
|
273
|
-
if (controller) {
|
274
|
-
controller.abort();
|
275
|
-
this.activeRequests.delete(requestKey);
|
276
|
-
}
|
277
|
-
}
|
278
|
-
cancelAllRequests() {
|
279
|
-
this.activeRequests.forEach((controller)=>{
|
280
|
-
controller.abort();
|
281
|
-
});
|
282
|
-
this.activeRequests.clear();
|
283
|
-
}
|
284
|
-
getActiveRequestCount() {
|
285
|
-
return this.activeRequests.size;
|
286
|
-
}
|
287
|
-
}
|
288
|
-
const apiProxy = new ApiProxy();
|
289
|
-
const core = apiProxy;
|
290
|
-
export { ApiError, core as default };
|
@@ -1,38 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* api panel
|
3
|
-
* 获取open-api.json
|
4
|
-
*/
|
5
|
-
declare function getOpenApiJson(): Promise<{}>;
|
6
|
-
/**
|
7
|
-
* 获取日志json
|
8
|
-
*/
|
9
|
-
declare function getLogJson(): Promise<{}>;
|
10
|
-
/**
|
11
|
-
* GET 请求
|
12
|
-
*/
|
13
|
-
declare function api_get(url: any, config: any): Promise<import("../api-proxy/core").ApiResponse<any>>;
|
14
|
-
/**
|
15
|
-
* POST 请求
|
16
|
-
*/
|
17
|
-
declare function api_post(url: any, data: any, config: any): Promise<import("../api-proxy/core").ApiResponse<any>>;
|
18
|
-
/**
|
19
|
-
* PUT 请求
|
20
|
-
*/
|
21
|
-
declare function api_put(url: any, data: any, config: any): Promise<import("../api-proxy/core").ApiResponse<any>>;
|
22
|
-
/**
|
23
|
-
* DELETE 请求
|
24
|
-
*/
|
25
|
-
declare function api_delete(url: any, config: any): Promise<import("../api-proxy/core").ApiResponse<any>>;
|
26
|
-
/**
|
27
|
-
* PATCH 请求
|
28
|
-
*/
|
29
|
-
declare function api_patch(url: any, data: any, config: any): Promise<import("../api-proxy/core").ApiResponse<any>>;
|
30
|
-
/**
|
31
|
-
* HEAD 请求
|
32
|
-
*/
|
33
|
-
declare function api_head(url: any, config: any): Promise<import("../api-proxy/core").ApiResponse<any>>;
|
34
|
-
/**
|
35
|
-
* OPTIONS 请求
|
36
|
-
*/
|
37
|
-
declare function api_options(url: any, config: any): Promise<import("../api-proxy/core").ApiResponse<any>>;
|
38
|
-
export { getOpenApiJson, getLogJson, api_get, api_post, api_put, api_delete, api_patch, api_head, api_options, };
|
@@ -1,84 +0,0 @@
|
|
1
|
-
import { normalizeBasePath } from "../../../utils/utils.js";
|
2
|
-
import core from "../api-proxy/core.js";
|
3
|
-
async function getOpenApiJson() {
|
4
|
-
let apiJson = {};
|
5
|
-
try {
|
6
|
-
const basePath = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
7
|
-
const res = await fetch(`${basePath}/openapi.json`);
|
8
|
-
apiJson = await res.json();
|
9
|
-
} catch (error) {
|
10
|
-
console.warn('get openapi.json error', error);
|
11
|
-
}
|
12
|
-
return apiJson;
|
13
|
-
}
|
14
|
-
async function getLogJson() {
|
15
|
-
let logJson = {};
|
16
|
-
try {
|
17
|
-
const basePath = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
18
|
-
const res = await fetch(`${basePath}/log.json`);
|
19
|
-
logJson = await res.json();
|
20
|
-
} catch (error) {
|
21
|
-
console.warn('get log.json error', error);
|
22
|
-
}
|
23
|
-
return logJson;
|
24
|
-
}
|
25
|
-
async function api_get(url, config) {
|
26
|
-
const basePath = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
27
|
-
return core.get({
|
28
|
-
...config,
|
29
|
-
url: `${basePath}${url}`,
|
30
|
-
method: 'GET'
|
31
|
-
});
|
32
|
-
}
|
33
|
-
async function api_post(url, data, config) {
|
34
|
-
const basePath = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
35
|
-
return core.post({
|
36
|
-
...config,
|
37
|
-
url: `${basePath}${url}`,
|
38
|
-
method: 'POST',
|
39
|
-
data
|
40
|
-
});
|
41
|
-
}
|
42
|
-
async function api_put(url, data, config) {
|
43
|
-
const basePath = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
44
|
-
return core.put({
|
45
|
-
...config,
|
46
|
-
url: `${basePath}${url}`,
|
47
|
-
method: 'PUT',
|
48
|
-
data
|
49
|
-
});
|
50
|
-
}
|
51
|
-
async function api_delete(url, config) {
|
52
|
-
const basePath = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
53
|
-
return core["delete"]({
|
54
|
-
...config,
|
55
|
-
url: `${basePath}${url}`,
|
56
|
-
method: 'DELETE'
|
57
|
-
});
|
58
|
-
}
|
59
|
-
async function api_patch(url, data, config) {
|
60
|
-
const basePath = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
61
|
-
return core.patch({
|
62
|
-
...config,
|
63
|
-
url: `${basePath}${url}`,
|
64
|
-
method: 'PATCH',
|
65
|
-
data
|
66
|
-
});
|
67
|
-
}
|
68
|
-
async function api_head(url, config) {
|
69
|
-
const basePath = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
70
|
-
return core.head({
|
71
|
-
...config,
|
72
|
-
url: `${basePath}${url}`,
|
73
|
-
method: 'HEAD'
|
74
|
-
});
|
75
|
-
}
|
76
|
-
async function api_options(url, config) {
|
77
|
-
const basePath = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
78
|
-
return core.options({
|
79
|
-
...config,
|
80
|
-
url: `${basePath}${url}`,
|
81
|
-
method: 'OPTIONS'
|
82
|
-
});
|
83
|
-
}
|
84
|
-
export { api_delete, api_get, api_head, api_options, api_patch, api_post, api_put, getLogJson, getOpenApiJson };
|