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
@@ -0,0 +1,181 @@
|
||||
import ky from "ky";
|
||||
import { EkbApiError } from "../../core/errors.js";
|
||||
import {
|
||||
DocCatalogSchema,
|
||||
type DocCatalog,
|
||||
type OpenApiDocItem,
|
||||
} from "./types.js";
|
||||
|
||||
export const DEFAULT_DOCS_BASE_URL = "https://docs.ekuaibao.com";
|
||||
|
||||
interface RawSearchDoc {
|
||||
i: number;
|
||||
t: string;
|
||||
u: string;
|
||||
b?: string[];
|
||||
h?: string;
|
||||
p?: number;
|
||||
s?: string;
|
||||
}
|
||||
|
||||
interface RawSearchSection {
|
||||
documents?: RawSearchDoc[];
|
||||
index?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export function parseRawSearchIndexToCatalog(
|
||||
rawSections: RawSearchSection[],
|
||||
baseUrl = DEFAULT_DOCS_BASE_URL
|
||||
): DocCatalog {
|
||||
const rawDocs = rawSections[0]?.documents || [];
|
||||
const rawHeadings = rawSections[1]?.documents || [];
|
||||
const rawParagraphs = rawSections[2]?.documents || [];
|
||||
|
||||
const apiDocs = rawDocs.filter((d) => d.u?.startsWith("/docs/open-api/"));
|
||||
const items: OpenApiDocItem[] = [];
|
||||
|
||||
for (const doc of apiDocs) {
|
||||
const headings = rawHeadings
|
||||
.filter((h) => h.p === doc.i)
|
||||
.map((h) => h.t)
|
||||
.filter(Boolean);
|
||||
const paragraphs = rawParagraphs.filter((p) => p.p === doc.i);
|
||||
|
||||
let method: string | undefined;
|
||||
let path: string | undefined;
|
||||
let curl: string | undefined;
|
||||
let responseSample: string | undefined;
|
||||
let parameters: string | undefined;
|
||||
const sections: Array<{ title: string; content: string }> = [];
|
||||
|
||||
for (const p of paragraphs) {
|
||||
const sectionTitle = p.s || "概述";
|
||||
const text = (p.t || "").trim();
|
||||
if (!text) continue;
|
||||
|
||||
// Extract HTTP method and path from paragraph text
|
||||
if (!method || !path) {
|
||||
const methodMatch = text.match(
|
||||
/\b(GET|POST|PUT|DELETE|PATCH)\s*(\/api\/[^\s]+)/i
|
||||
);
|
||||
if (methodMatch) {
|
||||
method = methodMatch[1].toUpperCase();
|
||||
path = methodMatch[2];
|
||||
}
|
||||
}
|
||||
|
||||
// Extract CURL
|
||||
if (
|
||||
sectionTitle === "CURL" ||
|
||||
text.startsWith("curl ") ||
|
||||
text.includes("curl --location")
|
||||
) {
|
||||
curl = text;
|
||||
// Fallback method/path extraction from curl if not found yet
|
||||
if (!method || !path) {
|
||||
const curlMatch = text.match(
|
||||
/--request\s+(GET|POST|PUT|DELETE|PATCH)\s+['"][^'"]*?(\/api\/[^?'"\s]+)/i
|
||||
);
|
||||
if (curlMatch) {
|
||||
method = curlMatch[1].toUpperCase();
|
||||
path = curlMatch[2];
|
||||
}
|
||||
}
|
||||
} else if (
|
||||
sectionTitle === "成功响应" ||
|
||||
sectionTitle === "Response" ||
|
||||
(text.startsWith("{") && text.endsWith("}"))
|
||||
) {
|
||||
responseSample = text;
|
||||
} else if (
|
||||
sectionTitle.includes("Parameter") ||
|
||||
sectionTitle.includes("参数") ||
|
||||
sectionTitle.includes("Body")
|
||||
) {
|
||||
parameters = parameters ? `${parameters}\n${text}` : text;
|
||||
}
|
||||
|
||||
sections.push({
|
||||
title: sectionTitle,
|
||||
content: text,
|
||||
});
|
||||
}
|
||||
|
||||
const category = doc.b?.[1] || "未分类";
|
||||
const cleanUrl = doc.u || "";
|
||||
|
||||
items.push({
|
||||
id: doc.i,
|
||||
title: doc.t || "未命名文档",
|
||||
category,
|
||||
url: cleanUrl,
|
||||
permalink: `${baseUrl.replace(/\/+$/, "")}${cleanUrl}`,
|
||||
method,
|
||||
path,
|
||||
breadcrumbs: doc.b || [],
|
||||
headings,
|
||||
sections,
|
||||
parameters,
|
||||
curl,
|
||||
responseSample,
|
||||
});
|
||||
}
|
||||
|
||||
const catalog: DocCatalog = {
|
||||
version: "1.0.0",
|
||||
updatedAt: new Date().toISOString(),
|
||||
sourceUrl: `${baseUrl.replace(/\/+$/, "")}/search-index.json`,
|
||||
totalDocs: items.length,
|
||||
totalEndpoints: items.filter((i) => i.method && i.path).length,
|
||||
items,
|
||||
};
|
||||
|
||||
return DocCatalogSchema.parse(catalog);
|
||||
}
|
||||
|
||||
/**
|
||||
* 实时从线上文档站点获取最新接口定义索引
|
||||
*/
|
||||
export async function fetchLiveDocsCatalog(options?: {
|
||||
baseUrl?: string;
|
||||
timeoutMs?: number;
|
||||
}): Promise<DocCatalog> {
|
||||
const baseUrl = options?.baseUrl || DEFAULT_DOCS_BASE_URL;
|
||||
const timeoutMs = options?.timeoutMs ?? 15000;
|
||||
const targetUrl = `${baseUrl.replace(/\/+$/, "")}/search-index.json`;
|
||||
|
||||
try {
|
||||
const response = await ky.get(targetUrl, {
|
||||
timeout: timeoutMs,
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"User-Agent": "hose-use/docs-client",
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new EkbApiError(
|
||||
`获取开放平台文档索引失败: HTTP ${response.status}`,
|
||||
"DOCS_FETCH_FAILED",
|
||||
response.status
|
||||
);
|
||||
}
|
||||
|
||||
const raw = (await response.json()) as RawSearchSection[];
|
||||
if (!Array.isArray(raw) || raw.length === 0) {
|
||||
throw new EkbApiError(
|
||||
"开放平台文档索引数据异常: 返回格式不是有效的分区数组",
|
||||
"DOCS_INVALID_DATA"
|
||||
);
|
||||
}
|
||||
|
||||
return parseRawSearchIndexToCatalog(raw, baseUrl);
|
||||
} catch (error: any) {
|
||||
if (error instanceof EkbApiError) throw error;
|
||||
throw new EkbApiError(
|
||||
`请求开放平台文档站点 (${targetUrl}) 失败: ${error?.message || String(error)}`,
|
||||
"DOCS_NETWORK_ERROR",
|
||||
502
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user