@seayoo-web/kms 3.3.1 → 3.4.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/dist/index.js CHANGED
@@ -1,39 +1,77 @@
1
- const h = typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")();
2
- function v(r) {
3
- return typeof r == "function";
1
+ const c = typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")();
2
+ function j(t) {
3
+ return typeof t == "function";
4
4
  }
5
- function x(r) {
6
- return typeof r == "object" && r !== null;
5
+ function x(t) {
6
+ return typeof t == "object" && t !== null;
7
7
  }
8
- function C() {
9
- return "localStorage" in h && x(h.localStorage) && "getItem" in h.localStorage && v(h.localStorage.getItem);
8
+ function S() {
9
+ return "wx" in c && x(c.wx) && "getSystemInfo" in c.wx && j(c.wx.getSystemInfo);
10
10
  }
11
- function I(r, t) {
11
+ function D() {
12
+ return "localStorage" in c && x(c.localStorage) && "getItem" in c.localStorage && j(c.localStorage.getItem);
13
+ }
14
+ function C(t, e) {
12
15
  try {
13
- const e = JSON.parse(r);
14
- return t ? t(e) ? e : null : e;
16
+ const r = JSON.parse(t);
17
+ return e ? e(r) ? r : null : r;
15
18
  } catch {
16
19
  return null;
17
20
  }
18
21
  }
19
- function P(r) {
20
- const t = {}, { group: e, filter: n, filterLogic: o, range: i, created: a, updated: u, size: f, page: c, sort: s } = r, { sn: d, fields: l, dataFields: p, multi: S, combo: $, pure: A = "items", label: y, value: g, option: j } = r;
21
- if (d && (t.sn = d), e && (t.g = Array.isArray(e) ? e.join(",") : e), n) {
22
- const w = o === "or" ? "|" : "+", O = Object.keys(n);
23
- t.filter = O.map((m) => {
22
+ const P = {
23
+ get(t) {
24
+ return localStorage.getItem(t) || "";
25
+ },
26
+ set(t, e) {
27
+ localStorage.setItem(t, e);
28
+ },
29
+ remove(t) {
30
+ localStorage.removeItem(t);
31
+ }
32
+ }, E = {
33
+ get(t) {
34
+ try {
35
+ if (S()) return wx.getStorageSync(t) || "";
36
+ } catch (e) {
37
+ console.error("getStorageSyncError", e);
38
+ }
39
+ return "";
40
+ },
41
+ set(t, e) {
42
+ try {
43
+ S() && wx.setStorageSync(t, e);
44
+ } catch (r) {
45
+ console.error("setStorageSyncError", r);
46
+ }
47
+ },
48
+ remove(t) {
49
+ try {
50
+ S() && wx.removeStorageSync(t);
51
+ } catch (e) {
52
+ console.error("removeStorageSync", e);
53
+ }
54
+ }
55
+ };
56
+ function J(t) {
57
+ const e = {}, { group: r, filter: n, filterLogic: i, range: o, created: s, updated: g, size: f, page: l, sort: a } = t, { sn: w, fields: u, dataFields: y, multi: v, combo: $, pure: A = "items", label: h, value: p, option: d } = t;
58
+ if (w && (e.sn = w), r && (e.g = Array.isArray(r) ? r.join(",") : r), n) {
59
+ const I = i === "or" ? "|" : "+", O = Object.keys(n);
60
+ e.filter = O.map((m) => {
24
61
  const b = n[m];
25
62
  return Array.isArray(b) ? `${m}:${b.join(",")}` : `${m}:${b}`;
26
- }).join(w);
63
+ }).join(I);
27
64
  }
28
- return i && (t.r = i), a && (t.created = a.join("-")), u && (t.updated = u.join("-")), s && (t.sort = Array.isArray(s) ? s.join(",") : s), S === !1 && (t.m = "n"), f && (t.s = `${f}`), c && (t.p = `${c}`), l && (t.f = Array.isArray(l) ? l.join(",") : l), p && (t.df = Array.isArray(p) ? p.join(",") : p), $ === !0 && (l ? t.c = "y" : console.warn("设置 combo:true 必须要同步设置 fields")), A && (t.pure = A === "items" ? "items" : "y"), y && (t.label = Array.isArray(y) ? y.join(",") : y), g && (t.value = Array.isArray(g) ? g.join(",") : g), j && (t.option = j), t._ = Math.random().toString(16).slice(-8), t;
65
+ return o && (e.r = o), s && (e.created = s.join("-")), g && (e.updated = g.join("-")), a && (e.sort = Array.isArray(a) ? a.join(",") : a), v === !1 && (e.m = "n"), f && (e.s = `${f}`), l && (e.p = `${l}`), u && (e.f = Array.isArray(u) ? u.join(",") : u), y && (e.df = Array.isArray(y) ? y.join(",") : y), $ === !0 && (u ? e.c = "y" : console.warn("设置 combo:true 必须要同步设置 fields")), A && (e.pure = A === "items" ? "items" : "y"), h && (e.label = Array.isArray(h) ? h.join(",") : h), p && (e.value = Array.isArray(p) ? p.join(",") : p), d && (e.option = d), e._ = Math.random().toString(16).slice(-8), e;
29
66
  }
30
- function D(r) {
31
- return r.replace(/(?:^https?:\/\/|\/*$)/gi, "").toLowerCase();
67
+ function N(t) {
68
+ return t.replace(/(?:^https?:\/\/|\/*$)/gi, "").toLowerCase();
32
69
  }
33
- class N {
70
+ class L {
34
71
  req;
35
72
  enabledCache;
36
73
  cachePrefix;
74
+ storage;
37
75
  /**
38
76
  * 设置请求的 endpoint 和请求工具来初始化 KMS Dsi,第三个可选参数 enabledCache 来激活本地缓存数据
39
77
  *
@@ -41,37 +79,37 @@ class N {
41
79
  *
42
80
  * 本地缓存有效期最长为 24 小时
43
81
  */
44
- constructor(t, e, n = !1) {
45
- const o = `https://${D(t)}`;
46
- this.enabledCache = n, this.cachePrefix = o.replace(/^https:\/\//i, "").replace(/[./]/g, "-") + ":", this.req = e({ baseURL: o, timeout: 6e3, maxRetry: 2, retryInterval: 150 });
82
+ constructor(e, r, n = !1) {
83
+ const i = `https://${N(e)}`;
84
+ this.storage = S() ? E : D() ? P : null, this.enabledCache = n, this.storage === null && n && console.warn("[KMSDsi] 缓存功能需要本地存储支持"), this.cachePrefix = i.replace(/^https:\/\//i, "").replace(/[./]/g, "-") + ":", this.req = r({ baseURL: i, timeout: 6e3, maxRetry: 2, retryInterval: 150 });
47
85
  }
48
86
  /**
49
87
  * 查询 Dsi 数据接口并返回接口响应,可设置是否启用本地缓存以应对网络错误的情况
50
88
  */
51
- async query({ key: t, query: e, guard: n, cache: o }) {
52
- const { data: i } = await this.req.get(t, n, { params: P(e) }), a = (o ?? this.enabledCache) && C(), u = this.cachePrefix + t + (e.page ? `:p${e.page}` : "");
53
- if (i !== null)
54
- return a && localStorage.setItem(u, JSON.stringify([Date.now(), e, i])), i;
55
- if (!a)
56
- return i;
57
- const f = localStorage.getItem(`${this.cachePrefix}${t}`);
89
+ async query({ key: e, query: r, guard: n, cache: i }) {
90
+ const { data: o } = await this.req.get(e, n, { params: J(r) }), s = i ?? this.enabledCache, g = this.cachePrefix + e + (r.page ? `:p${r.page}` : "");
91
+ if (o !== null)
92
+ return s && this.storage?.set(g, JSON.stringify([Date.now(), r, o])), o;
93
+ if (!s)
94
+ return o;
95
+ const f = this.storage?.get(`${this.cachePrefix}${e}`);
58
96
  if (f) {
59
- const c = I(f, function(s) {
60
- return Array.isArray(s) && s.length === 2 && typeof s[0] == "number" && n(s[1]);
97
+ const l = C(f, function(a) {
98
+ return Array.isArray(a) && a.length === 3 && typeof a[0] == "number" && n(a[1]);
61
99
  });
62
- if (c && J(e, c[1]) && c[0] + 24 * 3600 * 1e3 > Date.now())
63
- return c[2];
100
+ if (l && K(r, l[1]) && l[0] + 24 * 3600 * 1e3 > Date.now())
101
+ return l[2];
64
102
  }
65
- return i;
103
+ return o;
66
104
  }
67
105
  }
68
- function J(r, t) {
69
- const e = Object.keys(r);
70
- return e.length === Object.keys(t).length && e.every((n) => {
71
- const o = r[n], i = t[n], a = typeof o;
72
- return a === "string" || a === "number" || a === "boolean" || a === "bigint" ? o === i : JSON.stringify(o) === JSON.stringify(i);
106
+ function K(t, e) {
107
+ const r = Object.keys(t);
108
+ return r.length === Object.keys(e).length && r.every((n) => {
109
+ const i = t[n], o = e[n], s = typeof i;
110
+ return s === "string" || s === "number" || s === "boolean" || s === "bigint" ? i === o : JSON.stringify(i) === JSON.stringify(o);
73
111
  });
74
112
  }
75
113
  export {
76
- N as KMSDsi
114
+ L as KMSDsi
77
115
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seayoo-web/kms",
3
- "version": "3.3.1",
3
+ "version": "3.4.0",
4
4
  "description": "agent for seayoo.kms",
5
5
  "type": "module",
6
6
  "source": "index.ts",
@@ -19,13 +19,13 @@
19
19
  "access": "public"
20
20
  },
21
21
  "devDependencies": {
22
- "@seayoo-web/request": "^3.4.0",
23
- "@seayoo-web/utils": "4.1.3",
24
- "@seayoo-web/scripts": "^3.1.2",
22
+ "@seayoo-web/request": "^3.4.2",
23
+ "@seayoo-web/utils": "4.2.0",
24
+ "@seayoo-web/scripts": "^3.1.7",
25
25
  "@seayoo-web/tsconfig": "^1.0.5"
26
26
  },
27
27
  "peerDependencies": {
28
- "@seayoo-web/request": "^3.4.0"
28
+ "@seayoo-web/request": "^3.4.2"
29
29
  },
30
30
  "scripts": {
31
31
  "prebuild": "pnpm --F request build",
@@ -68,6 +68,8 @@ export interface DsiQueryParam {
68
68
  sort?: QuerySortWithDefault | QuerySortWithDefault[];
69
69
  /**
70
70
  * 是否返回所有符合要求的数据记录合集,默认 true
71
+ *
72
+ * 设置为 false 时,仅仅返回第一个符合要求的记录,如果没有任何符合要求的记录响应为 null
71
73
  */
72
74
  multi?: boolean;
73
75
  /**
@@ -155,6 +157,7 @@ export declare class KMSDsi {
155
157
  req: ReturnType<NetRequestFactory>;
156
158
  private enabledCache;
157
159
  private cachePrefix;
160
+ private storage;
158
161
  /**
159
162
  * 设置请求的 endpoint 和请求工具来初始化 KMS Dsi,第三个可选参数 enabledCache 来激活本地缓存数据
160
163
  *