55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
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 };
|