befly-shared 2.8.0 → 2.10.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/package.json +1 -1
- package/utils/createHttp.js +42 -44
package/package.json
CHANGED
package/utils/createHttp.js
CHANGED
|
@@ -1,40 +1,35 @@
|
|
|
1
|
-
function shouldDropHttpValue(key, value, dropList, dropKeyMap) {
|
|
2
|
-
for (const dropValue of dropList) {
|
|
3
|
-
if (Object.is(value, dropValue)) {
|
|
4
|
-
return true;
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
if (Array.isArray(dropKeyMap[key])) {
|
|
9
|
-
for (const dropValue of dropKeyMap[key]) {
|
|
10
|
-
if (Object.is(value, dropValue)) {
|
|
11
|
-
return true;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
1
|
export function createHttp(options) {
|
|
20
|
-
const
|
|
21
|
-
const apiPath = typeof config.apiPath === "string" ? config.apiPath : "";
|
|
22
|
-
const getToken = typeof config.getToken === "function" ? config.getToken : () => "";
|
|
23
|
-
const onReturn = typeof config.onReturn === "function" ? config.onReturn : null;
|
|
24
|
-
|
|
25
|
-
const request = async function (url, data, dropValues, dropKeyValue) {
|
|
2
|
+
const request = async function (url, data, dropValues = [], dropKeyValue = {}) {
|
|
26
3
|
try {
|
|
27
|
-
const fullUrl = /^https?:\/\//i.test(url) ? url : `${apiPath}${url}`;
|
|
4
|
+
const fullUrl = /^https?:\/\//i.test(url) ? url : `${options.apiPath}${url}`;
|
|
28
5
|
const isForm = data instanceof FormData;
|
|
29
|
-
const dropList =
|
|
30
|
-
const dropKeyMap =
|
|
6
|
+
const dropList = [null, undefined, ...dropValues];
|
|
7
|
+
const dropKeyMap = {
|
|
8
|
+
...dropKeyValue,
|
|
9
|
+
keyword: [""]
|
|
10
|
+
};
|
|
31
11
|
let payloadData = data ?? {};
|
|
32
12
|
if (isForm) {
|
|
33
13
|
const nextForm = new FormData();
|
|
34
14
|
for (const entry of payloadData.entries()) {
|
|
35
15
|
const key = entry[0];
|
|
36
16
|
const value = entry[1];
|
|
37
|
-
|
|
17
|
+
let shouldDrop = false;
|
|
18
|
+
for (const dropValue of dropList) {
|
|
19
|
+
if (Object.is(value, dropValue)) {
|
|
20
|
+
shouldDrop = true;
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (!shouldDrop) {
|
|
25
|
+
for (const dropValue of dropKeyMap[key] || []) {
|
|
26
|
+
if (Object.is(value, dropValue)) {
|
|
27
|
+
shouldDrop = true;
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (!shouldDrop) {
|
|
38
33
|
nextForm.append(key, value);
|
|
39
34
|
}
|
|
40
35
|
}
|
|
@@ -43,7 +38,22 @@ export function createHttp(options) {
|
|
|
43
38
|
const nextData = {};
|
|
44
39
|
for (const key of Object.keys(payloadData)) {
|
|
45
40
|
const value = payloadData[key];
|
|
46
|
-
|
|
41
|
+
let shouldDrop = false;
|
|
42
|
+
for (const dropValue of dropList) {
|
|
43
|
+
if (Object.is(value, dropValue)) {
|
|
44
|
+
shouldDrop = true;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (!shouldDrop) {
|
|
49
|
+
for (const dropValue of dropKeyMap[key] || []) {
|
|
50
|
+
if (Object.is(value, dropValue)) {
|
|
51
|
+
shouldDrop = true;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (!shouldDrop) {
|
|
47
57
|
nextData[key] = value;
|
|
48
58
|
}
|
|
49
59
|
}
|
|
@@ -53,7 +63,7 @@ export function createHttp(options) {
|
|
|
53
63
|
if (!isForm) {
|
|
54
64
|
headers.set("Content-Type", "application/json");
|
|
55
65
|
}
|
|
56
|
-
const tokenValue = getToken();
|
|
66
|
+
const tokenValue = options.getToken();
|
|
57
67
|
if (tokenValue) {
|
|
58
68
|
headers.set("Authorization", "Bearer " + tokenValue);
|
|
59
69
|
}
|
|
@@ -75,12 +85,7 @@ export function createHttp(options) {
|
|
|
75
85
|
msg: `请求失败:HTTP ${res.status}`
|
|
76
86
|
};
|
|
77
87
|
}
|
|
78
|
-
|
|
79
|
-
const result = await onReturn(payload);
|
|
80
|
-
if (result && typeof result === "object") {
|
|
81
|
-
payload = result;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
88
|
+
payload = await options.onReturn(payload);
|
|
84
89
|
if (payload?.code !== 0) {
|
|
85
90
|
return Promise.reject({
|
|
86
91
|
msg: payload.msg ?? "请求失败",
|
|
@@ -97,12 +102,5 @@ export function createHttp(options) {
|
|
|
97
102
|
}
|
|
98
103
|
};
|
|
99
104
|
|
|
100
|
-
return
|
|
101
|
-
get: function (url, data, dropValues, dropKeyValue) {
|
|
102
|
-
return request(url, data, Array.isArray(dropValues) ? [null, undefined, "", ...dropValues] : [null, undefined, ""], dropKeyValue);
|
|
103
|
-
},
|
|
104
|
-
post: function (url, data, dropValues, dropKeyValue) {
|
|
105
|
-
return request(url, data, Array.isArray(dropValues) ? [null, undefined, ...dropValues] : [null, undefined], dropKeyValue);
|
|
106
|
-
}
|
|
107
|
-
};
|
|
105
|
+
return request;
|
|
108
106
|
}
|