stream-axios 1.0.1 → 1.1.0
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 +1 -1
- package/README.zh-CN.md +148 -149
- package/dist/index.cjs +2 -50
- package/dist/index.js +2 -50
- package/package.json +1 -1
package/README.md
CHANGED
package/README.zh-CN.md
CHANGED
|
@@ -1,149 +1,148 @@
|
|
|
1
|
-
# stream-axios
|
|
2
|
-
|
|
3
|
-
[English](./README.md) | [简体中文](./README.zh-CN.md)
|
|
4
|
-
|
|
5
|
-
这是一个基于 axios 的二次封装库,保留了 axios 的原始配置能力,并提供了开箱即用的流式请求(Streaming)接口,旨在提升开发效率。
|
|
6
|
-
|
|
7
|
-
## 特性
|
|
8
|
-
|
|
9
|
-
- 🚀 **完全兼容**:基于 axios,保留原有拦截器、配置项等所有特性。
|
|
10
|
-
- 🌊 **流式支持**:内置 `stream` 方法,轻松处理流式响应(如 LLM 打字机效果)。
|
|
11
|
-
- 🛠 **开箱即用**:提供默认实例,也支持创建自定义实例。
|
|
12
|
-
- 📦 **SSE 助手**:内置 SSE 解析工具,方便处理 Server-Sent Events。
|
|
13
|
-
|
|
14
|
-
## 安装
|
|
15
|
-
|
|
16
|
-
```bash
|
|
17
|
-
npm install stream-axios
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
## 使用指南
|
|
21
|
-
|
|
22
|
-
### 1. 基础请求 (同 axios)
|
|
23
|
-
|
|
24
|
-
```javascript
|
|
25
|
-
import request from
|
|
26
|
-
|
|
27
|
-
// GET 请求
|
|
28
|
-
request
|
|
29
|
-
.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
request
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
import
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
本项目基于 [Axios](https://github.com/axios/axios) 开发。
|
|
1
|
+
# stream-axios
|
|
2
|
+
|
|
3
|
+
[English](./README.md) | [简体中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
这是一个基于 axios 的二次封装库,保留了 axios 的原始配置能力,并提供了开箱即用的流式请求(Streaming)接口,旨在提升开发效率。
|
|
6
|
+
|
|
7
|
+
## 特性
|
|
8
|
+
|
|
9
|
+
- 🚀 **完全兼容**:基于 axios,保留原有拦截器、配置项等所有特性。
|
|
10
|
+
- 🌊 **流式支持**:内置 `stream` 方法,轻松处理流式响应(如 LLM 打字机效果)。
|
|
11
|
+
- 🛠 **开箱即用**:提供默认实例,也支持创建自定义实例。
|
|
12
|
+
- 📦 **SSE 助手**:内置 SSE 解析工具,方便处理 Server-Sent Events。
|
|
13
|
+
|
|
14
|
+
## 安装
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install stream-axios
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 使用指南
|
|
21
|
+
|
|
22
|
+
### 1. 基础请求 (同 axios)
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
import request from "stream-axios";
|
|
26
|
+
|
|
27
|
+
// GET 请求
|
|
28
|
+
request
|
|
29
|
+
.get("/user?ID=12345")
|
|
30
|
+
.then(function (response) {
|
|
31
|
+
console.log(response);
|
|
32
|
+
})
|
|
33
|
+
.catch(function (error) {
|
|
34
|
+
console.log(error);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// POST 请求
|
|
38
|
+
request
|
|
39
|
+
.post("/user", {
|
|
40
|
+
firstName: "Fred",
|
|
41
|
+
lastName: "Flintstone",
|
|
42
|
+
})
|
|
43
|
+
.then(function (response) {
|
|
44
|
+
console.log(response);
|
|
45
|
+
})
|
|
46
|
+
.catch(function (error) {
|
|
47
|
+
console.log(error);
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 2. 流式请求 (Streaming)
|
|
52
|
+
|
|
53
|
+
适用于接收大文件或 AI 对话流等场景。
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
import request from "stream-axios";
|
|
57
|
+
|
|
58
|
+
const cancel = await request.stream(
|
|
59
|
+
{
|
|
60
|
+
url: "/api/chat",
|
|
61
|
+
method: "POST",
|
|
62
|
+
data: { message: "Hello" },
|
|
63
|
+
},
|
|
64
|
+
(chunk) => {
|
|
65
|
+
// 收到数据片段
|
|
66
|
+
console.log("Received chunk:", chunk);
|
|
67
|
+
},
|
|
68
|
+
() => {
|
|
69
|
+
// 请求完成
|
|
70
|
+
console.log("Stream completed");
|
|
71
|
+
},
|
|
72
|
+
(error) => {
|
|
73
|
+
// 发生错误
|
|
74
|
+
console.error("Stream error:", error);
|
|
75
|
+
},
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
// 如果需要取消请求
|
|
79
|
+
// cancel();
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 3. 自定义实例
|
|
83
|
+
|
|
84
|
+
如果你需要独立的配置或拦截器:
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
import { createInstance } from "stream-axios";
|
|
88
|
+
|
|
89
|
+
const myRequest = createInstance({
|
|
90
|
+
baseURL: "https://api.mydomain.com",
|
|
91
|
+
timeout: 5000,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// 添加自定义拦截器
|
|
95
|
+
myRequest.interceptors.request.use((config) => {
|
|
96
|
+
config.headers["Authorization"] = "Bearer token";
|
|
97
|
+
return config;
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// 使用流式方法
|
|
101
|
+
myRequest.stream({ url: "/stream" }, (chunk) => console.log(chunk));
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### 4. SSE 解析助手
|
|
105
|
+
|
|
106
|
+
如果你处理的是 SSE (Server-Sent Events) 格式的数据:
|
|
107
|
+
|
|
108
|
+
```javascript
|
|
109
|
+
import request, { parseSSEChunk } from "stream-axios";
|
|
110
|
+
|
|
111
|
+
request.stream({ url: "/sse-endpoint", method: "GET" }, (chunk) => {
|
|
112
|
+
// 解析 SSE 数据
|
|
113
|
+
parseSSEChunk(chunk, (content) => {
|
|
114
|
+
console.log("SSE Message:", content);
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### 5. 使用现有的 Axios 实例
|
|
120
|
+
|
|
121
|
+
如果你项目中已经有了配置好的 axios 实例,你可以将 stream 方法挂载到该实例上:
|
|
122
|
+
|
|
123
|
+
```javascript
|
|
124
|
+
import axios from "axios";
|
|
125
|
+
import { attachStream } from "stream-axios";
|
|
126
|
+
|
|
127
|
+
// 你现有的 axios 实例
|
|
128
|
+
const myAxios = axios.create({
|
|
129
|
+
baseURL: "https://api.myproject.com",
|
|
130
|
+
headers: { "X-Custom-Header": "foobar" },
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// 挂载 stream 方法
|
|
134
|
+
attachStream(myAxios);
|
|
135
|
+
|
|
136
|
+
// 现在你可以在实例上使用 .stream() 方法了
|
|
137
|
+
myAxios.stream({ url: "/chat" }, (chunk) => {
|
|
138
|
+
console.log(chunk);
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## License
|
|
143
|
+
|
|
144
|
+
MIT
|
|
145
|
+
|
|
146
|
+
## 致谢
|
|
147
|
+
|
|
148
|
+
本项目基于 [Axios](https://github.com/axios/axios) 开发。
|
package/dist/index.cjs
CHANGED
|
@@ -5,57 +5,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var axios = require('axios');
|
|
6
6
|
|
|
7
7
|
const defaultConfig = {
|
|
8
|
-
timeout:
|
|
8
|
+
timeout: 1000 * 15,
|
|
9
9
|
headers: {
|
|
10
|
-
"Content-Type": "application/json",
|
|
10
|
+
"Content-Type": "application/json;charset=utf-8",
|
|
11
11
|
},
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
/**
|
|
15
|
-
* Setup request interceptor
|
|
16
|
-
* @param {import('axios').AxiosInstance} instance
|
|
17
|
-
*/
|
|
18
|
-
const setupRequestInterceptor = (instance) => {
|
|
19
|
-
instance.interceptors.request.use(
|
|
20
|
-
(config) => {
|
|
21
|
-
// Add custom headers if needed
|
|
22
|
-
// config.headers["test"] = "testHEADER";
|
|
23
|
-
return config;
|
|
24
|
-
},
|
|
25
|
-
(error) => {
|
|
26
|
-
console.error("Request error:", error);
|
|
27
|
-
return Promise.reject(error);
|
|
28
|
-
},
|
|
29
|
-
);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Setup response interceptor
|
|
34
|
-
* @param {import('axios').AxiosInstance} instance
|
|
35
|
-
*/
|
|
36
|
-
const setupResponseInterceptor = (instance) => {
|
|
37
|
-
instance.interceptors.response.use(
|
|
38
|
-
(response) => {
|
|
39
|
-
// If stream request, return response directly
|
|
40
|
-
if (response.config.isStream) {
|
|
41
|
-
return response;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Normal response handling
|
|
45
|
-
return response.data;
|
|
46
|
-
},
|
|
47
|
-
(error) => {
|
|
48
|
-
console.error("Response error:", error);
|
|
49
|
-
return Promise.reject(error);
|
|
50
|
-
},
|
|
51
|
-
);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const setupInterceptors = (instance) => {
|
|
55
|
-
setupRequestInterceptor(instance);
|
|
56
|
-
setupResponseInterceptor(instance);
|
|
57
|
-
};
|
|
58
|
-
|
|
59
14
|
/**
|
|
60
15
|
* Parse SSE data chunk
|
|
61
16
|
* @param {string} sseText
|
|
@@ -161,9 +116,6 @@ const createInstance = (config = {}) => {
|
|
|
161
116
|
const finalConfig = { ...defaultConfig, ...config };
|
|
162
117
|
const instance = axios.create(finalConfig);
|
|
163
118
|
|
|
164
|
-
// Setup interceptors
|
|
165
|
-
setupInterceptors(instance);
|
|
166
|
-
|
|
167
119
|
// Attach stream method
|
|
168
120
|
instance.stream = createStreamRequest(instance);
|
|
169
121
|
|
package/dist/index.js
CHANGED
|
@@ -1,57 +1,12 @@
|
|
|
1
1
|
import axios from 'axios';
|
|
2
2
|
|
|
3
3
|
const defaultConfig = {
|
|
4
|
-
timeout:
|
|
4
|
+
timeout: 1000 * 15,
|
|
5
5
|
headers: {
|
|
6
|
-
"Content-Type": "application/json",
|
|
6
|
+
"Content-Type": "application/json;charset=utf-8",
|
|
7
7
|
},
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
-
/**
|
|
11
|
-
* Setup request interceptor
|
|
12
|
-
* @param {import('axios').AxiosInstance} instance
|
|
13
|
-
*/
|
|
14
|
-
const setupRequestInterceptor = (instance) => {
|
|
15
|
-
instance.interceptors.request.use(
|
|
16
|
-
(config) => {
|
|
17
|
-
// Add custom headers if needed
|
|
18
|
-
// config.headers["test"] = "testHEADER";
|
|
19
|
-
return config;
|
|
20
|
-
},
|
|
21
|
-
(error) => {
|
|
22
|
-
console.error("Request error:", error);
|
|
23
|
-
return Promise.reject(error);
|
|
24
|
-
},
|
|
25
|
-
);
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Setup response interceptor
|
|
30
|
-
* @param {import('axios').AxiosInstance} instance
|
|
31
|
-
*/
|
|
32
|
-
const setupResponseInterceptor = (instance) => {
|
|
33
|
-
instance.interceptors.response.use(
|
|
34
|
-
(response) => {
|
|
35
|
-
// If stream request, return response directly
|
|
36
|
-
if (response.config.isStream) {
|
|
37
|
-
return response;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// Normal response handling
|
|
41
|
-
return response.data;
|
|
42
|
-
},
|
|
43
|
-
(error) => {
|
|
44
|
-
console.error("Response error:", error);
|
|
45
|
-
return Promise.reject(error);
|
|
46
|
-
},
|
|
47
|
-
);
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
const setupInterceptors = (instance) => {
|
|
51
|
-
setupRequestInterceptor(instance);
|
|
52
|
-
setupResponseInterceptor(instance);
|
|
53
|
-
};
|
|
54
|
-
|
|
55
10
|
/**
|
|
56
11
|
* Parse SSE data chunk
|
|
57
12
|
* @param {string} sseText
|
|
@@ -157,9 +112,6 @@ const createInstance = (config = {}) => {
|
|
|
157
112
|
const finalConfig = { ...defaultConfig, ...config };
|
|
158
113
|
const instance = axios.create(finalConfig);
|
|
159
114
|
|
|
160
|
-
// Setup interceptors
|
|
161
|
-
setupInterceptors(instance);
|
|
162
|
-
|
|
163
115
|
// Attach stream method
|
|
164
116
|
instance.stream = createStreamRequest(instance);
|
|
165
117
|
|