@seayoo-web/kms 1.0.1 → 2.0.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/README.md CHANGED
@@ -1,5 +1,7 @@
1
- # kms agent
1
+ # KMS Agent
2
2
 
3
- ! internal experimental project
4
-
5
- ! 内部工具,勿作他用
3
+ - dsi 查询工具
4
+ - `queryDsi` 以 ajax 方式查询 dsi 数据
5
+ - `jsonpDsi` 以 jsonp 方式查询 dsi 数据
6
+ - `jsonxDsi` 以 script 方式查询 dsi 数据
7
+ - `convertDsiParam` 工具函数,将 dsi query 参数转成 request param
package/dist/index.js ADDED
@@ -0,0 +1,32 @@
1
+ import { NetRequest as L, jsonp as k, jsonx as q } from "@seayoo-web/request";
2
+ const { get: z } = L({ timeout: 15e3 });
3
+ function y(t) {
4
+ const r = {}, { group: i, filter: a, filterLogic: n, range: l, created: d, updated: m, size: A, page: $, sort: s } = t, { sn: g, fields: o, dataFields: e, multi: h, combo: v, pure: b = "items", label: f, value: c, option: w } = t;
5
+ if (g && (r.sn = g), i && (r.g = Array.isArray(i) ? i.join(",") : i), a) {
6
+ const D = n === "or" ? "|" : "+", x = Object.keys(a);
7
+ r.filter = x.map((p) => {
8
+ const u = a[p];
9
+ return Array.isArray(u) ? `${p}:${u.join(",")}` : `${p}:${u}`;
10
+ }).join(D);
11
+ }
12
+ return l && (r.r = l), d && (r.created = d.join("-")), m && (r.updated = m.join("-")), s && (r.sort = Array.isArray(s) ? s.join(",") : s), h === !1 && (r.m = "n"), A && (r.s = `${A}`), $ && (r.p = `${$}`), o && (r.f = Array.isArray(o) ? o.join(",") : o), e && (r.df = Array.isArray(e) ? e.join(",") : e), v === !0 && (o ? r.c = "y" : console.warn("设置 combo:true 必须要同步设置 fields")), b && (r.pure = b === "items" ? "items" : "y"), f && (r.label = Array.isArray(f) ? f.join(",") : f), c && (r.value = Array.isArray(c) ? c.join(",") : c), w && (r.option = w), r;
13
+ }
14
+ async function E({ endpoint: t, key: r, query: i, guard: a }) {
15
+ const { data: n } = await z(`https://${j(t)}/${r}`, a, { params: y(i) });
16
+ return n;
17
+ }
18
+ async function F({ endpoint: t, key: r, query: i, guard: a }) {
19
+ return await k(`https://${j(t)}/${r}/jsonp`, a, y(i));
20
+ }
21
+ async function N({ endpoint: t, key: r, query: i, guard: a }) {
22
+ return await q(`https://${j(t)}/${r}/script`, a, y(i));
23
+ }
24
+ function j(t) {
25
+ return t.replace(/(?:^https?:\/\/|\/*$)/gi, "").toLowerCase();
26
+ }
27
+ export {
28
+ y as convertDsiParam,
29
+ F as jsonpDsi,
30
+ N as jsonxDsi,
31
+ E as queryDsi
32
+ };
package/package.json CHANGED
@@ -1,23 +1,38 @@
1
1
  {
2
2
  "name": "@seayoo-web/kms",
3
- "version": "1.0.1",
4
- "description": "agent for kms",
5
- "main": "index.js",
6
- "scripts": {
7
- "minify": "node minify",
8
- "pubup": "node minify && npm publish"
3
+ "version": "2.0.1",
4
+ "description": "agent for seayoo.kms",
5
+ "type": "module",
6
+ "source": "index.ts",
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.js",
9
+ "types": "./types/index.d.ts",
10
+ "files": [
11
+ "dist",
12
+ "types",
13
+ "README.md"
14
+ ],
15
+ "exports": {
16
+ ".": {
17
+ "types": "./types/index.d.ts",
18
+ "import": "./dist/index.js",
19
+ "default": "./dist/index.js"
20
+ }
9
21
  },
10
22
  "author": "web@seayoo.com",
11
23
  "license": "MIT",
12
24
  "publishConfig": {
13
25
  "access": "public"
14
26
  },
15
- "dependencies": {
16
- "needle": "^3.1.0",
17
- "colors": "^1.4.0",
18
- "open": "^8.4.0"
19
- },
20
27
  "devDependencies": {
21
- "terser": "^5.12.1"
28
+ "@seayoo-web/request": "^2.0.6",
29
+ "@seayoo-web/tsconfig": "^1.0.2"
30
+ },
31
+ "peerDependencies": {
32
+ "@seayoo-web/request": "^2.0.6"
33
+ },
34
+ "scripts": {
35
+ "build": "vite build && tsc --emitDeclarationOnly",
36
+ "prepublish": "pnpm build"
22
37
  }
23
- }
38
+ }
@@ -0,0 +1 @@
1
+ export * from "./src/dsi";
@@ -0,0 +1,165 @@
1
+ type BaseFieldType = string | number | null;
2
+ type QuerySortBaseField = "start" | "end" | "created" | "order" | "updated" | `data.${string}`;
3
+ type QuerySortField = QuerySortBaseField | `-${QuerySortBaseField}`;
4
+ type QuerySortWithDefault = QuerySortField | `${QuerySortField}|${QuerySortField}`;
5
+ type RecordRootField = "group" | "sn" | "start" | "end" | "order" | "data" | "created" | "updated";
6
+ export interface DsiQueryParam {
7
+ /**
8
+ * 指定获取某个配置,设定 sn 后,所有过滤以及分页参数均失效
9
+ *
10
+ * 此时不考虑配置是否过期,一律返回该配置
11
+ */
12
+ sn?: string;
13
+ /**
14
+ * 指定获取某个或多个分组的数据
15
+ */
16
+ group?: string | string[];
17
+ /**
18
+ * 过滤 data 字段根节点的数据
19
+ */
20
+ filter?: Record<string, BaseFieldType | BaseFieldType[]>;
21
+ /**
22
+ * 如果设置了 filter 则限制 filter 条件的逻辑组合关系,默认是 and
23
+ */
24
+ filterLogic?: "or" | "and";
25
+ /**
26
+ * 可选有效期过滤参数,对配置的有效期进行过滤,可选值有五个:
27
+ *
28
+ * - left 过滤所有已经失效的配置
29
+ * - center 过滤所有生效中的配置(默认值)
30
+ * - right 过滤所有尚未生效的配置
31
+ * - forward 过滤所有生效中以及待生效的配置
32
+ * - all 返回所有配置
33
+ */
34
+ range?: "left" | "center" | "right" | "forward" | "all";
35
+ /**
36
+ * 表示过滤出 created 为指定时间范围内的记录,时间格式支持:
37
+ *
38
+ * yyyy / yyyyMM / yyyyMMdd / yyyyMMddhh / yyyyMMddhhmm / yyyyMMddhhmmss
39
+ *
40
+ * - 其中起始时间不足 14 位的情况下,将以 1月1日0点0分0秒进行补足;
41
+ * - 结束时间不足 14 位的情况下,将以 12月31日23点59分59秒进行补足;
42
+ * - 起始时间和结束时间可以忽略其中一个(设置为空字符串),表示开放时间段进行选择;
43
+ * - 时间格式为双闭区间,即 >= 起始时间 且 <= 结束时间的记录都会被选中;
44
+ */
45
+ created?: [string, string];
46
+ /**
47
+ * 表示过滤出 updated 为指定时间范围内的记录,时间格式支持:
48
+ *
49
+ * yyyy / yyyyMM / yyyyMMdd / yyyyMMddhh / yyyyMMddhhmm / yyyyMMddhhmmss
50
+ *
51
+ * - 其中起始时间不足 14 位的情况下,将以 1月1日0点0分0秒进行补足;
52
+ * - 结束时间不足 14 位的情况下,将以 12月31日23点59分59秒进行补足;
53
+ * - 起始时间和结束时间可以忽略其中一个(设置为空字符串),表示开放时间段进行选择;
54
+ * - 当记录的 updated 属性无值时,以 created 作为默认值;
55
+ * - 时间格式为双闭区间,即 >= 起始时间 且 <= 结束时间的记录都会被选中;
56
+ */
57
+ updated?: [string, string];
58
+ /**
59
+ * 用于对集合数据指定排序方法,默认是 sort=order 即按照 order 字段从大到小排序;
60
+ *
61
+ * - 如果是从小到大排序,需要在参数值前加一个中划线;
62
+ * - 字段必须是数字类型,如果类型不符,排序出的结果将可能变得不可靠,并会影响分页的准确性;
63
+ * - 如果字段为非真值,则转化为数字 0 参与排序;
64
+ * - sort支持多个字段排序,按照数组顺序从左到右依次排序;
65
+ * - 其中每个排序字段可以设置多个默认值字段(以竖线分割)
66
+ */
67
+ sort?: QuerySortWithDefault | QuerySortWithDefault[];
68
+ /**
69
+ * 是否返回所有符合要求的数据记录合集,默认 true
70
+ */
71
+ multi?: boolean;
72
+ /**
73
+ * 返回单页数据记录的最大数量,默认返回所有符合记录的数据。
74
+ *
75
+ * 可设置的最大值 100
76
+ */
77
+ size?: number;
78
+ /**
79
+ * 返回数据记录指定页数的内容,默认第一页,最大值 10000。
80
+ *
81
+ * 如果超过最后一页,则返回最后一页。
82
+ */
83
+ page?: number;
84
+ /**
85
+ * 过滤数据根节点的字段,默认不过滤
86
+ */
87
+ fields?: RecordRootField | RecordRootField[];
88
+ /**
89
+ * 过滤 data 字段根节点的字段,区分大小写,默认不过滤
90
+ */
91
+ dataFields?: string | string[];
92
+ /**
93
+ * 是否将 fields 参数指定的根节点数据全部合并到一起。
94
+ *
95
+ * - 其中系统自带的字段优先级高于 data 根节点字段,相同字段内容会被覆盖;
96
+ * - 为了防止 key 误覆盖,必须显式设置 fields 参数后方才生效;
97
+ */
98
+ combo?: boolean;
99
+ /**
100
+ * 设置数据整理的方式,当设置为 true 时仅仅对 ajax 模式有效,items 对所有模式有效
101
+ *
102
+ * - true 将直接返回数据本身,而不带有 { err, data } 包装结构
103
+ * - false 默认行为,ajax 返回会带有 { err, data } 包装结构
104
+ * - "items" 在 pure=true 的基础上进一步精简,删除 pagination 信息,直接返回 items 数组。如果同时设置了 sn 或者 multi=false 则效果等同于 pure=true
105
+ */
106
+ pure?: boolean | "items";
107
+ /**
108
+ * 设定有效值后,格式化输出数据为 [{ label, value, key }] 形式,方便前端选择组件直接使用。
109
+ *
110
+ * - label 值为 data 字段的属性名,区分大小写,支持链式取值,比如 a.b.c;
111
+ * - value 值为 data 字段的属性名,区分大小写,支持链式取值,默认是该条记录的 sn;
112
+ * - 返回数据中的 key 固定为记录的 sn;
113
+ *
114
+ * - 任何数据错误都不会报错,而是返回空数组 [];
115
+ * - 设置了 label / value 后,除了 group / sn / filter 参数外,其他用于过滤数据的参数均无效;
116
+ * - 设置多个字段后,比如 label=["name","desc"] 此时返回的内容,会是 data.name 和 data.desc 拼接成的字符串,并用空格连接;
117
+ */
118
+ label?: string | string[];
119
+ /**
120
+ * 设定有效值后,格式化输出数据为 [{ label, value, key }] 形式,方便前端选择组件直接使用。
121
+ *
122
+ * - label 值为 data 字段的属性名,区分大小写,支持链式取值,比如 a.b.c;
123
+ * - value 值为 data 字段的属性名,区分大小写,支持链式取值,默认是该条记录的 sn;
124
+ * - 返回数据中的 key 固定为记录的 sn;
125
+ *
126
+ * - 任何数据错误都不会报错,而是返回空数组 [];
127
+ * - 设置了 label / value 后,除了 group / sn / filter 参数外,其他用于过滤数据的参数均无效;
128
+ * - 设置多个字段后,比如 value=["id","name"] 此时返回的数据,会多出一个 payload 字段,内容为 { id, name }
129
+ * - 此时会强制 value 和 key 均为 该条记录的 sn;
130
+ * - 这种特殊结构的选项数据在 KMS 中直接受到支持,并将 payload 作为实际的选项值;
131
+ * - 这种方式可以突破 value 不能为 object 复合数据的限制;
132
+ */
133
+ value?: string | string[];
134
+ /**
135
+ * 设置 option 后,等价于设置 label=option 和 value=option;其优先级低于 label/value
136
+ */
137
+ option?: string;
138
+ }
139
+ /**
140
+ * 将查询配置转成 dsi 查询参数
141
+ */
142
+ export declare function convertDsiParam(query: DsiQueryParam): Record<string, string>;
143
+ interface DsiQueryOption<T> {
144
+ /** dsi 服务器地址,比如 xxx.dsi.seayoo.com */
145
+ endpoint: string;
146
+ /** dsi key / alias */
147
+ key: string;
148
+ /** 查询参数 */
149
+ query: DsiQueryParam;
150
+ /** 响应数据守卫函数 */
151
+ guard: (data: unknown) => data is T;
152
+ }
153
+ /**
154
+ * 查询 Dsi 数据接口并返回接口响应
155
+ */
156
+ export declare function queryDsi<T = unknown>({ endpoint, key, query, guard }: DsiQueryOption<T>): Promise<T | null>;
157
+ /**
158
+ * 通过 jsonp 方式加载 dsi 数据
159
+ */
160
+ export declare function jsonpDsi<T = unknown>({ endpoint, key, query, guard }: DsiQueryOption<T>): Promise<T | null>;
161
+ /**
162
+ * 通过 jsonx(script) 方式加载 dsi 数据
163
+ */
164
+ export declare function jsonxDsi<T = unknown>({ endpoint, key, query, guard }: DsiQueryOption<T>): Promise<T | null>;
165
+ export {};
package/dist/core.js DELETED
@@ -1 +0,0 @@
1
- const pkg=require("../package.json"),needle=require("needle"),path=require("path"),fs=require("fs");async function syncTemplate({user:e,service:t},s){if(!t)return!1;if(!e)throw"插件参数缺失,需要传递 user";if(/^https:\/\/([^.]*.)?kms\.seayoo\.(?:com|io)\/sync\/template\/[^/]+/i.test(t))throw"service 参数格式错误,请检查";const n=path.join(s,"index.html");if(!fs.existsSync(n))throw"index.html 没有找到";const r=fs.readFileSync(n).toString("utf-8");if(!r.includes("<script src=/")&&!r.includes("<link href=/"))throw"SPA 资源引用路径需要是以 / 开头的绝对路径";return new Promise(((s,n)=>{needle.post(t,{content:r},{headers:{"x-kms-user":e,"User-Agent":"kms agent/"+pkg.version}},(function(e){e?n(e):s(!0)}))}))}exports.syncTemplate=syncTemplate,exports.getTemplateListUrl=function(e){const t=e.match(/^(https:\/\/[^\/]+)\/.+\/([^/]+)$/);return t?`${t[1]}/tmpl/edit/${t[2]}`:null};
@@ -1 +0,0 @@
1
- const{syncTemplate:syncTemplate,getTemplateListUrl:getTemplateListUrl}=require("./core"),distCacheKey="_kms_dist",path=require("path");require("colors"),module.exports=function(e){return{name:"kmsAgent",generateBundle({dir:e}){let t=process.cwd();e&&(t=path.resolve(t,e)),this.cache.set("_kms_dist",t)},writeBundle(t,s){this.cache.get("_kms_dist");return syncTemplate({user:e.user,service:e.service}).then((function(e){e&&console.log("[KMS Agent]".bgGreen,"模板同步成功".green,getTemplateListUrl(syncOptions.service))}))}}};
@@ -1 +0,0 @@
1
- const{syncTemplate:syncTemplate,getTemplateListUrl:getTemplateListUrl}=require("./core"),path=require("path");function syncKMSTemplate({user:e,service:t}){this.syncOptions={user:e,service:t}}function log(...e){setTimeout((()=>{console.log(...e)}))}require("colors"),syncKMSTemplate.prototype.apply=function(e){const t=this.syncOptions;e.hooks.done.tapAsync("kmsAgent",(function({compilation:e},n){const s=e.outputOptions.path;syncTemplate(t,s).then((e=>{e&&log("[KMS Agent]".bgGreen,"模板同步成功".green,getTemplateListUrl(t.service)),n()})).catch((e=>{log("[KMS Agent]".bgBrightRed,e.bgBrightRed),n()}))}))},module.exports=syncKMSTemplate;
package/index.js DELETED
@@ -1,9 +0,0 @@
1
- const WebpackPlugin = require('./dist/plugin.webpack');
2
- const VitePlugin = require('./dist/plugin.vite');
3
-
4
- module.exports = {
5
- templateSyncPlugin: {
6
- webpack: WebpackPlugin,
7
- vite: VitePlugin
8
- }
9
- }