mphttpx 1.0.4 → 1.0.5

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/README.md CHANGED
@@ -1,35 +1,79 @@
1
- # A polyfill for mini-program.
1
+ # MPHTTPX
2
2
 
3
- ## install
4
- ```bash
3
+ The `request` function of Mini Programs does not support invocation in a Promise style
4
+ and differs significantly from the request style in browsers. This results in the
5
+ need to write two different sets of code logic for the same request in Mini Programs
6
+ versus browsers. Therefore, the `mphttpx` library aims to make requests in Mini Programs
7
+ as consistent as possible with those in browsers. This project implements a subset of
8
+ standard browser APIs, and these implemented features are sufficient to send requests
9
+ using the full `XMLHttpRequest` and `fetch` in Mini Programs.
10
+
11
+ ## Table of Contents
12
+
13
+ ## Installation
14
+
15
+ ```
5
16
  npm install mphttpx
6
17
  ```
7
18
 
8
- ## usage
19
+ ## Mini-Program Support
20
+
21
+ | WeChat | Alipay | Baidu | ByteDance | QQ | Kwai | JD | RedNote |
22
+ |:------:|:------:|:-----:|:---------:|:--:|:----:|:--:|:-------:|
23
+ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ | Latest ✔ |
24
+
25
+ ## Usage
26
+
27
+ ### XMLHttpRequest
28
+
29
+ #### Example: GET
30
+
31
+ ```javascript
32
+ import { XMLHttpRequest } from "mphttpx";
33
+
34
+ const xhr = new XMLHttpRequest();
35
+ xhr.open("GET", "https://example.com/server?foo=bar&lorem=ipsum");
36
+
37
+ xhr.onload = () => {
38
+ // Request finished. Do processing here.
39
+ };
40
+
41
+ xhr.send();
42
+ ```
43
+
44
+ #### Example: POST
45
+
9
46
  ```javascript
10
- import {
11
- TextEncoder,
12
- TextDecoder,
13
-
14
- Event,
15
- EventTarget,
16
- CustomEvent,
17
- ProgressEvent,
18
-
19
- Blob,
20
- File,
21
- FileReader,
22
-
23
- FormData,
24
- URLSearchParams,
25
-
26
- AbortSignal,
27
- AbortController,
28
-
29
- XMLHttpRequest,
30
- fetch,
31
- Headers,
32
- Request,
33
- Response
34
- } from "mphttpx";
47
+ import { XMLHttpRequest } from "mphttpx";
48
+
49
+ const xhr = new XMLHttpRequest();
50
+ xhr.open("POST", "https://example.com/server");
51
+
52
+ // Send the proper header information along with the request
53
+ xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
54
+
55
+ xhr.onreadystatechange = () => {
56
+ // Call a function when the state changes.
57
+ if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
58
+ // Request finished. Do processing here.
59
+ }
60
+ };
61
+
62
+ xhr.send(JSON.stringify({ foo: "bar", lorem: "ipsum" }));
35
63
  ```
64
+
65
+ #### Compatibility
66
+
67
+ | Property | Available | Description |
68
+ | -------- | --------- | -------------|
69
+ | readyState | ✔ | States 2: HEADERS_RECEIVED and 3: LOADING are simulated; in reality, the request has just completed at this point. |
70
+ | response | ✔ |
71
+ | responseText | ✔ |
72
+ | responseType | ✔ | The `"document"` is not supported, as the `Document` does not exist in mini-programs. |
73
+ | responseURL | ✔ | Since mini-programs cannot obtain the URL after redirection, the `responseURL` returns the URL used in the original request. |
74
+ | responseXML | ✖ |
75
+ | status | ✔ |
76
+ | statusText | ✔ |
77
+ | timeout | ✔ |
78
+ | upload | ✔ | The `upload` is simulated; in reality, regular requests in mini-programs cannot track the upload progress of the request body. |
79
+ | withCredentials | ✔ | Related to Cookies, setting `withCredentials` has no effect at present; support may be added in the future. |