feat(docs): 新增合思开放平台官方接口文档检索独立业务域与 CLI 客户端
- 新增 `src/domains/docs/` 领域(api.ts, tools.ts, types.ts, README.docs.md, docs.test.ts) - 实现从线上 `docs.ekuaibao.com/search-index.json` 实时拉取最新规范与端点提取能力 - 集成多级缓存(L1 内存 + L2 SQLite)与 `specs/docs-catalog.json` 静态预编译保底,支持零网络离线毫秒级检索 - 提供 270+ 个 OpenAPI HTTP 接口端点、参数表格、CURL 示范与成功响应示例的高精度解析与结构化输出 - 注册 `hose-use docs` CLI 命令(search, show, categories, sync)及 `doc`, `api-doc` 别名 - 严格遵循 AGENTS.md 架构分层约束,全量 1144+ 自动化单测与 tsc 类型检查 100% 通过
This commit is contained in:
1 parent
448dd438ff
commit
4f572ad9c1
11 files changed
+1013
-2
No files matched your search
@@ -904,6 +904,41 @@ export const COMMAND_METADATA: Record<string, CliCommandMetadata> = {
|
||||
...flagList("all a logout admin save import persist switch current overwrite candidates list-candidates qr check", "boolean"),
|
||||
...flagList("status", "boolean-or-value")
|
||||
}
|
||||
},
|
||||
"docs": {
|
||||
"summary": "合思开放平台官方接口文档与端点规范检索",
|
||||
"usage": "hose-use docs <search|show|categories|sync> [关键词|路径] [--json]",
|
||||
"commands": {
|
||||
"search": "按关键词、分类或方法检索开放平台 HTTP 接口",
|
||||
"show": "查看指定接口的完整规范、参数定义、CURL 与响应示例",
|
||||
"categories": "查看开放平台所有业务模块分类与端点统计",
|
||||
"sync": "强制与线上文档中心全量同步最新索引"
|
||||
},
|
||||
"aliases": [
|
||||
"doc",
|
||||
"api-doc",
|
||||
"api-docs"
|
||||
],
|
||||
"commandAliases": {
|
||||
"find": "search",
|
||||
"list": "search",
|
||||
"ls": "search",
|
||||
"get": "show",
|
||||
"info": "show",
|
||||
"cat": "show",
|
||||
"read": "show",
|
||||
"detail": "show",
|
||||
"category": "categories",
|
||||
"cats": "categories",
|
||||
"modules": "categories",
|
||||
"groups": "categories",
|
||||
"update": "sync",
|
||||
"refresh": "sync"
|
||||
},
|
||||
flags: {
|
||||
...flagList("keyword query q category cat module method m path p target id name url limit count n", "value"),
|
||||
...flagList("refresh sync force", "boolean")
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { runCli } from "../runner.js";
|
||||
|
||||
describe("CLI - 开放平台官方接口文档域 (docs / openapi-docs)", () => {
|
||||
test("hose-use docs 输出开放平台文档概览与端点总数", async () => {
|
||||
const res = await runCli(["docs"]);
|
||||
expect(res.exitCode).toBe(0);
|
||||
expect(res.stdout).toContain("合思开放平台官方文档中心");
|
||||
expect(res.stdout).toContain("文档总数:");
|
||||
expect(res.stdout).toContain("端点总数:");
|
||||
expect(res.stdout).toContain("hose-use docs search");
|
||||
});
|
||||
|
||||
test("hose-use docs --json 输出概览结构化元数据", async () => {
|
||||
const res = await runCli(["docs", "--json"]);
|
||||
expect(res.exitCode).toBe(0);
|
||||
const data = JSON.parse(res.stdout);
|
||||
expect(data.totalDocs).toBeGreaterThan(100);
|
||||
expect(data.totalEndpoints).toBeGreaterThan(100);
|
||||
});
|
||||
|
||||
test("hose-use docs search roleDefGroups 能检索出角色组接口", async () => {
|
||||
const res = await runCli(["docs", "search", "roleDefGroups"]);
|
||||
expect(res.exitCode).toBe(0);
|
||||
expect(res.stdout).toContain("GET");
|
||||
expect(res.stdout).toContain("/api/openapi/v1/roleDefGroups");
|
||||
expect(res.stdout).toContain("查询角色组和角色");
|
||||
expect(res.stdout).toContain("企业通讯录");
|
||||
});
|
||||
|
||||
test("hose-use docs search 角色组 --json 输出精准结构化搜索结果", async () => {
|
||||
const res = await runCli(["docs", "search", "角色组", "--json"]);
|
||||
expect(res.exitCode).toBe(0);
|
||||
const data = JSON.parse(res.stdout);
|
||||
expect(data.total).toBeGreaterThanOrEqual(1);
|
||||
expect(data.items.some((i: any) => i.path === "/api/openapi/v1/roleDefGroups")).toBe(true);
|
||||
});
|
||||
|
||||
test("hose-use docs show /api/openapi/v1/roleDefGroups 展示完整接口规格与 CURL 示范", async () => {
|
||||
const res = await runCli(["docs", "show", "/api/openapi/v1/roleDefGroups"]);
|
||||
expect(res.exitCode).toBe(0);
|
||||
expect(res.stdout).toContain("查询角色组和角色");
|
||||
expect(res.stdout).toContain("GET");
|
||||
expect(res.stdout).toContain("/api/openapi/v1/roleDefGroups");
|
||||
expect(res.stdout).toContain("CURL 请求示范");
|
||||
expect(res.stdout).toContain("成功响应示例");
|
||||
expect(res.stdout).toContain("accessToken");
|
||||
});
|
||||
|
||||
test("hose-use docs show /api/openapi/v1/roleDefGroups --json 返回全量结构化文档项", async () => {
|
||||
const res = await runCli(["docs", "show", "/api/openapi/v1/roleDefGroups", "--json"]);
|
||||
expect(res.exitCode).toBe(0);
|
||||
const doc = JSON.parse(res.stdout);
|
||||
expect(doc.path).toBe("/api/openapi/v1/roleDefGroups");
|
||||
expect(doc.method).toBe("GET");
|
||||
expect(doc.category).toBe("企业通讯录");
|
||||
expect(doc.permalink).toContain("get-roles-group");
|
||||
});
|
||||
|
||||
test("hose-use docs show 未知目标时返回友好错误", async () => {
|
||||
const res = await runCli(["docs", "show", "non_existing_api_path_xyz"]);
|
||||
expect(res.exitCode).toBe(1);
|
||||
expect(res.stderr).toContain("NOT FOUND");
|
||||
});
|
||||
|
||||
test("hose-use docs categories 输出所有业务分类与端点分布", async () => {
|
||||
const res = await runCli(["docs", "categories"]);
|
||||
expect(res.exitCode).toBe(0);
|
||||
expect(res.stdout).toContain("合思开放平台业务模块分类");
|
||||
expect(res.stdout).toContain("企业通讯录");
|
||||
expect(res.stdout).toContain("预算");
|
||||
expect(res.stdout).toContain("单据");
|
||||
});
|
||||
|
||||
test("hose-use doc 别名也能正常调用", async () => {
|
||||
const res = await runCli(["doc", "search", "roleDefGroups"]);
|
||||
expect(res.exitCode).toBe(0);
|
||||
expect(res.stdout).toContain("/api/openapi/v1/roleDefGroups");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,237 @@
|
||||
import { booleanOption, numberOption, stringOption } from "../parser.js";
|
||||
import { printJson, printKeyValue, printTable } from "../printer.js";
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../types.js";
|
||||
import {
|
||||
getDocDetail,
|
||||
getDocsCatalog,
|
||||
listCategories,
|
||||
searchDocs,
|
||||
syncDocsCatalog,
|
||||
} from "../../domains/docs/tools.js";
|
||||
|
||||
export async function handleDocsCommand(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const action = parsed.action?.toLowerCase() || "";
|
||||
const forceRefresh =
|
||||
booleanOption(parsed.options, ["refresh", "force", "sync"]) ||
|
||||
action === "sync";
|
||||
|
||||
// 1. 同步命令: hose-use docs sync
|
||||
if (action === "sync") {
|
||||
const res = await syncDocsCatalog();
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(res, runtime.outputOptions),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
return {
|
||||
stdout: `====== 开放平台文档同步完成 ======\n文档总数: ${res.totalDocs}\n接口端点: ${res.totalEndpoints}\n数据源: ${res.source}\n更新时间: ${res.updatedAt}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// 2. 分类列表: hose-use docs categories
|
||||
if (["categories", "category", "cats", "modules"].includes(action)) {
|
||||
const cats = await listCategories({ forceRefresh });
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson({ count: cats.length, categories: cats }, runtime.outputOptions),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
const rows = cats.map((c, i) => [
|
||||
i + 1,
|
||||
c.name,
|
||||
`${c.count} 篇`,
|
||||
`${c.endpointsCount} 个`,
|
||||
]);
|
||||
const table = printTable(["#", "业务分类", "文档篇数", "HTTP 端点数"], rows);
|
||||
return {
|
||||
stdout: `====== 合思开放平台业务模块分类 (共 ${cats.length} 个) ======\n\n${table}\n\n提示: 运行 hose-use docs search --category=<模块名> 可查看该模块下的所有接口\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
// 3. 详情命令: hose-use docs show <target>
|
||||
if (["show", "get", "info", "cat", "read"].includes(action)) {
|
||||
const target = parsed.args[0] || stringOption(parsed.options, ["path", "url", "id", "name", "target"]);
|
||||
if (!target) {
|
||||
throw new Error("请指定要查看的接口路径、文档 URL 或名称: hose-use docs show <接口或URL>");
|
||||
}
|
||||
|
||||
const doc = await getDocDetail(target, { forceRefresh });
|
||||
if (!doc) {
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson({ found: false, target }, runtime.outputOptions),
|
||||
stderr: "",
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[NOT FOUND] 未找到匹配的文档或接口: ${target}\n提示: 可以先运行 hose-use docs search ${target} 检索相近接口。\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(doc, runtime.outputOptions),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
let out = `====== ${doc.title} ======\n`;
|
||||
out += printKeyValue({
|
||||
"业务模块": doc.category,
|
||||
"HTTP 方法": doc.method || "非端点/指南页面",
|
||||
"接口路径": doc.path || "-",
|
||||
"在线文档": doc.permalink,
|
||||
"文档标识": `#${doc.id}`,
|
||||
});
|
||||
|
||||
if (doc.parameters) {
|
||||
out += `\n\n### 参数规格与定义\n${doc.parameters}\n`;
|
||||
}
|
||||
|
||||
if (doc.curl) {
|
||||
out += `\n### CURL 请求示范\n\`\`\`bash\n${doc.curl}\n\`\`\`\n`;
|
||||
}
|
||||
|
||||
if (doc.responseSample) {
|
||||
out += `\n### 成功响应示例\n\`\`\`json\n${doc.responseSample}\n\`\`\`\n`;
|
||||
}
|
||||
|
||||
return { stdout: `${out}\n`, stderr: "", exitCode: 0 };
|
||||
}
|
||||
|
||||
// 4. 搜索命令 (默认行为): hose-use docs search <keyword> 或 hose-use docs <keyword>
|
||||
const isSearchAction = ["search", "find", "list", "ls", ""].includes(action);
|
||||
if (isSearchAction) {
|
||||
const keyword =
|
||||
(action === "search" || action === "find" ? parsed.args[0] : action || parsed.args[0]) ||
|
||||
stringOption(parsed.options, ["keyword", "q", "query"]);
|
||||
|
||||
const category = stringOption(parsed.options, ["category", "cat", "module"]);
|
||||
const method = stringOption(parsed.options, ["method", "m"]);
|
||||
const path = stringOption(parsed.options, ["path", "p"]);
|
||||
const limit = numberOption(parsed.options, ["limit", "n", "count"], 25);
|
||||
|
||||
// 如果没有任何搜索条件且没有关键词,展示全局概览
|
||||
if (!keyword && !category && !method && !path && !action) {
|
||||
const catalog = await getDocsCatalog({ forceRefresh });
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(
|
||||
{
|
||||
totalDocs: catalog.totalDocs,
|
||||
totalEndpoints: catalog.totalEndpoints,
|
||||
sourceUrl: catalog.sourceUrl,
|
||||
updatedAt: catalog.updatedAt,
|
||||
},
|
||||
runtime.outputOptions
|
||||
),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
return {
|
||||
stdout: `====== 合思开放平台官方文档中心 ======
|
||||
文档总数: ${catalog.totalDocs} 篇
|
||||
端点总数: ${catalog.totalEndpoints} 个
|
||||
数据来源: ${catalog.sourceUrl}
|
||||
更新时间: ${catalog.updatedAt}
|
||||
|
||||
常用子命令:
|
||||
hose-use docs search <关键词> 检索开放平台 HTTP 接口与文档
|
||||
hose-use docs show <路径|名称|URL> 查看单篇接口规格、参数表格与 CURL 响应
|
||||
hose-use docs categories 查看 26+ 个业务分类及其端点数
|
||||
hose-use docs sync 强制与线上最新文档中心全量同步
|
||||
`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const searchRes = await searchDocs(
|
||||
{
|
||||
keyword,
|
||||
category,
|
||||
method,
|
||||
path,
|
||||
limit,
|
||||
},
|
||||
{ forceRefresh }
|
||||
);
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
const summaryItems = searchRes.items.map((item) => ({
|
||||
id: item.id,
|
||||
title: item.title,
|
||||
category: item.category,
|
||||
method: item.method,
|
||||
path: item.path,
|
||||
url: item.url,
|
||||
permalink: item.permalink,
|
||||
}));
|
||||
return {
|
||||
stdout: printJson(
|
||||
{
|
||||
total: searchRes.total,
|
||||
count: summaryItems.length,
|
||||
keyword: searchRes.keyword,
|
||||
category: searchRes.category,
|
||||
method: searchRes.method,
|
||||
items: summaryItems,
|
||||
},
|
||||
runtime.outputOptions
|
||||
),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
if (searchRes.items.length === 0) {
|
||||
return {
|
||||
stdout: `未找到匹配的文档或接口 (关键词: ${keyword || "-"}, 分类: ${category || "-"}, 方法: ${method || "-"})\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const rows = searchRes.items.map((item) => [
|
||||
item.method || "DOC",
|
||||
item.path || item.url,
|
||||
item.title,
|
||||
item.category,
|
||||
]);
|
||||
|
||||
const table = printTable(["Method", "Path / URL", "标题", "分类"], rows);
|
||||
const filterDesc = [
|
||||
keyword ? `关键词: '${keyword}'` : "",
|
||||
category ? `分类: '${category}'` : "",
|
||||
method ? `方法: '${method}'` : "",
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(", ");
|
||||
|
||||
return {
|
||||
stdout: `====== 开放平台接口搜索结果 (找到 ${searchRes.total} 条${filterDesc ? ` | ${filterDesc}` : ""}) ======\n\n${table}\n\n提示: 运行 hose-use docs show <Path> 可查看对应接口的完整参数和 CURL 示范\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`未知的 docs 子命令: ${action}。支持: search <关键词>, show <路径|URL>, categories, sync`
|
||||
);
|
||||
}
|
||||
@@ -30,6 +30,7 @@ import { handleKeysCommand } from "./commands/keys.js";
|
||||
import { handleStoreCommand } from "./commands/store.js";
|
||||
import { handleFeeTypeCommand } from "./commands/feetype.js";
|
||||
import { handleAssistPlatformCommand } from "./commands/assist-platform/index.js";
|
||||
import { handleDocsCommand } from "./commands/docs.js";
|
||||
import { COMMAND_METADATA, type CliCommandMetadata } from "./command-metadata.js";
|
||||
import type { CliParsedArgs, CliResult, CliRunOptions, CliRuntime } from "./types.js";
|
||||
|
||||
@@ -206,6 +207,11 @@ export const CLI_COMMAND_REGISTRY: readonly CliCommandDefinition[] = [
|
||||
...COMMAND_METADATA["assist-platform"],
|
||||
handler: (parsed, runtime) => handleAssistPlatformCommand(parsed, runtime),
|
||||
},
|
||||
{
|
||||
area: "docs",
|
||||
...COMMAND_METADATA.docs,
|
||||
handler: (parsed, runtime) => handleDocsCommand(parsed, runtime),
|
||||
},
|
||||
];
|
||||
|
||||
const COMMAND_LOOKUP = new Map<string, CliCommandDefinition>();
|
||||
|
||||
Reference in new issue
Block a user