@vlian/framework 1.2.25 → 1.2.37
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/dist/analytics.umd.js +208 -2395
- package/dist/analytics.umd.js.map +1 -1
- package/dist/core/error/ErrorHandler.cjs.map +1 -1
- package/dist/core/error/ErrorHandler.d.ts +1 -1
- package/dist/core/error/ErrorHandler.js.map +1 -1
- package/dist/core/router/monitoring/RouterMonitoring.cjs +1 -1
- package/dist/core/router/monitoring/RouterMonitoring.cjs.map +1 -1
- package/dist/core/router/monitoring/RouterMonitoring.js +1 -1
- package/dist/core/router/monitoring/RouterMonitoring.js.map +1 -1
- package/dist/core/router/utils/adapters/react-router/transform.cjs +4 -0
- package/dist/core/router/utils/adapters/react-router/transform.cjs.map +1 -1
- package/dist/core/router/utils/adapters/react-router/transform.js +4 -0
- package/dist/core/router/utils/adapters/react-router/transform.js.map +1 -1
- package/dist/core/startup/initializeServices.cjs +1 -1
- package/dist/core/startup/initializeServices.cjs.map +1 -1
- package/dist/core/startup/initializeServices.d.ts +1 -1
- package/dist/core/startup/initializeServices.js +1 -1
- package/dist/core/startup/initializeServices.js.map +1 -1
- package/dist/core/startup/performanceTracker.cjs.map +1 -1
- package/dist/core/startup/performanceTracker.d.ts +1 -1
- package/dist/core/startup/performanceTracker.js.map +1 -1
- package/dist/core/startup/renderApp.cjs +1 -1
- package/dist/core/startup/renderApp.cjs.map +1 -1
- package/dist/core/startup/renderApp.d.ts +1 -1
- package/dist/core/startup/renderApp.js +1 -1
- package/dist/core/startup/renderApp.js.map +1 -1
- package/dist/core/startup/startApp.cjs +2 -2
- package/dist/core/startup/startApp.cjs.map +1 -1
- package/dist/core/startup/startApp.js +3 -3
- package/dist/core/startup/startApp.js.map +1 -1
- package/dist/core/types.d.ts +1 -1
- package/dist/core/types.js.map +1 -1
- package/dist/index.cjs +15 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +6 -4
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -1
- package/dist/index.umd.js +11648 -32616
- package/dist/index.umd.js.map +1 -1
- package/dist/kernel/constants.cjs +67 -0
- package/dist/kernel/constants.cjs.map +1 -0
- package/dist/kernel/constants.d.ts +5 -0
- package/dist/kernel/constants.js +43 -0
- package/dist/kernel/constants.js.map +1 -0
- package/dist/kernel/index.cjs +40 -0
- package/dist/kernel/index.cjs.map +1 -0
- package/dist/kernel/index.d.ts +3 -0
- package/dist/kernel/index.js +4 -0
- package/dist/kernel/index.js.map +1 -0
- package/dist/kernel/kernel.cjs +296 -0
- package/dist/kernel/kernel.cjs.map +1 -0
- package/dist/kernel/kernel.d.ts +40 -0
- package/dist/kernel/kernel.js +272 -0
- package/dist/kernel/kernel.js.map +1 -0
- package/dist/kernel/manager/cacheManager.cjs +46 -0
- package/dist/kernel/manager/cacheManager.cjs.map +1 -0
- package/dist/kernel/manager/cacheManager.d.ts +6 -0
- package/dist/kernel/manager/cacheManager.js +36 -0
- package/dist/kernel/manager/cacheManager.js.map +1 -0
- package/dist/kernel/manager/i18nManager.cjs +68 -0
- package/dist/kernel/manager/i18nManager.cjs.map +1 -0
- package/dist/kernel/manager/i18nManager.d.ts +8 -0
- package/dist/kernel/manager/i18nManager.js +58 -0
- package/dist/kernel/manager/i18nManager.js.map +1 -0
- package/dist/kernel/manager/index.cjs +30 -0
- package/dist/kernel/manager/index.cjs.map +1 -0
- package/dist/kernel/manager/index.d.ts +4 -0
- package/dist/kernel/manager/index.js +6 -0
- package/dist/kernel/manager/index.js.map +1 -0
- package/dist/kernel/manager/loggerManager.cjs +70 -0
- package/dist/kernel/manager/loggerManager.cjs.map +1 -0
- package/dist/kernel/manager/loggerManager.d.ts +14 -0
- package/dist/kernel/manager/loggerManager.js +60 -0
- package/dist/kernel/manager/loggerManager.js.map +1 -0
- package/dist/kernel/manager/persistence.cjs +93 -0
- package/dist/kernel/manager/persistence.cjs.map +1 -0
- package/dist/kernel/manager/persistence.d.ts +3 -0
- package/dist/kernel/manager/persistence.js +75 -0
- package/dist/kernel/manager/persistence.js.map +1 -0
- package/dist/kernel/manager/themeManager.cjs +85 -0
- package/dist/kernel/manager/themeManager.cjs.map +1 -0
- package/dist/kernel/manager/themeManager.d.ts +9 -0
- package/dist/kernel/manager/themeManager.js +75 -0
- package/dist/kernel/manager/themeManager.js.map +1 -0
- package/dist/kernel/types.cjs +6 -0
- package/dist/kernel/types.cjs.map +1 -0
- package/dist/kernel/types.d.ts +72 -0
- package/dist/kernel/types.js +3 -0
- package/dist/kernel/types.js.map +1 -0
- package/dist/request/adapter.d.ts +1 -0
- package/dist/request/core.d.ts +1 -0
- package/dist/request/index.d.ts +1 -42
- package/dist/request/plugin/csrfPlugin.d.ts +2 -2
- package/dist/request/plugin/queue.d.ts +2 -2
- package/dist/request/plugin.d.ts +1 -0
- package/dist/request/runtime.d.ts +1 -0
- package/dist/request/types.d.ts +1 -394
- package/dist/request/utils.d.ts +1 -0
- package/dist/state.umd.js +1 -1
- package/dist/utils/csrf.cjs +13 -152
- package/dist/utils/csrf.cjs.map +1 -1
- package/dist/utils/csrf.d.ts +1 -72
- package/dist/utils/csrf.js +1 -142
- package/dist/utils/csrf.js.map +1 -1
- package/dist/utils/errors/ErrorCodes.cjs +6 -76
- package/dist/utils/errors/ErrorCodes.cjs.map +1 -1
- package/dist/utils/errors/ErrorCodes.d.ts +1 -45
- package/dist/utils/errors/ErrorCodes.js +1 -84
- package/dist/utils/errors/ErrorCodes.js.map +1 -1
- package/dist/utils/errors.cjs +15 -344
- package/dist/utils/errors.cjs.map +1 -1
- package/dist/utils/errors.d.ts +1 -183
- package/dist/utils/errors.js +1 -352
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/logger.cjs +5 -374
- package/dist/utils/logger.cjs.map +1 -1
- package/dist/utils/logger.d.ts +2 -189
- package/dist/utils/logger.js +1 -379
- package/dist/utils/logger.js.map +1 -1
- package/dist/utils/logger.types.cjs +3 -12
- package/dist/utils/logger.types.cjs.map +1 -1
- package/dist/utils/logger.types.d.ts +2 -57
- package/dist/utils/logger.types.js +1 -10
- package/dist/utils/logger.types.js.map +1 -1
- package/dist/utils/monitoring.cjs +11 -302
- package/dist/utils/monitoring.cjs.map +1 -1
- package/dist/utils/monitoring.d.ts +1 -163
- package/dist/utils/monitoring.js +1 -294
- package/dist/utils/monitoring.js.map +1 -1
- package/dist/utils/performance.cjs +5 -352
- package/dist/utils/performance.cjs.map +1 -1
- package/dist/utils/performance.d.ts +2 -246
- package/dist/utils/performance.js +1 -354
- package/dist/utils/performance.js.map +1 -1
- package/dist/utils/resourceLoader.cjs +5 -303
- package/dist/utils/resourceLoader.cjs.map +1 -1
- package/dist/utils/resourceLoader.d.ts +2 -130
- package/dist/utils/resourceLoader.js +1 -305
- package/dist/utils/resourceLoader.js.map +1 -1
- package/dist/utils/runtimeSecurity.cjs +2 -140
- package/dist/utils/runtimeSecurity.cjs.map +1 -1
- package/dist/utils/runtimeSecurity.d.ts +2 -104
- package/dist/utils/runtimeSecurity.js +1 -141
- package/dist/utils/runtimeSecurity.js.map +1 -1
- package/dist/utils/security.cjs +3 -314
- package/dist/utils/security.cjs.map +1 -1
- package/dist/utils/security.d.ts +2 -80
- package/dist/utils/security.js +1 -311
- package/dist/utils/security.js.map +1 -1
- package/dist/utils/traceId.cjs +10 -111
- package/dist/utils/traceId.cjs.map +1 -1
- package/dist/utils/traceId.d.ts +1 -63
- package/dist/utils/traceId.js +1 -116
- package/dist/utils/traceId.js.map +1 -1
- package/dist/utils/validation.cjs +3 -173
- package/dist/utils/validation.cjs.map +1 -1
- package/dist/utils/validation.d.ts +2 -110
- package/dist/utils/validation.js +1 -175
- package/dist/utils/validation.js.map +1 -1
- package/package.json +15 -24
- package/dist/lazy/index.cjs +0 -104
- package/dist/lazy/index.cjs.map +0 -1
- package/dist/lazy/index.d.ts +0 -19
- package/dist/lazy/index.js +0 -24
- package/dist/lazy/index.js.map +0 -1
- package/dist/request/adapter/RequestAdapter.cjs +0 -78
- package/dist/request/adapter/RequestAdapter.cjs.map +0 -1
- package/dist/request/adapter/axiosAdapter.cjs +0 -164
- package/dist/request/adapter/axiosAdapter.cjs.map +0 -1
- package/dist/request/adapter/fetchAdapter.cjs +0 -134
- package/dist/request/adapter/fetchAdapter.cjs.map +0 -1
- package/dist/request/adapter/index.cjs +0 -80
- package/dist/request/adapter/index.cjs.map +0 -1
- package/dist/request/adapter/kyAdapter.cjs +0 -191
- package/dist/request/adapter/kyAdapter.cjs.map +0 -1
- package/dist/request/adapter/undiciAdapter.cjs +0 -213
- package/dist/request/adapter/undiciAdapter.cjs.map +0 -1
- package/dist/request/core/RequestClient.cjs +0 -558
- package/dist/request/core/RequestClient.cjs.map +0 -1
- package/dist/request/core/index.cjs +0 -15
- package/dist/request/core/index.cjs.map +0 -1
- package/dist/request/index.cjs +0 -149
- package/dist/request/index.cjs.map +0 -1
- package/dist/request/plugin/RequestPlugin.cjs +0 -218
- package/dist/request/plugin/RequestPlugin.cjs.map +0 -1
- package/dist/request/plugin/cache.cjs +0 -269
- package/dist/request/plugin/cache.cjs.map +0 -1
- package/dist/request/plugin/csrfPlugin.cjs +0 -40
- package/dist/request/plugin/csrfPlugin.cjs.map +0 -1
- package/dist/request/plugin/index.cjs +0 -53
- package/dist/request/plugin/index.cjs.map +0 -1
- package/dist/request/plugin/monitoring.cjs +0 -216
- package/dist/request/plugin/monitoring.cjs.map +0 -1
- package/dist/request/plugin/queue.cjs +0 -140
- package/dist/request/plugin/queue.cjs.map +0 -1
- package/dist/request/plugin/retry.cjs +0 -98
- package/dist/request/plugin/retry.cjs.map +0 -1
- package/dist/request/plugin/validation.cjs +0 -121
- package/dist/request/plugin/validation.cjs.map +0 -1
- package/dist/request/runtime/RequestContext.cjs +0 -77
- package/dist/request/runtime/RequestContext.cjs.map +0 -1
- package/dist/request/runtime/index.cjs +0 -32
- package/dist/request/runtime/index.cjs.map +0 -1
- package/dist/request/types.cjs +0 -112
- package/dist/request/types.cjs.map +0 -1
- package/dist/request/utils/RequestQueueManager.cjs +0 -168
- package/dist/request/utils/RequestQueueManager.cjs.map +0 -1
- package/dist/request/utils/dependencyCheck.cjs +0 -237
- package/dist/request/utils/dependencyCheck.cjs.map +0 -1
- package/dist/request/utils/index.cjs +0 -30
- package/dist/request/utils/index.cjs.map +0 -1
- package/dist/request.umd.js +0 -5392
- package/dist/request.umd.js.map +0 -1
package/dist/request/index.cjs
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 请求库主入口
|
|
3
|
-
*
|
|
4
|
-
* 提供企业级、可扩展、类型安全的 HTTP 请求能力
|
|
5
|
-
*/ // 导出核心类型
|
|
6
|
-
"use strict";
|
|
7
|
-
Object.defineProperty(exports, "__esModule", {
|
|
8
|
-
value: true
|
|
9
|
-
});
|
|
10
|
-
function _export(target, all) {
|
|
11
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
12
|
-
enumerable: true,
|
|
13
|
-
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
_export(exports, {
|
|
17
|
-
get MonitoringEventType () {
|
|
18
|
-
return _plugin.MonitoringEventType;
|
|
19
|
-
},
|
|
20
|
-
get PluginLifecycleStage () {
|
|
21
|
-
return _RequestPlugin.PluginLifecycleStage;
|
|
22
|
-
},
|
|
23
|
-
get PluginManager () {
|
|
24
|
-
return _RequestPlugin.PluginManager;
|
|
25
|
-
},
|
|
26
|
-
get PluginPriority () {
|
|
27
|
-
return _RequestPlugin.PluginPriority;
|
|
28
|
-
},
|
|
29
|
-
get RequestClient () {
|
|
30
|
-
return _RequestClient.RequestClient;
|
|
31
|
-
},
|
|
32
|
-
get RequestError () {
|
|
33
|
-
return _types.RequestError;
|
|
34
|
-
},
|
|
35
|
-
get RequestErrorCode () {
|
|
36
|
-
return _types.RequestErrorCode;
|
|
37
|
-
},
|
|
38
|
-
get RequestErrorType () {
|
|
39
|
-
return _types.RequestErrorType;
|
|
40
|
-
},
|
|
41
|
-
get adapterRegistry () {
|
|
42
|
-
return _RequestAdapter.adapterRegistry;
|
|
43
|
-
},
|
|
44
|
-
get axiosAdapter () {
|
|
45
|
-
return _adapter.axiosAdapter;
|
|
46
|
-
},
|
|
47
|
-
get cancelRequestContext () {
|
|
48
|
-
return _RequestContext.cancelRequestContext;
|
|
49
|
-
},
|
|
50
|
-
get completeRequestContext () {
|
|
51
|
-
return _RequestContext.completeRequestContext;
|
|
52
|
-
},
|
|
53
|
-
get createCachePlugin () {
|
|
54
|
-
return _plugin.createCachePlugin;
|
|
55
|
-
},
|
|
56
|
-
get createMonitoringPlugin () {
|
|
57
|
-
return _plugin.createMonitoringPlugin;
|
|
58
|
-
},
|
|
59
|
-
get createQueuePlugin () {
|
|
60
|
-
return _plugin.createQueuePlugin;
|
|
61
|
-
},
|
|
62
|
-
get createRequestClient () {
|
|
63
|
-
return createRequestClient;
|
|
64
|
-
},
|
|
65
|
-
get createRequestContext () {
|
|
66
|
-
return _RequestContext.createRequestContext;
|
|
67
|
-
},
|
|
68
|
-
get createRetryPlugin () {
|
|
69
|
-
return _plugin.createRetryPlugin;
|
|
70
|
-
},
|
|
71
|
-
get createValidationPlugin () {
|
|
72
|
-
return _plugin.createValidationPlugin;
|
|
73
|
-
},
|
|
74
|
-
get failRequestContext () {
|
|
75
|
-
return _RequestContext.failRequestContext;
|
|
76
|
-
},
|
|
77
|
-
get fetchAdapter () {
|
|
78
|
-
return _adapter.fetchAdapter;
|
|
79
|
-
},
|
|
80
|
-
get kyAdapter () {
|
|
81
|
-
return _adapter.kyAdapter;
|
|
82
|
-
},
|
|
83
|
-
get undiciAdapter () {
|
|
84
|
-
return undiciAdapter;
|
|
85
|
-
},
|
|
86
|
-
get updateRequestContext () {
|
|
87
|
-
return _RequestContext.updateRequestContext;
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
const _types = require("./types");
|
|
91
|
-
const _RequestClient = require("./core/RequestClient");
|
|
92
|
-
const _RequestAdapter = require("./adapter/RequestAdapter");
|
|
93
|
-
const _adapter = require("./adapter");
|
|
94
|
-
const _RequestPlugin = require("./plugin/RequestPlugin");
|
|
95
|
-
const _plugin = require("./plugin");
|
|
96
|
-
const _RequestContext = require("./runtime/RequestContext");
|
|
97
|
-
function _getRequireWildcardCache(nodeInterop) {
|
|
98
|
-
if (typeof WeakMap !== "function") return null;
|
|
99
|
-
var cacheBabelInterop = new WeakMap();
|
|
100
|
-
var cacheNodeInterop = new WeakMap();
|
|
101
|
-
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
102
|
-
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
103
|
-
})(nodeInterop);
|
|
104
|
-
}
|
|
105
|
-
function _interop_require_wildcard(obj, nodeInterop) {
|
|
106
|
-
if (!nodeInterop && obj && obj.__esModule) {
|
|
107
|
-
return obj;
|
|
108
|
-
}
|
|
109
|
-
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
110
|
-
return {
|
|
111
|
-
default: obj
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
var cache = _getRequireWildcardCache(nodeInterop);
|
|
115
|
-
if (cache && cache.has(obj)) {
|
|
116
|
-
return cache.get(obj);
|
|
117
|
-
}
|
|
118
|
-
var newObj = {
|
|
119
|
-
__proto__: null
|
|
120
|
-
};
|
|
121
|
-
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
122
|
-
for(var key in obj){
|
|
123
|
-
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
124
|
-
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
125
|
-
if (desc && (desc.get || desc.set)) {
|
|
126
|
-
Object.defineProperty(newObj, key, desc);
|
|
127
|
-
} else {
|
|
128
|
-
newObj[key] = obj[key];
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
newObj.default = obj;
|
|
133
|
-
if (cache) {
|
|
134
|
-
cache.set(obj, newObj);
|
|
135
|
-
}
|
|
136
|
-
return newObj;
|
|
137
|
-
}
|
|
138
|
-
async function undiciAdapter() {
|
|
139
|
-
const { undiciAdapter: adapter } = await Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard(require("./adapter")));
|
|
140
|
-
return adapter();
|
|
141
|
-
}
|
|
142
|
-
async function createRequestClient(config) {
|
|
143
|
-
const { RequestClient } = await Promise.resolve().then(()=>/*#__PURE__*/ _interop_require_wildcard(require("./core/RequestClient")));
|
|
144
|
-
const client = new RequestClient(config);
|
|
145
|
-
await client.initialize();
|
|
146
|
-
return client;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/request/index.ts"],"sourcesContent":["/**\n * 请求库主入口\n * \n * 提供企业级、可扩展、类型安全的 HTTP 请求能力\n */\n\n// 导出核心类型\nexport type {\n RequestOptions,\n Response,\n RequestContext,\n RequestHeaders,\n RequestQuery,\n RequestBody,\n HttpMethod,\n RetryConfig,\n CacheConfig,\n ValidationConfig,\n AdapterCapabilities,\n} from './types';\n\nexport {\n RequestError,\n RequestErrorType,\n RequestErrorCode,\n} from './types';\n\n// 导出请求客户端\nexport { RequestClient } from './core/RequestClient';\nexport type { RequestClientConfig } from './types';\n\n// 导出适配器\nexport type { RequestAdapter, AdapterFactory } from './adapter/RequestAdapter';\nexport { adapterRegistry } from './adapter/RequestAdapter';\nexport { fetchAdapter, kyAdapter, axiosAdapter } from './adapter';\n\n// undiciAdapter 使用懒加载,避免在未使用时触发依赖检查\nexport async function undiciAdapter() {\n const { undiciAdapter: adapter } = await import('./adapter');\n return adapter();\n}\n\n// 导出插件系统\nexport type { RequestPlugin } from './plugin/RequestPlugin';\nexport { PluginManager, PluginLifecycleStage, PluginPriority } from './plugin/RequestPlugin';\n\n// 导出企业级插件\nexport {\n createCachePlugin,\n createRetryPlugin,\n createValidationPlugin,\n createMonitoringPlugin,\n createQueuePlugin,\n} from './plugin';\n\nexport type {\n CachePluginConfig,\n RetryPluginConfig,\n ValidationPluginConfig,\n MonitoringPluginConfig,\n MonitoringEvent,\n MonitoringReporter,\n QueuePluginConfig,\n QueueTriggerCondition,\n} from './plugin';\n\nexport { MonitoringEventType } from './plugin';\n\n// 导出运行时工具\nexport {\n createRequestContext,\n updateRequestContext,\n completeRequestContext,\n failRequestContext,\n cancelRequestContext,\n} from './runtime/RequestContext';\n\n/**\n * 创建请求客户端实例\n * \n * @param config - 请求客户端配置\n * @returns 请求客户端实例\n * \n * @example\n * ```typescript\n * import { createRequestClient } from '@vlian/framework/request';\n * \n * const client = createRequestClient({\n * baseURL: 'https://api.example.com',\n * defaultTimeout: 5000,\n * plugins: [\n * createCachePlugin(),\n * createRetryPlugin(),\n * ],\n * });\n * \n * const response = await client.get<User>('/user/1');\n * ```\n */\nexport async function createRequestClient(config?: import('./types').RequestClientConfig) {\n const { RequestClient } = await import('./core/RequestClient');\n const client = new RequestClient(config);\n await client.initialize();\n return client;\n}\n"],"names":["MonitoringEventType","PluginLifecycleStage","PluginManager","PluginPriority","RequestClient","RequestError","RequestErrorCode","RequestErrorType","adapterRegistry","axiosAdapter","cancelRequestContext","completeRequestContext","createCachePlugin","createMonitoringPlugin","createQueuePlugin","createRequestClient","createRequestContext","createRetryPlugin","createValidationPlugin","failRequestContext","fetchAdapter","kyAdapter","undiciAdapter","updateRequestContext","adapter","config","client","initialize"],"mappings":"AAAA;;;;CAIC,GAED,SAAS;;;;;;;;;;;;QA4DAA;eAAAA,2BAAmB;;QAtBJC;eAAAA,mCAAoB;;QAAnCC;eAAAA,4BAAa;;QAAwBC;eAAAA,6BAAc;;QAhBnDC;eAAAA,4BAAa;;QANpBC;eAAAA,mBAAY;;QAEZC;eAAAA,uBAAgB;;QADhBC;eAAAA,uBAAgB;;QAUTC;eAAAA,+BAAe;;QACUC;eAAAA,qBAAY;;QAwC5CC;eAAAA,oCAAoB;;QAFpBC;eAAAA,sCAAsB;;QAxBtBC;eAAAA,yBAAiB;;QAGjBC;eAAAA,8BAAsB;;QACtBC;eAAAA,yBAAiB;;QA+CGC;eAAAA;;QA7BpBC;eAAAA,oCAAoB;;QArBpBC;eAAAA,yBAAiB;;QACjBC;eAAAA,8BAAsB;;QAuBtBC;eAAAA,kCAAkB;;QAvCXC;eAAAA,qBAAY;;QAAEC;eAAAA,kBAAS;;QAGVC;eAAAA;;QAkCpBC;eAAAA,oCAAoB;;;uBA9Cf;+BAGuB;gCAKE;yBACsB;+BAUc;wBAS7D;gCAsBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAtCA,eAAeD;IACpB,MAAM,EAAEA,eAAeE,OAAO,EAAE,GAAG,MAAM,mEAAA,QAAO;IAChD,OAAOA;AACT;AA2DO,eAAeT,oBAAoBU,MAA8C;IACtF,MAAM,EAAErB,aAAa,EAAE,GAAG,MAAM,mEAAA,QAAO;IACvC,MAAMsB,SAAS,IAAItB,cAAcqB;IACjC,MAAMC,OAAOC,UAAU;IACvB,OAAOD;AACT"}
|
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 请求插件系统
|
|
3
|
-
*
|
|
4
|
-
* 插件系统提供生命周期钩子,允许扩展请求能力
|
|
5
|
-
*/ "use strict";
|
|
6
|
-
Object.defineProperty(exports, "__esModule", {
|
|
7
|
-
value: true
|
|
8
|
-
});
|
|
9
|
-
function _export(target, all) {
|
|
10
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
11
|
-
enumerable: true,
|
|
12
|
-
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
_export(exports, {
|
|
16
|
-
get PluginLifecycleStage () {
|
|
17
|
-
return PluginLifecycleStage;
|
|
18
|
-
},
|
|
19
|
-
get PluginManager () {
|
|
20
|
-
return PluginManager;
|
|
21
|
-
},
|
|
22
|
-
get PluginPriority () {
|
|
23
|
-
return PluginPriority;
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
const _logger = require("../../utils/logger");
|
|
27
|
-
function _define_property(obj, key, value) {
|
|
28
|
-
if (key in obj) {
|
|
29
|
-
Object.defineProperty(obj, key, {
|
|
30
|
-
value: value,
|
|
31
|
-
enumerable: true,
|
|
32
|
-
configurable: true,
|
|
33
|
-
writable: true
|
|
34
|
-
});
|
|
35
|
-
} else {
|
|
36
|
-
obj[key] = value;
|
|
37
|
-
}
|
|
38
|
-
return obj;
|
|
39
|
-
}
|
|
40
|
-
var PluginLifecycleStage = /*#__PURE__*/ function(PluginLifecycleStage) {
|
|
41
|
-
/**
|
|
42
|
-
* 初始化阶段
|
|
43
|
-
*/ PluginLifecycleStage["INIT"] = "init";
|
|
44
|
-
/**
|
|
45
|
-
* 请求前阶段
|
|
46
|
-
*/ PluginLifecycleStage["BEFORE_REQUEST"] = "beforeRequest";
|
|
47
|
-
/**
|
|
48
|
-
* 请求后阶段(成功)
|
|
49
|
-
*/ PluginLifecycleStage["AFTER_REQUEST"] = "afterRequest";
|
|
50
|
-
/**
|
|
51
|
-
* 错误处理阶段
|
|
52
|
-
*/ PluginLifecycleStage["ON_ERROR"] = "onError";
|
|
53
|
-
/**
|
|
54
|
-
* 最终阶段(无论成功或失败)
|
|
55
|
-
*/ PluginLifecycleStage["FINALLY"] = "finally";
|
|
56
|
-
return PluginLifecycleStage;
|
|
57
|
-
}({});
|
|
58
|
-
var PluginPriority = /*#__PURE__*/ function(PluginPriority) {
|
|
59
|
-
/**
|
|
60
|
-
* 最高优先级(如:认证、签名)
|
|
61
|
-
*/ PluginPriority[PluginPriority["HIGHEST"] = 0] = "HIGHEST";
|
|
62
|
-
/**
|
|
63
|
-
* 高优先级(如:请求转换、参数验证)
|
|
64
|
-
*/ PluginPriority[PluginPriority["HIGH"] = 25] = "HIGH";
|
|
65
|
-
/**
|
|
66
|
-
* 默认优先级
|
|
67
|
-
*/ PluginPriority[PluginPriority["NORMAL"] = 50] = "NORMAL";
|
|
68
|
-
/**
|
|
69
|
-
* 低优先级(如:日志、监控)
|
|
70
|
-
*/ PluginPriority[PluginPriority["LOW"] = 75] = "LOW";
|
|
71
|
-
/**
|
|
72
|
-
* 最低优先级(如:缓存、重试)
|
|
73
|
-
*/ PluginPriority[PluginPriority["LOWEST"] = 100] = "LOWEST";
|
|
74
|
-
return PluginPriority;
|
|
75
|
-
}({});
|
|
76
|
-
let PluginManager = class PluginManager {
|
|
77
|
-
/**
|
|
78
|
-
* 注册插件
|
|
79
|
-
*
|
|
80
|
-
* @param plugin - 请求插件
|
|
81
|
-
*/ register(plugin) {
|
|
82
|
-
if (this.plugins.has(plugin.name)) {
|
|
83
|
-
_logger.logger.warn(`插件 ${plugin.name} 已存在,将被覆盖`);
|
|
84
|
-
}
|
|
85
|
-
this.plugins.set(plugin.name, plugin);
|
|
86
|
-
this.updatePluginOrder();
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* 注销插件
|
|
90
|
-
*
|
|
91
|
-
* @param name - 插件名称
|
|
92
|
-
*/ unregister(name) {
|
|
93
|
-
this.plugins.delete(name);
|
|
94
|
-
this.updatePluginOrder();
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* 获取插件
|
|
98
|
-
*
|
|
99
|
-
* @param name - 插件名称
|
|
100
|
-
* @returns 插件实例
|
|
101
|
-
*/ get(name) {
|
|
102
|
-
return this.plugins.get(name);
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* 获取所有插件
|
|
106
|
-
*
|
|
107
|
-
* @returns 插件数组
|
|
108
|
-
*/ getAll() {
|
|
109
|
-
return this.pluginOrder.map((name)=>this.plugins.get(name)).filter(Boolean);
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* 更新插件执行顺序(考虑依赖和优先级)
|
|
113
|
-
*/ updatePluginOrder() {
|
|
114
|
-
const plugins = Array.from(this.plugins.values());
|
|
115
|
-
// 构建依赖图
|
|
116
|
-
const dependencyGraph = new Map();
|
|
117
|
-
const priorities = new Map();
|
|
118
|
-
for (const plugin of plugins){
|
|
119
|
-
dependencyGraph.set(plugin.name, plugin.dependencies || []);
|
|
120
|
-
priorities.set(plugin.name, plugin.priority ?? 50);
|
|
121
|
-
}
|
|
122
|
-
// 拓扑排序
|
|
123
|
-
const visited = new Set();
|
|
124
|
-
const visiting = new Set();
|
|
125
|
-
const result = [];
|
|
126
|
-
const visit = (pluginName)=>{
|
|
127
|
-
if (visiting.has(pluginName)) {
|
|
128
|
-
_logger.logger.warn(`检测到循环依赖,涉及插件: ${pluginName}`);
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
if (visited.has(pluginName)) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
visiting.add(pluginName);
|
|
135
|
-
const deps = dependencyGraph.get(pluginName) || [];
|
|
136
|
-
for (const dep of deps){
|
|
137
|
-
visit(dep);
|
|
138
|
-
}
|
|
139
|
-
visiting.delete(pluginName);
|
|
140
|
-
visited.add(pluginName);
|
|
141
|
-
result.push(pluginName);
|
|
142
|
-
};
|
|
143
|
-
// 按优先级排序后拓扑排序
|
|
144
|
-
const sortedPlugins = plugins.sort((a, b)=>{
|
|
145
|
-
const priorityA = priorities.get(a.name) ?? 50;
|
|
146
|
-
const priorityB = priorities.get(b.name) ?? 50;
|
|
147
|
-
return priorityA - priorityB;
|
|
148
|
-
});
|
|
149
|
-
for (const plugin of sortedPlugins){
|
|
150
|
-
visit(plugin.name);
|
|
151
|
-
}
|
|
152
|
-
this.pluginOrder = result;
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* 执行插件钩子
|
|
156
|
-
*
|
|
157
|
-
* @param stage - 生命周期阶段
|
|
158
|
-
* @param context - 请求上下文
|
|
159
|
-
* @returns 处理结果
|
|
160
|
-
*/ async executeHook(stage, context) {
|
|
161
|
-
const plugins = this.getAll();
|
|
162
|
-
for (const plugin of plugins){
|
|
163
|
-
// 检查是否跳过此插件
|
|
164
|
-
if (context.request.skipPlugins?.includes(plugin.name)) {
|
|
165
|
-
continue;
|
|
166
|
-
}
|
|
167
|
-
try {
|
|
168
|
-
let result = undefined;
|
|
169
|
-
switch(stage){
|
|
170
|
-
case "init":
|
|
171
|
-
result = await plugin.onInit?.(context);
|
|
172
|
-
break;
|
|
173
|
-
case "beforeRequest":
|
|
174
|
-
result = await plugin.onBeforeRequest?.(context);
|
|
175
|
-
if (result && typeof result === 'object' && 'url' in result) {
|
|
176
|
-
context.request = result;
|
|
177
|
-
// 如果返回了修改后的请求配置,立即返回
|
|
178
|
-
return result;
|
|
179
|
-
}
|
|
180
|
-
break;
|
|
181
|
-
case "afterRequest":
|
|
182
|
-
result = await plugin.onAfterRequest?.(context);
|
|
183
|
-
if (result && typeof result === 'object' && 'data' in result) {
|
|
184
|
-
context.response = result;
|
|
185
|
-
// 如果返回了修改后的响应,立即返回
|
|
186
|
-
return result;
|
|
187
|
-
}
|
|
188
|
-
break;
|
|
189
|
-
case "onError":
|
|
190
|
-
result = await plugin.onError?.(context);
|
|
191
|
-
if (result === true) {
|
|
192
|
-
return true; // 错误已处理,停止传播
|
|
193
|
-
}
|
|
194
|
-
break;
|
|
195
|
-
case "finally":
|
|
196
|
-
await plugin.onFinally?.(context);
|
|
197
|
-
break;
|
|
198
|
-
}
|
|
199
|
-
} catch (error) {
|
|
200
|
-
_logger.logger.error(`插件 ${plugin.name} 的 ${stage} 钩子执行失败:`, error);
|
|
201
|
-
// 继续执行其他插件
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
return undefined;
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* 清除所有插件
|
|
208
|
-
*/ clear() {
|
|
209
|
-
this.plugins.clear();
|
|
210
|
-
this.pluginOrder = [];
|
|
211
|
-
}
|
|
212
|
-
constructor(){
|
|
213
|
-
_define_property(this, "plugins", new Map());
|
|
214
|
-
_define_property(this, "pluginOrder", []);
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
//# sourceMappingURL=RequestPlugin.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/request/plugin/RequestPlugin.ts"],"sourcesContent":["/**\n * 请求插件系统\n * \n * 插件系统提供生命周期钩子,允许扩展请求能力\n */\n\nimport type { RequestOptions, Response, RequestContext } from '../types';\nimport { logger } from '../../utils/logger';\n\n/**\n * 插件生命周期阶段\n */\nexport enum PluginLifecycleStage {\n /**\n * 初始化阶段\n */\n INIT = 'init',\n\n /**\n * 请求前阶段\n */\n BEFORE_REQUEST = 'beforeRequest',\n\n /**\n * 请求后阶段(成功)\n */\n AFTER_REQUEST = 'afterRequest',\n\n /**\n * 错误处理阶段\n */\n ON_ERROR = 'onError',\n\n /**\n * 最终阶段(无论成功或失败)\n */\n FINALLY = 'finally',\n}\n\n/**\n * 插件优先级\n */\nexport enum PluginPriority {\n /**\n * 最高优先级(如:认证、签名)\n */\n HIGHEST = 0,\n\n /**\n * 高优先级(如:请求转换、参数验证)\n */\n HIGH = 25,\n\n /**\n * 默认优先级\n */\n NORMAL = 50,\n\n /**\n * 低优先级(如:日志、监控)\n */\n LOW = 75,\n\n /**\n * 最低优先级(如:缓存、重试)\n */\n LOWEST = 100,\n}\n\n/**\n * 请求插件接口\n * \n * @template TRequestMeta - 请求元数据类型\n * @template TResponseMeta - 响应元数据类型\n * @template TContextMeta - 上下文元数据类型\n */\nexport interface RequestPlugin<TContextMeta = Record<string, unknown>> {\n /**\n * 插件名称\n */\n readonly name: string;\n\n /**\n * 插件版本\n */\n readonly version?: string;\n\n /**\n * 插件优先级(数字越小优先级越高)\n */\n readonly priority?: number;\n\n /**\n * 插件依赖列表(插件名称数组)\n */\n readonly dependencies?: string[];\n\n /**\n * 初始化钩子\n * \n * @param context - 请求上下文\n */\n onInit?(context: RequestContext<RequestOptions, Response, TContextMeta>): void | Promise<void>;\n\n /**\n * 请求前钩子\n * \n * 可以修改请求配置、添加请求头、验证参数等\n * \n * @param context - 请求上下文\n * @returns 修改后的请求配置(可选)\n */\n onBeforeRequest?(\n context: RequestContext<RequestOptions, Response, TContextMeta>\n ): RequestOptions | void | Promise<RequestOptions | void>;\n\n /**\n * 请求后钩子(成功)\n * \n * 可以修改响应数据、添加响应元数据等\n * \n * @param context - 请求上下文\n * @returns 修改后的响应(可选)\n */\n onAfterRequest?(\n context: RequestContext<RequestOptions, Response, TContextMeta>\n ): Response | void | Promise<Response | void>;\n\n /**\n * 错误处理钩子\n * \n * 可以处理错误、重试、降级等\n * \n * @param context - 请求上下文\n * @returns 是否已处理错误(如果返回 true,错误不会继续传播)\n */\n onError?(\n context: RequestContext<RequestOptions, Response, TContextMeta>\n ): boolean | void | Promise<boolean | void>;\n\n /**\n * 最终钩子(无论成功或失败)\n * \n * 可以清理资源、记录日志等\n * \n * @param context - 请求上下文\n */\n onFinally?(\n context: RequestContext<RequestOptions, Response, TContextMeta>\n ): void | Promise<void>;\n}\n\n/**\n * 插件管理器\n */\nexport class PluginManager {\n private plugins: Map<string, RequestPlugin> = new Map();\n private pluginOrder: string[] = [];\n\n /**\n * 注册插件\n * \n * @param plugin - 请求插件\n */\n register(plugin: RequestPlugin): void {\n if (this.plugins.has(plugin.name)) {\n logger.warn(`插件 ${plugin.name} 已存在,将被覆盖`);\n }\n\n this.plugins.set(plugin.name, plugin);\n this.updatePluginOrder();\n }\n\n /**\n * 注销插件\n * \n * @param name - 插件名称\n */\n unregister(name: string): void {\n this.plugins.delete(name);\n this.updatePluginOrder();\n }\n\n /**\n * 获取插件\n * \n * @param name - 插件名称\n * @returns 插件实例\n */\n get(name: string): RequestPlugin | undefined {\n return this.plugins.get(name);\n }\n\n /**\n * 获取所有插件\n * \n * @returns 插件数组\n */\n getAll(): RequestPlugin[] {\n return this.pluginOrder.map((name) => this.plugins.get(name)!).filter(Boolean);\n }\n\n /**\n * 更新插件执行顺序(考虑依赖和优先级)\n */\n private updatePluginOrder(): void {\n const plugins = Array.from(this.plugins.values());\n\n // 构建依赖图\n const dependencyGraph = new Map<string, string[]>();\n const priorities = new Map<string, number>();\n\n for (const plugin of plugins) {\n dependencyGraph.set(plugin.name, plugin.dependencies || []);\n priorities.set(plugin.name, plugin.priority ?? PluginPriority.NORMAL);\n }\n\n // 拓扑排序\n const visited = new Set<string>();\n const visiting = new Set<string>();\n const result: string[] = [];\n\n const visit = (pluginName: string): void => {\n if (visiting.has(pluginName)) {\n logger.warn(`检测到循环依赖,涉及插件: ${pluginName}`);\n return;\n }\n if (visited.has(pluginName)) {\n return;\n }\n\n visiting.add(pluginName);\n const deps = dependencyGraph.get(pluginName) || [];\n for (const dep of deps) {\n visit(dep);\n }\n visiting.delete(pluginName);\n visited.add(pluginName);\n result.push(pluginName);\n };\n\n // 按优先级排序后拓扑排序\n const sortedPlugins = plugins.sort((a, b) => {\n const priorityA = priorities.get(a.name) ?? PluginPriority.NORMAL;\n const priorityB = priorities.get(b.name) ?? PluginPriority.NORMAL;\n return priorityA - priorityB;\n });\n\n for (const plugin of sortedPlugins) {\n visit(plugin.name);\n }\n\n this.pluginOrder = result;\n }\n\n /**\n * 执行插件钩子\n * \n * @param stage - 生命周期阶段\n * @param context - 请求上下文\n * @returns 处理结果\n */\n async executeHook<TContext extends RequestContext>(\n stage: PluginLifecycleStage,\n context: TContext\n ): Promise<RequestOptions | Response | boolean | void> {\n const plugins = this.getAll();\n\n for (const plugin of plugins) {\n // 检查是否跳过此插件\n if (context.request.skipPlugins?.includes(plugin.name)) {\n continue;\n }\n\n try {\n let result: RequestOptions | Response | boolean | void = undefined;\n\n switch (stage) {\n case PluginLifecycleStage.INIT:\n result = await plugin.onInit?.(context);\n break;\n case PluginLifecycleStage.BEFORE_REQUEST:\n result = await plugin.onBeforeRequest?.(context);\n if (result && typeof result === 'object' && 'url' in result) {\n context.request = result as RequestOptions;\n // 如果返回了修改后的请求配置,立即返回\n return result as RequestOptions;\n }\n break;\n case PluginLifecycleStage.AFTER_REQUEST:\n result = await plugin.onAfterRequest?.(context);\n if (result && typeof result === 'object' && 'data' in result) {\n context.response = result as Response;\n // 如果返回了修改后的响应,立即返回\n return result as Response;\n }\n break;\n case PluginLifecycleStage.ON_ERROR:\n result = await plugin.onError?.(context);\n if (result === true) {\n return true; // 错误已处理,停止传播\n }\n break;\n case PluginLifecycleStage.FINALLY:\n await plugin.onFinally?.(context);\n break;\n }\n } catch (error) {\n logger.error(`插件 ${plugin.name} 的 ${stage} 钩子执行失败:`, error);\n // 继续执行其他插件\n }\n }\n\n return undefined;\n }\n\n /**\n * 清除所有插件\n */\n clear(): void {\n this.plugins.clear();\n this.pluginOrder = [];\n }\n}\n"],"names":["PluginLifecycleStage","PluginManager","PluginPriority","register","plugin","plugins","has","name","logger","warn","set","updatePluginOrder","unregister","delete","get","getAll","pluginOrder","map","filter","Boolean","Array","from","values","dependencyGraph","Map","priorities","dependencies","priority","visited","Set","visiting","result","visit","pluginName","add","deps","dep","push","sortedPlugins","sort","a","b","priorityA","priorityB","executeHook","stage","context","request","skipPlugins","includes","undefined","onInit","onBeforeRequest","onAfterRequest","response","onError","onFinally","error","clear"],"mappings":"AAAA;;;;CAIC;;;;;;;;;;;QAQWA;eAAAA;;QA+ICC;eAAAA;;QAjHDC;eAAAA;;;wBAnCW;;;;;;;;;;;;;;AAKhB,IAAA,AAAKF,8CAAAA;IACV;;GAEC;IAGD;;GAEC;IAGD;;GAEC;IAGD;;GAEC;IAGD;;GAEC;WAvBSA;;AA8BL,IAAA,AAAKE,wCAAAA;IACV;;GAEC;IAGD;;GAEC;IAGD;;GAEC;IAGD;;GAEC;IAGD;;GAEC;WAvBSA;;AAiHL,IAAA,AAAMD,gBAAN,MAAMA;IAIX;;;;GAIC,GACDE,SAASC,MAAqB,EAAQ;QACpC,IAAI,IAAI,CAACC,OAAO,CAACC,GAAG,CAACF,OAAOG,IAAI,GAAG;YACjCC,cAAM,CAACC,IAAI,CAAC,CAAC,GAAG,EAAEL,OAAOG,IAAI,CAAC,SAAS,CAAC;QAC1C;QAEA,IAAI,CAACF,OAAO,CAACK,GAAG,CAACN,OAAOG,IAAI,EAAEH;QAC9B,IAAI,CAACO,iBAAiB;IACxB;IAEA;;;;GAIC,GACDC,WAAWL,IAAY,EAAQ;QAC7B,IAAI,CAACF,OAAO,CAACQ,MAAM,CAACN;QACpB,IAAI,CAACI,iBAAiB;IACxB;IAEA;;;;;GAKC,GACDG,IAAIP,IAAY,EAA6B;QAC3C,OAAO,IAAI,CAACF,OAAO,CAACS,GAAG,CAACP;IAC1B;IAEA;;;;GAIC,GACDQ,SAA0B;QACxB,OAAO,IAAI,CAACC,WAAW,CAACC,GAAG,CAAC,CAACV,OAAS,IAAI,CAACF,OAAO,CAACS,GAAG,CAACP,OAAQW,MAAM,CAACC;IACxE;IAEA;;GAEC,GACD,AAAQR,oBAA0B;QAChC,MAAMN,UAAUe,MAAMC,IAAI,CAAC,IAAI,CAAChB,OAAO,CAACiB,MAAM;QAE9C,QAAQ;QACR,MAAMC,kBAAkB,IAAIC;QAC5B,MAAMC,aAAa,IAAID;QAEvB,KAAK,MAAMpB,UAAUC,QAAS;YAC5BkB,gBAAgBb,GAAG,CAACN,OAAOG,IAAI,EAAEH,OAAOsB,YAAY,IAAI,EAAE;YAC1DD,WAAWf,GAAG,CAACN,OAAOG,IAAI,EAAEH,OAAOuB,QAAQ;QAC7C;QAEA,OAAO;QACP,MAAMC,UAAU,IAAIC;QACpB,MAAMC,WAAW,IAAID;QACrB,MAAME,SAAmB,EAAE;QAE3B,MAAMC,QAAQ,CAACC;YACb,IAAIH,SAASxB,GAAG,CAAC2B,aAAa;gBAC5BzB,cAAM,CAACC,IAAI,CAAC,CAAC,cAAc,EAAEwB,YAAY;gBACzC;YACF;YACA,IAAIL,QAAQtB,GAAG,CAAC2B,aAAa;gBAC3B;YACF;YAEAH,SAASI,GAAG,CAACD;YACb,MAAME,OAAOZ,gBAAgBT,GAAG,CAACmB,eAAe,EAAE;YAClD,KAAK,MAAMG,OAAOD,KAAM;gBACtBH,MAAMI;YACR;YACAN,SAASjB,MAAM,CAACoB;YAChBL,QAAQM,GAAG,CAACD;YACZF,OAAOM,IAAI,CAACJ;QACd;QAEA,cAAc;QACd,MAAMK,gBAAgBjC,QAAQkC,IAAI,CAAC,CAACC,GAAGC;YACrC,MAAMC,YAAYjB,WAAWX,GAAG,CAAC0B,EAAEjC,IAAI;YACvC,MAAMoC,YAAYlB,WAAWX,GAAG,CAAC2B,EAAElC,IAAI;YACvC,OAAOmC,YAAYC;QACrB;QAEA,KAAK,MAAMvC,UAAUkC,cAAe;YAClCN,MAAM5B,OAAOG,IAAI;QACnB;QAEA,IAAI,CAACS,WAAW,GAAGe;IACrB;IAEA;;;;;;GAMC,GACD,MAAMa,YACJC,KAA2B,EAC3BC,OAAiB,EACoC;QACrD,MAAMzC,UAAU,IAAI,CAACU,MAAM;QAE3B,KAAK,MAAMX,UAAUC,QAAS;YAC5B,YAAY;YACZ,IAAIyC,QAAQC,OAAO,CAACC,WAAW,EAAEC,SAAS7C,OAAOG,IAAI,GAAG;gBACtD;YACF;YAEA,IAAI;gBACF,IAAIwB,SAAqDmB;gBAEzD,OAAQL;oBACN;wBACEd,SAAS,MAAM3B,OAAO+C,MAAM,GAAGL;wBAC/B;oBACF;wBACEf,SAAS,MAAM3B,OAAOgD,eAAe,GAAGN;wBACxC,IAAIf,UAAU,OAAOA,WAAW,YAAY,SAASA,QAAQ;4BAC3De,QAAQC,OAAO,GAAGhB;4BAClB,qBAAqB;4BACrB,OAAOA;wBACT;wBACA;oBACF;wBACEA,SAAS,MAAM3B,OAAOiD,cAAc,GAAGP;wBACvC,IAAIf,UAAU,OAAOA,WAAW,YAAY,UAAUA,QAAQ;4BAC5De,QAAQQ,QAAQ,GAAGvB;4BACnB,mBAAmB;4BACnB,OAAOA;wBACT;wBACA;oBACF;wBACEA,SAAS,MAAM3B,OAAOmD,OAAO,GAAGT;wBAChC,IAAIf,WAAW,MAAM;4BACnB,OAAO,MAAM,aAAa;wBAC5B;wBACA;oBACF;wBACE,MAAM3B,OAAOoD,SAAS,GAAGV;wBACzB;gBACJ;YACF,EAAE,OAAOW,OAAO;gBACdjD,cAAM,CAACiD,KAAK,CAAC,CAAC,GAAG,EAAErD,OAAOG,IAAI,CAAC,GAAG,EAAEsC,MAAM,QAAQ,CAAC,EAAEY;YACrD,WAAW;YACb;QACF;QAEA,OAAOP;IACT;IAEA;;GAEC,GACDQ,QAAc;QACZ,IAAI,CAACrD,OAAO,CAACqD,KAAK;QAClB,IAAI,CAAC1C,WAAW,GAAG,EAAE;IACvB;;QAtKA,uBAAQX,WAAsC,IAAImB;QAClD,uBAAQR,eAAwB,EAAE;;AAsKpC"}
|
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 缓存插件
|
|
3
|
-
*
|
|
4
|
-
* 提供请求缓存能力,支持内存、localStorage、sessionStorage 存储
|
|
5
|
-
*/ "use strict";
|
|
6
|
-
Object.defineProperty(exports, "__esModule", {
|
|
7
|
-
value: true
|
|
8
|
-
});
|
|
9
|
-
Object.defineProperty(exports, "createCachePlugin", {
|
|
10
|
-
enumerable: true,
|
|
11
|
-
get: function() {
|
|
12
|
-
return createCachePlugin;
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
const _RequestPlugin = require("./RequestPlugin");
|
|
16
|
-
const _logger = require("../../utils/logger");
|
|
17
|
-
function _define_property(obj, key, value) {
|
|
18
|
-
if (key in obj) {
|
|
19
|
-
Object.defineProperty(obj, key, {
|
|
20
|
-
value: value,
|
|
21
|
-
enumerable: true,
|
|
22
|
-
configurable: true,
|
|
23
|
-
writable: true
|
|
24
|
-
});
|
|
25
|
-
} else {
|
|
26
|
-
obj[key] = value;
|
|
27
|
-
}
|
|
28
|
-
return obj;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* 内存缓存存储实现
|
|
32
|
-
*/ let MemoryCacheStorage = class MemoryCacheStorage {
|
|
33
|
-
async get(key) {
|
|
34
|
-
const item = this.cache.get(key);
|
|
35
|
-
if (!item) {
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
if (Date.now() > item.expires) {
|
|
39
|
-
this.cache.delete(key);
|
|
40
|
-
return null;
|
|
41
|
-
}
|
|
42
|
-
return item.value;
|
|
43
|
-
}
|
|
44
|
-
async set(key, value, ttl) {
|
|
45
|
-
const expires = Date.now() + ttl;
|
|
46
|
-
this.cache.set(key, {
|
|
47
|
-
value,
|
|
48
|
-
expires
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
async delete(key) {
|
|
52
|
-
this.cache.delete(key);
|
|
53
|
-
}
|
|
54
|
-
async clear() {
|
|
55
|
-
this.cache.clear();
|
|
56
|
-
}
|
|
57
|
-
constructor(){
|
|
58
|
-
_define_property(this, "cache", new Map());
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
/**
|
|
62
|
-
* LocalStorage 缓存存储实现
|
|
63
|
-
*/ let LocalStorageCacheStorage = class LocalStorageCacheStorage {
|
|
64
|
-
async get(key) {
|
|
65
|
-
if (typeof localStorage === 'undefined') {
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
try {
|
|
69
|
-
const item = localStorage.getItem(this.prefix + key);
|
|
70
|
-
if (!item) {
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
const { value, expires } = JSON.parse(item);
|
|
74
|
-
if (Date.now() > expires) {
|
|
75
|
-
localStorage.removeItem(this.prefix + key);
|
|
76
|
-
return null;
|
|
77
|
-
}
|
|
78
|
-
return value;
|
|
79
|
-
} catch {
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
async set(key, value, ttl) {
|
|
84
|
-
if (typeof localStorage === 'undefined') {
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
try {
|
|
88
|
-
const expires = Date.now() + ttl;
|
|
89
|
-
localStorage.setItem(this.prefix + key, JSON.stringify({
|
|
90
|
-
value,
|
|
91
|
-
expires
|
|
92
|
-
}));
|
|
93
|
-
} catch (error) {
|
|
94
|
-
// 存储失败(可能是存储空间不足)
|
|
95
|
-
_logger.logger.warn('缓存存储失败:', error);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
async delete(key) {
|
|
99
|
-
if (typeof localStorage === 'undefined') {
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
localStorage.removeItem(this.prefix + key);
|
|
103
|
-
}
|
|
104
|
-
async clear() {
|
|
105
|
-
if (typeof localStorage === 'undefined') {
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
const keys = Object.keys(localStorage);
|
|
109
|
-
for (const key of keys){
|
|
110
|
-
if (key.startsWith(this.prefix)) {
|
|
111
|
-
localStorage.removeItem(key);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
constructor(){
|
|
116
|
-
_define_property(this, "prefix", 'request_cache_');
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
/**
|
|
120
|
-
* SessionStorage 缓存存储实现
|
|
121
|
-
*/ let SessionStorageCacheStorage = class SessionStorageCacheStorage {
|
|
122
|
-
async get(key) {
|
|
123
|
-
if (typeof sessionStorage === 'undefined') {
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
try {
|
|
127
|
-
const item = sessionStorage.getItem(this.prefix + key);
|
|
128
|
-
if (!item) {
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
const { value, expires } = JSON.parse(item);
|
|
132
|
-
if (Date.now() > expires) {
|
|
133
|
-
sessionStorage.removeItem(this.prefix + key);
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
return value;
|
|
137
|
-
} catch {
|
|
138
|
-
return null;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
async set(key, value, ttl) {
|
|
142
|
-
if (typeof sessionStorage === 'undefined') {
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
try {
|
|
146
|
-
const expires = Date.now() + ttl;
|
|
147
|
-
sessionStorage.setItem(this.prefix + key, JSON.stringify({
|
|
148
|
-
value,
|
|
149
|
-
expires
|
|
150
|
-
}));
|
|
151
|
-
} catch (error) {
|
|
152
|
-
_logger.logger.warn('缓存存储失败:', error);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
async delete(key) {
|
|
156
|
-
if (typeof sessionStorage === 'undefined') {
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
sessionStorage.removeItem(this.prefix + key);
|
|
160
|
-
}
|
|
161
|
-
async clear() {
|
|
162
|
-
if (typeof sessionStorage === 'undefined') {
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
const keys = Object.keys(sessionStorage);
|
|
166
|
-
for (const key of keys){
|
|
167
|
-
if (key.startsWith(this.prefix)) {
|
|
168
|
-
sessionStorage.removeItem(key);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
constructor(){
|
|
173
|
-
_define_property(this, "prefix", 'request_cache_');
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
/**
|
|
177
|
-
* 获取缓存存储实例
|
|
178
|
-
*/ function getCacheStorage(storage = 'memory') {
|
|
179
|
-
switch(storage){
|
|
180
|
-
case 'localStorage':
|
|
181
|
-
return new LocalStorageCacheStorage();
|
|
182
|
-
case 'sessionStorage':
|
|
183
|
-
return new SessionStorageCacheStorage();
|
|
184
|
-
case 'memory':
|
|
185
|
-
default:
|
|
186
|
-
return new MemoryCacheStorage();
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* 生成缓存键
|
|
191
|
-
*/ function generateCacheKey(request) {
|
|
192
|
-
const { url, method = 'GET', query, body } = request;
|
|
193
|
-
const cacheKey = request.cache?.key || `${method}:${url}`;
|
|
194
|
-
// 包含查询参数和请求体(如果存在)
|
|
195
|
-
const keyParts = [
|
|
196
|
-
cacheKey
|
|
197
|
-
];
|
|
198
|
-
if (query) {
|
|
199
|
-
keyParts.push(JSON.stringify(query));
|
|
200
|
-
}
|
|
201
|
-
if (body && typeof body === 'object') {
|
|
202
|
-
keyParts.push(JSON.stringify(body));
|
|
203
|
-
}
|
|
204
|
-
return keyParts.join('|');
|
|
205
|
-
}
|
|
206
|
-
function createCachePlugin(config = {}) {
|
|
207
|
-
const { defaultTTL = 5 * 60 * 1000, defaultStorage = 'memory', enabled = true } = config;
|
|
208
|
-
return {
|
|
209
|
-
name: 'cache',
|
|
210
|
-
priority: _RequestPlugin.PluginPriority.LOWEST,
|
|
211
|
-
async onBeforeRequest (context) {
|
|
212
|
-
if (!enabled) {
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
const cacheConfig = context.request.cache;
|
|
216
|
-
if (!cacheConfig) {
|
|
217
|
-
// 如果没有配置缓存,跳过
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
// 如果强制刷新,跳过缓存
|
|
221
|
-
if (cacheConfig.forceRefresh) {
|
|
222
|
-
return;
|
|
223
|
-
}
|
|
224
|
-
// 只缓存 GET 请求
|
|
225
|
-
if (context.request.method && context.request.method !== 'GET') {
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
const storage = getCacheStorage(cacheConfig.storage || defaultStorage);
|
|
229
|
-
const cacheKey = generateCacheKey(context.request);
|
|
230
|
-
// 尝试从缓存获取
|
|
231
|
-
const cachedResponse = await storage.get(cacheKey);
|
|
232
|
-
if (cachedResponse) {
|
|
233
|
-
// 缓存命中,标记并设置缓存的响应
|
|
234
|
-
context.meta.hit = true;
|
|
235
|
-
context.response = cachedResponse;
|
|
236
|
-
// 标记请求已取消,避免实际发送请求
|
|
237
|
-
context.cancelled = true;
|
|
238
|
-
return;
|
|
239
|
-
}
|
|
240
|
-
// 缓存未命中
|
|
241
|
-
context.meta.hit = false;
|
|
242
|
-
},
|
|
243
|
-
async onAfterRequest (context) {
|
|
244
|
-
if (!enabled || !context.response) {
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
const cacheConfig = context.request.cache;
|
|
248
|
-
if (!cacheConfig) {
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
// 检查是否应该缓存
|
|
252
|
-
const shouldCache = cacheConfig.shouldCache ? cacheConfig.shouldCache(context.response) : context.response.status >= 200 && context.response.status < 300;
|
|
253
|
-
if (!shouldCache) {
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
// 只缓存 GET 请求
|
|
257
|
-
if (context.request.method && context.request.method !== 'GET') {
|
|
258
|
-
return;
|
|
259
|
-
}
|
|
260
|
-
const storage = getCacheStorage(cacheConfig.storage || defaultStorage);
|
|
261
|
-
const cacheKey = generateCacheKey(context.request);
|
|
262
|
-
const ttl = cacheConfig.ttl || defaultTTL;
|
|
263
|
-
// 存储到缓存
|
|
264
|
-
await storage.set(cacheKey, context.response, ttl);
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
//# sourceMappingURL=cache.js.map
|