@czf0613/http_client 0.0.2 → 0.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/dist/http_client.d.ts +4 -1
- package/dist/http_client.js +18 -4
- package/dist/timer.d.ts +2 -0
- package/dist/timer.js +4 -0
- package/package.json +1 -1
package/dist/http_client.d.ts
CHANGED
|
@@ -14,6 +14,7 @@ type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'HEAD';
|
|
|
14
14
|
* @param queryParams 查询参数,会被自动拼接到url中(会自动进行转义)
|
|
15
15
|
* @param customHeaders 自定义请求头,不需要写content-type这种会被自动处理的头
|
|
16
16
|
* @param body 请求体,适用于POST/PUT请求,传入字符串、数字会被处理成text/plain,传入对象会被处理成application/json,传入FormData会被处理成multipart/form-data,目前不建议直接发送二进制对象
|
|
17
|
+
* @param timeoutMs 超时时间,单位毫秒,默认5秒。这个超时的时间指的是发出请求,到接收到请求头的时间,不包含读取请求体的时间。如果需要控制读取请求体的超时,请自行用Promise.race实现。
|
|
17
18
|
* @returns 返回Fetch API的Response对象
|
|
18
19
|
*/
|
|
19
20
|
export declare function makeHttpRequest(url: string, method?: HttpMethod, queryParams?: Record<string, string | number> | null, customHeaders?: Record<string, string> | null, body?: any | null, timeoutMs?: number): Promise<Response>;
|
|
@@ -23,7 +24,9 @@ export declare function makeHttpRequest(url: string, method?: HttpMethod, queryP
|
|
|
23
24
|
* 目前这个接口只支持处理后端不停发送data: xxx\n\n这种格式的响应
|
|
24
25
|
* 默认不抛出异常,会在生成器的返回值里面指明成功还是失败
|
|
25
26
|
* @see makeHttpRequest 这里的参数说明
|
|
27
|
+
* @param connectTimeoutMs 连接超时时间,单位毫秒,默认30秒
|
|
28
|
+
* @param messageTimeoutMs 每条消息超时时间,单位毫秒,默认30秒
|
|
26
29
|
* @returns 返回一个异步生成器,每次迭代返回一个字符串(流式响应)
|
|
27
30
|
*/
|
|
28
|
-
export declare function makeSSERequest(url: string, method?: HttpMethod, queryParams?: Record<string, string | number> | null, customHeaders?: Record<string, string> | null, body?: any | null): AsyncGenerator<string, boolean, undefined>;
|
|
31
|
+
export declare function makeSSERequest(url: string, method?: HttpMethod, queryParams?: Record<string, string | number> | null, customHeaders?: Record<string, string> | null, body?: any | null, connectTimeoutMs?: number, messageTimeoutMs?: number): AsyncGenerator<string, boolean, undefined>;
|
|
29
32
|
export {};
|
package/dist/http_client.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { sleep, TIMEOUT_MARKER } from './timer';
|
|
1
2
|
/**
|
|
2
3
|
* 拼接URL和查询参数,会自动处理转义
|
|
3
4
|
* @param url 基础URL,不要带任何查询参数
|
|
@@ -29,6 +30,7 @@ const DEFAULT_TIMEOUT = 5000;
|
|
|
29
30
|
* @param queryParams 查询参数,会被自动拼接到url中(会自动进行转义)
|
|
30
31
|
* @param customHeaders 自定义请求头,不需要写content-type这种会被自动处理的头
|
|
31
32
|
* @param body 请求体,适用于POST/PUT请求,传入字符串、数字会被处理成text/plain,传入对象会被处理成application/json,传入FormData会被处理成multipart/form-data,目前不建议直接发送二进制对象
|
|
33
|
+
* @param timeoutMs 超时时间,单位毫秒,默认5秒。这个超时的时间指的是发出请求,到接收到请求头的时间,不包含读取请求体的时间。如果需要控制读取请求体的超时,请自行用Promise.race实现。
|
|
32
34
|
* @returns 返回Fetch API的Response对象
|
|
33
35
|
*/
|
|
34
36
|
export async function makeHttpRequest(url, method = 'GET', queryParams = null, customHeaders = null, body = null, timeoutMs = DEFAULT_TIMEOUT) {
|
|
@@ -44,7 +46,9 @@ export async function makeHttpRequest(url, method = 'GET', queryParams = null, c
|
|
|
44
46
|
// 无body时,不设置Content-Type
|
|
45
47
|
}
|
|
46
48
|
else if (body instanceof FormData) {
|
|
47
|
-
// 使用FormData时,浏览器会自动设置Content-Type和boundary
|
|
49
|
+
// 使用FormData时,浏览器会自动设置Content-Type和boundary,不要多手,有我也得给你删了
|
|
50
|
+
delete customHeaders['Content-Type'];
|
|
51
|
+
delete customHeaders['content-type'];
|
|
48
52
|
}
|
|
49
53
|
else if (typeof body === 'string') {
|
|
50
54
|
customHeaders['Content-Type'] = 'text/plain';
|
|
@@ -75,11 +79,13 @@ export async function makeHttpRequest(url, method = 'GET', queryParams = null, c
|
|
|
75
79
|
* 目前这个接口只支持处理后端不停发送data: xxx\n\n这种格式的响应
|
|
76
80
|
* 默认不抛出异常,会在生成器的返回值里面指明成功还是失败
|
|
77
81
|
* @see makeHttpRequest 这里的参数说明
|
|
82
|
+
* @param connectTimeoutMs 连接超时时间,单位毫秒,默认30秒
|
|
83
|
+
* @param messageTimeoutMs 每条消息超时时间,单位毫秒,默认30秒
|
|
78
84
|
* @returns 返回一个异步生成器,每次迭代返回一个字符串(流式响应)
|
|
79
85
|
*/
|
|
80
|
-
export async function* makeSSERequest(url, method = 'GET', queryParams = null, customHeaders = null, body = null) {
|
|
86
|
+
export async function* makeSSERequest(url, method = 'GET', queryParams = null, customHeaders = null, body = null, connectTimeoutMs = 30000, messageTimeoutMs = 30000) {
|
|
81
87
|
var _a;
|
|
82
|
-
const resp = await makeHttpRequest(url, method, queryParams, customHeaders, body,
|
|
88
|
+
const resp = await makeHttpRequest(url, method, queryParams, customHeaders, body, connectTimeoutMs);
|
|
83
89
|
if (!resp.ok) {
|
|
84
90
|
return false;
|
|
85
91
|
}
|
|
@@ -90,7 +96,15 @@ export async function* makeSSERequest(url, method = 'GET', queryParams = null, c
|
|
|
90
96
|
let buffer = new Uint8Array(0);
|
|
91
97
|
try {
|
|
92
98
|
while (true) {
|
|
93
|
-
const
|
|
99
|
+
const raceResult = await Promise.race([
|
|
100
|
+
sleep(messageTimeoutMs),
|
|
101
|
+
reader.read(),
|
|
102
|
+
]);
|
|
103
|
+
// 如果timeout先完成,说明超时了
|
|
104
|
+
if (raceResult === TIMEOUT_MARKER) {
|
|
105
|
+
throw new Error('SSE message timeout');
|
|
106
|
+
}
|
|
107
|
+
const { done, value } = raceResult;
|
|
94
108
|
if (done || !value) {
|
|
95
109
|
break;
|
|
96
110
|
}
|
package/dist/timer.d.ts
ADDED
package/dist/timer.js
ADDED