ziya-utils 1.1.2 → 1.1.4

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.
Files changed (48) hide show
  1. package/README.md +35 -13
  2. package/README.zh-CN.md +42 -0
  3. package/dist/ziya-utils.js +1 -1
  4. package/es/async/index.d.ts +20 -0
  5. package/es/count/index.d.ts +25 -0
  6. package/es/count/index.js +30 -0
  7. package/es/date/index.d.ts +10 -2
  8. package/es/date/index.js +66 -19
  9. package/es/document/index.d.ts +7 -0
  10. package/es/file/index.d.ts +1 -1
  11. package/es/file/index.js +50 -58
  12. package/es/form/index.d.ts +15 -0
  13. package/es/form/index.js +51 -0
  14. package/es/index.d.ts +3 -0
  15. package/es/index.js +5 -2
  16. package/es/loger/index.d.ts +7 -0
  17. package/es/node_modules/tslib/tslib.es6.js +1 -43
  18. package/es/regexp/index.d.ts +1 -1
  19. package/es/regexp/index.js +20 -30
  20. package/es/security/index.d.ts +11 -3
  21. package/es/security/index.js +41 -4
  22. package/es/time/index.d.ts +1 -1
  23. package/es/time/index.js +4 -4
  24. package/es/xhr/index.d.ts +74 -0
  25. package/es/xhr/index.js +172 -0
  26. package/lib/async/index.d.ts +20 -0
  27. package/lib/count/index.d.ts +25 -0
  28. package/lib/count/index.js +32 -0
  29. package/lib/date/index.d.ts +10 -2
  30. package/lib/date/index.js +66 -18
  31. package/lib/document/index.d.ts +7 -0
  32. package/lib/file/index.d.ts +1 -1
  33. package/lib/file/index.js +49 -57
  34. package/lib/form/index.d.ts +15 -0
  35. package/lib/form/index.js +53 -0
  36. package/lib/index.d.ts +3 -0
  37. package/lib/index.js +9 -0
  38. package/lib/loger/index.d.ts +7 -0
  39. package/lib/node_modules/tslib/tslib.es6.js +0 -44
  40. package/lib/regexp/index.d.ts +1 -1
  41. package/lib/regexp/index.js +20 -30
  42. package/lib/security/index.d.ts +11 -3
  43. package/lib/security/index.js +41 -3
  44. package/lib/time/index.d.ts +1 -1
  45. package/lib/time/index.js +4 -4
  46. package/lib/xhr/index.d.ts +74 -0
  47. package/lib/xhr/index.js +175 -0
  48. package/package.json +17 -3
