befly-shared 2.0.6 → 2.0.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/package.json +2 -2
- package/utils/createHttp.md +0 -2
- package/utils/createStore.js +66 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly-shared",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.7",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Befly Shared - 通用工具函数库",
|
|
6
6
|
"type": "module",
|
|
@@ -21,5 +21,5 @@
|
|
|
21
21
|
"engines": {
|
|
22
22
|
"bun": ">=1.3.0"
|
|
23
23
|
},
|
|
24
|
-
"gitHead": "
|
|
24
|
+
"gitHead": "c98b9286276d0f73a0b0894d21c10baa76db5343"
|
|
25
25
|
}
|
package/utils/createHttp.md
CHANGED
|
@@ -103,8 +103,6 @@ const res = await $Http("/core/upload/file", form);
|
|
|
103
103
|
const res = await $Http("/core/user/list", { page: 1, limit: 30, keyword: "", state: null }, [null, undefined, ""], { page: [0] });
|
|
104
104
|
```
|
|
105
105
|
|
|
106
|
-
---
|
|
107
|
-
|
|
108
106
|
## 行为细节
|
|
109
107
|
|
|
110
108
|
- 当 `data instanceof FormData` 时:
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
function resolveStorage(storage) {
|
|
2
|
+
if (!storage || typeof storage.getItem !== "function" || typeof storage.setItem !== "function") {
|
|
3
|
+
return null;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
return storage;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function serializeValue(value) {
|
|
10
|
+
if (typeof value === "string") {
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return JSON.stringify(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function parseValue(value, defaultValue) {
|
|
18
|
+
if (value === null) {
|
|
19
|
+
return defaultValue;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
return JSON.parse(value);
|
|
24
|
+
} catch {
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function createStorageApi(storage) {
|
|
30
|
+
return {
|
|
31
|
+
set(key, value) {
|
|
32
|
+
if (typeof key !== "string" || key.trim() === "") {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!storage) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
storage.setItem(key, serializeValue(value));
|
|
41
|
+
return true;
|
|
42
|
+
},
|
|
43
|
+
get(key, defaultValue = null) {
|
|
44
|
+
if (typeof key !== "string" || key.trim() === "") {
|
|
45
|
+
return defaultValue;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (!storage) {
|
|
49
|
+
return defaultValue;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return parseValue(storage.getItem(key), defaultValue);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function createStore(options) {
|
|
58
|
+
const config = options && typeof options === "object" ? options : {};
|
|
59
|
+
const localStorageRef = resolveStorage(config.localStorage ?? globalThis.localStorage);
|
|
60
|
+
const sessionStorageRef = resolveStorage(config.sessionStorage ?? globalThis.sessionStorage);
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
local: createStorageApi(localStorageRef),
|
|
64
|
+
session: createStorageApi(sessionStorageRef)
|
|
65
|
+
};
|
|
66
|
+
}
|