@tarojs/plugin-http 3.6.6 → 3.6.7
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 +52 -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';
|
|
@@ -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
|
}
|
|
@@ -600,23 +633,27 @@ _XMLHttpRequest_method = new WeakMap(), _XMLHttpRequest_url = new WeakMap(), _XM
|
|
|
600
633
|
// 处理返回数据
|
|
601
634
|
if (data) {
|
|
602
635
|
__classPrivateFieldGet(this, _XMLHttpRequest_instances, "m", _XMLHttpRequest_callReadyStateChange).call(this, XMLHttpRequest.LOADING);
|
|
603
|
-
|
|
604
|
-
|
|
636
|
+
const loadstartEvent = createXMLHttpRequestEvent('loadstart', this, header['Content-Length']);
|
|
637
|
+
this.trigger('loadstart', loadstartEvent);
|
|
638
|
+
isFunction(this.onloadstart) && this.onloadstart(loadstartEvent);
|
|
605
639
|
__classPrivateFieldSet(this, _XMLHttpRequest_response, data, "f");
|
|
606
|
-
|
|
607
|
-
|
|
640
|
+
const loadEvent = createXMLHttpRequestEvent('load', this, header['Content-Length']);
|
|
641
|
+
this.trigger('load', loadEvent);
|
|
642
|
+
isFunction(this.onload) && this.onload(loadEvent);
|
|
608
643
|
}
|
|
609
644
|
}, _XMLHttpRequest_requestFail = function _XMLHttpRequest_requestFail(err) {
|
|
610
645
|
__classPrivateFieldSet(this, _XMLHttpRequest_status, 0, "f");
|
|
611
646
|
__classPrivateFieldSet(this, _XMLHttpRequest_statusText, err.errMsg, "f");
|
|
612
|
-
|
|
613
|
-
|
|
647
|
+
const errorEvent = createXMLHttpRequestEvent('error', this, 0);
|
|
648
|
+
this.trigger('error', errorEvent);
|
|
649
|
+
isFunction(this.onerror) && this.onerror(errorEvent);
|
|
614
650
|
}, _XMLHttpRequest_requestComplete = function _XMLHttpRequest_requestComplete() {
|
|
615
651
|
__classPrivateFieldSet(this, _XMLHttpRequest_requestTask, null, "f");
|
|
616
652
|
__classPrivateFieldGet(this, _XMLHttpRequest_instances, "m", _XMLHttpRequest_callReadyStateChange).call(this, XMLHttpRequest.DONE);
|
|
617
653
|
if (__classPrivateFieldGet(this, _XMLHttpRequest_status, "f")) {
|
|
618
|
-
|
|
619
|
-
|
|
654
|
+
const loadendEvent = createXMLHttpRequestEvent('loadend', this, __classPrivateFieldGet(this, _XMLHttpRequest_header, "f")['Content-Length']);
|
|
655
|
+
this.trigger('loadend', loadendEvent);
|
|
656
|
+
isFunction(this.onloadend) && this.onloadend(loadendEvent);
|
|
620
657
|
}
|
|
621
658
|
};
|
|
622
659
|
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",
|
|
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",
|
|
27
|
+
"@tarojs/service": "3.6.7",
|
|
28
|
+
"@tarojs/shared": "3.6.7"
|
|
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 }
|