mphttpx 1.0.9 → 1.0.10

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.
Files changed (2) hide show
  1. package/README.md +930 -716
  2. package/package.json +10 -2
package/README.md CHANGED
@@ -1,716 +1,930 @@
1
- # MPHTTPX
2
-
3
- The `mphttpx` library aims to provide a more ES6-styled [Blob.js][0],
4
- along with a `fetch` polyfill that works seamlessly with the Blob-polyfill.
5
- This allows web code to be reused in other environments (such as mini-programs).
6
-
7
- ## Table of Contents
8
-
9
- - [MPHTTPX](#mphttpx)
10
- - [Table of Contents](#table-of-contents)
11
- - [Features](#features)
12
- - [Installation](#installation)
13
- - [Mini-Program Support](#mini-program-support)
14
- - [Usage](#usage)
15
- - [TextEncoder](#textencoder)
16
- - [Compatibility](#compatibility)
17
- - [TextDecoder](#textdecoder)
18
- - [Compatibility](#compatibility-1)
19
- - [Blob](#blob)
20
- - [Compatibility](#compatibility-2)
21
- - [File](#file)
22
- - [Compatibility](#compatibility-3)
23
- - [FileReader](#filereader)
24
- - [Compatibility](#compatibility-4)
25
- - [FormData](#formdata)
26
- - [Compatibility](#compatibility-5)
27
- - [URLSearchParams](#urlsearchparams)
28
- - [Compatibility](#compatibility-6)
29
- - [fetch](#fetch)
30
- - [Compatibility](#compatibility-7)
31
- - [Request](#request)
32
- - [Compatibility](#compatibility-8)
33
- - [Response](#response)
34
- - [Compatibility](#compatibility-9)
35
- - [Headers](#headers)
36
- - [Compatibility](#compatibility-10)
37
- - [AbortController](#abortcontroller)
38
- - [Compatibility](#compatibility-11)
39
- - [AbortSignal](#abortsignal)
40
- - [Compatibility](#compatibility-12)
41
- - [EventTarget](#eventtarget)
42
- - [Compatibility](#compatibility-13)
43
- - [XMLHttpRequest (mini-programs)](#xmlhttprequest-mini-programs)
44
- - [Compatibility](#compatibility-14)
45
- - [UniApp \& Taro](#uniapp--taro)
46
- - [Node.js](#nodejs)
47
- - [License](#license)
48
-
49
- ## Features
50
-
51
- - **TextEncoder**
52
- - **TextDecoder**
53
- - **Blob**
54
- - **File**
55
- - **FileReader**
56
- - **FormData**
57
- - **URLSearchParams**
58
- - **fetch**
59
- - **Headers**
60
- - **Request**
61
- - **Response**
62
- - **AbortController**
63
- - **AbortSignal**
64
- - **EventTarget**
65
- - **XMLHttpRequest (mini-programs)**
66
-
67
- ## Installation
68
-
69
- ```
70
- npm install mphttpx
71
- ```
72
-
73
- ## Mini-Program Support
74
-
75
- | WeChat | Alipay | Baidu | ByteDance | QQ | Kwai | JD | RedNote |
76
- |:------:|:------:|:-----:|:---------:|:--:|:----:|:--:|:-------:|
77
- | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ |
78
-
79
- Note: Modern browsers such as Chrome, Firefox, Edge, and Safari do not need these polyfills;
80
- the relevant implementations imported from the mphttpx library will directly return the native functions of the browser.
81
-
82
- ## Usage
83
-
84
- ### TextEncoder
85
-
86
- ```javascript
87
- import { TextEncoder } from "mphttpx";
88
-
89
- const encoder = new TextEncoder();
90
- const view = encoder.encode("€");
91
- console.log(view); // Uint8Array(3) [226, 130, 172]
92
- ```
93
-
94
- #### Compatibility
95
-
96
- | Property | Available | Description |
97
- | --------- | --------- | -------------|
98
- | encoding | ✔ | utf-8 |
99
-
100
- | Method | Available | Description |
101
- | ------- | --------- | -------------|
102
- | encode(string) | |
103
- | encodeInto(string, uint8Array) | |
104
-
105
- ### TextDecoder
106
-
107
- ```javascript
108
- import { TextDecoder } from "mphttpx";
109
-
110
- const utf8decoder = new TextDecoder(); // default 'utf-8'
111
- const encodedText = new Uint8Array([240, 160, 174, 183]);
112
-
113
- console.log(utf8decoder.decode(encodedText));
114
- ```
115
-
116
- #### Compatibility
117
-
118
- | Property | Available | Description |
119
- | --------- | --------- | -------------|
120
- | encoding | ✔ | utf-8 |
121
- | fatal | ✔ |
122
- | ignoreBOM | ✔ |
123
-
124
- | Method | Available | Description |
125
- | ------- | --------- | -------------|
126
- | decode() | ✔ |
127
- | decode(buffer) | ✔ |
128
- | decode(buffer, options) | ✔ |
129
-
130
- ### Blob
131
-
132
- Creating a blob
133
-
134
- ```javascript
135
- import { Blob } from "mphttpx";
136
-
137
- const obj = { hello: "world" };
138
- const blob = new Blob([JSON.stringify(obj, null, 2)], {
139
- type: "application/json",
140
- });
141
- ```
142
-
143
- Extracting data from a blob
144
-
145
- ```javascript
146
- import { Blob, FileReader } from "mphttpx";
147
-
148
- const reader = new FileReader();
149
- reader.addEventListener("loadend", () => {
150
- // reader.result contains the contents of blob as a typed array
151
- });
152
- reader.readAsArrayBuffer(blob);
153
- ```
154
-
155
- #### Compatibility
156
-
157
- | Property | Available | Description |
158
- | --------- | --------- | -------------|
159
- | size | ✔ |
160
- | type | |
161
-
162
- | Method | Available | Description |
163
- | ------- | --------- | -------------|
164
- | arrayBuffer() | ✔ |
165
- | bytes() | ✔ |
166
- | slice() | ✔ |
167
- | slice(start) | ✔ |
168
- | slice(start, end) | ✔ |
169
- | slice(start, end, contentType) | ✔ |
170
- | stream() | ✖ |
171
- | text() | ✔ |
172
-
173
- ### File
174
-
175
- ```javascript
176
- import { File } from "mphttpx";
177
-
178
- const file = new File(["foo"], "foo.txt", {
179
- type: "text/plain",
180
- });
181
- ```
182
-
183
- #### Compatibility
184
-
185
- | Property | Available | Description |
186
- | --------- | --------- | -------------|
187
- | lastModified | ✔ |
188
- | name | ✔ |
189
- | webkitRelativePath | ✖ |
190
-
191
- ### FileReader
192
-
193
- ```javascript
194
- import { File, FileReader } from "mphttpx";
195
-
196
- // Read the file
197
- const reader = new FileReader();
198
- reader.onload = () => {
199
- console.log(reader.result);
200
- };
201
- reader.readAsText(file);
202
- ```
203
-
204
- #### Compatibility
205
-
206
- | Property | Available | Description |
207
- | --------- | --------- | -------------|
208
- | error | ✔ |
209
- | readyState | ✔ |
210
- | result | ✔ |
211
-
212
- | Method | Available | Description |
213
- | ------- | --------- | -------------|
214
- | abort() | ✔ | simulated |
215
- | readAsArrayBuffer() | ✔ |
216
- | readAsBinaryString() | ✔ |
217
- | readAsDataURL() | ✔ |
218
- | readAsText() | ✔ |
219
-
220
- ### FormData
221
-
222
- ```javascript
223
- import { FormData } from "mphttpx";
224
-
225
- const formData = new FormData();
226
- formData.append("username", "Chris");
227
- ```
228
-
229
- #### Compatibility
230
-
231
- | Constructor | Available | Description |
232
- | ----------- | --------- | -------------|
233
- | new FormData() | ✔ |
234
- | new FormData(form) | |
235
- | new FormData(form, submitter) | |
236
-
237
- | Method | Available | Description |
238
- | ------- | --------- | -------------|
239
- | append(name, value) | ✔ |
240
- | append(name, value, filename) | ✔ |
241
- | delete(name) | ✔ |
242
- | entries() | ✔ |
243
- | get(name) | ✔ |
244
- | getAll(name) | ✔ |
245
- | has(name) | ✔ |
246
- | keys() | |
247
- | set(name, value) | ✔ |
248
- | set(name, value, filename) | ✔ |
249
- | values() | ✔ |
250
-
251
- ### URLSearchParams
252
-
253
- ```javascript
254
- import { URLSearchParams } from "mphttpx";
255
-
256
- const paramsString = "q=URLUtils.searchParams&topic=api";
257
- const searchParams = new URLSearchParams(paramsString);
258
-
259
- // Iterating the search parameters
260
- for (const p of searchParams) {
261
- console.log(p);
262
- }
263
-
264
- console.log(searchParams.has("topic")); // true
265
- console.log(searchParams.has("topic", "fish")); // false
266
- console.log(searchParams.get("topic") === "api"); // true
267
- console.log(searchParams.getAll("topic")); // ["api"]
268
- console.log(searchParams.get("foo") === null); // true
269
- console.log(searchParams.append("topic", "webdev"));
270
- console.log(searchParams.toString()); // "q=URLUtils.searchParams&topic=api&topic=webdev"
271
- console.log(searchParams.set("topic", "More webdev"));
272
- console.log(searchParams.toString()); // "q=URLUtils.searchParams&topic=More+webdev"
273
- console.log(searchParams.delete("topic"));
274
- console.log(searchParams.toString()); // "q=URLUtils.searchParams"
275
- ```
276
-
277
- Search parameters can also be an object.
278
-
279
- ```javascript
280
- import { URLSearchParams } from "mphttpx";
281
-
282
- const paramsObj = { foo: "bar", baz: "bar" };
283
- const searchParams = new URLSearchParams(paramsObj);
284
-
285
- console.log(searchParams.toString()); // "foo=bar&baz=bar"
286
- console.log(searchParams.has("foo")); // true
287
- console.log(searchParams.get("foo")); // "bar"
288
- ```
289
-
290
- #### Compatibility
291
-
292
- | Property | Available | Description |
293
- | --------- | --------- | -------------|
294
- | size | |
295
-
296
- | Method | Available | Description |
297
- | ------- | --------- | -------------|
298
- | append(name, value) | ✔ |
299
- | delete(name) | ✔ |
300
- | delete(name, value) | ✔ |
301
- | entries() | ✔ |
302
- | forEach(callback) | ✔ |
303
- | forEach(callback, thisArg) | ✔ |
304
- | get(name) | ✔ |
305
- | getAll(name) | ✔ |
306
- | has(name) | ✔ |
307
- | has(name, value) | |
308
- | keys() | ✔ |
309
- | set(name, value) | ✔ |
310
- | sort() | |
311
- | toString() | ✔ |
312
- | values() | |
313
-
314
- ### fetch
315
-
316
- ```javascript
317
- import { fetch } from "mphttpx";
318
-
319
- fetch("http://example.com/movies.json")
320
- .then((response) => response.json())
321
- .then((data) => console.log(data));
322
- ```
323
-
324
- Using fetch() to POST JSON data
325
-
326
- ```javascript
327
- import { fetch } from "mphttpx";
328
-
329
- const data = { username: "example" };
330
-
331
- fetch("https://example.com/profile", {
332
- method: "POST", // or 'PUT'
333
- headers: {
334
- "Content-Type": "application/json",
335
- },
336
- body: JSON.stringify(data),
337
- })
338
- .then((response) => response.json())
339
- .then((data) => {
340
- console.log("Success:", data);
341
- })
342
- .catch((error) => {
343
- console.error("Error:", error);
344
- });
345
- ```
346
-
347
- Uploading files
348
-
349
- ```javascript
350
- import { fetch, File } from "mphttpx";
351
-
352
- const formData = new FormData();
353
-
354
- formData.append("username", "abc123");
355
- formData.append("file", new File(["foo"], "foo.txt", { type: "text/plain" }));
356
-
357
- fetch("https://example.com/profile/avatar", {
358
- method: "PUT",
359
- body: formData,
360
- })
361
- .then((response) => response.json())
362
- .then((result) => {
363
- console.log("Success:", result);
364
- })
365
- .catch((error) => {
366
- console.error("Error:", error);
367
- });
368
- ```
369
-
370
- | Syntax | Available | Description |
371
- | ------- | --------- | -------------|
372
- | fetch(resource) | ✔ |
373
- | fetch(resource, options) | ✔ |
374
-
375
- #### Compatibility
376
-
377
- Refer to Request below.
378
-
379
- ### Request
380
-
381
- ```javascript
382
- import { fetch, Request } from "mphttpx";
383
-
384
- const request = new Request("https://www.mozilla.org/favicon.ico");
385
-
386
- const url = request.url;
387
- const method = request.method;
388
- const credentials = request.credentials;
389
-
390
- fetch(request)
391
- .then((response) => response.blob())
392
- .then((blob) => {
393
- console.log(blob);
394
- });
395
- ```
396
-
397
- ```javascript
398
- import { fetch, Request } from "mphttpx";
399
-
400
- const request = new Request("https://example.com", {
401
- method: "POST",
402
- body: '{"foo": "bar"}',
403
- });
404
-
405
- const url = request.url;
406
- const method = request.method;
407
- const credentials = request.credentials;
408
- const bodyUsed = request.bodyUsed;
409
-
410
- fetch(request)
411
- .then((response) => {
412
- if (response.status === 200) {
413
- return response.json();
414
- } else {
415
- throw new Error("Something went wrong on API server!");
416
- }
417
- })
418
- .then((response) => {
419
- console.debug(response);
420
- //
421
- })
422
- .catch((error) => {
423
- console.error(error);
424
- });
425
- ```
426
-
427
- #### Compatibility
428
-
429
- | Property | Available | Description |
430
- | --------- | --------- | -------------|
431
- | body | ✖ |
432
- | bodyUsed | |
433
- | cache | ✔ |
434
- | credentials | ✔ |
435
- | destination | |
436
- | headers | ✔ |
437
- | integrity | ✖ |
438
- | keepalive | ✖ |
439
- | method | |
440
- | mode | ✖ |
441
- | redirect | ✖ |
442
- | referrer | |
443
- | referrerPolicy | ✖ |
444
- | signal | |
445
- | url | ✔ |
446
-
447
- | Method | Available | Description |
448
- | ------- | --------- | -------------|
449
- | arrayBuffer() | ✔ |
450
- | blob() | ✔ |
451
- | bytes() | ✔ |
452
- | clone() | ✔ |
453
- | formData() | |
454
- | json() | |
455
- | text() | ✔ |
456
-
457
- ### Response
458
-
459
- ```javascript
460
- import { Response, Blob, fetch } from "mphttpx";
461
-
462
- const myBlob = new Blob();
463
- const myOptions = { status: 200, statusText: "SuperSmashingGreat!" };
464
- const myResponse = new Response(myBlob, myOptions);
465
- ```
466
-
467
- #### Compatibility
468
-
469
- | Property | Available | Description |
470
- | --------- | --------- | -------------|
471
- | body | ✖ |
472
- | bodyUsed | ✔ |
473
- | headers | ✔ |
474
- | ok | ✔ |
475
- | redirected | ✖ |
476
- | status | ✔ |
477
- | statusText | ✔ |
478
- | type | ✖ |
479
- | url | ✔ |
480
-
481
- | Method | Available | Description |
482
- | ------- | --------- | -------------|
483
- | arrayBuffer() | ✔ |
484
- | blob() | ✔ |
485
- | bytes() | |
486
- | clone() | ✔ |
487
- | formData() | ✔ |
488
- | json() | |
489
- | text() | |
490
-
491
- ### Headers
492
-
493
- ```javascript
494
- import { Headers } from "mphttpx";
495
-
496
- const myHeaders = new Headers();
497
-
498
- myHeaders.append("Content-Type", "text/plain");
499
- myHeaders.get("Content-Type"); // should return 'text/plain'
500
- ```
501
-
502
- The same can be achieved by passing an array of arrays or an object literal to the constructor:
503
-
504
- ```javascript
505
- import { Headers } from "mphttpx";
506
-
507
- let myHeaders = new Headers({
508
- "Content-Type": "text/plain",
509
- });
510
-
511
- // or, using an array of arrays:
512
- myHeaders = new Headers([["Content-Type", "text/plain"]]);
513
-
514
- myHeaders.get("Content-Type"); // should return 'text/plain'
515
- ```
516
-
517
- #### Compatibility
518
-
519
- | Method | Available | Description |
520
- | ------- | --------- | -------------|
521
- | append(name, value) | ✔ |
522
- | delete(name) | |
523
- | entries() | ✔ |
524
- | forEach(callbackFn) | |
525
- | forEach(callbackFn, thisArg) | ✔ |
526
- | get(name) | ✔ |
527
- | getSetCookie() | ✔ |
528
- | has(name) | ✔ |
529
- | keys() | |
530
- | set(name, value) | ✔ |
531
- | values() | |
532
-
533
- ### AbortController
534
-
535
- ```javascript
536
- import { AbortController } from "mphttpx";
537
-
538
- const controller = new AbortController();
539
- ```
540
-
541
- #### Compatibility
542
-
543
- | Property | Available | Description |
544
- | --------- | --------- | -------------|
545
- | signal | ✔ |
546
-
547
- | Method | Available | Description |
548
- | ------- | --------- | -------------|
549
- | abort() | ✔ |
550
- | abort(reason) | ✔ |
551
-
552
- ### AbortSignal
553
-
554
- ```javascript
555
- import { AbortController, AbortSignal, Request, fetch } from "mphttpx";
556
-
557
- async function get() {
558
- const controller = new AbortController();
559
- const request = new Request("https://example.org/get", {
560
- signal: controller.signal,
561
- });
562
-
563
- const response = await fetch(request);
564
- controller.abort();
565
- // The next line will throw `AbortError`
566
- const text = await response.text();
567
- console.log(text);
568
- }
569
- ```
570
-
571
- #### Compatibility
572
-
573
- | Property | Available | Description |
574
- | --------- | --------- | -------------|
575
- | aborted | |
576
- | reason | |
577
-
578
- | Method | Available | Description |
579
- | ------- | --------- | -------------|
580
- | throwIfAborted() | ✔ |
581
-
582
- ### EventTarget
583
-
584
- ```javascript
585
- import { EventTarget, Event, CustomEvent } from "mphttpx";
586
-
587
- const target = new EventTarget();
588
-
589
- target.addEventListener("foo", function (evt) {
590
- console.log(evt);
591
- });
592
-
593
- const evt = new Event("foo");
594
- target.dispatchEvent(evt);
595
-
596
- target.addEventListener("animalfound", function (evt) {
597
- console.log(evt.detail.name);
598
- });
599
-
600
- const catFound = new CustomEvent("animalfound", {
601
- detail: {
602
- name: "cat",
603
- },
604
- });
605
- target.dispatchEvent(catFound);
606
- ```
607
-
608
- #### Compatibility
609
-
610
- | Method | Available | Description |
611
- | ------- | --------- | -------------|
612
- | addEventListener(type, listener) | ✔ |
613
- | addEventListener(type, listener, options) | ✔ |
614
- | addEventListener(type, listener, useCapture) | ✔ |
615
- | dispatchEvent(event) | |
616
- | removeEventListener(type, listener) | |
617
- | removeEventListener(type, listener, options) | ✔ |
618
- | removeEventListener(type, listener, useCapture) | ✔ |
619
-
620
- ### XMLHttpRequest (mini-programs)
621
-
622
- GET
623
-
624
- ```javascript
625
- import { XMLHttpRequest } from "mphttpx";
626
-
627
- const xhr = new XMLHttpRequest();
628
- xhr.open("GET", "https://example.com/server?foo=bar&lorem=ipsum");
629
-
630
- xhr.onload = () => {
631
- // Request finished. Do processing here.
632
- };
633
-
634
- xhr.send();
635
- ```
636
-
637
- POST
638
-
639
- ```javascript
640
- import { XMLHttpRequest } from "mphttpx";
641
-
642
- const xhr = new XMLHttpRequest();
643
- xhr.open("POST", "https://example.com/server");
644
-
645
- // Send the proper header information along with the request
646
- xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
647
-
648
- xhr.onreadystatechange = () => {
649
- // Call a function when the state changes.
650
- if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
651
- // Request finished. Do processing here.
652
- }
653
- };
654
-
655
- xhr.send(JSON.stringify({ foo: "bar", lorem: "ipsum" }));
656
- ```
657
-
658
- #### Compatibility
659
-
660
- | Property | Available | Description |
661
- | -------- | --------- | -------------|
662
- | readyState | | 2, 3: simulated |
663
- | response | ✔ |
664
- | responseText | ✔ |
665
- | responseType | ✔ | The `"document"` is not supported |
666
- | responseURL | ✔ | The `responseURL` returns the URL used in the original request. |
667
- | responseXML | |
668
- | status | ✔ |
669
- | statusText | ✔ |
670
- | timeout | ✔ | This value must be less than the default timeout of mini-programs. |
671
- | upload | ✔ | simulated |
672
- | withCredentials | |
673
-
674
- | Method | Available | Description |
675
- | ------- | --------- | -------------|
676
- | abort() | ✔ |
677
- | getAllResponseHeaders() | ✔ |
678
- | getResponseHeader(headerName) | ✔ |
679
- | open(method, url) | ✔ |
680
- | open(method, url, async) | |
681
- | open(method, url, async, user) | ✔ |
682
- | open(method, url, async, user, password) | ✔ |
683
- | overrideMimeType(mimeType) | ✖ |
684
- | send() | ✔ |
685
- | send(body) | ✔ |
686
- | setRequestHeader(header, value) | ✔ |
687
-
688
- ## UniApp & Taro
689
-
690
- ```javascript
691
- import { setRequest } from "mphttpx";
692
-
693
- setRequest(uni.request);
694
- // setRequest(Taro.request);
695
- ```
696
-
697
- Note: When using in UniApp or Taro, if `fetch` or `XMLHttpRequest` fails to work, try explicitly setting the request function.
698
-
699
- ## Node.js
700
-
701
- ```bash
702
- npm install xhr2
703
- ```
704
-
705
- ```javascript
706
- import XMLHttpRequest from "xhr2";
707
- import { setXMLHttpRequest } from "mphttpx";
708
-
709
- setXMLHttpRequest(XMLHttpRequest);
710
- ```
711
-
712
- ## License
713
-
714
- MIT
715
-
716
- [0]: https://github.com/eligrey/Blob.js
1
+ # MPHTTPX
2
+
3
+ The `mphttpx` library aims to provide a more ES6-styled [Blob.js][0],
4
+ along with a [fetch][1] polyfill that works seamlessly with the Blob-polyfill.
5
+ This allows web code to be reused in other environments (such as mini-programs).
6
+
7
+ ## Table of Contents
8
+
9
+ - [MPHTTPX](#mphttpx)
10
+ - [Table of Contents](#table-of-contents)
11
+ - [Features](#features)
12
+ - [Installation](#installation)
13
+ - [Mini-Program Support](#mini-program-support)
14
+ - [Usage](#usage)
15
+ - [TextEncoder](#textencoder)
16
+ - [Example](#example)
17
+ - [Compatibility](#compatibility)
18
+ - [TextDecoder](#textdecoder)
19
+ - [Example](#example-1)
20
+ - [Compatibility](#compatibility-1)
21
+ - [Blob](#blob)
22
+ - [Example](#example-2)
23
+ - [Compatibility](#compatibility-2)
24
+ - [File](#file)
25
+ - [Example](#example-3)
26
+ - [Compatibility](#compatibility-3)
27
+ - [FileReader](#filereader)
28
+ - [Example](#example-4)
29
+ - [Compatibility](#compatibility-4)
30
+ - [URLSearchParams](#urlsearchparams)
31
+ - [Example](#example-5)
32
+ - [Compatibility](#compatibility-5)
33
+ - [FormData](#formdata)
34
+ - [Example](#example-6)
35
+ - [Compatibility](#compatibility-6)
36
+ - [fetch](#fetch)
37
+ - [Example](#example-7)
38
+ - [Compatibility](#compatibility-7)
39
+ - [Request](#request)
40
+ - [Example](#example-8)
41
+ - [Compatibility](#compatibility-8)
42
+ - [Response](#response)
43
+ - [Example](#example-9)
44
+ - [Compatibility](#compatibility-9)
45
+ - [Headers](#headers)
46
+ - [Example](#example-10)
47
+ - [Compatibility](#compatibility-10)
48
+ - [AbortController](#abortcontroller)
49
+ - [Example](#example-11)
50
+ - [Compatibility](#compatibility-11)
51
+ - [EventTarget](#eventtarget)
52
+ - [Example](#example-12)
53
+ - [Compatibility](#compatibility-12)
54
+ - [XMLHttpRequest (mini-programs)](#xmlhttprequest-mini-programs)
55
+ - [Example](#example-13)
56
+ - [Compatibility](#compatibility-13)
57
+ - [Auto Import](#auto-import)
58
+ - [UniApp \& Taro](#uniapp--taro)
59
+ - [Node.js](#nodejs)
60
+ - [License](#license)
61
+
62
+ ## Features
63
+
64
+ - **TextEncoder**
65
+ - **TextDecoder**
66
+ - **Blob**
67
+ - **File**
68
+ - **FileReader**
69
+ - **URLSearchParams**
70
+ - **FormData**
71
+ - **fetch**
72
+ - **Headers**
73
+ - **Request**
74
+ - **Response**
75
+ - **AbortController**
76
+ - **EventTarget**
77
+ - **XMLHttpRequest (mini-programs)**
78
+
79
+ ## Installation
80
+
81
+ ```
82
+ npm install mphttpx
83
+ ```
84
+
85
+ ## Mini-Program Support
86
+
87
+ | WeChat | Alipay | Baidu | ByteDance | QQ | Kwai | JD | RedNote |
88
+ |:------:|:------:|:-----:|:---------:|:--:|:----:|:--:|:-------:|
89
+ | Latest | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ |
90
+
91
+ Note: Modern browsers such as Chrome, Firefox, Edge, and Safari do not need these polyfills;
92
+ the relevant implementations imported from the mphttpx library will directly return the native functions of the browser.
93
+
94
+ ## Usage
95
+
96
+ Note: Every module in mphttpx has a corresponding version with the same name ending in the letter P.
97
+ The difference between them is that the versions ending in P are polyfill implementations.
98
+
99
+ For example:
100
+
101
+ ```javascript
102
+ // mphttpx does not modify globalThis in any way.
103
+ import { TextEncoder } from "mphttpx"; // returns the global object first; falls back to the polyfill if unavailable.
104
+ import { TextEncoderP } from "mphttpx"; // returns the polyfill directly.
105
+ ```
106
+
107
+ ### TextEncoder
108
+
109
+ #### Example
110
+
111
+ ```javascript
112
+ import { TextEncoder } from "mphttpx";
113
+
114
+ const encoder = new TextEncoder();
115
+ const encoded = encoder.encode("€");
116
+
117
+ console.log(encoded); // Uint8Array(3) [226, 130, 172]
118
+ ```
119
+
120
+ #### Compatibility
121
+
122
+ Properties
123
+
124
+ | Property | Available | Description |
125
+ | --------- | --------- | -------------|
126
+ | encoding | ✔ | utf-8 |
127
+
128
+ Methods
129
+
130
+ | Method | Available | Description |
131
+ | ------- | --------- | -------------|
132
+ | encode(string) | ✔ |
133
+ | encodeInto(string, uint8Array) | ✔ |
134
+
135
+ ### TextDecoder
136
+
137
+ #### Example
138
+
139
+ ```javascript
140
+ import { TextDecoder } from "mphttpx";
141
+
142
+ const utf8decoder = new TextDecoder(); // default 'utf-8'
143
+ const encodedText = new Uint8Array([240, 160, 174, 183]);
144
+
145
+ console.log(utf8decoder.decode(encodedText)); // 𠮷
146
+ ```
147
+
148
+ #### Compatibility
149
+
150
+ Properties
151
+
152
+ | Property | Available | Description |
153
+ | --------- | --------- | -------------|
154
+ | encoding | ✔ | only utf-8 |
155
+ | fatal | ✔ |
156
+ | ignoreBOM | ✔ |
157
+
158
+ Methods
159
+
160
+ | Method | Available | Description |
161
+ | ------- | --------- | -------------|
162
+ | decode() | |
163
+ | decode(buffer) | |
164
+ | decode(buffer, options) | ✔ |
165
+
166
+ ### Blob
167
+
168
+ #### Example
169
+
170
+ Creating a blob
171
+
172
+ ```javascript
173
+ import { Blob, fetch } from "mphttpx";
174
+
175
+ const obj = { hello: "world" };
176
+ const blob = new Blob([JSON.stringify(obj, null, 2)], {
177
+ type: "application/json",
178
+ });
179
+
180
+ const another_blob = new Blob(["Hello, World!"], {
181
+ type: "text/plain"
182
+ });
183
+
184
+ fetch("https://www.test.com/blob", {
185
+ method: "POST",
186
+ body: another_blob,
187
+ });
188
+ ```
189
+
190
+ Extracting data from a blob
191
+
192
+ ```javascript
193
+ import { Blob, FileReader, fetch } from "mphttpx";
194
+
195
+ const blob = new Blob([JSON.stringify({ foo: "bar" })], {
196
+ type: "application/json",
197
+ });
198
+
199
+ const reader = new FileReader();
200
+ reader.addEventListener("loadend", () => {
201
+ // reader.result contains the contents of blob as a typed array
202
+ });
203
+ reader.readAsArrayBuffer(blob);
204
+
205
+ fetch("https://www.test.com/blob", {
206
+ method: "POST",
207
+ body: blob,
208
+ })
209
+ .then(r => r.blob())
210
+ .then(r => {
211
+ const reader2 = new FileReader();
212
+ reader2.onload = () => {
213
+ // reader2.result
214
+ }
215
+ reader2.readAsDataURL(r); // base64
216
+ });
217
+ ```
218
+
219
+ #### Compatibility
220
+
221
+ Properties
222
+
223
+ | Property | Available | Description |
224
+ | --------- | --------- | -------------|
225
+ | size | |
226
+ | type | ✔ |
227
+
228
+ Methods
229
+
230
+ | Method | Available | Description |
231
+ | ------- | --------- | -------------|
232
+ | arrayBuffer() | |
233
+ | bytes() | ✔ |
234
+ | slice() | |
235
+ | slice(start) | |
236
+ | slice(start, end) | ✔ |
237
+ | slice(start, end, contentType) | |
238
+ | stream() | |
239
+ | text() | ✔ |
240
+
241
+ ### File
242
+
243
+ #### Example
244
+
245
+ ```javascript
246
+ import { File } from "mphttpx";
247
+
248
+ const file = new File(["foo"], "foo.txt", {
249
+ type: "text/plain",
250
+ });
251
+ ```
252
+
253
+ #### Compatibility
254
+
255
+ Properties
256
+
257
+ | Property | Available | Description |
258
+ | --------- | --------- | -------------|
259
+ | lastModified | |
260
+ | name | |
261
+ | webkitRelativePath | ✖ |
262
+
263
+ ### FileReader
264
+
265
+ #### Example
266
+
267
+ ```javascript
268
+ import { File, FileReader } from "mphttpx";
269
+
270
+ const file = new File([JSON.stringify({ foo: "bar" })], "test.json", {
271
+ type: "application/json",
272
+ });
273
+
274
+ // Read the file
275
+ const reader = new FileReader();
276
+ reader.onload = () => {
277
+ console.log(reader.result);
278
+ };
279
+ reader.readAsText(file);
280
+ ```
281
+
282
+ #### Compatibility
283
+
284
+ Properties
285
+
286
+ | Property | Available | Description |
287
+ | --------- | --------- | -------------|
288
+ | error | ✔ |
289
+ | readyState | ✔ |
290
+ | result | ✔ |
291
+
292
+ Methods
293
+
294
+ | Method | Available | Description |
295
+ | ------- | --------- | -------------|
296
+ | abort() | | simulated |
297
+ | readAsArrayBuffer() | |
298
+ | readAsBinaryString() | ✔ |
299
+ | readAsDataURL() | ✔ |
300
+ | readAsText() | ✔ | only utf-8 |
301
+
302
+ ### URLSearchParams
303
+
304
+ #### Example
305
+
306
+ ```javascript
307
+ import { URLSearchParams, fetch } from "mphttpx";
308
+
309
+ const paramsString = "q=URLUtils.searchParams&topic=api";
310
+ const searchParams = new URLSearchParams(paramsString);
311
+
312
+ // Iterating the search parameters
313
+ for (const p of searchParams) {
314
+ console.log(p);
315
+ }
316
+
317
+ console.log(searchParams.has("topic")); // true
318
+ console.log(searchParams.has("topic", "fish")); // false
319
+ console.log(searchParams.get("topic") === "api"); // true
320
+ console.log(searchParams.getAll("topic")); // ["api"]
321
+ console.log(searchParams.get("foo") === null); // true
322
+ console.log(searchParams.append("topic", "webdev"));
323
+ console.log(searchParams.toString()); // "q=URLUtils.searchParams&topic=api&topic=webdev"
324
+ console.log(searchParams.set("topic", "More webdev"));
325
+ console.log(searchParams.toString()); // "q=URLUtils.searchParams&topic=More+webdev"
326
+ console.log(searchParams.delete("topic"));
327
+ console.log(searchParams.toString()); // "q=URLUtils.searchParams"
328
+
329
+ // GET
330
+ fetch("https://www.test.com/get" + `?${searchParams.toString()}`);
331
+
332
+ // POST
333
+ fetch("https://www.test.com/post", {
334
+ method: "POST",
335
+ body: searchParams,
336
+ });
337
+ ```
338
+
339
+ Search parameters can also be an object.
340
+
341
+ ```javascript
342
+ import { URLSearchParams } from "mphttpx";
343
+
344
+ const paramsObj = { foo: "bar", baz: "bar" };
345
+ const searchParams = new URLSearchParams(paramsObj);
346
+
347
+ console.log(searchParams.toString()); // "foo=bar&baz=bar"
348
+ console.log(searchParams.has("foo")); // true
349
+ console.log(searchParams.get("foo")); // "bar"
350
+ ```
351
+
352
+ #### Compatibility
353
+
354
+ Properties
355
+
356
+ | Property | Available | Description |
357
+ | --------- | --------- | -------------|
358
+ | size | ✔ |
359
+
360
+ Methods
361
+
362
+ | Method | Available | Description |
363
+ | ------- | --------- | -------------|
364
+ | append(name, value) | ✔ |
365
+ | delete(name) | |
366
+ | delete(name, value) | ✔ |
367
+ | entries() | ✔ |
368
+ | forEach(callback) | ✔ |
369
+ | forEach(callback, thisArg) | ✔ |
370
+ | get(name) | |
371
+ | getAll(name) | |
372
+ | has(name) | ✔ |
373
+ | has(name, value) | ✔ |
374
+ | keys() | ✔ |
375
+ | set(name, value) | ✔ |
376
+ | sort() | ✔ |
377
+ | toString() | |
378
+ | values() | ✔ |
379
+
380
+ ### FormData
381
+
382
+ #### Example
383
+
384
+ ```javascript
385
+ import { FormData, fetch } from "mphttpx";
386
+
387
+ const formData = new FormData();
388
+ formData.append("username", "Chris");
389
+
390
+ const file = new File(["Hello, World!"], "file.txt", {
391
+ type: "text/plain",
392
+ });
393
+ formData.append("file", file);
394
+
395
+ fetch("https://www.test.com/formdata", {
396
+ method: "POST",
397
+ body: formData,
398
+ });
399
+ ```
400
+
401
+ #### Compatibility
402
+
403
+ Constructors
404
+
405
+ | Constructor | Available | Description |
406
+ | ----------- | --------- | -------------|
407
+ | new FormData() | ✔ |
408
+ | new FormData(form) | ✖ |
409
+ | new FormData(form, submitter) | ✖ |
410
+
411
+ Methods
412
+
413
+ | Method | Available | Description |
414
+ | ------- | --------- | -------------|
415
+ | append(name, value) | |
416
+ | append(name, value, filename) | ✔ |
417
+ | delete(name) | ✔ |
418
+ | entries() | |
419
+ | get(name) | ✔ |
420
+ | getAll(name) | ✔ |
421
+ | has(name) | ✔ |
422
+ | keys() | |
423
+ | set(name, value) | ✔ |
424
+ | set(name, value, filename) | ✔ |
425
+ | values() | ✔ |
426
+
427
+ ### fetch
428
+
429
+ #### Example
430
+
431
+ ```javascript
432
+ import { fetch } from "mphttpx";
433
+
434
+ fetch("http://example.com/movies.json")
435
+ .then((response) => response.json())
436
+ .then((data) => console.log(data));
437
+ ```
438
+
439
+ Using fetch() to POST JSON data
440
+
441
+ ```javascript
442
+ import { fetch } from "mphttpx";
443
+
444
+ const data = { username: "example" };
445
+
446
+ fetch("https://example.com/profile", {
447
+ method: "POST", // or 'PUT'
448
+ headers: {
449
+ "Content-Type": "application/json",
450
+ },
451
+ body: JSON.stringify(data),
452
+ })
453
+ .then((response) => response.json())
454
+ .then((data) => {
455
+ console.log("Success:", data);
456
+ })
457
+ .catch((error) => {
458
+ console.error("Error:", error);
459
+ });
460
+ ```
461
+
462
+ Uploading files
463
+
464
+ ```javascript
465
+ import { fetch, File } from "mphttpx";
466
+
467
+ const formData = new FormData();
468
+
469
+ formData.append("username", "abc123");
470
+ formData.append("file", new File(["foo"], "foo.txt", { type: "text/plain" }));
471
+
472
+ fetch("https://example.com/profile/avatar", {
473
+ method: "PUT",
474
+ body: formData,
475
+ })
476
+ .then((response) => response.json())
477
+ .then((result) => {
478
+ console.log("Success:", result);
479
+ })
480
+ .catch((error) => {
481
+ console.error("Error:", error);
482
+ });
483
+ ```
484
+
485
+ Note: The fetch method of mphttpx is implemented based on XMLHttpRequest, and this underlying implementation is replaceable.
486
+
487
+ ```javascript
488
+ import { setXMLHttpRequest } from "mphttpx";
489
+ setXMLHttpRequest(another_XMLHttpRequest); // custom XMLHttpRequest implementation
490
+ ```
491
+
492
+ | Syntax | Available | Description |
493
+ | ------- | --------- | -------------|
494
+ | fetch(resource) | |
495
+ | fetch(resource, options) | ✔ |
496
+
497
+ #### Compatibility
498
+
499
+ Refer to Request below.
500
+
501
+ ### Request
502
+
503
+ #### Example
504
+
505
+ ```javascript
506
+ import { fetch, Request } from "mphttpx";
507
+
508
+ const request = new Request("https://www.mozilla.org/favicon.ico");
509
+
510
+ const url = request.url;
511
+ const method = request.method;
512
+ const credentials = request.credentials;
513
+
514
+ fetch(request)
515
+ .then((response) => response.blob())
516
+ .then((blob) => {
517
+ console.log(blob);
518
+ });
519
+ ```
520
+
521
+ ```javascript
522
+ import { fetch, Request } from "mphttpx";
523
+
524
+ const request = new Request("https://example.com", {
525
+ method: "POST",
526
+ body: '{"foo": "bar"}',
527
+ });
528
+
529
+ const url = request.url;
530
+ const method = request.method;
531
+ const credentials = request.credentials;
532
+ const bodyUsed = request.bodyUsed;
533
+
534
+ fetch(request)
535
+ .then((response) => {
536
+ if (response.status === 200) {
537
+ return response.json();
538
+ } else {
539
+ throw new Error("Something went wrong on API server!");
540
+ }
541
+ })
542
+ .then((response) => {
543
+ console.debug(response);
544
+ //
545
+ })
546
+ .catch((error) => {
547
+ console.error(error);
548
+ });
549
+ ```
550
+
551
+ #### Compatibility
552
+
553
+ Properties
554
+
555
+ | Property | Available | Description |
556
+ | --------- | --------- | -------------|
557
+ | body | |
558
+ | bodyUsed | |
559
+ | cache | |
560
+ | credentials | ✔ |
561
+ | destination | ✖ |
562
+ | headers | ✔ |
563
+ | integrity | |
564
+ | keepalive | ✖ |
565
+ | method | |
566
+ | mode | |
567
+ | redirect | ✖ |
568
+ | referrer | ✖ |
569
+ | referrerPolicy | ✖ |
570
+ | signal | ✔ |
571
+ | url | ✔ |
572
+
573
+ Methods
574
+
575
+ | Method | Available | Description |
576
+ | ------- | --------- | -------------|
577
+ | arrayBuffer() | ✔ |
578
+ | blob() | |
579
+ | bytes() | |
580
+ | clone() | ✔ |
581
+ | formData() | ✔ |
582
+ | json() | ✔ |
583
+ | text() | ✔ |
584
+
585
+ ### Response
586
+
587
+ #### Example
588
+
589
+ ```javascript
590
+ import { Response, Blob, fetch } from "mphttpx";
591
+
592
+ const myBlob = new Blob();
593
+ const myOptions = { status: 200, statusText: "SuperSmashingGreat!" };
594
+ const myResponse = new Response(myBlob, myOptions);
595
+ ```
596
+
597
+ #### Compatibility
598
+
599
+ Properties
600
+
601
+ | Property | Available | Description |
602
+ | --------- | --------- | -------------|
603
+ | body | ✖ |
604
+ | bodyUsed | ✔ |
605
+ | headers | ✔ |
606
+ | ok | ✔ |
607
+ | redirected | ✖ |
608
+ | status | ✔ |
609
+ | statusText | ✔ |
610
+ | type | |
611
+ | url | |
612
+
613
+ Methods
614
+
615
+ | Method | Available | Description |
616
+ | ------- | --------- | -------------|
617
+ | arrayBuffer() | ✔ |
618
+ | blob() | ✔ |
619
+ | bytes() | ✔ |
620
+ | clone() | ✔ |
621
+ | formData() | ✔ |
622
+ | json() | ✔ |
623
+ | text() | ✔ |
624
+
625
+ ### Headers
626
+
627
+ #### Example
628
+
629
+ ```javascript
630
+ import { Headers, fetch } from "mphttpx";
631
+
632
+ const myHeaders = new Headers();
633
+
634
+ myHeaders.append("Content-Type", "text/plain");
635
+ myHeaders.get("Content-Type"); // should return 'text/plain'
636
+
637
+ fetch("https://www.test.com/headers", {
638
+ headers: myHeaders,
639
+ });
640
+ ```
641
+
642
+ The same can be achieved by passing an array of arrays or an object literal to the constructor:
643
+
644
+ ```javascript
645
+ import { Headers } from "mphttpx";
646
+
647
+ let myHeaders = new Headers({
648
+ "Content-Type": "text/plain",
649
+ });
650
+
651
+ // or, using an array of arrays:
652
+ myHeaders = new Headers([["Content-Type", "text/plain"]]);
653
+
654
+ myHeaders.get("Content-Type"); // should return 'text/plain'
655
+ ```
656
+
657
+ #### Compatibility
658
+
659
+ Methods
660
+
661
+ | Method | Available | Description |
662
+ | ------- | --------- | -------------|
663
+ | append(name, value) | ✔ |
664
+ | delete(name) | ✔ |
665
+ | entries() | ✔ |
666
+ | forEach(callbackFn) | ✔ |
667
+ | forEach(callbackFn, thisArg) | |
668
+ | get(name) | ✔ |
669
+ | getSetCookie() | ✔ |
670
+ | has(name) | ✔ |
671
+ | keys() | ✔ |
672
+ | set(name, value) | |
673
+ | values() | ✔ |
674
+
675
+ ### AbortController
676
+
677
+ #### Example
678
+
679
+ ```javascript
680
+ import { AbortController, fetch } from "mphttpx";
681
+
682
+ const controller = new AbortController();
683
+
684
+ fetch("https://www.test.com/abort", {
685
+ signal: controller.signal,
686
+ });
687
+ ```
688
+
689
+ ```javascript
690
+ import { AbortController, AbortSignal, Request, fetch } from "mphttpx";
691
+
692
+ async function get() {
693
+ const controller = new AbortController();
694
+ const request = new Request("https://example.org/get", {
695
+ signal: controller.signal,
696
+ });
697
+
698
+ const response = await fetch(request);
699
+ controller.abort();
700
+ // The next line will throw `AbortError`
701
+ const text = await response.text();
702
+ console.log(text);
703
+ }
704
+ ```
705
+
706
+ #### Compatibility
707
+
708
+ AbortController Properties
709
+
710
+ | Property | Available | Description |
711
+ | --------- | --------- | -------------|
712
+ | signal | ✔ |
713
+
714
+ AbortController Methods
715
+
716
+ | Method | Available | Description |
717
+ | ------- | --------- | -------------|
718
+ | abort() | ✔ |
719
+ | abort(reason) | ✔ |
720
+
721
+ AbortSignal Properties
722
+
723
+ | Property | Available | Description |
724
+ | --------- | --------- | -------------|
725
+ | aborted | ✔ |
726
+ | reason | ✔ |
727
+
728
+ AbortSignal Methods
729
+
730
+ | Method | Available | Description |
731
+ | ------- | --------- | -------------|
732
+ | throwIfAborted() | ✔ |
733
+
734
+ ### EventTarget
735
+
736
+ #### Example
737
+
738
+ ```javascript
739
+ import { EventTarget, Event, CustomEvent } from "mphttpx";
740
+
741
+ const target = new EventTarget();
742
+
743
+ target.addEventListener("foo", function (evt) {
744
+ console.log(evt);
745
+ });
746
+
747
+ const evt = new Event("foo");
748
+ target.dispatchEvent(evt);
749
+
750
+ target.addEventListener("animalfound", function (evt) {
751
+ console.log(evt.detail.name);
752
+ });
753
+
754
+ const catFound = new CustomEvent("animalfound", {
755
+ detail: {
756
+ name: "cat",
757
+ },
758
+ });
759
+ target.dispatchEvent(catFound);
760
+ ```
761
+
762
+ #### Compatibility
763
+
764
+ Methods
765
+
766
+ | Method | Available | Description |
767
+ | ------- | --------- | -------------|
768
+ | addEventListener(type, listener) | ✔ |
769
+ | addEventListener(type, listener, options) | ✔ |
770
+ | addEventListener(type, listener, useCapture) | ✔ |
771
+ | dispatchEvent(event) | ✔ |
772
+ | removeEventListener(type, listener) | ✔ |
773
+ | removeEventListener(type, listener, options) | ✔ |
774
+ | removeEventListener(type, listener, useCapture) | ✔ |
775
+
776
+ ### XMLHttpRequest (mini-programs)
777
+
778
+ #### Example
779
+
780
+ GET
781
+
782
+ ```javascript
783
+ import { XMLHttpRequest } from "mphttpx";
784
+
785
+ const xhr = new XMLHttpRequest();
786
+ xhr.open("GET", "https://example.com/server?foo=bar&lorem=ipsum");
787
+
788
+ xhr.onload = () => {
789
+ // Request finished. Do processing here.
790
+ };
791
+
792
+ xhr.send();
793
+ ```
794
+
795
+ POST
796
+
797
+ ```javascript
798
+ import { XMLHttpRequest } from "mphttpx";
799
+
800
+ const xhr = new XMLHttpRequest();
801
+ xhr.open("POST", "https://example.com/server");
802
+
803
+ // Send the proper header information along with the request
804
+ xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
805
+
806
+ xhr.onreadystatechange = () => {
807
+ // Call a function when the state changes.
808
+ if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
809
+ // Request finished. Do processing here.
810
+ }
811
+ };
812
+
813
+ xhr.send(JSON.stringify({ foo: "bar", lorem: "ipsum" }));
814
+ ```
815
+
816
+ #### Compatibility
817
+
818
+ Properties
819
+
820
+ | Property | Available | Description |
821
+ | -------- | --------- | -------------|
822
+ | readyState | ✔ | 2, 3: simulated |
823
+ | response | ✔ |
824
+ | responseText | ✔ |
825
+ | responseType | ✔ | The `"document"` is not supported |
826
+ | responseURL | ✔ | The `responseURL` returns the URL used in the original request. |
827
+ | responseXML | ✖ |
828
+ | status | ✔ |
829
+ | statusText | ✔ |
830
+ | timeout | ✔ | This value must be less than the default timeout of mini-programs. |
831
+ | upload | ✔ | simulated |
832
+ | withCredentials | ✖ |
833
+
834
+ Methods
835
+
836
+ | Method | Available | Description |
837
+ | ------- | --------- | -------------|
838
+ | abort() | ✔ |
839
+ | getAllResponseHeaders() | ✔ |
840
+ | getResponseHeader(headerName) | ✔ |
841
+ | open(method, url) | ✔ |
842
+ | open(method, url, async) | ✔ |
843
+ | open(method, url, async, user) | ✔ |
844
+ | open(method, url, async, user, password) | ✔ |
845
+ | overrideMimeType(mimeType) | ✖ |
846
+ | send() | ✔ |
847
+ | send(body) | ✔ |
848
+ | setRequestHeader(header, value) | ✔ |
849
+
850
+ ## Auto Import
851
+
852
+ See [unplugin-auto-import][2] for more details.
853
+
854
+ ```javascript
855
+ // for reference only
856
+ AutoImport({
857
+ // other configs
858
+
859
+ imports: [
860
+ // other imports
861
+
862
+ {
863
+ "mphttpx": [
864
+ "TextEncoder",
865
+ "TextDecoder",
866
+
867
+ "Blob",
868
+ "File",
869
+ "FileReader",
870
+
871
+ "URLSearchParams",
872
+ "FormData",
873
+
874
+ "fetch",
875
+ "Headers",
876
+ "Request",
877
+ "Response",
878
+
879
+ "AbortController",
880
+ "AbortSignal",
881
+
882
+ "EventTarget",
883
+ "Event",
884
+ "CustomEvent",
885
+
886
+ "XMLHttpRequest", // mini-programs
887
+ ],
888
+ },
889
+
890
+ // other imports
891
+ ],
892
+
893
+ // other configs
894
+ });
895
+ ```
896
+
897
+ Note for UniApp developers: If your project is a UniApp mini-program created via HBuilderX using the legacy Vue2 template,
898
+ try installing an older version of the unplugin-auto-import plugin that supports CMD, such as version 0.16.7.
899
+
900
+ ## UniApp & Taro
901
+
902
+ ```javascript
903
+ import { setRequest } from "mphttpx";
904
+
905
+ setRequest(uni.request);
906
+ // setRequest(Taro.request);
907
+ ```
908
+
909
+ Note: When using in UniApp or Taro, if `fetch` or `XMLHttpRequest` fails to work, try explicitly setting the request function.
910
+
911
+ ## Node.js
912
+
913
+ ```bash
914
+ npm install xhr2
915
+ ```
916
+
917
+ ```javascript
918
+ import XMLHttpRequest from "xhr2";
919
+ import { setXMLHttpRequest } from "mphttpx";
920
+
921
+ setXMLHttpRequest(XMLHttpRequest);
922
+ ```
923
+
924
+ ## License
925
+
926
+ MIT
927
+
928
+ [0]: https://github.com/eligrey/Blob.js
929
+ [1]: https://github.com/github/fetch
930
+ [2]: https://www.npmjs.com/package/unplugin-auto-import