@tarojs/plugin-http 3.7.0-alpha.1 → 3.7.0-alpha.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/index.js +15 -0
- package/dist/runtime.d.ts +15 -9
- package/dist/runtime.js +60 -16
- package/package.json +5 -5
- package/src/index.ts +17 -1
- package/src/runtime/XMLHttpRequest.ts +68 -23
- package/src/runtime/index.ts +8 -2
package/dist/index.js
CHANGED
|
@@ -26,6 +26,21 @@ var index = (ctx, options) => {
|
|
|
26
26
|
((_b = options.disabledBlob) !== null && _b !== void 0 ? _b : true) && ((_d = args[0]).Blob || (_d.Blob = [runtimeAlias, 'Blob']));
|
|
27
27
|
return args;
|
|
28
28
|
});
|
|
29
|
+
if (ctx.initialConfig.compiler === 'webpack4' || (shared.isObject(ctx.initialConfig.compiler) && ctx.initialConfig.compiler.type === 'webpack4')) {
|
|
30
|
+
// taro webpack4 中, 未正确识别到 axios package.json 中的 browser 字段, 以致于打包进入了 node 相关的代码(https://github.com/axios/axios/blob/59eb99183546d822bc27e881f5dcd748daa04173/package.json#L128-L132)
|
|
31
|
+
const inAxiosReg = /(\/|\\)(node_modules)(\/|\\)(axios)(\/|\\)/;
|
|
32
|
+
chain.merge({
|
|
33
|
+
externals: [
|
|
34
|
+
(context, request, callback) => {
|
|
35
|
+
if (inAxiosReg.test(context) && request.includes('http.js')) {
|
|
36
|
+
// 将 http 适配器从源码里干掉 https://github.com/axios/axios/blob/59eb99183546d822bc27e881f5dcd748daa04173/lib/adapters/adapters.js#L2
|
|
37
|
+
return callback(null, 'var undefined');
|
|
38
|
+
}
|
|
39
|
+
callback();
|
|
40
|
+
}
|
|
41
|
+
]
|
|
42
|
+
});
|
|
43
|
+
}
|
|
29
44
|
}
|
|
30
45
|
});
|
|
31
46
|
ctx.registerMethod({
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { document, Events } from '@tarojs/runtime';
|
|
1
|
+
import { document, Events, TaroEvent } from '@tarojs/runtime';
|
|
2
2
|
declare class Cookie {
|
|
3
3
|
#private;
|
|
4
4
|
constructor();
|
|
@@ -41,6 +41,12 @@ declare class Cookie {
|
|
|
41
41
|
*/
|
|
42
42
|
deserialize(str: any): void;
|
|
43
43
|
}
|
|
44
|
+
interface XMLHttpRequestEvent extends TaroEvent {
|
|
45
|
+
target: XMLHttpRequest;
|
|
46
|
+
currentTarget: XMLHttpRequest;
|
|
47
|
+
loaded: number;
|
|
48
|
+
total: number;
|
|
49
|
+
}
|
|
44
50
|
// https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest
|
|
45
51
|
declare class XMLHttpRequest extends Events {
|
|
46
52
|
#private;
|
|
@@ -54,19 +60,19 @@ declare class XMLHttpRequest extends Events {
|
|
|
54
60
|
toString(): string;
|
|
55
61
|
// 事件
|
|
56
62
|
/** 当 request 被停止时触发,例如当程序调用 XMLHttpRequest.abort() 时 */
|
|
57
|
-
onabort: (() => void) | null;
|
|
63
|
+
onabort: ((e: XMLHttpRequestEvent) => void) | null;
|
|
58
64
|
/** 当 request 遭遇错误时触发 */
|
|
59
|
-
onerror: ((
|
|
65
|
+
onerror: ((e: XMLHttpRequestEvent) => void) | null;
|
|
60
66
|
/** 接收到响应数据时触发 */
|
|
61
|
-
onloadstart: (() => void) | null;
|
|
67
|
+
onloadstart: ((e: XMLHttpRequestEvent) => void) | null;
|
|
62
68
|
/** 请求成功完成时触发 */
|
|
63
|
-
onload: (() => void) | null;
|
|
69
|
+
onload: ((e: XMLHttpRequestEvent) => void) | null;
|
|
64
70
|
/** 当请求结束时触发,无论请求成功 ( load) 还是失败 (abort 或 error)。 */
|
|
65
|
-
onloadend: (() => void) | null;
|
|
71
|
+
onloadend: ((e: XMLHttpRequestEvent) => void) | null;
|
|
66
72
|
/** 在预设时间内没有接收到响应时触发 */
|
|
67
|
-
ontimeout: (() => void) | null;
|
|
73
|
+
ontimeout: ((e: XMLHttpRequestEvent) => void) | null;
|
|
68
74
|
/** 当 readyState 属性发生变化时,调用的事件处理器 */
|
|
69
|
-
onreadystatechange: (() => void) | null;
|
|
75
|
+
onreadystatechange: ((e: XMLHttpRequestEvent) => void) | null;
|
|
70
76
|
constructor();
|
|
71
77
|
addEventListener(event: string, callback: (arg: any) => void): void;
|
|
72
78
|
removeEventListener(event: string, callback: (arg: any) => void): void;
|
|
@@ -91,4 +97,4 @@ declare class XMLHttpRequest extends Events {
|
|
|
91
97
|
setRequestHeader(header: any, value: any): void;
|
|
92
98
|
send(data: any): void;
|
|
93
99
|
}
|
|
94
|
-
export { Cookie, document, XMLHttpRequest };
|
|
100
|
+
export { Cookie, document, XMLHttpRequest, XMLHttpRequestEvent };
|
package/dist/runtime.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { parseUrl, Events, window, document } from '@tarojs/runtime';
|
|
1
|
+
import { parseUrl, Events, createEvent, window, document } from '@tarojs/runtime';
|
|
2
2
|
export { document } from '@tarojs/runtime';
|
|
3
3
|
import { isString, isFunction, isWebPlatform } from '@tarojs/shared';
|
|
4
4
|
import { setStorage, getStorageSync, request } from '@tarojs/taro';
|
|
@@ -17,6 +17,8 @@ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
17
17
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
18
18
|
PERFORMANCE OF THIS SOFTWARE.
|
|
19
19
|
***************************************************************************** */
|
|
20
|
+
/* global Reflect, Promise */
|
|
21
|
+
|
|
20
22
|
|
|
21
23
|
function __classPrivateFieldGet(receiver, state, kind, f) {
|
|
22
24
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
@@ -351,6 +353,34 @@ const STATUS_TEXT_MAP = {
|
|
|
351
353
|
504: 'Gateway Timeout',
|
|
352
354
|
505: 'HTTP Version Not Supported',
|
|
353
355
|
};
|
|
356
|
+
function createXMLHttpRequestEvent(event, target, loaded) {
|
|
357
|
+
const e = createEvent(event);
|
|
358
|
+
try {
|
|
359
|
+
Object.defineProperties(e, {
|
|
360
|
+
'currentTarget': {
|
|
361
|
+
enumerable: true,
|
|
362
|
+
value: target
|
|
363
|
+
},
|
|
364
|
+
'target': {
|
|
365
|
+
enumerable: true,
|
|
366
|
+
value: target
|
|
367
|
+
},
|
|
368
|
+
'loaded': {
|
|
369
|
+
enumerable: true,
|
|
370
|
+
value: loaded || 0
|
|
371
|
+
},
|
|
372
|
+
// 读 Content-Range 字段,目前来说作用不大,先和 loaded 保持一致
|
|
373
|
+
'total': {
|
|
374
|
+
enumerable: true,
|
|
375
|
+
value: loaded || 0
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
catch (err) {
|
|
380
|
+
// no handler
|
|
381
|
+
}
|
|
382
|
+
return e;
|
|
383
|
+
}
|
|
354
384
|
// https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest
|
|
355
385
|
class XMLHttpRequest extends Events {
|
|
356
386
|
// 欺骗一些库让其认为是原生的xhr
|
|
@@ -466,8 +496,9 @@ class XMLHttpRequest extends Events {
|
|
|
466
496
|
abort() {
|
|
467
497
|
if (__classPrivateFieldGet(this, _XMLHttpRequest_requestTask, "f")) {
|
|
468
498
|
__classPrivateFieldGet(this, _XMLHttpRequest_requestTask, "f").abort();
|
|
469
|
-
|
|
470
|
-
|
|
499
|
+
const abortEvent = createXMLHttpRequestEvent('abort', this, 0);
|
|
500
|
+
this.trigger('abort', abortEvent);
|
|
501
|
+
isFunction(this.onabort) && this.onabort(abortEvent);
|
|
471
502
|
}
|
|
472
503
|
}
|
|
473
504
|
getAllResponseHeaders() {
|
|
@@ -512,8 +543,9 @@ _XMLHttpRequest_method = new WeakMap(), _XMLHttpRequest_url = new WeakMap(), _XM
|
|
|
512
543
|
const hasChange = readyState !== __classPrivateFieldGet(this, _XMLHttpRequest_readyState, "f");
|
|
513
544
|
__classPrivateFieldSet(this, _XMLHttpRequest_readyState, readyState, "f");
|
|
514
545
|
if (hasChange) {
|
|
515
|
-
|
|
516
|
-
|
|
546
|
+
const readystatechangeEvent = createXMLHttpRequestEvent('readystatechange', this, 0);
|
|
547
|
+
this.trigger('readystatechange', readystatechangeEvent);
|
|
548
|
+
isFunction(this.onreadystatechange) && this.onreadystatechange(readystatechangeEvent);
|
|
517
549
|
}
|
|
518
550
|
}, _XMLHttpRequest_callRequest = function _XMLHttpRequest_callRequest() {
|
|
519
551
|
if (!window || !window.document) {
|
|
@@ -527,8 +559,9 @@ _XMLHttpRequest_method = new WeakMap(), _XMLHttpRequest_url = new WeakMap(), _XM
|
|
|
527
559
|
if (__classPrivateFieldGet(this, _XMLHttpRequest_requestTask, "f"))
|
|
528
560
|
__classPrivateFieldGet(this, _XMLHttpRequest_requestTask, "f").abort();
|
|
529
561
|
__classPrivateFieldGet(this, _XMLHttpRequest_instances, "m", _XMLHttpRequest_callReadyStateChange).call(this, XMLHttpRequest.DONE);
|
|
530
|
-
|
|
531
|
-
|
|
562
|
+
const timeoutEvent = createXMLHttpRequestEvent('timeout', this, 0);
|
|
563
|
+
this.trigger('timeout', timeoutEvent);
|
|
564
|
+
isFunction(this.ontimeout) && this.ontimeout(timeoutEvent);
|
|
532
565
|
}
|
|
533
566
|
}, __classPrivateFieldGet(this, _XMLHttpRequest_timeout, "f"));
|
|
534
567
|
}
|
|
@@ -543,7 +576,8 @@ _XMLHttpRequest_method = new WeakMap(), _XMLHttpRequest_url = new WeakMap(), _XM
|
|
|
543
576
|
url = url.indexOf('//') === -1 ? window.location.origin + url : url;
|
|
544
577
|
// 头信息
|
|
545
578
|
const header = Object.assign({}, __classPrivateFieldGet(this, _XMLHttpRequest_header, "f"));
|
|
546
|
-
|
|
579
|
+
// https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Cookies
|
|
580
|
+
header.cookie = window.document.$$cookie;
|
|
547
581
|
if (!this.withCredentials) {
|
|
548
582
|
// 不同源,要求 withCredentials 为 true 才携带 cookie
|
|
549
583
|
const { origin } = parseUrl(url);
|
|
@@ -600,23 +634,27 @@ _XMLHttpRequest_method = new WeakMap(), _XMLHttpRequest_url = new WeakMap(), _XM
|
|
|
600
634
|
// 处理返回数据
|
|
601
635
|
if (data) {
|
|
602
636
|
__classPrivateFieldGet(this, _XMLHttpRequest_instances, "m", _XMLHttpRequest_callReadyStateChange).call(this, XMLHttpRequest.LOADING);
|
|
603
|
-
|
|
604
|
-
|
|
637
|
+
const loadstartEvent = createXMLHttpRequestEvent('loadstart', this, header['Content-Length']);
|
|
638
|
+
this.trigger('loadstart', loadstartEvent);
|
|
639
|
+
isFunction(this.onloadstart) && this.onloadstart(loadstartEvent);
|
|
605
640
|
__classPrivateFieldSet(this, _XMLHttpRequest_response, data, "f");
|
|
606
|
-
|
|
607
|
-
|
|
641
|
+
const loadEvent = createXMLHttpRequestEvent('load', this, header['Content-Length']);
|
|
642
|
+
this.trigger('load', loadEvent);
|
|
643
|
+
isFunction(this.onload) && this.onload(loadEvent);
|
|
608
644
|
}
|
|
609
645
|
}, _XMLHttpRequest_requestFail = function _XMLHttpRequest_requestFail(err) {
|
|
610
646
|
__classPrivateFieldSet(this, _XMLHttpRequest_status, 0, "f");
|
|
611
647
|
__classPrivateFieldSet(this, _XMLHttpRequest_statusText, err.errMsg, "f");
|
|
612
|
-
|
|
613
|
-
|
|
648
|
+
const errorEvent = createXMLHttpRequestEvent('error', this, 0);
|
|
649
|
+
this.trigger('error', errorEvent);
|
|
650
|
+
isFunction(this.onerror) && this.onerror(errorEvent);
|
|
614
651
|
}, _XMLHttpRequest_requestComplete = function _XMLHttpRequest_requestComplete() {
|
|
615
652
|
__classPrivateFieldSet(this, _XMLHttpRequest_requestTask, null, "f");
|
|
616
653
|
__classPrivateFieldGet(this, _XMLHttpRequest_instances, "m", _XMLHttpRequest_callReadyStateChange).call(this, XMLHttpRequest.DONE);
|
|
617
654
|
if (__classPrivateFieldGet(this, _XMLHttpRequest_status, "f")) {
|
|
618
|
-
|
|
619
|
-
|
|
655
|
+
const loadendEvent = createXMLHttpRequestEvent('loadend', this, __classPrivateFieldGet(this, _XMLHttpRequest_header, "f")['Content-Length']);
|
|
656
|
+
this.trigger('loadend', loadendEvent);
|
|
657
|
+
isFunction(this.onloadend) && this.onloadend(loadendEvent);
|
|
620
658
|
}
|
|
621
659
|
};
|
|
622
660
|
XMLHttpRequest.UNSENT = 0;
|
|
@@ -646,6 +684,12 @@ if (!isWebPlatform()) {
|
|
|
646
684
|
_cookie.setCookie(value, this.URL);
|
|
647
685
|
},
|
|
648
686
|
},
|
|
687
|
+
/** 获取完整的 cookie,包括 httpOnly 也能获取到 */
|
|
688
|
+
$$cookie: {
|
|
689
|
+
get() {
|
|
690
|
+
return _cookie.getCookie(this.URL, true);
|
|
691
|
+
},
|
|
692
|
+
}
|
|
649
693
|
});
|
|
650
694
|
}
|
|
651
695
|
window.XMLHttpRequest = XMLHttpRequest;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tarojs/plugin-http",
|
|
3
|
-
"version": "3.7.0-alpha.
|
|
3
|
+
"version": "3.7.0-alpha.3",
|
|
4
4
|
"description": "Taro 小程序端支持使用 web 请求 的插件",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -23,12 +23,12 @@
|
|
|
23
23
|
},
|
|
24
24
|
"homepage": "https://github.com/NervJS/taro#readme",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@tarojs/runtime": "3.7.0-alpha.
|
|
27
|
-
"@tarojs/service": "3.7.0-alpha.
|
|
28
|
-
"@tarojs/shared": "3.7.0-alpha.
|
|
26
|
+
"@tarojs/runtime": "3.7.0-alpha.3",
|
|
27
|
+
"@tarojs/service": "3.7.0-alpha.3",
|
|
28
|
+
"@tarojs/shared": "3.7.0-alpha.3"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@rollup/plugin-json": "^
|
|
31
|
+
"@rollup/plugin-json": "^6.0.0",
|
|
32
32
|
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
33
33
|
"jest": "^29.3.1",
|
|
34
34
|
"jest-cli": "^29.3.1",
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isArray, isString } from '@tarojs/shared'
|
|
1
|
+
import { isArray, isObject, isString } from '@tarojs/shared'
|
|
2
2
|
import path from 'path'
|
|
3
3
|
|
|
4
4
|
import { name as packageName } from '../package.json'
|
|
@@ -35,6 +35,22 @@ export default (ctx: IPluginContext, options: IOptions) => {
|
|
|
35
35
|
|
|
36
36
|
return args
|
|
37
37
|
})
|
|
38
|
+
|
|
39
|
+
if (ctx.initialConfig.compiler === 'webpack4' || (isObject<boolean>(ctx.initialConfig.compiler) && ctx.initialConfig.compiler.type === 'webpack4')) {
|
|
40
|
+
// taro webpack4 中, 未正确识别到 axios package.json 中的 browser 字段, 以致于打包进入了 node 相关的代码(https://github.com/axios/axios/blob/59eb99183546d822bc27e881f5dcd748daa04173/package.json#L128-L132)
|
|
41
|
+
const inAxiosReg = /(\/|\\)(node_modules)(\/|\\)(axios)(\/|\\)/
|
|
42
|
+
chain.merge({
|
|
43
|
+
externals: [
|
|
44
|
+
(context, request, callback) => {
|
|
45
|
+
if (inAxiosReg.test(context) && request.includes('http.js')) {
|
|
46
|
+
// 将 http 适配器从源码里干掉 https://github.com/axios/axios/blob/59eb99183546d822bc27e881f5dcd748daa04173/lib/adapters/adapters.js#L2
|
|
47
|
+
return callback(null, 'var undefined')
|
|
48
|
+
}
|
|
49
|
+
callback()
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
})
|
|
53
|
+
}
|
|
38
54
|
}
|
|
39
55
|
})
|
|
40
56
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Events, parseUrl, window } from '@tarojs/runtime'
|
|
1
|
+
import { createEvent, Events, parseUrl, TaroEvent, window } from '@tarojs/runtime'
|
|
2
2
|
import { isFunction, isString } from '@tarojs/shared'
|
|
3
3
|
import { request } from '@tarojs/taro'
|
|
4
4
|
|
|
@@ -51,6 +51,42 @@ const STATUS_TEXT_MAP = {
|
|
|
51
51
|
504: 'Gateway Timeout',
|
|
52
52
|
505: 'HTTP Version Not Supported',
|
|
53
53
|
}
|
|
54
|
+
|
|
55
|
+
export interface XMLHttpRequestEvent extends TaroEvent {
|
|
56
|
+
target: XMLHttpRequest
|
|
57
|
+
currentTarget: XMLHttpRequest
|
|
58
|
+
loaded: number
|
|
59
|
+
total: number
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function createXMLHttpRequestEvent (event: string, target:XMLHttpRequest, loaded: number): XMLHttpRequestEvent {
|
|
63
|
+
const e = createEvent(event) as XMLHttpRequestEvent
|
|
64
|
+
try {
|
|
65
|
+
Object.defineProperties(e, {
|
|
66
|
+
'currentTarget': {
|
|
67
|
+
enumerable: true,
|
|
68
|
+
value: target
|
|
69
|
+
},
|
|
70
|
+
'target': {
|
|
71
|
+
enumerable: true,
|
|
72
|
+
value: target
|
|
73
|
+
},
|
|
74
|
+
'loaded': {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
value: loaded || 0
|
|
77
|
+
},
|
|
78
|
+
// 读 Content-Range 字段,目前来说作用不大,先和 loaded 保持一致
|
|
79
|
+
'total': {
|
|
80
|
+
enumerable: true,
|
|
81
|
+
value: loaded || 0
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
} catch (err) {
|
|
85
|
+
// no handler
|
|
86
|
+
}
|
|
87
|
+
return e
|
|
88
|
+
}
|
|
89
|
+
|
|
54
90
|
// https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest
|
|
55
91
|
export class XMLHttpRequest extends Events {
|
|
56
92
|
static readonly UNSENT = 0
|
|
@@ -85,25 +121,25 @@ export class XMLHttpRequest extends Events {
|
|
|
85
121
|
// 事件
|
|
86
122
|
|
|
87
123
|
/** 当 request 被停止时触发,例如当程序调用 XMLHttpRequest.abort() 时 */
|
|
88
|
-
onabort: (() => void) | null = null
|
|
124
|
+
onabort: ((e: XMLHttpRequestEvent) => void) | null = null
|
|
89
125
|
|
|
90
126
|
/** 当 request 遭遇错误时触发 */
|
|
91
|
-
onerror: ((
|
|
127
|
+
onerror: ((e: XMLHttpRequestEvent) => void) | null = null
|
|
92
128
|
|
|
93
129
|
/** 接收到响应数据时触发 */
|
|
94
|
-
onloadstart: (() => void) | null = null
|
|
130
|
+
onloadstart: ((e: XMLHttpRequestEvent) => void) | null = null
|
|
95
131
|
|
|
96
132
|
/** 请求成功完成时触发 */
|
|
97
|
-
onload: (() => void) | null = null
|
|
133
|
+
onload: ((e: XMLHttpRequestEvent) => void) | null = null
|
|
98
134
|
|
|
99
135
|
/** 当请求结束时触发,无论请求成功 ( load) 还是失败 (abort 或 error)。 */
|
|
100
|
-
onloadend: (() => void) | null = null
|
|
136
|
+
onloadend: ((e: XMLHttpRequestEvent) => void) | null = null
|
|
101
137
|
|
|
102
138
|
/** 在预设时间内没有接收到响应时触发 */
|
|
103
|
-
ontimeout: (() => void) | null = null
|
|
139
|
+
ontimeout: ((e: XMLHttpRequestEvent) => void) | null = null
|
|
104
140
|
|
|
105
141
|
/** 当 readyState 属性发生变化时,调用的事件处理器 */
|
|
106
|
-
onreadystatechange: (() => void) | null = null
|
|
142
|
+
onreadystatechange: ((e: XMLHttpRequestEvent) => void) | null = null
|
|
107
143
|
|
|
108
144
|
constructor () {
|
|
109
145
|
super()
|
|
@@ -145,8 +181,9 @@ export class XMLHttpRequest extends Events {
|
|
|
145
181
|
this.#readyState = readyState
|
|
146
182
|
|
|
147
183
|
if (hasChange) {
|
|
148
|
-
|
|
149
|
-
|
|
184
|
+
const readystatechangeEvent = createXMLHttpRequestEvent('readystatechange', this, 0)
|
|
185
|
+
this.trigger('readystatechange', readystatechangeEvent)
|
|
186
|
+
isFunction(this.onreadystatechange) && this.onreadystatechange(readystatechangeEvent)
|
|
150
187
|
}
|
|
151
188
|
}
|
|
152
189
|
|
|
@@ -165,8 +202,9 @@ export class XMLHttpRequest extends Events {
|
|
|
165
202
|
// 超时
|
|
166
203
|
if (this.#requestTask) this.#requestTask.abort()
|
|
167
204
|
this.#callReadyStateChange(XMLHttpRequest.DONE)
|
|
168
|
-
|
|
169
|
-
|
|
205
|
+
const timeoutEvent = createXMLHttpRequestEvent('timeout', this, 0)
|
|
206
|
+
this.trigger('timeout', timeoutEvent)
|
|
207
|
+
isFunction(this.ontimeout) && this.ontimeout(timeoutEvent)
|
|
170
208
|
}
|
|
171
209
|
}, this.#timeout)
|
|
172
210
|
}
|
|
@@ -184,7 +222,8 @@ export class XMLHttpRequest extends Events {
|
|
|
184
222
|
|
|
185
223
|
// 头信息
|
|
186
224
|
const header = Object.assign({}, this.#header)
|
|
187
|
-
|
|
225
|
+
// https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Cookies
|
|
226
|
+
header.cookie = window.document.$$cookie
|
|
188
227
|
if (!this.withCredentials) {
|
|
189
228
|
// 不同源,要求 withCredentials 为 true 才携带 cookie
|
|
190
229
|
const { origin } = parseUrl(url)
|
|
@@ -255,11 +294,14 @@ export class XMLHttpRequest extends Events {
|
|
|
255
294
|
// 处理返回数据
|
|
256
295
|
if (data) {
|
|
257
296
|
this.#callReadyStateChange(XMLHttpRequest.LOADING)
|
|
258
|
-
|
|
259
|
-
|
|
297
|
+
const loadstartEvent = createXMLHttpRequestEvent('loadstart', this, header['Content-Length'])
|
|
298
|
+
this.trigger('loadstart', loadstartEvent)
|
|
299
|
+
isFunction(this.onloadstart) && this.onloadstart(loadstartEvent)
|
|
260
300
|
this.#response = data
|
|
261
|
-
|
|
262
|
-
|
|
301
|
+
|
|
302
|
+
const loadEvent = createXMLHttpRequestEvent('load', this, header['Content-Length'])
|
|
303
|
+
this.trigger('load', loadEvent)
|
|
304
|
+
isFunction(this.onload) && this.onload(loadEvent)
|
|
263
305
|
}
|
|
264
306
|
}
|
|
265
307
|
|
|
@@ -269,8 +311,9 @@ export class XMLHttpRequest extends Events {
|
|
|
269
311
|
#requestFail (err) {
|
|
270
312
|
this.#status = 0
|
|
271
313
|
this.#statusText = err.errMsg
|
|
272
|
-
|
|
273
|
-
|
|
314
|
+
const errorEvent = createXMLHttpRequestEvent('error', this, 0)
|
|
315
|
+
this.trigger('error', errorEvent)
|
|
316
|
+
isFunction(this.onerror) && this.onerror(errorEvent)
|
|
274
317
|
}
|
|
275
318
|
|
|
276
319
|
/**
|
|
@@ -281,8 +324,9 @@ export class XMLHttpRequest extends Events {
|
|
|
281
324
|
this.#callReadyStateChange(XMLHttpRequest.DONE)
|
|
282
325
|
|
|
283
326
|
if (this.#status) {
|
|
284
|
-
|
|
285
|
-
|
|
327
|
+
const loadendEvent = createXMLHttpRequestEvent('loadend', this, this.#header['Content-Length'])
|
|
328
|
+
this.trigger('loadend', loadendEvent)
|
|
329
|
+
isFunction(this.onloadend) && this.onloadend(loadendEvent)
|
|
286
330
|
}
|
|
287
331
|
}
|
|
288
332
|
|
|
@@ -346,8 +390,9 @@ export class XMLHttpRequest extends Events {
|
|
|
346
390
|
abort () {
|
|
347
391
|
if (this.#requestTask) {
|
|
348
392
|
this.#requestTask.abort()
|
|
349
|
-
|
|
350
|
-
|
|
393
|
+
const abortEvent = createXMLHttpRequestEvent('abort', this, 0)
|
|
394
|
+
this.trigger('abort', abortEvent)
|
|
395
|
+
isFunction(this.onabort) && this.onabort(abortEvent)
|
|
351
396
|
}
|
|
352
397
|
}
|
|
353
398
|
|
package/src/runtime/index.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { document, window } from '@tarojs/runtime'
|
|
|
2
2
|
import { isWebPlatform } from '@tarojs/shared'
|
|
3
3
|
|
|
4
4
|
import { Cookie, createCookieInstance } from './Cookie'
|
|
5
|
-
import { XMLHttpRequest } from './XMLHttpRequest'
|
|
5
|
+
import { type XMLHttpRequestEvent, XMLHttpRequest } from './XMLHttpRequest'
|
|
6
6
|
|
|
7
7
|
declare const ENABLE_COOKIE: boolean
|
|
8
8
|
|
|
@@ -26,10 +26,16 @@ if (!isWebPlatform()) {
|
|
|
26
26
|
_cookie.setCookie(value, this.URL)
|
|
27
27
|
},
|
|
28
28
|
},
|
|
29
|
+
/** 获取完整的 cookie,包括 httpOnly 也能获取到 */
|
|
30
|
+
$$cookie: {
|
|
31
|
+
get () {
|
|
32
|
+
return _cookie.getCookie(this.URL, true)
|
|
33
|
+
},
|
|
34
|
+
}
|
|
29
35
|
})
|
|
30
36
|
}
|
|
31
37
|
|
|
32
38
|
window.XMLHttpRequest = XMLHttpRequest
|
|
33
39
|
}
|
|
34
40
|
|
|
35
|
-
export { Cookie, document, XMLHttpRequest }
|
|
41
|
+
export { Cookie, document, XMLHttpRequest, XMLHttpRequestEvent }
|