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 CHANGED
@@ -55,7 +55,7 @@ Suitable for scenarios like receiving large files or AI conversation streams.
55
55
  ```javascript
56
56
  import request from "stream-axios";
57
57
 
58
- const cancel = request.stream(
58
+ const cancel = await request.stream(
59
59
  {
60
60
  url: "/api/chat",
61
61
  method: "POST",
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 'stream-axios';
26
-
27
- // GET 请求
28
- request.get('/user?ID=12345')
29
- .then(function (response) {
30
- console.log(response);
31
- })
32
- .catch(function (error) {
33
- console.log(error);
34
- });
35
-
36
- // POST 请求
37
- request.post('/user', {
38
- firstName: 'Fred',
39
- lastName: 'Flintstone'
40
- })
41
- .then(function (response) {
42
- console.log(response);
43
- })
44
- .catch(function (error) {
45
- console.log(error);
46
- });
47
- ```
48
-
49
- ### 2. 流式请求 (Streaming)
50
-
51
- 适用于接收大文件或 AI 对话流等场景。
52
-
53
- ```javascript
54
- import request from 'stream-axios';
55
-
56
- const cancel = request.stream(
57
- {
58
- url: '/api/chat',
59
- method: 'POST',
60
- data: { message: 'Hello' }
61
- },
62
- (chunk) => {
63
- // 收到数据片段
64
- console.log('Received chunk:', chunk);
65
- },
66
- () => {
67
- // 请求完成
68
- console.log('Stream completed');
69
- },
70
- (error) => {
71
- // 发生错误
72
- console.error('Stream error:', error);
73
- }
74
- );
75
-
76
- // 如果需要取消请求
77
- // cancel();
78
- ```
79
-
80
- ### 3. 自定义实例
81
-
82
- 如果你需要独立的配置或拦截器:
83
-
84
- ```javascript
85
- import { createInstance } from 'stream-axios';
86
-
87
- const myRequest = createInstance({
88
- baseURL: 'https://api.mydomain.com',
89
- timeout: 5000
90
- });
91
-
92
- // 添加自定义拦截器
93
- myRequest.interceptors.request.use(config => {
94
- config.headers['Authorization'] = 'Bearer token';
95
- return config;
96
- });
97
-
98
- // 使用流式方法
99
- myRequest.stream({ url: '/stream' }, (chunk) => console.log(chunk));
100
- ```
101
-
102
- ### 4. SSE 解析助手
103
-
104
- 如果你处理的是 SSE (Server-Sent Events) 格式的数据:
105
-
106
- ```javascript
107
- import request, { parseSSEChunk } from 'stream-axios';
108
-
109
- request.stream(
110
- { url: '/sse-endpoint', method: 'GET' },
111
- (chunk) => {
112
- // 解析 SSE 数据
113
- parseSSEChunk(chunk, (content) => {
114
- console.log('SSE Message:', content);
115
- });
116
- }
117
- );
118
- ```
119
-
120
- ### 5. 使用现有的 Axios 实例
121
-
122
- 如果你项目中已经有了配置好的 axios 实例,你可以将 stream 方法挂载到该实例上:
123
-
124
- ```javascript
125
- import axios from 'axios';
126
- import { attachStream } from 'stream-axios';
127
-
128
- // 你现有的 axios 实例
129
- const myAxios = axios.create({
130
- baseURL: 'https://api.myproject.com',
131
- headers: { 'X-Custom-Header': 'foobar' }
132
- });
133
-
134
- // 挂载 stream 方法
135
- attachStream(myAxios);
136
-
137
- // 现在你可以在实例上使用 .stream() 方法了
138
- myAxios.stream({ url: '/chat' }, (chunk) => {
139
- console.log(chunk);
140
- });
141
- ```
142
-
143
- ## License
144
-
145
- MIT
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: 10000,
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: 10000,
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stream-axios",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "二次封装 axios,保留原始配置,提供流式接口,提升开发效率",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",