@lark-apaas/miaoda-core 0.0.1-alpha.1
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/LICENSE +13 -0
- package/README.md +8 -0
- package/lib/apis/components/AppContainer.d.ts +1 -0
- package/lib/apis/components/AppContainer.js +2 -0
- package/lib/apis/components/ErrorRender.d.ts +1 -0
- package/lib/apis/components/ErrorRender.js +2 -0
- package/lib/apis/components/NotFoundRender.d.ts +1 -0
- package/lib/apis/components/NotFoundRender.js +2 -0
- package/lib/apis/components/SidebarNav.d.ts +1 -0
- package/lib/apis/components/SidebarNav.js +2 -0
- package/lib/apis/components/User.d.ts +1 -0
- package/lib/apis/components/User.js +1 -0
- package/lib/apis/components/Welcome.d.ts +1 -0
- package/lib/apis/components/Welcome.js +2 -0
- package/lib/apis/constants/img-resources/avatar.d.ts +11 -0
- package/lib/apis/constants/img-resources/avatar.js +12 -0
- package/lib/apis/constants/img-resources/banner.d.ts +24 -0
- package/lib/apis/constants/img-resources/banner.js +25 -0
- package/lib/apis/constants/img-resources/cover.d.ts +36 -0
- package/lib/apis/constants/img-resources/cover.js +37 -0
- package/lib/apis/dataloom.d.ts +1 -0
- package/lib/apis/dataloom.js +2 -0
- package/lib/apis/hooks/useAppInfo.d.ts +1 -0
- package/lib/apis/hooks/useAppInfo.js +1 -0
- package/lib/apis/hooks/useCurrentUserProfile.d.ts +1 -0
- package/lib/apis/hooks/useCurrentUserProfile.js +1 -0
- package/lib/apis/logger.d.ts +1 -0
- package/lib/apis/logger.js +1 -0
- package/lib/apis/tools/generateImage.d.ts +1 -0
- package/lib/apis/tools/generateImage.js +1 -0
- package/lib/apis/tools/generateTextStream.d.ts +1 -0
- package/lib/apis/tools/generateTextStream.js +1 -0
- package/lib/apis/tools/getAppInfo.d.ts +1 -0
- package/lib/apis/tools/getAppInfo.js +1 -0
- package/lib/apis/tools/getCurrentUserProfile.d.ts +1 -0
- package/lib/apis/tools/getCurrentUserProfile.js +1 -0
- package/lib/apis/tools/storage.d.ts +1 -0
- package/lib/apis/tools/storage.js +4 -0
- package/lib/apis/udt-types.d.ts +16 -0
- package/lib/apis/udt-types.js +0 -0
- package/lib/apis/utils/registerChinaMap.d.ts +1 -0
- package/lib/apis/utils/registerChinaMap.js +103319 -0
- package/lib/components/AppContainer/IframeBridge.d.ts +4 -0
- package/lib/components/AppContainer/IframeBridge.js +92 -0
- package/lib/components/AppContainer/LogInterceptor.d.ts +1 -0
- package/lib/components/AppContainer/LogInterceptor.js +44 -0
- package/lib/components/AppContainer/PageHoc.d.ts +4 -0
- package/lib/components/AppContainer/PageHoc.js +40 -0
- package/lib/components/AppContainer/api-proxy/core.d.ts +188 -0
- package/lib/components/AppContainer/api-proxy/core.js +290 -0
- package/lib/components/AppContainer/dayjsPlugins.d.ts +1 -0
- package/lib/components/AppContainer/dayjsPlugins.js +69 -0
- package/lib/components/AppContainer/index.d.ts +7 -0
- package/lib/components/AppContainer/index.js +65 -0
- package/lib/components/AppContainer/sonner.css +101 -0
- package/lib/components/AppContainer/sonner.d.ts +8 -0
- package/lib/components/AppContainer/sonner.js +26 -0
- package/lib/components/AppContainer/utils/api-panel.d.ts +11 -0
- package/lib/components/AppContainer/utils/api-panel.js +21 -0
- package/lib/components/AppContainer/utils/childApi.d.ts +5 -0
- package/lib/components/AppContainer/utils/childApi.js +26 -0
- package/lib/components/AppContainer/utils/listenHot.d.ts +1 -0
- package/lib/components/AppContainer/utils/listenHot.js +43 -0
- package/lib/components/ErrorRender/index.d.ts +5 -0
- package/lib/components/ErrorRender/index.js +79 -0
- package/lib/components/NotFoundRender/index.d.ts +3 -0
- package/lib/components/NotFoundRender/index.js +59 -0
- package/lib/components/SidebarNav/DrawerNav.d.ts +3 -0
- package/lib/components/SidebarNav/DrawerNav.js +64 -0
- package/lib/components/SidebarNav/DropdownNav.d.ts +3 -0
- package/lib/components/SidebarNav/DropdownNav.js +40 -0
- package/lib/components/SidebarNav/Sidebar.d.ts +3 -0
- package/lib/components/SidebarNav/Sidebar.js +33 -0
- package/lib/components/SidebarNav/index.d.ts +5 -0
- package/lib/components/SidebarNav/index.js +61 -0
- package/lib/components/User/UserDisplay.d.ts +9 -0
- package/lib/components/User/UserDisplay.js +39 -0
- package/lib/components/User/UserProfile/UserProfile.css +34 -0
- package/lib/components/User/UserProfile/UserProfile.d.ts +6 -0
- package/lib/components/User/UserProfile/UserProfile.js +41 -0
- package/lib/components/User/UserProfile/UserProfileContainer.d.ts +4 -0
- package/lib/components/User/UserProfile/UserProfileContainer.js +8 -0
- package/lib/components/User/UserProfile/UserProfileSkeleton.d.ts +3 -0
- package/lib/components/User/UserProfile/UserProfileSkeleton.js +36 -0
- package/lib/components/User/UserProfile/UserProfileUI.d.ts +8 -0
- package/lib/components/User/UserProfile/UserProfileUI.js +113 -0
- package/lib/components/User/UserProfile/index.d.ts +1 -0
- package/lib/components/User/UserProfile/index.js +2 -0
- package/lib/components/User/UserProfile/type.d.ts +8 -0
- package/lib/components/User/UserProfile/type.js +0 -0
- package/lib/components/User/UserProfile/utils.d.ts +5 -0
- package/lib/components/User/UserProfile/utils.js +26 -0
- package/lib/components/User/UserSelect.css +11 -0
- package/lib/components/User/UserSelect.d.ts +11 -0
- package/lib/components/User/UserSelect.js +172 -0
- package/lib/components/User/UserWithAvatar.d.ts +3 -0
- package/lib/components/User/UserWithAvatar.js +41 -0
- package/lib/components/User/index.d.ts +7 -0
- package/lib/components/User/index.js +5 -0
- package/lib/components/User/type.d.ts +13 -0
- package/lib/components/User/type.js +0 -0
- package/lib/components/Welcome/index.d.ts +3 -0
- package/lib/components/Welcome/index.js +17 -0
- package/lib/components/common/LogoInfo.d.ts +5 -0
- package/lib/components/common/LogoInfo.js +30 -0
- package/lib/components/common/NavItem.d.ts +20 -0
- package/lib/components/common/NavItem.js +112 -0
- package/lib/components/common/NavMenu.d.ts +9 -0
- package/lib/components/common/NavMenu.js +50 -0
- package/lib/components/common/UserAvatarLayout.d.ts +4 -0
- package/lib/components/common/UserAvatarLayout.js +41 -0
- package/lib/components/common/UserAvatarMenu.d.ts +4 -0
- package/lib/components/common/UserAvatarMenu.js +58 -0
- package/lib/components/common/index.d.ts +9 -0
- package/lib/components/common/index.js +10 -0
- package/lib/components/index.d.ts +8 -0
- package/lib/components/index.js +6 -0
- package/lib/components/theme/ThemeProvider.d.ts +20 -0
- package/lib/components/theme/ThemeProvider.js +75 -0
- package/lib/components/theme/constants.d.ts +48 -0
- package/lib/components/theme/constants.js +557 -0
- package/lib/components/theme/index.d.ts +4 -0
- package/lib/components/theme/index.js +5 -0
- package/lib/components/theme/miaoDarkTheme.d.ts +2 -0
- package/lib/components/theme/miaoDarkTheme.js +310 -0
- package/lib/components/theme/miaoLightTheme.d.ts +2 -0
- package/lib/components/theme/miaoLightTheme.js +296 -0
- package/lib/components/theme/ui-config.d.ts +1 -0
- package/lib/components/theme/ui-config.js +2 -0
- package/lib/components/theme/util.d.ts +20 -0
- package/lib/components/theme/util.js +188 -0
- package/lib/font-family.css +40 -0
- package/lib/hooks/index.d.ts +5 -0
- package/lib/hooks/index.js +5 -0
- package/lib/hooks/useAppInfo.d.ts +6 -0
- package/lib/hooks/useAppInfo.js +21 -0
- package/lib/hooks/useCurrentUserProfile.d.ts +12 -0
- package/lib/hooks/useCurrentUserProfile.js +58 -0
- package/lib/hooks/useIsMobile.d.ts +1 -0
- package/lib/hooks/useIsMobile.js +20 -0
- package/lib/hooks/useLogout.d.ts +4 -0
- package/lib/hooks/useLogout.js +37 -0
- package/lib/hooks/useTheme.d.ts +4 -0
- package/lib/hooks/useTheme.js +8 -0
- package/lib/hooks/useUpdatingRef.d.ts +1 -0
- package/lib/hooks/useUpdatingRef.js +7 -0
- package/lib/index.css +5 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.js +5 -0
- package/lib/inspector.dev.css +10 -0
- package/lib/integrations/dataloom.d.ts +2 -0
- package/lib/integrations/dataloom.js +32 -0
- package/lib/integrations/generateImage.d.ts +1 -0
- package/lib/integrations/generateImage.js +47 -0
- package/lib/integrations/generateTextStream.d.ts +21 -0
- package/lib/integrations/generateTextStream.js +98 -0
- package/lib/integrations/getAppInfo.d.ts +2 -0
- package/lib/integrations/getAppInfo.js +14 -0
- package/lib/integrations/getCurrentUserProfile.d.ts +6 -0
- package/lib/integrations/getCurrentUserProfile.js +5 -0
- package/lib/logger/index.d.ts +6 -0
- package/lib/logger/index.js +54 -0
- package/lib/override.css +29 -0
- package/lib/tailwind-theme.css +335 -0
- package/lib/theme-layer.css +3 -0
- package/lib/types/common.d.ts +4 -0
- package/lib/types/common.js +0 -0
- package/lib/types/iframe-events.d.ts +55 -0
- package/lib/types/iframe-events.js +0 -0
- package/lib/types/index.d.ts +43 -0
- package/lib/types/index.js +0 -0
- package/lib/utils/copyToClipboard.d.ts +6 -0
- package/lib/utils/copyToClipboard.js +32 -0
- package/lib/utils/getAppId.d.ts +6 -0
- package/lib/utils/getAppId.js +11 -0
- package/lib/utils/getCsrfToken.d.ts +5 -0
- package/lib/utils/getCsrfToken.js +10 -0
- package/lib/utils/getEnvPath.d.ts +4 -0
- package/lib/utils/getEnvPath.js +4 -0
- package/lib/utils/getInitialInfo.d.ts +20 -0
- package/lib/utils/getInitialInfo.js +32 -0
- package/lib/utils/getParentOrigin.d.ts +5 -0
- package/lib/utils/getParentOrigin.js +7 -0
- package/lib/utils/getUserProfile.d.ts +47 -0
- package/lib/utils/getUserProfile.js +39 -0
- package/lib/utils/postMessage.d.ts +8 -0
- package/lib/utils/postMessage.js +11 -0
- package/lib/utils/url.d.ts +8 -0
- package/lib/utils/url.js +22 -0
- package/lib/utils/utils.d.ts +20 -0
- package/lib/utils/utils.js +22 -0
- package/package.json +134 -0
@@ -0,0 +1,92 @@
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
2
|
+
import { useCallback, useEffect, useMemo, useRef } from "react";
|
3
|
+
import { useLocation, useNavigate } from "react-router-dom";
|
4
|
+
import { connectToParent } from "penpal";
|
5
|
+
import { useUpdatingRef } from "../../hooks/useUpdatingRef.js";
|
6
|
+
import { postMessage } from "../../utils/postMessage.js";
|
7
|
+
import { getPreviewParentOrigin } from "../../utils/getParentOrigin.js";
|
8
|
+
import { childApi } from "./utils/childApi.js";
|
9
|
+
import "./utils/listenHot.js";
|
10
|
+
import "./LogInterceptor.js";
|
11
|
+
var IframeBridge_RouteMessageType = /*#__PURE__*/ function(RouteMessageType) {
|
12
|
+
RouteMessageType["RouteChange"] = "RouteChange";
|
13
|
+
RouteMessageType["RouteBack"] = "RouteBack";
|
14
|
+
RouteMessageType["RouteForward"] = "RouteForward";
|
15
|
+
return RouteMessageType;
|
16
|
+
}(IframeBridge_RouteMessageType || {});
|
17
|
+
function isRouteMessageType(type) {
|
18
|
+
return Object.values(IframeBridge_RouteMessageType).includes(type);
|
19
|
+
}
|
20
|
+
async function connectParent() {
|
21
|
+
postMessage({
|
22
|
+
type: 'PreviewReady',
|
23
|
+
data: {}
|
24
|
+
});
|
25
|
+
const connection = connectToParent({
|
26
|
+
parentOrigin: getPreviewParentOrigin(),
|
27
|
+
methods: {
|
28
|
+
...childApi
|
29
|
+
}
|
30
|
+
});
|
31
|
+
await connection.promise;
|
32
|
+
}
|
33
|
+
'production' !== process.env.NODE_ENV && connectParent();
|
34
|
+
function IframeBridge() {
|
35
|
+
const location = useLocation();
|
36
|
+
const navigate = useNavigate();
|
37
|
+
const navigateRef = useUpdatingRef(navigate);
|
38
|
+
const isActive = useRef(false);
|
39
|
+
const historyBack = useCallback(()=>{
|
40
|
+
navigateRef.current(-1);
|
41
|
+
isActive.current = true;
|
42
|
+
}, [
|
43
|
+
navigateRef
|
44
|
+
]);
|
45
|
+
const historyForward = useCallback(()=>{
|
46
|
+
navigateRef.current(1);
|
47
|
+
isActive.current = true;
|
48
|
+
}, [
|
49
|
+
navigateRef
|
50
|
+
]);
|
51
|
+
const operatorMessage = useMemo(()=>({
|
52
|
+
["RouteBack"]: historyBack,
|
53
|
+
["RouteForward"]: historyForward,
|
54
|
+
["RouteChange"]: navigateRef.current
|
55
|
+
}), [
|
56
|
+
historyBack,
|
57
|
+
historyForward,
|
58
|
+
navigateRef
|
59
|
+
]);
|
60
|
+
useEffect(()=>{
|
61
|
+
if (isActive.current) {
|
62
|
+
isActive.current = false;
|
63
|
+
return;
|
64
|
+
}
|
65
|
+
postMessage({
|
66
|
+
type: 'ChildLocationChange',
|
67
|
+
data: location
|
68
|
+
});
|
69
|
+
}, [
|
70
|
+
location
|
71
|
+
]);
|
72
|
+
const handleMessage = useCallback((event)=>{
|
73
|
+
const { data } = event;
|
74
|
+
if (isRouteMessageType(data?.type)) operatorMessage[data?.type](data?.data);
|
75
|
+
}, [
|
76
|
+
operatorMessage
|
77
|
+
]);
|
78
|
+
useEffect(()=>{
|
79
|
+
window.addEventListener('message', handleMessage);
|
80
|
+
return ()=>{
|
81
|
+
window.removeEventListener('message', handleMessage);
|
82
|
+
};
|
83
|
+
}, [
|
84
|
+
handleMessage
|
85
|
+
]);
|
86
|
+
return /*#__PURE__*/ jsx("div", {
|
87
|
+
style: {
|
88
|
+
display: 'none'
|
89
|
+
}
|
90
|
+
});
|
91
|
+
}
|
92
|
+
export { IframeBridge as default };
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,44 @@
|
|
1
|
+
import { postMessage } from "../../utils/postMessage.js";
|
2
|
+
const PROXY_CONSOLE_METHOD = [
|
3
|
+
'log',
|
4
|
+
'info',
|
5
|
+
'warn',
|
6
|
+
'error'
|
7
|
+
];
|
8
|
+
const LOG_FILTER_PREFIX = [
|
9
|
+
'[Dataloom]',
|
10
|
+
'[MiaoDa]'
|
11
|
+
];
|
12
|
+
const initHandleError = ()=>{
|
13
|
+
window.onerror = (message, source, lineno, colno, error)=>{
|
14
|
+
const errorList = [];
|
15
|
+
if (error) errorList.push(error);
|
16
|
+
else {
|
17
|
+
if ('string' == typeof message && [
|
18
|
+
'Script error.'
|
19
|
+
].includes(message)) return;
|
20
|
+
if (message) errorList.push(message);
|
21
|
+
else if (source) errorList.push(source);
|
22
|
+
}
|
23
|
+
console.error('[MiaoDa]', ...errorList);
|
24
|
+
};
|
25
|
+
};
|
26
|
+
const initLogInterceptor = ()=>{
|
27
|
+
PROXY_CONSOLE_METHOD.forEach((method)=>{
|
28
|
+
const originalMethod = window.console[method];
|
29
|
+
window.console[method] = (...args)=>{
|
30
|
+
originalMethod(...args);
|
31
|
+
const log = args[0];
|
32
|
+
if ('string' == typeof log && LOG_FILTER_PREFIX.some((prefix)=>log.startsWith(prefix))) postMessage({
|
33
|
+
type: 'Console',
|
34
|
+
method,
|
35
|
+
data: args
|
36
|
+
});
|
37
|
+
};
|
38
|
+
});
|
39
|
+
};
|
40
|
+
function init() {
|
41
|
+
initHandleError();
|
42
|
+
initLogInterceptor();
|
43
|
+
}
|
44
|
+
'production' !== process.env.NODE_ENV && init();
|
@@ -0,0 +1,40 @@
|
|
1
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
2
|
+
import { useEffect } from "react";
|
3
|
+
import html2canvas_pro from "html2canvas-pro";
|
4
|
+
import { postMessage } from "../../utils/postMessage.js";
|
5
|
+
function PageHoc(props) {
|
6
|
+
const { children } = props;
|
7
|
+
useEffect(()=>{
|
8
|
+
if ('production' !== process.env.NODE_ENV) setTimeout(()=>{
|
9
|
+
const viewportWidth = window.innerWidth;
|
10
|
+
const viewportHeight = window.innerHeight;
|
11
|
+
const devicePixelRatio = window.devicePixelRatio || 1;
|
12
|
+
const captureWidth = Math.min(viewportWidth, document.documentElement.scrollWidth);
|
13
|
+
const captureHeight = Math.min(viewportHeight, document.documentElement.scrollHeight);
|
14
|
+
html2canvas_pro(document.body, {
|
15
|
+
width: captureWidth,
|
16
|
+
height: captureHeight,
|
17
|
+
scrollX: window.scrollX,
|
18
|
+
scrollY: window.scrollY,
|
19
|
+
useCORS: true,
|
20
|
+
allowTaint: true,
|
21
|
+
backgroundColor: '#ffffff',
|
22
|
+
scale: devicePixelRatio,
|
23
|
+
windowWidth: viewportWidth,
|
24
|
+
windowHeight: viewportHeight
|
25
|
+
}).then((canvas)=>{
|
26
|
+
const base64 = canvas.toDataURL('image/png');
|
27
|
+
postMessage({
|
28
|
+
type: 'PageScreenshot',
|
29
|
+
data: base64
|
30
|
+
});
|
31
|
+
}).catch((error)=>{
|
32
|
+
console.error('截图失败:', error);
|
33
|
+
});
|
34
|
+
}, 1000);
|
35
|
+
}, []);
|
36
|
+
return /*#__PURE__*/ jsx(Fragment, {
|
37
|
+
children: children
|
38
|
+
});
|
39
|
+
}
|
40
|
+
export { PageHoc };
|
@@ -0,0 +1,188 @@
|
|
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;
|
@@ -0,0 +1,290 @@
|
|
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 };
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function registerDayjsPlugins(): void;
|