ajax-hooker 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -3
- package/README.zh-CN.md +32 -3
- package/dist/cjs/index.js +621 -1
- package/dist/esm/index.js +613 -1
- package/dist/iife/index.js +610 -1
- package/dist/types/interceptor.d.ts +8 -4
- package/dist/types/xhr.d.ts +1 -0
- package/package.json +22 -5
package/README.md
CHANGED
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
English | [中文](./README.zh-CN.md)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/ajax-hooker)
|
|
6
|
+
[](https://www.npmjs.com/package/ajax-hooker)
|
|
7
|
+
[](https://github.com/Arktomson/ajaxInterceptor/actions/workflows/ci.yml)
|
|
8
|
+
[](https://bundlephobia.com/package/ajax-hooker)
|
|
9
|
+
|
|
10
|
+
`ajax-hooker` is a browser-side AJAX interception library (XMLHttpRequest interceptor + Fetch interceptor). It deeply hooks native `XMLHttpRequest` and `fetch`, then normalizes both into one hook lifecycle so interception logic can be written once and reused across request types.
|
|
6
11
|
|
|
7
12
|
## Highlights
|
|
8
13
|
|
|
@@ -10,6 +15,22 @@ English | [中文](./README.zh-CN.md)
|
|
|
10
15
|
- Unified request lifecycle: flattens XHR/Fetch differences into two main phases: before request (request mutation) and after response (response handling).
|
|
11
16
|
- Stream-aware interception: supports chunk-level interception for streaming responses.
|
|
12
17
|
|
|
18
|
+
## Comparison
|
|
19
|
+
|
|
20
|
+
| Solution | Native XHR + Fetch Coverage | Unified Hook Lifecycle | Streaming Chunk Interception | Best For |
|
|
21
|
+
| --- | --- | --- | --- | --- |
|
|
22
|
+
| `ajax-hooker` | Yes | Yes | Yes | Browser-side request governance and cross-stack interception |
|
|
23
|
+
| Axios interceptors | No (Axios only) | Partial (Axios chain only) | No | Axios-centric projects |
|
|
24
|
+
| Handwritten monkey patch | Depends on implementation | Depends on implementation | Depends on implementation | One-off quick experiments |
|
|
25
|
+
| Service Worker interception | Fetch only | No | Limited/indirect | Offline/caching gateway scenarios |
|
|
26
|
+
|
|
27
|
+
## Typical Use Cases
|
|
28
|
+
|
|
29
|
+
- Browser extension request governance (header rewrite, auth injection, endpoint switching)
|
|
30
|
+
- API observability and debugging (capture normalized request/response in one place)
|
|
31
|
+
- Runtime compatibility layer for mixed XHR + Fetch codebases
|
|
32
|
+
- Streaming response transformation (SSE/NDJSON/JSONL chunk processing)
|
|
33
|
+
|
|
13
34
|
## Features
|
|
14
35
|
|
|
15
36
|
- Works with both `XMLHttpRequest` and `fetch`
|
|
@@ -26,6 +47,13 @@ English | [中文](./README.zh-CN.md)
|
|
|
26
47
|
npm install ajax-hooker
|
|
27
48
|
```
|
|
28
49
|
|
|
50
|
+
## Examples
|
|
51
|
+
|
|
52
|
+
- [Example Index](./examples/README.md)
|
|
53
|
+
- [Vanilla XHR + Fetch Demo](./examples/vanilla-xhr-fetch/index.html)
|
|
54
|
+
- [Streaming NDJSON Demo](./examples/streaming-ndjson/index.html)
|
|
55
|
+
- [Chrome Extension (MV3) Demo](./examples/chrome-extension/README.md)
|
|
56
|
+
|
|
29
57
|
## Quick Start
|
|
30
58
|
|
|
31
59
|
```typescript
|
|
@@ -107,9 +135,9 @@ interceptor.unhook(undefined, 'xhr');
|
|
|
107
135
|
interceptor.unhook();
|
|
108
136
|
```
|
|
109
137
|
|
|
110
|
-
###
|
|
138
|
+
### Compatibility Fields (1.x)
|
|
111
139
|
|
|
112
|
-
`interceptor.xhrInterceptor` and `interceptor.fetchInterceptor` are still available in 1.x for backward compatibility
|
|
140
|
+
`interceptor.xhrInterceptor` and `interceptor.fetchInterceptor` are still available in 1.x for backward compatibility. They are planned to become private in 2.x.
|
|
113
141
|
|
|
114
142
|
Prefer the public APIs:
|
|
115
143
|
|
|
@@ -437,6 +465,7 @@ interceptor.hook((request) => {
|
|
|
437
465
|
- UMD: not emitted by default (can be added later for legacy loader scenarios)
|
|
438
466
|
|
|
439
467
|
> Type declarations are generated once into `dist/types` and shared by both ESM and CJS consumers.
|
|
468
|
+
> IIFE global variable: `window.AjaxHooker`.
|
|
440
469
|
|
|
441
470
|
```bash
|
|
442
471
|
# Install dependencies
|
package/README.zh-CN.md
CHANGED
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
[English](./README.md) | 中文
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/ajax-hooker)
|
|
6
|
+
[](https://www.npmjs.com/package/ajax-hooker)
|
|
7
|
+
[](https://github.com/Arktomson/ajaxInterceptor/actions/workflows/ci.yml)
|
|
8
|
+
[](https://bundlephobia.com/package/ajax-hooker)
|
|
9
|
+
|
|
10
|
+
`ajax-hooker` 是一个浏览器端 AJAX 拦截库(XMLHttpRequest 拦截 + Fetch 拦截)。它会对原生 `XMLHttpRequest` 和 `fetch` 做深度劫持,并把两者抹平为统一的 Hook 生命周期,让拦截逻辑写一次即可复用。
|
|
6
11
|
|
|
7
12
|
## 项目亮点
|
|
8
13
|
|
|
@@ -10,6 +15,22 @@
|
|
|
10
15
|
- 统一请求生命周期: 抹平 XHR/Fetch 差异,聚合为两个核心阶段: 请求前(请求参数改写)与请求后(响应处理)。
|
|
11
16
|
- 流式能力完整: 支持对流式响应进行逐块拦截和改写。
|
|
12
17
|
|
|
18
|
+
## 方案对比
|
|
19
|
+
|
|
20
|
+
| 方案 | 原生 XHR + Fetch 覆盖 | 统一 Hook 生命周期 | 流式 Chunk 拦截 | 适用场景 |
|
|
21
|
+
| --- | --- | --- | --- | --- |
|
|
22
|
+
| `ajax-hooker` | 支持 | 支持 | 支持 | 浏览器侧请求治理和跨栈拦截 |
|
|
23
|
+
| Axios interceptors | 不支持(仅 Axios) | 部分支持(仅 Axios 链路) | 不支持 | Axios 主导项目 |
|
|
24
|
+
| 手写 monkey patch | 取决于实现 | 取决于实现 | 取决于实现 | 一次性快速实验 |
|
|
25
|
+
| Service Worker 拦截 | 仅 Fetch | 不支持 | 有限/间接支持 | 离线缓存网关场景 |
|
|
26
|
+
|
|
27
|
+
## 典型使用场景
|
|
28
|
+
|
|
29
|
+
- 浏览器扩展中的请求治理(请求头改写、鉴权注入、接口切换)
|
|
30
|
+
- API 调试与观测(在同一模型下采集请求/响应数据)
|
|
31
|
+
- 混合 XHR + Fetch 代码库的统一拦截层
|
|
32
|
+
- 流式响应处理(SSE/NDJSON/JSONL 的 chunk 级改写)
|
|
33
|
+
|
|
13
34
|
## 特性
|
|
14
35
|
|
|
15
36
|
- 同时支持 `XMLHttpRequest` 和 `fetch`
|
|
@@ -26,6 +47,13 @@
|
|
|
26
47
|
npm install ajax-hooker
|
|
27
48
|
```
|
|
28
49
|
|
|
50
|
+
## 示例
|
|
51
|
+
|
|
52
|
+
- [示例索引](./examples/README.md)
|
|
53
|
+
- [原生 XHR + Fetch 示例](./examples/vanilla-xhr-fetch/index.html)
|
|
54
|
+
- [流式 NDJSON 示例](./examples/streaming-ndjson/index.html)
|
|
55
|
+
- [Chrome Extension (MV3) 示例](./examples/chrome-extension/README.md)
|
|
56
|
+
|
|
29
57
|
## 快速开始
|
|
30
58
|
|
|
31
59
|
```typescript
|
|
@@ -107,9 +135,9 @@ interceptor.unhook(undefined, 'xhr');
|
|
|
107
135
|
interceptor.unhook();
|
|
108
136
|
```
|
|
109
137
|
|
|
110
|
-
###
|
|
138
|
+
### 兼容字段 (1.x)
|
|
111
139
|
|
|
112
|
-
`interceptor.xhrInterceptor` 和 `interceptor.fetchInterceptor` 在 1.x
|
|
140
|
+
`interceptor.xhrInterceptor` 和 `interceptor.fetchInterceptor` 在 1.x 中仍保留用于向后兼容,并计划在 2.x 改为私有。
|
|
113
141
|
|
|
114
142
|
建议使用公开 API:
|
|
115
143
|
|
|
@@ -437,6 +465,7 @@ interceptor.hook((request) => {
|
|
|
437
465
|
- UMD: 默认不产出(如需兼容老式加载器可后续追加)
|
|
438
466
|
|
|
439
467
|
> 类型声明只生成一份到 `dist/types`,由 ESM/CJS 共同复用。
|
|
468
|
+
> IIFE 全局变量: `window.AjaxHooker`。
|
|
440
469
|
|
|
441
470
|
```bash
|
|
442
471
|
# 安装依赖
|