@seayoo-web/kms 3.2.0 → 3.3.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,35 +1,63 @@
1
- function w(t) {
2
- const r = {}, { group: i, filter: e, filterLogic: c, range: u, created: p, updated: m, size: A, page: d, sort: o } = t, { sn: j, fields: a, dataFields: s, multi: b, combo: h, pure: g = "items", label: n, value: f, option: $ } = t;
3
- if (j && (r.sn = j), i && (r.g = Array.isArray(i) ? i.join(",") : i), e) {
4
- const v = c === "or" ? "|" : "+", q = Object.keys(e);
5
- r.filter = q.map((y) => {
6
- const l = e[y];
7
- return Array.isArray(l) ? `${y}:${l.join(",")}` : `${y}:${l}`;
8
- }).join(v);
1
+ const g = typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")();
2
+ function v(r) {
3
+ return typeof r == "function";
4
+ }
5
+ function C(r) {
6
+ return typeof r == "object" && r !== null;
7
+ }
8
+ function I() {
9
+ return "localStorage" in g && C(g.localStorage) && "getItem" in g.localStorage && v(g.localStorage.getItem);
10
+ }
11
+ function P(r, e) {
12
+ try {
13
+ const t = JSON.parse(r);
14
+ return e ? e(t) ? t : null : t;
15
+ } catch {
16
+ return null;
17
+ }
18
+ }
19
+ function D(r) {
20
+ const e = {}, { group: t, filter: n, filterLogic: a, range: o, created: s, updated: u, size: f, page: c, sort: i } = r, { sn: b, fields: l, dataFields: y, multi: S, combo: $, pure: d = "items", label: h, value: p, option: j } = r;
21
+ if (b && (e.sn = b), t && (e.g = Array.isArray(t) ? t.join(",") : t), n) {
22
+ const w = a === "or" ? "|" : "+", x = Object.keys(n);
23
+ e.filter = x.map((m) => {
24
+ const A = n[m];
25
+ return Array.isArray(A) ? `${m}:${A.join(",")}` : `${m}:${A}`;
26
+ }).join(w);
9
27
  }
10
- return u && (r.r = u), p && (r.created = p.join("-")), m && (r.updated = m.join("-")), o && (r.sort = Array.isArray(o) ? o.join(",") : o), b === !1 && (r.m = "n"), A && (r.s = `${A}`), d && (r.p = `${d}`), a && (r.f = Array.isArray(a) ? a.join(",") : a), s && (r.df = Array.isArray(s) ? s.join(",") : s), h === !0 && (a ? r.c = "y" : console.warn("设置 combo:true 必须要同步设置 fields")), g && (r.pure = g === "items" ? "items" : "y"), n && (r.label = Array.isArray(n) ? n.join(",") : n), f && (r.value = Array.isArray(f) ? f.join(",") : f), $ && (r.option = $), r._ = Math.random().toString(16).slice(-8), r;
28
+ return o && (e.r = o), s && (e.created = s.join("-")), u && (e.updated = u.join("-")), i && (e.sort = Array.isArray(i) ? i.join(",") : i), S === !1 && (e.m = "n"), f && (e.s = `${f}`), c && (e.p = `${c}`), l && (e.f = Array.isArray(l) ? l.join(",") : l), y && (e.df = Array.isArray(y) ? y.join(",") : y), $ === !0 && (l ? e.c = "y" : console.warn("设置 combo:true 必须要同步设置 fields")), d && (e.pure = d === "items" ? "items" : "y"), h && (e.label = Array.isArray(h) ? h.join(",") : h), p && (e.value = Array.isArray(p) ? p.join(",") : p), j && (e.option = j), e._ = Math.random().toString(16).slice(-8), e;
11
29
  }
12
- function L(t) {
13
- return t.replace(/(?:^https?:\/\/|\/*$)/gi, "").toLowerCase();
30
+ function q(r) {
31
+ return r.replace(/(?:^https?:\/\/|\/*$)/gi, "").toLowerCase();
14
32
  }
15
- class x {
33
+ class L {
16
34
  req;
17
- constructor(r, i) {
18
- this.req = i({
19
- baseURL: `https://${L(r)}`,
20
- timeout: 8e3,
21
- maxRetry: 3,
22
- retryInterval: 150
23
- });
35
+ enabledCache;
36
+ cachePrefix;
37
+ constructor(e, t, n = !1) {
38
+ const a = `https://${q(e)}`;
39
+ this.enabledCache = n, this.cachePrefix = a.replace(/^https:\/\//i, "").replace(/[./]/g, "-") + ":", this.req = t({ baseURL: a, timeout: 6e3, maxRetry: 2, retryInterval: 150 });
24
40
  }
25
41
  /**
26
- * 查询 Dsi 数据接口并返回接口响应
42
+ * 查询 Dsi 数据接口并返回接口响应,可设置是否启用本地缓存以应对网络错误的情况
27
43
  */
28
- async query({ key: r, query: i, guard: e }) {
29
- const { data: c } = await this.req.get(r, e, { params: w(i) });
30
- return c;
44
+ async query({ key: e, query: t, guard: n, option: a }) {
45
+ const { data: o } = await this.req.get(e, n, { params: D(t) }), s = (a?.cache ?? this.enabledCache) && I(), u = this.cachePrefix + e;
46
+ if (o !== null)
47
+ return s && localStorage.setItem(u, JSON.stringify([Date.now(), o])), o;
48
+ if (!s)
49
+ return o;
50
+ const f = localStorage.getItem(`${this.cachePrefix}${e}`);
51
+ if (f) {
52
+ const c = P(f, function(i) {
53
+ return Array.isArray(i) && i.length === 2 && typeof i[0] == "number" && n(i[1]);
54
+ });
55
+ if (c && c[0] + 24 * 3600 * 1e3 > Date.now())
56
+ return c[1];
57
+ }
58
+ return o;
31
59
  }
32
60
  }
33
61
  export {
34
- x as KMSDsi
62
+ L as KMSDsi
35
63
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seayoo-web/kms",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "agent for seayoo.kms",
5
5
  "type": "module",
6
6
  "source": "index.ts",
@@ -20,7 +20,8 @@
20
20
  },
21
21
  "devDependencies": {
22
22
  "@seayoo-web/request": "^3.4.0",
23
- "@seayoo-web/scripts": "^3.0.0",
23
+ "@seayoo-web/scripts": "^3.1.2",
24
+ "@seayoo-web/utils": "4.1.3",
24
25
  "@seayoo-web/tsconfig": "^1.0.5"
25
26
  },
26
27
  "peerDependencies": {
@@ -149,13 +149,20 @@ export interface DsiQueryOption<T> {
149
149
  query: DsiQueryParam;
150
150
  /** 响应数据守卫函数 */
151
151
  guard: (data: unknown) => data is T;
152
+ /** 可选更多配置 */
153
+ option?: {
154
+ /** 开启自动缓存配置,开启后将缓存 dsi 数据,并在 dsi 接口错误时以缓存数据作为返回 */
155
+ cache?: boolean;
156
+ };
152
157
  }
153
158
  export declare class KMSDsi {
154
159
  req: ReturnType<NetRequestFactory>;
155
- constructor(endpoint: string, request: NetRequestFactory);
160
+ private enabledCache;
161
+ private cachePrefix;
162
+ constructor(endpoint: string, request: NetRequestFactory, enabledCache?: boolean);
156
163
  /**
157
- * 查询 Dsi 数据接口并返回接口响应
164
+ * 查询 Dsi 数据接口并返回接口响应,可设置是否启用本地缓存以应对网络错误的情况
158
165
  */
159
- query<T = unknown>({ key, query, guard }: DsiQueryOption<T>): Promise<T | null>;
166
+ query<T = unknown>({ key, query, guard, option }: DsiQueryOption<T>): Promise<T | null>;
160
167
  }
161
168
  export {};