Files
wechatWeb/api/service/biz_proxy_api_catalog.js
张成 d03916290a 1
2026-04-01 13:26:41 +08:00

55 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 };