@guandata/decidex 0.1.2 → 0.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/README.md +268 -16
- package/docs/api-coverage.md +51 -0
- package/package.json +4 -3
- package/src/api-catalog.json +6849 -0
- package/src/catalog.js +36 -0
- package/src/cli.js +806 -104
- package/src/compatibility.js +94 -0
package/src/catalog.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
export const apiCatalog = JSON.parse(readFileSync(new URL("./api-catalog.json", import.meta.url), "utf8"));
|
|
4
|
+
|
|
5
|
+
export function apiOperation(id) {
|
|
6
|
+
const operation = apiCatalog.operations.find((item) => item.id === id);
|
|
7
|
+
if (!operation) throw new Error(`未知平台操作:${id};使用 decidex platform list --query 关键词 查找`);
|
|
8
|
+
return operation;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function parameterFlag(name) {
|
|
12
|
+
return name.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function operationPath(operation, values) {
|
|
16
|
+
let path = operation.path;
|
|
17
|
+
for (const name of operation.parameters) {
|
|
18
|
+
const value = values[parameterFlag(name)];
|
|
19
|
+
if (typeof value !== "string" || !value.trim() || value === "." || value === "..") throw new Error(`缺少或无效路径参数 --${parameterFlag(name)}`);
|
|
20
|
+
path = path.replace(`[${name}]`, encodeURIComponent(value));
|
|
21
|
+
}
|
|
22
|
+
return path;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function appendQuery(path, query) {
|
|
26
|
+
if (!query || typeof query !== "object" || Array.isArray(query)) throw new Error("查询参数文件必须是 JSON 对象");
|
|
27
|
+
const result = new URLSearchParams();
|
|
28
|
+
for (const [key, raw] of Object.entries(query)) {
|
|
29
|
+
if (raw === null || raw === undefined) continue;
|
|
30
|
+
for (const value of Array.isArray(raw) ? raw : [raw]) {
|
|
31
|
+
if (!["string", "boolean", "number"].includes(typeof value)) throw new Error(`查询参数 ${key} 必须是标量或标量数组;复杂参数请传入服务端要求的 JSON 字符串`);
|
|
32
|
+
result.append(key, String(value));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return `${path}${result.size ? `?${result}` : ""}`;
|
|
36
|
+
}
|