befly-shared 2.7.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/utils/createHttp.js +43 -36
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "befly-shared",
3
- "version": "2.7.0",
3
+ "version": "2.10.0",
4
4
  "gitHead": "70c48ac058875255ac8b54c5f4b5987b997c3bfd",
5
5
  "private": false,
6
6
  "description": "Befly Shared - 通用工具函数库",
@@ -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 config = options && typeof options === "object" ? options : {};
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
- return async function requestPost(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 = Array.isArray(dropValues) ? dropValues : [null, undefined];
30
- const dropKeyMap = dropKeyValue && typeof dropKeyValue === "object" ? dropKeyValue : {};
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
- if (!shouldDropHttpValue(key, value, dropList, dropKeyMap)) {
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
- if (!shouldDropHttpValue(key, value, dropList, dropKeyMap)) {
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
- if (onReturn) {
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 ?? "请求失败",
@@ -96,4 +101,6 @@ export function createHttp(options) {
96
101
  return Promise.reject({ msg: "网络连接失败" });
97
102
  }
98
103
  };
104
+
105
+ return request;
99
106
  }