@seayoo-web/kms 3.3.0 → 3.3.1

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,63 +1,77 @@
1
- const g = typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")();
1
+ const h = typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")();
2
2
  function v(r) {
3
3
  return typeof r == "function";
4
4
  }
5
- function C(r) {
5
+ function x(r) {
6
6
  return typeof r == "object" && r !== null;
7
7
  }
8
- function I() {
9
- return "localStorage" in g && C(g.localStorage) && "getItem" in g.localStorage && v(g.localStorage.getItem);
8
+ function C() {
9
+ return "localStorage" in h && x(h.localStorage) && "getItem" in h.localStorage && v(h.localStorage.getItem);
10
10
  }
11
- function P(r, e) {
11
+ function I(r, t) {
12
12
  try {
13
- const t = JSON.parse(r);
14
- return e ? e(t) ? t : null : t;
13
+ const e = JSON.parse(r);
14
+ return t ? t(e) ? e : null : e;
15
15
  } catch {
16
16
  return null;
17
17
  }
18
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}`;
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) => {
24
+ const b = n[m];
25
+ return Array.isArray(b) ? `${m}:${b.join(",")}` : `${m}:${b}`;
26
26
  }).join(w);
27
27
  }
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;
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;
29
29
  }
30
- function q(r) {
30
+ function D(r) {
31
31
  return r.replace(/(?:^https?:\/\/|\/*$)/gi, "").toLowerCase();
32
32
  }
33
- class L {
33
+ class N {
34
34
  req;
35
35
  enabledCache;
36
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 });
37
+ /**
38
+ * 设置请求的 endpoint 和请求工具来初始化 KMS Dsi,第三个可选参数 enabledCache 来激活本地缓存数据
39
+ *
40
+ * 当启用本地缓存数据时,会将成功返回的数据缓存到本地,并在后续 dsi 接口出现问题时以缓存数据返回
41
+ *
42
+ * 本地缓存有效期最长为 24 小时
43
+ */
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 });
40
47
  }
41
48
  /**
42
49
  * 查询 Dsi 数据接口并返回接口响应,可设置是否启用本地缓存以应对网络错误的情况
43
50
  */
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
+ 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}`);
51
58
  if (f) {
52
- const c = P(f, function(i) {
53
- return Array.isArray(i) && i.length === 2 && typeof i[0] == "number" && n(i[1]);
59
+ const c = I(f, function(s) {
60
+ return Array.isArray(s) && s.length === 2 && typeof s[0] == "number" && n(s[1]);
54
61
  });
55
- if (c && c[0] + 24 * 3600 * 1e3 > Date.now())
56
- return c[1];
62
+ if (c && J(e, c[1]) && c[0] + 24 * 3600 * 1e3 > Date.now())
63
+ return c[2];
57
64
  }
58
- return o;
65
+ return i;
59
66
  }
60
67
  }
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);
73
+ });
74
+ }
61
75
  export {
62
- L as KMSDsi
76
+ N as KMSDsi
63
77
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seayoo-web/kms",
3
- "version": "3.3.0",
3
+ "version": "3.3.1",
4
4
  "description": "agent for seayoo.kms",
5
5
  "type": "module",
6
6
  "source": "index.ts",
@@ -20,8 +20,8 @@
20
20
  },
21
21
  "devDependencies": {
22
22
  "@seayoo-web/request": "^3.4.0",
23
- "@seayoo-web/scripts": "^3.1.2",
24
23
  "@seayoo-web/utils": "4.1.3",
24
+ "@seayoo-web/scripts": "^3.1.2",
25
25
  "@seayoo-web/tsconfig": "^1.0.5"
26
26
  },
27
27
  "peerDependencies": {
@@ -4,7 +4,6 @@ type QuerySortBaseField = "start" | "end" | "created" | "order" | "updated" | `d
4
4
  type QuerySortField = QuerySortBaseField | `-${QuerySortBaseField}`;
5
5
  type QuerySortWithDefault = QuerySortField | `${QuerySortField}|${QuerySortField}`;
6
6
  type RecordRootField = "group" | "sn" | "start" | "end" | "order" | "data" | "created" | "updated";
7
- /** spell-checker:ignore Mddhh, Mddhhmm */
8
7
  export interface DsiQueryParam {
9
8
  /**
10
9
  * 指定获取某个配置,设定 sn 后,所有过滤以及分页参数均失效
@@ -149,20 +148,24 @@ export interface DsiQueryOption<T> {
149
148
  query: DsiQueryParam;
150
149
  /** 响应数据守卫函数 */
151
150
  guard: (data: unknown) => data is T;
152
- /** 可选更多配置 */
153
- option?: {
154
- /** 开启自动缓存配置,开启后将缓存 dsi 数据,并在 dsi 接口错误时以缓存数据作为返回 */
155
- cache?: boolean;
156
- };
151
+ /** 开启自动缓存配置,开启后将缓存 dsi 数据,并在 dsi 接口错误时以缓存数据作为返回 */
152
+ cache?: boolean;
157
153
  }
158
154
  export declare class KMSDsi {
159
155
  req: ReturnType<NetRequestFactory>;
160
156
  private enabledCache;
161
157
  private cachePrefix;
158
+ /**
159
+ * 设置请求的 endpoint 和请求工具来初始化 KMS Dsi,第三个可选参数 enabledCache 来激活本地缓存数据
160
+ *
161
+ * 当启用本地缓存数据时,会将成功返回的数据缓存到本地,并在后续 dsi 接口出现问题时以缓存数据返回
162
+ *
163
+ * 本地缓存有效期最长为 24 小时
164
+ */
162
165
  constructor(endpoint: string, request: NetRequestFactory, enabledCache?: boolean);
163
166
  /**
164
167
  * 查询 Dsi 数据接口并返回接口响应,可设置是否启用本地缓存以应对网络错误的情况
165
168
  */
166
- query<T = unknown>({ key, query, guard, option }: DsiQueryOption<T>): Promise<T | null>;
169
+ query<T = unknown>({ key, query, guard, cache }: DsiQueryOption<T>): Promise<T | null>;
167
170
  }
168
171
  export {};