112 lines
3.1 KiB
JavaScript
112 lines
3.1 KiB
JavaScript
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { crawlApi } from "../apidata/crawler.mjs";
|
|
|
|
const APIDATA_DIR = path.join("workflows", "api-doc-system", "apidata");
|
|
const APIDATA_CONFIG_FILE = path.join(APIDATA_DIR, "config.json");
|
|
const APIDATA_DATA_FILE = path.join(APIDATA_DIR, "data.json");
|
|
const RESPONSE_MARKDOWN_FILE = path.join("workflows", "api-doc-agent", "config", "响应.md");
|
|
|
|
const CRAWLER_CONFIG = {
|
|
baseUrl: "http://192.168.11.56:6060/system",
|
|
version: "1.0",
|
|
timeoutMs: 20000,
|
|
};
|
|
|
|
function ensurePlainObject(value, fallbackValue = {}) {
|
|
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
return fallbackValue;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
async function readJson(filePath) {
|
|
const content = await readFile(filePath, "utf8");
|
|
return JSON.parse(content);
|
|
}
|
|
|
|
async function writeJson(filePath, data) {
|
|
await mkdir(path.dirname(filePath), { recursive: true });
|
|
await writeFile(filePath, `${JSON.stringify(data, null, 2)}\n`, "utf8");
|
|
}
|
|
|
|
function normalizeConfigRequest(item) {
|
|
const method = String(item?.method ?? "").trim();
|
|
if (!method) {
|
|
return null;
|
|
}
|
|
return {
|
|
method,
|
|
body: ensurePlainObject(item?.body, {}),
|
|
needAuth: item?.needAuth ?? method !== "user.login",
|
|
};
|
|
}
|
|
|
|
function normalizeConfig(rawConfig) {
|
|
const rawRequests = Array.isArray(rawConfig) ? rawConfig : rawConfig?.requests;
|
|
const requests = Array.isArray(rawRequests) ? rawRequests.map(normalizeConfigRequest).filter(Boolean) : [];
|
|
|
|
if (requests.length === 0) {
|
|
throw new Error("apidata/config.json 缺少有效接口配置,请至少提供一个 method");
|
|
}
|
|
|
|
return {
|
|
requests,
|
|
};
|
|
}
|
|
|
|
function createMarkdownContent(rows) {
|
|
const generatedAt = new Date().toLocaleString("zh-CN", { hour12: false });
|
|
return [
|
|
"# 接口响应",
|
|
"",
|
|
`更新时间: ${generatedAt}`,
|
|
"",
|
|
"```json",
|
|
JSON.stringify(rows, null, 2),
|
|
"```",
|
|
"",
|
|
].join("\n");
|
|
}
|
|
|
|
async function run() {
|
|
const config = normalizeConfig(await readJson(APIDATA_CONFIG_FILE));
|
|
|
|
await writeJson(APIDATA_DATA_FILE, []);
|
|
|
|
const rows = [];
|
|
let authToken = null;
|
|
|
|
for (const req of config.requests) {
|
|
const result = await crawlApi({
|
|
baseUrl: CRAWLER_CONFIG.baseUrl,
|
|
version: CRAWLER_CONFIG.version,
|
|
method: req.method,
|
|
body: req.body,
|
|
timeoutMs: CRAWLER_CONFIG.timeoutMs,
|
|
token: req.needAuth ? authToken : null,
|
|
});
|
|
|
|
if (result.response?.data?.token) {
|
|
authToken = result.response.data.token;
|
|
}
|
|
|
|
rows.push({
|
|
接口: req.method,
|
|
message: result.response?.message ?? "",
|
|
data: result.response?.data ?? null,
|
|
});
|
|
}
|
|
|
|
await writeJson(APIDATA_DATA_FILE, rows);
|
|
await mkdir(path.dirname(RESPONSE_MARKDOWN_FILE), { recursive: true });
|
|
await writeFile(RESPONSE_MARKDOWN_FILE, createMarkdownContent(rows), "utf8");
|
|
console.log(`已写入接口响应数据: ${APIDATA_DATA_FILE}`);
|
|
console.log(`已更新响应文档: ${RESPONSE_MARKDOWN_FILE}`);
|
|
}
|
|
|
|
run().catch((error) => {
|
|
console.error("执行 apidata 抓取失败:", error);
|
|
process.exitCode = 1;
|
|
});
|