This commit is contained in:
张成
2026-04-01 13:26:41 +08:00
parent fa9abf83ae
commit d03916290a
7 changed files with 315 additions and 30 deletions

View File

@@ -0,0 +1,54 @@
const swagger = require("../../_docs/swagger.json");
const HTTP_METHODS = new Set(["get", "post", "put", "delete", "patch", "head", "options"]);
/**
* 与 proxy_api.buildProxyRoutes 使用同一套 swagger.paths供套餐「接口白名单」勾选
* @returns {{ items: object[], groups: Record<string, object[]>, tags: string[] }}
*/
function buildCatalog() {
const byPath = new Map();
const paths = swagger.paths || {};
for (const [routePath, methods] of Object.entries(paths)) {
if (!methods || typeof methods !== "object") continue;
for (const [method, spec] of Object.entries(methods)) {
if (!HTTP_METHODS.has(method.toLowerCase())) continue;
if (!spec || typeof spec !== "object") continue;
const tag = (spec.tags && spec.tags[0]) || "其他";
const summary = spec.summary || spec.operationId || "";
if (!byPath.has(routePath)) {
byPath.set(routePath, {
path: routePath,
methods: new Set(),
summary: summary || "",
tag,
});
}
const row = byPath.get(routePath);
row.methods.add(method.toUpperCase());
}
}
const items = Array.from(byPath.values())
.map((x) => ({
path: x.path,
methods: Array.from(x.methods).sort(),
summary: x.summary || "",
tag: x.tag,
}))
.sort((a, b) => a.path.localeCompare(b.path));
/** @type {Record<string, object[]>} */
const groups = {};
for (const it of items) {
if (!groups[it.tag]) groups[it.tag] = [];
groups[it.tag].push(it);
}
const tags = Object.keys(groups).sort((a, b) => a.localeCompare(b, "zh-CN"));
return { items, groups, tags };
}
module.exports = { buildCatalog };