@tarojs/plugin-http 3.6.6 → 3.6.7-alpha.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/runtime.d.ts +15 -9
- package/dist/runtime.js +50 -15
- package/package.json +4 -4
- package/src/runtime/XMLHttpRequest.ts +66 -22
- package/src/runtime/index.ts +2 -2
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';
|
|
@@ -351,6 +351,34 @@ const STATUS_TEXT_MAP = {
|
|
|
351
351
|
504: 'Gateway Timeout',
|
|
352
352
|
505: 'HTTP Version Not Supported',
|
|
353
353
|
};
|
|
354
|
+
function createXMLHttpRequestEvent(event, target, loaded) {
|
|
355
|
+
const e = createEvent(event);
|
|
356
|
+
try {
|
|
357
|
+
Object.defineProperties(e, {
|
|
358
|
+
'currentTarget': {
|
|
359
|
+
enumerable: true,
|
|
360
|
+
value: target
|
|
361
|
+
},
|
|
362
|
+
'target': {
|
|
363
|
+
enumerable: true,
|
|
364
|
+
value: target
|
|
365
|
+
},
|
|
366
|
+
'loaded': {
|
|
367
|
+
enumerable: true,
|
|
368
|
+
value: loaded || 0
|
|
369
|
+
},
|
|
370
|
+
// 读 Content-Range 字段,目前来说作用不大,先和 loaded 保持一致
|
|
371
|
+
'total': {
|
|
372
|
+
enumerable: true,
|
|
373
|
+
value: loaded || 0
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
catch (err) {
|
|
378
|
+
// no handler
|
|
379
|
+
}
|
|
380
|
+
return e;
|
|
381
|
+
}
|
|
354
382
|
// https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest
|
|
355
383
|
class XMLHttpRequest extends Events {
|
|
356
384
|
// 欺骗一些库让其认为是原生的xhr
|
|
@@ -466,8 +494,9 @@ class XMLHttpRequest extends Events {
|
|
|
466
494
|
abort() {
|
|
467
495
|
if (__classPrivateFieldGet(this, _XMLHttpRequest_requestTask, "f")) {
|
|
468
496
|
__classPrivateFieldGet(this, _XMLHttpRequest_requestTask, "f").abort();
|
|
469
|
-
|
|
470
|
-
|
|
497
|
+
const abortEvent = createXMLHttpRequestEvent('abort', this, 0);
|
|
498
|
+
this.trigger('abort', abortEvent);
|
|
499
|
+
isFunction(this.onabort) && this.onabort(abortEvent);
|
|
471
500
|
}
|
|
472
501
|
}
|
|
473
502
|
getAllResponseHeaders() {
|
|
@@ -512,8 +541,9 @@ _XMLHttpRequest_method = new WeakMap(), _XMLHttpRequest_url = new WeakMap(), _XM
|
|
|
512
541
|
const hasChange = readyState !== __classPrivateFieldGet(this, _XMLHttpRequest_readyState, "f");
|
|
513
542
|
__classPrivateFieldSet(this, _XMLHttpRequest_readyState, readyState, "f");
|
|
514
543
|
if (hasChange) {
|
|
515
|
-
|
|
516
|
-
|
|
544
|
+
const readystatechangeEvent = createXMLHttpRequestEvent('readystatechange', this, 0);
|
|
545
|
+
this.trigger('readystatechange', readystatechangeEvent);
|
|
546
|
+
isFunction(this.onreadystatechange) && this.onreadystatechange(readystatechangeEvent);
|
|
517
547
|
}
|
|
518
548
|
}, _XMLHttpRequest_callRequest = function _XMLHttpRequest_callRequest() {
|
|
519
549
|
if (!window || !window.document) {
|
|
@@ -527,8 +557,9 @@ _XMLHttpRequest_method = new WeakMap(), _XMLHttpRequest_url = new WeakMap(), _XM
|
|
|
527
557
|
if (__classPrivateFieldGet(this, _XMLHttpRequest_requestTask, "f"))
|
|
528
558
|
__classPrivateFieldGet(this, _XMLHttpRequest_requestTask, "f").abort();
|
|
529
559
|
__classPrivateFieldGet(this, _XMLHttpRequest_instances, "m", _XMLHttpRequest_callReadyStateChange).call(this, XMLHttpRequest.DONE);
|
|
530
|
-
|
|
531
|
-
|
|
560
|
+
const timeoutEvent = createXMLHttpRequestEvent('timeout', this, 0);
|
|
561
|
+
this.trigger('timeout', timeoutEvent);
|
|
562
|
+
isFunction(this.ontimeout) && this.ontimeout(timeoutEvent);
|
|
532
563
|
}
|
|
533
564
|
}, __classPrivateFieldGet(this, _XMLHttpRequest_timeout, "f"));
|
|
534
565
|
}
|
|
@@ -600,23 +631,27 @@ _XMLHttpRequest_method = new WeakMap(), _XMLHttpRequest_url = new WeakMap(), _XM
|
|
|
600
631
|
// 处理返回数据
|
|
601
632
|
if (data) {
|
|
602
633
|
__classPrivateFieldGet(this, _XMLHttpRequest_instances, "m", _XMLHttpRequest_callReadyStateChange).call(this, XMLHttpRequest.LOADING);
|
|
603
|
-
|
|
604
|
-
|
|
634
|
+
const loadstartEvent = createXMLHttpRequestEvent('loadstart', this, header['Content-Length']);
|
|
635
|
+
this.trigger('loadstart', loadstartEvent);
|
|
636
|
+
isFunction(this.onloadstart) && this.onloadstart(loadstartEvent);
|
|
605
637
|
__classPrivateFieldSet(this, _XMLHttpRequest_response, data, "f");
|
|
606
|
-
|
|
607
|
-
|
|
638
|
+
const loadEvent = createXMLHttpRequestEvent('load', this, header['Content-Length']);
|
|
639
|
+
this.trigger('load', loadEvent);
|
|
640
|
+
isFunction(this.onload) && this.onload(loadEvent);
|
|
608
641
|
}
|
|
609
642
|
}, _XMLHttpRequest_requestFail = function _XMLHttpRequest_requestFail(err) {
|
|
610
643
|
__classPrivateFieldSet(this, _XMLHttpRequest_status, 0, "f");
|
|
611
644
|
__classPrivateFieldSet(this, _XMLHttpRequest_statusText, err.errMsg, "f");
|
|
612
|
-
|
|
613
|
-
|
|
645
|
+
const errorEvent = createXMLHttpRequestEvent('error', this, 0);
|
|
646
|
+
this.trigger('error', errorEvent);
|
|
647
|
+
isFunction(this.onerror) && this.onerror(errorEvent);
|
|
614
648
|
}, _XMLHttpRequest_requestComplete = function _XMLHttpRequest_requestComplete() {
|
|
615
649
|
__classPrivateFieldSet(this, _XMLHttpRequest_requestTask, null, "f");
|
|
616
650
|
__classPrivateFieldGet(this, _XMLHttpRequest_instances, "m", _XMLHttpRequest_callReadyStateChange).call(this, XMLHttpRequest.DONE);
|
|
617
651
|
if (__classPrivateFieldGet(this, _XMLHttpRequest_status, "f")) {
|
|
618
|
-
|
|
619
|
-
|
|
652
|
+
const loadendEvent = createXMLHttpRequestEvent('loadend', this, __classPrivateFieldGet(this, _XMLHttpRequest_header, "f")['Content-Length']);
|
|
653
|
+
this.trigger('loadend', loadendEvent);
|
|
654
|
+
isFunction(this.onloadend) && this.onloadend(loadendEvent);
|
|
620
655
|
}
|
|
621
656
|
};
|
|
622
657
|
XMLHttpRequest.UNSENT = 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tarojs/plugin-http",
|
|
3
|
-
"version": "3.6.
|
|
3
|
+
"version": "3.6.7-alpha.0",
|
|
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.6.
|
|
27
|
-
"@tarojs/service": "3.6.
|
|
28
|
-
"@tarojs/shared": "3.6.
|
|
26
|
+
"@tarojs/runtime": "3.6.7-alpha.0",
|
|
27
|
+
"@tarojs/service": "3.6.7-alpha.0",
|
|
28
|
+
"@tarojs/shared": "3.6.7-alpha.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@rollup/plugin-json": "^4.1.0",
|
|
@@ -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
|
}
|
|
@@ -255,11 +293,14 @@ export class XMLHttpRequest extends Events {
|
|
|
255
293
|
// 处理返回数据
|
|
256
294
|
if (data) {
|
|
257
295
|
this.#callReadyStateChange(XMLHttpRequest.LOADING)
|
|
258
|
-
|
|
259
|
-
|
|
296
|
+
const loadstartEvent = createXMLHttpRequestEvent('loadstart', this, header['Content-Length'])
|
|
297
|
+
this.trigger('loadstart', loadstartEvent)
|
|
298
|
+
isFunction(this.onloadstart) && this.onloadstart(loadstartEvent)
|
|
260
299
|
this.#response = data
|
|
261
|
-
|
|
262
|
-
|
|
300
|
+
|
|
301
|
+
const loadEvent = createXMLHttpRequestEvent('load', this, header['Content-Length'])
|
|
302
|
+
this.trigger('load', loadEvent)
|
|
303
|
+
isFunction(this.onload) && this.onload(loadEvent)
|
|
263
304
|
}
|
|
264
305
|
}
|
|
265
306
|
|
|
@@ -269,8 +310,9 @@ export class XMLHttpRequest extends Events {
|
|
|
269
310
|
#requestFail (err) {
|
|
270
311
|
this.#status = 0
|
|
271
312
|
this.#statusText = err.errMsg
|
|
272
|
-
|
|
273
|
-
|
|
313
|
+
const errorEvent = createXMLHttpRequestEvent('error', this, 0)
|
|
314
|
+
this.trigger('error', errorEvent)
|
|
315
|
+
isFunction(this.onerror) && this.onerror(errorEvent)
|
|
274
316
|
}
|
|
275
317
|
|
|
276
318
|
/**
|
|
@@ -281,8 +323,9 @@ export class XMLHttpRequest extends Events {
|
|
|
281
323
|
this.#callReadyStateChange(XMLHttpRequest.DONE)
|
|
282
324
|
|
|
283
325
|
if (this.#status) {
|
|
284
|
-
|
|
285
|
-
|
|
326
|
+
const loadendEvent = createXMLHttpRequestEvent('loadend', this, this.#header['Content-Length'])
|
|
327
|
+
this.trigger('loadend', loadendEvent)
|
|
328
|
+
isFunction(this.onloadend) && this.onloadend(loadendEvent)
|
|
286
329
|
}
|
|
287
330
|
}
|
|
288
331
|
|
|
@@ -346,8 +389,9 @@ export class XMLHttpRequest extends Events {
|
|
|
346
389
|
abort () {
|
|
347
390
|
if (this.#requestTask) {
|
|
348
391
|
this.#requestTask.abort()
|
|
349
|
-
|
|
350
|
-
|
|
392
|
+
const abortEvent = createXMLHttpRequestEvent('abort', this, 0)
|
|
393
|
+
this.trigger('abort', abortEvent)
|
|
394
|
+
isFunction(this.onabort) && this.onabort(abortEvent)
|
|
351
395
|
}
|
|
352
396
|
}
|
|
353
397
|
|
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
|
|
|
@@ -32,4 +32,4 @@ if (!isWebPlatform()) {
|
|
|
32
32
|
window.XMLHttpRequest = XMLHttpRequest
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
export { Cookie, document, XMLHttpRequest }
|
|
35
|
+
export { Cookie, document, XMLHttpRequest, XMLHttpRequestEvent }
|