@@ -0,0 +1,74 @@
1
+ /**
2
+ * @fileoverview XMLHttpRequest模块
3
+ * @created 2025-04-02
4
+ * @author glk
5
+ */
6
+ /**
7
+ * 拦截规则
8
+ */
9
+ interface InterceptRule {
10
+ url: string;
11
+ responseCallback: (data: any) => any | Promise<any>;
12
+ }
13
+ /**
14
+ * 拦截器配置
15
+ */
16
+ interface XHRInterceptorConfig {
17
+ enableLogging?: boolean;
18
+ }
19
+ /**
20
+ * XHR 拦截器类
21
+ */
22
+ declare class XHRInterceptor {
23
+ private originalXMLHttpRequest;
24
+ private interceptRules;
25
+ private isActive;
26
+ private enableLogging;
27
+ constructor(config?: XHRInterceptorConfig);
28
+ /**
29
+ * 启动拦截器
30
+ */
31
+ start(): void;
32
+ /**
33
+ * 停止拦截器并恢复原始XMLHttpRequest
34
+ */
35
+ stop(): void;
36
+ /**
37
+ * 设置拦截规则(会覆盖现有规则)
38
+ */
39
+ setRules(rules: InterceptRule[]): void;
40
+ /**
41
+ * 添加单个拦截规则
42
+ */
43
+ addRule(url: string, responseCallback: InterceptRule["responseCallback"]): void;
44
+ /**
45
+ * 移除指定URL的拦截规则
46
+ */
47
+ removeRule(url: string): boolean;
48
+ /**
49
+ * 清除所有拦截规则
50
+ */
51
+ clearRules(): void;
52
+ /**
53
+ * 获取当前拦截规则
54
+ */
55
+ getRules(): InterceptRule[];
56
+ /**
57
+ * 获取拦截器状态
58
+ */
59
+ getStatus(): {
60
+ isActive: boolean;
61
+ rulesCount: number;
62
+ };
63
+ /**
64
+ * 检查URL是否匹配拦截规则
65
+ */
66
+ private findMatchingRule;
67
+ /**
68
+ * 日志输出
69
+ */
70
+ private log;
71
+ }
72
+ declare const xhrInterceptor: XHRInterceptor;
73
+ export type { InterceptRule, XHRInterceptorConfig };
74
+ export { XHRInterceptor, xhrInterceptor };
@@ -0,0 +1,175 @@
1
+ 'use strict';
2
+
3
+ var tslib_es6 = require('../node_modules/tslib/tslib.es6.js');
4
+
5
+ /**
6
+ * @fileoverview XMLHttpRequest模块
7
+ * @created 2025-04-02
8
+ * @author glk
9
+ */
10
+ /**
11
+ * XHR 拦截器类
12
+ */
13
+ class XHRInterceptor {
14
+ constructor(config = {}) {
15
+ var _a;
16
+ this.interceptRules = [];
17
+ this.isActive = false;
18
+ this.originalXMLHttpRequest = window.XMLHttpRequest;
19
+ this.enableLogging = (_a = config.enableLogging) !== null && _a !== void 0 ? _a : true;
20
+ }
21
+ /**
22
+ * 启动拦截器
23
+ */
24
+ start() {
25
+ if (this.isActive) {
26
+ this.log("拦截器已经启动");
27
+ return;
28
+ }
29
+ const self = this;
30
+ // 重写 XMLHttpRequest
31
+ window.XMLHttpRequest = function () {
32
+ const xhr = new self.originalXMLHttpRequest();
33
+ let requestUrl = "";
34
+ // 保存原始的 open 方法
35
+ const originalOpen = xhr.open;
36
+ xhr.open = function (method, url, async = true, user, password) {
37
+ requestUrl = typeof url === "string" ? url : url.toString();
38
+ return originalOpen.call(this, method, url, async, user, password);
39
+ };
40
+ // 保存原始的 send 方法
41
+ const originalSend = xhr.send;
42
+ xhr.send = function (body) {
43
+ // 查找匹配的拦截规则
44
+ const matchingRule = self.findMatchingRule(requestUrl);
45
+ if (!matchingRule) {
46
+ // 没有匹配的规则,直接执行原始请求
47
+ return originalSend.call(this, body);
48
+ }
49
+ // 保存原始的 onreadystatechange
50
+ const originalOnReadyStateChange = xhr.onreadystatechange;
51
+ xhr.onreadystatechange = function (event) {
52
+ return tslib_es6.__awaiter(this, void 0, void 0, function* () {
53
+ if (xhr.readyState === 4 && xhr.status >= 200 && xhr.status < 300) {
54
+ try {
55
+ // 解析原始响应
56
+ const originalData = JSON.parse(xhr.responseText);
57
+ // 使用回调函数处理响应数据
58
+ const modifiedData = yield matchingRule.responseCallback(originalData);
59
+ // 重写响应属性
60
+ Object.defineProperty(xhr, "responseText", {
61
+ writable: true,
62
+ configurable: true,
63
+ value: JSON.stringify(modifiedData),
64
+ });
65
+ Object.defineProperty(xhr, "response", {
66
+ writable: true,
67
+ configurable: true,
68
+ value: JSON.stringify(modifiedData),
69
+ });
70
+ self.log(`已拦截并修改响应: ${requestUrl}`);
71
+ }
72
+ catch (error) {
73
+ self.log("拦截器处理失败:", error);
74
+ // 出错时保持原始响应
75
+ }
76
+ }
77
+ // 调用原始的 onreadystatechange
78
+ if (originalOnReadyStateChange) {
79
+ originalOnReadyStateChange.call(this, event);
80
+ }
81
+ });
82
+ };
83
+ return originalSend.call(this, body);
84
+ };
85
+ return xhr;
86
+ };
87
+ // 继承原始 XMLHttpRequest 的静态属性和原型
88
+ Object.setPrototypeOf(window.XMLHttpRequest, this.originalXMLHttpRequest);
89
+ window.XMLHttpRequest.prototype = this.originalXMLHttpRequest.prototype;
90
+ this.isActive = true;
91
+ this.log("XHR拦截器已启动");
92
+ }
93
+ /**
94
+ * 停止拦截器并恢复原始XMLHttpRequest
95
+ */
96
+ stop() {
97
+ if (!this.isActive) {
98
+ this.log("拦截器未启动");
99
+ return;
100
+ }
101
+ window.XMLHttpRequest = this.originalXMLHttpRequest;
102
+ this.isActive = false;
103
+ this.log("XHR拦截器已停止,已恢复原始XMLHttpRequest");
104
+ }
105
+ /**
106
+ * 设置拦截规则(会覆盖现有规则)
107
+ */
108
+ setRules(rules) {
109
+ this.interceptRules = [...rules];
110
+ this.log("已设置拦截规则:", rules.map((r) => r.url));
111
+ }
112
+ /**
113
+ * 添加单个拦截规则
114
+ */
115
+ addRule(url, responseCallback) {
116
+ this.interceptRules.push({ url, responseCallback });
117
+ this.log(`已添加拦截规则: ${url}`);
118
+ }
119
+ /**
120
+ * 移除指定URL的拦截规则
121
+ */
122
+ removeRule(url) {
123
+ const initialLength = this.interceptRules.length;
124
+ this.interceptRules = this.interceptRules.filter((rule) => rule.url !== url);
125
+ const removed = this.interceptRules.length < initialLength;
126
+ if (removed) {
127
+ this.log(`已移除拦截规则: ${url}`);
128
+ }
129
+ else {
130
+ this.log(`未找到拦截规则: ${url}`);
131
+ }
132
+ return removed;
133
+ }
134
+ /**
135
+ * 清除所有拦截规则
136
+ */
137
+ clearRules() {
138
+ this.interceptRules = [];
139
+ this.log("已清除所有拦截规则");
140
+ }
141
+ /**
142
+ * 获取当前拦截规则
143
+ */
144
+ getRules() {
145
+ return [...this.interceptRules];
146
+ }
147
+ /**
148
+ * 获取拦截器状态
149
+ */
150
+ getStatus() {
151
+ return {
152
+ isActive: this.isActive,
153
+ rulesCount: this.interceptRules.length,
154
+ };
155
+ }
156
+ /**
157
+ * 检查URL是否匹配拦截规则
158
+ */
159
+ findMatchingRule(url) {
160
+ return this.interceptRules.find((rule) => url.includes(rule.url));
161
+ }
162
+ /**
163
+ * 日志输出
164
+ */
165
+ log(message, ...args) {
166
+ if (this.enableLogging) {
167
+ console.log(`[XHRInterceptor] ${message}`, ...args);
168
+ }
169
+ }
170
+ }
171
+ // 创建单例实例
172
+ const xhrInterceptor = new XHRInterceptor({ enableLogging: true });
173
+
174
+ exports.XHRInterceptor = XHRInterceptor;
175
+ exports.xhrInterceptor = xhrInterceptor;
package/package.json CHANGED
@@ -1,18 +1,32 @@
1
1
  {
2
2
  "name": "ziya-utils",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "A javascript tool library.",
5
5
  "main": "./lib/index.js",
6
6
  "browser": "dist/ziya-utils.js",
7
7
  "module": "./es/index.js",
8
8
  "types": "./lib/index.d.ts",
9
9
  "unpkg": "dist/ziya-utils.js",
10
- "files": ["dist", "lib", "es", "package.json", "README.md"],
10
+ "files": [
11
+ "dist",
12
+ "lib",
13
+ "es",
14
+ "package.json",
15
+ "README.md",
16
+ "README.zh-CN.md"
17
+ ],
11
18
  "scripts": {
12
19
  "dev": "rollup -w -c --bundleConfigAsCjs",
20
+ "patch": "npm version patch --no-git-tag-version",
13
21
  "build": "rimraf dist lib es && rollup -c --bundleConfigAsCjs"
14
22
  },
15
- "keywords": ["utils", "ziya", "tools", "javascript", "typescript"],
23
+ "keywords": [
24
+ "utils",
25
+ "ziya",
26
+ "tools",
27
+ "javascript",
28
+ "typescript"
29
+ ],
16
30
  "publishConfig": {
17
31
  "registry": "https://registry.npmjs.org/"
18
32
  },