ajax-hooker 1.0.6 → 1.0.8

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/README.md CHANGED
@@ -1 +1,234 @@
1
- # ajaxInterceptor
1
+ # ajax-hooker
2
+
3
+ 一个轻量级的 AJAX 请求拦截器,支持拦截和修改 XMLHttpRequest 和 Fetch 请求。
4
+
5
+ ## 特性
6
+
7
+ - 🎯 同时支持 XMLHttpRequest 和 Fetch API
8
+ - 🔄 可拦截和修改请求参数(URL、Method、Headers、Body)
9
+ - 📦 可捕获响应数据
10
+ - 🌊 支持流式响应拦截(SSE、流式 JSON 等)
11
+ - 🪝 支持多个钩子函数链式执行
12
+ - 🔒 单例模式,确保全局唯一实例
13
+ - 📝 完整的 TypeScript 类型支持
14
+
15
+ ## 安装
16
+
17
+ ```bash
18
+ npm install ajax-hooker
19
+ ```
20
+
21
+ ## 快速开始
22
+
23
+ ```typescript
24
+ import AjaxInterceptor from 'ajax-hooker';
25
+
26
+ // 获取拦截器实例
27
+ const interceptor = AjaxInterceptor.getInstance();
28
+
29
+ // 注入拦截器
30
+ interceptor.inject();
31
+
32
+ // 添加钩子函数
33
+ interceptor.hook((request) => {
34
+ // 修改请求
35
+ request.headers.set('Authorization', 'Bearer token');
36
+
37
+ // 捕获响应
38
+ request.response = async (response) => {
39
+ console.log('响应状态:', response.status);
40
+ console.log('响应数据:', response.json);
41
+ };
42
+
43
+ return request;
44
+ });
45
+ ```
46
+
47
+ ## API
48
+
49
+ ### AjaxInterceptor.getInstance()
50
+
51
+ 获取拦截器单例实例。
52
+
53
+ ```typescript
54
+ const interceptor = AjaxInterceptor.getInstance();
55
+ ```
56
+
57
+ ### inject()
58
+
59
+ 注入拦截器,开始拦截请求。
60
+
61
+ ```typescript
62
+ interceptor.inject();
63
+ ```
64
+
65
+ ### uninject()
66
+
67
+ 移除拦截器,恢复原始的 XMLHttpRequest 和 Fetch。
68
+
69
+ ```typescript
70
+ interceptor.uninject();
71
+ ```
72
+
73
+ ### hook(fn, type?)
74
+
75
+ 添加钩子函数。
76
+
77
+ **参数:**
78
+ - `fn`: 钩子函数,接收请求对象并返回修改后的请求对象
79
+ - `type`: 可选,指定拦截类型 `'xhr'` 或 `'fetch'`,不指定则同时拦截两者
80
+
81
+ ```typescript
82
+ // 拦截所有请求
83
+ interceptor.hook((request) => {
84
+ console.log('请求:', request.url);
85
+ return request;
86
+ });
87
+
88
+ // 只拦截 XHR 请求
89
+ interceptor.hook((request) => {
90
+ console.log('XHR 请求:', request.url);
91
+ return request;
92
+ }, 'xhr');
93
+
94
+ // 只拦截 Fetch 请求
95
+ interceptor.hook((request) => {
96
+ console.log('Fetch 请求:', request.url);
97
+ return request;
98
+ }, 'fetch');
99
+ ```
100
+
101
+ ## 请求对象结构
102
+
103
+ 钩子函数接收的请求对象包含以下属性:
104
+
105
+ ```typescript
106
+ interface AjaxInterceptorRequest {
107
+ type: 'xhr' | 'fetch'; // 请求类型
108
+ method: string; // 请求方法
109
+ url: string; // 请求 URL
110
+ headers: Headers; // 请求头
111
+ data: any; // 请求体
112
+ response: (response: AjaxResponse) => void | Promise<void>; // 响应回调
113
+ onStreamChunk?: (chunk: StreamChunk) => string | void | Promise<string | void>; // 流式响应钩子
114
+ }
115
+ ```
116
+
117
+ ## 响应对象结构
118
+
119
+ ```typescript
120
+ interface AjaxResponse {
121
+ status: number; // 状态码
122
+ statusText: string; // 状态文本
123
+ headers: Headers; // 响应头
124
+ finalUrl: string; // 最终 URL
125
+
126
+ // XHR 响应
127
+ response?: any;
128
+
129
+ // Fetch 响应
130
+ ok?: boolean;
131
+ redirected?: boolean;
132
+ json?: any;
133
+ text?: string;
134
+ arrayBuffer?: ArrayBuffer;
135
+ blob?: Blob;
136
+ formData?: FormData;
137
+ }
138
+ ```
139
+
140
+ ## 使用示例
141
+
142
+ ### 修改请求 URL
143
+
144
+ ```typescript
145
+ interceptor.hook((request) => {
146
+ if (request.url.includes('/api/v1/')) {
147
+ request.url = request.url.replace('/api/v1/', '/api/v2/');
148
+ }
149
+ return request;
150
+ });
151
+ ```
152
+
153
+ ### 添加认证 Token
154
+
155
+ ```typescript
156
+ interceptor.hook((request) => {
157
+ request.headers.set('Authorization', `Bearer ${getToken()}`);
158
+ return request;
159
+ });
160
+ ```
161
+
162
+ ### 捕获响应数据
163
+
164
+ ```typescript
165
+ interceptor.hook((request) => {
166
+ request.response = async (response) => {
167
+ console.log('状态码:', response.status);
168
+ console.log('响应数据:', response.json || response.response);
169
+ };
170
+ return request;
171
+ });
172
+ ```
173
+
174
+ ### 拦截流式响应
175
+
176
+ ```typescript
177
+ interceptor.hook((request) => {
178
+ // 拦截流式响应的每个数据块
179
+ request.onStreamChunk = async (chunk) => {
180
+ console.log('收到数据块:', chunk.text);
181
+ console.log('数据块索引:', chunk.index);
182
+
183
+ // 可以修改数据块内容
184
+ return chunk.text.replace('old', 'new');
185
+ };
186
+
187
+ return request;
188
+ });
189
+ ```
190
+
191
+ ### 多个钩子链式执行
192
+
193
+ ```typescript
194
+ // 第一个钩子:添加 token
195
+ interceptor.hook((request) => {
196
+ request.headers.set('Authorization', 'Bearer token');
197
+ return request;
198
+ });
199
+
200
+ // 第二个钩子:添加时间戳
201
+ interceptor.hook((request) => {
202
+ request.headers.set('X-Timestamp', Date.now().toString());
203
+ return request;
204
+ });
205
+
206
+ // 第三个钩子:记录日志
207
+ interceptor.hook((request) => {
208
+ console.log(`${request.method} ${request.url}`);
209
+ return request;
210
+ });
211
+ ```
212
+
213
+ ## 开发
214
+
215
+ ```bash
216
+ # 安装依赖
217
+ npm install
218
+
219
+ # 开发模式
220
+ npm start
221
+
222
+ # 构建
223
+ npm run build
224
+
225
+ # 测试
226
+ npm test
227
+
228
+ # 测试覆盖率
229
+ npm run test:coverage
230
+ ```
231
+
232
+ ## License
233
+
234
+ MIT
@@ -3717,10 +3717,9 @@ class XhrInterceptor {
3717
3717
  if (target.readyState === 4) {
3718
3718
  await self.responseProcessor(target);
3719
3719
  }
3720
- Reflect.apply(value, target, args);
3720
+ Reflect.apply(value, this, args);
3721
3721
  };
3722
- Reflect.set(target, prop, fn);
3723
- return true; // Proxy set trap 必须返回 true
3722
+ return Reflect.set(target, prop, fn);
3724
3723
  }
3725
3724
  return Reflect.set(target, prop, value);
3726
3725
  },
@@ -3713,10 +3713,9 @@ class XhrInterceptor {
3713
3713
  if (target.readyState === 4) {
3714
3714
  await self.responseProcessor(target);
3715
3715
  }
3716
- Reflect.apply(value, target, args);
3716
+ Reflect.apply(value, this, args);
3717
3717
  };
3718
- Reflect.set(target, prop, fn);
3719
- return true; // Proxy set trap 必须返回 true
3718
+ return Reflect.set(target, prop, fn);
3720
3719
  }
3721
3720
  return Reflect.set(target, prop, value);
3722
3721
  },