@tarojs/plugin-http 3.7.0-beta.1 → 3.7.0-beta.3
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/runtime.d.ts +4 -1
- package/dist/runtime.js +27 -2
- package/package.json +4 -4
- package/src/runtime/XMLHttpRequest.ts +27 -2
package/dist/runtime.d.ts
CHANGED
|
@@ -58,7 +58,10 @@ declare class XMLHttpRequest extends Events {
|
|
|
58
58
|
// 欺骗一些库让其认为是原生的xhr
|
|
59
59
|
static toString(): string;
|
|
60
60
|
toString(): string;
|
|
61
|
-
//
|
|
61
|
+
// 事件正常流转: loadstart => progress(可能多次) => load => loadend
|
|
62
|
+
// error 流转: loadstart => error => loadend
|
|
63
|
+
// abort 流转: loadstart => abort => loadend
|
|
64
|
+
// web在线测试: https://developer.mozilla.org/zh-CN/play
|
|
62
65
|
/** 当 request 被停止时触发,例如当程序调用 XMLHttpRequest.abort() 时 */
|
|
63
66
|
onabort: ((e: XMLHttpRequestEvent) => void) | null;
|
|
64
67
|
/** 当 request 遭遇错误时触发 */
|
package/dist/runtime.js
CHANGED
|
@@ -406,7 +406,10 @@ class XMLHttpRequest extends Events {
|
|
|
406
406
|
_XMLHttpRequest_timeout.set(this, void 0);
|
|
407
407
|
_XMLHttpRequest_withCredentials.set(this, void 0);
|
|
408
408
|
_XMLHttpRequest_requestTask.set(this, void 0);
|
|
409
|
-
//
|
|
409
|
+
// 事件正常流转: loadstart => progress(可能多次) => load => loadend
|
|
410
|
+
// error 流转: loadstart => error => loadend
|
|
411
|
+
// abort 流转: loadstart => abort => loadend
|
|
412
|
+
// web在线测试: https://developer.mozilla.org/zh-CN/play
|
|
410
413
|
/** 当 request 被停止时触发,例如当程序调用 XMLHttpRequest.abort() 时 */
|
|
411
414
|
this.onabort = null;
|
|
412
415
|
/** 当 request 遭遇错误时触发 */
|
|
@@ -643,8 +646,30 @@ _XMLHttpRequest_method = new WeakMap(), _XMLHttpRequest_url = new WeakMap(), _XM
|
|
|
643
646
|
isFunction(this.onload) && this.onload(loadEvent);
|
|
644
647
|
}
|
|
645
648
|
}, _XMLHttpRequest_requestFail = function _XMLHttpRequest_requestFail(err) {
|
|
649
|
+
// 微信小程序,无论接口返回200还是其他,响应无论是否有错误,都会进入 success 回调;只有类似超时这种请求错误才会进入 fail 回调
|
|
650
|
+
//
|
|
651
|
+
/**
|
|
652
|
+
* 阿里系小程序,接口返回非200状态码,会进入 fail 回调, 此时 err 对象结构如下(当错误码为 14 或 19 时,会多返回 status、data、headers。可通过这些字段获取服务端相关错误信息):
|
|
653
|
+
{
|
|
654
|
+
data: "{\"code\": 401,\"msg\":\"登录过期,请重新登录\"}"
|
|
655
|
+
error: 19
|
|
656
|
+
errorMessage: "http status error"
|
|
657
|
+
headers: {date: 'Mon, 14 Aug 2023 08:54:58 GMT', content-type: 'application/json;charset=UTF-8', content-length: '52', connection: 'close', access-control-allow-credentials: 'true', …}
|
|
658
|
+
originalData: "{\"code\": 401,\"msg\":\"登录过期,请重新登录\"}"
|
|
659
|
+
status: 401
|
|
660
|
+
}
|
|
661
|
+
*/
|
|
662
|
+
// 统一行为,能正常响应的,都算 success.
|
|
663
|
+
if (err.status) {
|
|
664
|
+
__classPrivateFieldGet(this, _XMLHttpRequest_instances, "m", _XMLHttpRequest_requestSuccess).call(this, {
|
|
665
|
+
data: err,
|
|
666
|
+
statusCode: err.status,
|
|
667
|
+
header: err.headers
|
|
668
|
+
});
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
646
671
|
__classPrivateFieldSet(this, _XMLHttpRequest_status, 0, "f");
|
|
647
|
-
__classPrivateFieldSet(this, _XMLHttpRequest_statusText, err.errMsg, "f");
|
|
672
|
+
__classPrivateFieldSet(this, _XMLHttpRequest_statusText, err.errMsg || err.errorMessage, "f");
|
|
648
673
|
const errorEvent = createXMLHttpRequestEvent('error', this, 0);
|
|
649
674
|
this.trigger('error', errorEvent);
|
|
650
675
|
isFunction(this.onerror) && this.onerror(errorEvent);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tarojs/plugin-http",
|
|
3
|
-
"version": "3.7.0-beta.
|
|
3
|
+
"version": "3.7.0-beta.3",
|
|
4
4
|
"description": "Taro 小程序端支持使用 web 请求 的插件",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -23,9 +23,9 @@
|
|
|
23
23
|
},
|
|
24
24
|
"homepage": "https://github.com/NervJS/taro#readme",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@tarojs/runtime": "3.7.0-beta.
|
|
27
|
-
"@tarojs/
|
|
28
|
-
"@tarojs/
|
|
26
|
+
"@tarojs/runtime": "3.7.0-beta.3",
|
|
27
|
+
"@tarojs/shared": "3.7.0-beta.3",
|
|
28
|
+
"@tarojs/service": "3.7.0-beta.3"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@rollup/plugin-json": "^6.0.0",
|
|
@@ -118,7 +118,10 @@ export class XMLHttpRequest extends Events {
|
|
|
118
118
|
#withCredentials: boolean
|
|
119
119
|
#requestTask: null | Taro.RequestTask<any>
|
|
120
120
|
|
|
121
|
-
//
|
|
121
|
+
// 事件正常流转: loadstart => progress(可能多次) => load => loadend
|
|
122
|
+
// error 流转: loadstart => error => loadend
|
|
123
|
+
// abort 流转: loadstart => abort => loadend
|
|
124
|
+
// web在线测试: https://developer.mozilla.org/zh-CN/play
|
|
122
125
|
|
|
123
126
|
/** 当 request 被停止时触发,例如当程序调用 XMLHttpRequest.abort() 时 */
|
|
124
127
|
onabort: ((e: XMLHttpRequestEvent) => void) | null = null
|
|
@@ -309,8 +312,30 @@ export class XMLHttpRequest extends Events {
|
|
|
309
312
|
* 请求失败
|
|
310
313
|
*/
|
|
311
314
|
#requestFail (err) {
|
|
315
|
+
// 微信小程序,无论接口返回200还是其他,响应无论是否有错误,都会进入 success 回调;只有类似超时这种请求错误才会进入 fail 回调
|
|
316
|
+
//
|
|
317
|
+
/**
|
|
318
|
+
* 阿里系小程序,接口返回非200状态码,会进入 fail 回调, 此时 err 对象结构如下(当错误码为 14 或 19 时,会多返回 status、data、headers。可通过这些字段获取服务端相关错误信息):
|
|
319
|
+
{
|
|
320
|
+
data: "{\"code\": 401,\"msg\":\"登录过期,请重新登录\"}"
|
|
321
|
+
error: 19
|
|
322
|
+
errorMessage: "http status error"
|
|
323
|
+
headers: {date: 'Mon, 14 Aug 2023 08:54:58 GMT', content-type: 'application/json;charset=UTF-8', content-length: '52', connection: 'close', access-control-allow-credentials: 'true', …}
|
|
324
|
+
originalData: "{\"code\": 401,\"msg\":\"登录过期,请重新登录\"}"
|
|
325
|
+
status: 401
|
|
326
|
+
}
|
|
327
|
+
*/
|
|
328
|
+
// 统一行为,能正常响应的,都算 success.
|
|
329
|
+
if (err.status) {
|
|
330
|
+
this.#requestSuccess({
|
|
331
|
+
data: err,
|
|
332
|
+
statusCode: err.status,
|
|
333
|
+
header: err.headers
|
|
334
|
+
})
|
|
335
|
+
return
|
|
336
|
+
}
|
|
312
337
|
this.#status = 0
|
|
313
|
-
this.#statusText = err.errMsg
|
|
338
|
+
this.#statusText = err.errMsg || err.errorMessage
|
|
314
339
|
const errorEvent = createXMLHttpRequestEvent('error', this, 0)
|
|
315
340
|
this.trigger('error', errorEvent)
|
|
316
341
|
isFunction(this.onerror) && this.onerror(errorEvent)
|