chore: sync changes for v0.1.1
This commit is contained in:
@@ -14,13 +14,27 @@ const config = {
|
||||
backendUrl: process.env.BACKEND_URL || 'http://localhost:3000',
|
||||
docsDir: path.join(__dirname, '../src/wwjcloud/openapi/api'),
|
||||
apiGroups: {
|
||||
unified: {
|
||||
name: '统一 API',
|
||||
swaggerPath: '/docs-json',
|
||||
outputDir: 'unified',
|
||||
prefix: ''
|
||||
}
|
||||
}
|
||||
full: {
|
||||
name: '全量 API',
|
||||
swaggerPath: process.env.OPENAPI_FULL_PATH || '/api-json',
|
||||
outputDir: 'full',
|
||||
prefix: '',
|
||||
},
|
||||
admin: {
|
||||
name: '管理端 API',
|
||||
swaggerPath: process.env.OPENAPI_ADMIN_PATH || '/api/admin-json',
|
||||
outputDir: 'admin',
|
||||
prefix: '/adminapi',
|
||||
},
|
||||
frontend: {
|
||||
name: '前端 API',
|
||||
swaggerPath: process.env.OPENAPI_FRONTEND_PATH || '/api/frontend-json',
|
||||
outputDir: 'frontend',
|
||||
prefix: '/api',
|
||||
},
|
||||
},
|
||||
// 固定 Token(与后端 swagger.token 保持一致)
|
||||
token: '9f2a7c1e4b5d8a90c3f6e2b17a4c58d0f1b2c3d4e5f67890ab12cd34ef56a789',
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -30,7 +44,9 @@ async function getSwaggerJson(group) {
|
||||
try {
|
||||
const url = `${config.backendUrl}${group.swaggerPath}`;
|
||||
console.log(`获取 ${group.name} API: ${url}`);
|
||||
const response = await axios.get(url);
|
||||
const headers = {};
|
||||
if (config.token) headers.Authorization = `Bearer ${config.token}`;
|
||||
const response = await axios.get(url, { headers });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(`获取失败: ${error.message}`);
|
||||
@@ -44,28 +60,28 @@ async function getSwaggerJson(group) {
|
||||
function generateModuleDoc(group, tag, paths) {
|
||||
const moduleName = tag.name;
|
||||
let markdown = `---
|
||||
title: ${moduleName} API
|
||||
description: ${tag.description || `${moduleName} 相关接口`}
|
||||
:title: ${moduleName} API
|
||||
:description: ${tag.description || `${moduleName} 相关接口`}
|
||||
---
|
||||
|
||||
# ${moduleName} API
|
||||
|
||||
::: info ${moduleName}
|
||||
:::: info ${moduleName}
|
||||
|
||||
${tag.description || `${moduleName} 相关接口`}
|
||||
|
||||
:::
|
||||
::::
|
||||
|
||||
## API 列表
|
||||
|
||||
`;
|
||||
|
||||
// 查找该标签下的接口
|
||||
Object.keys(paths).forEach(path => {
|
||||
Object.keys(paths[path]).forEach(method => {
|
||||
const operation = paths[path][method];
|
||||
Object.keys(paths).forEach((p) => {
|
||||
Object.keys(paths[p]).forEach((method) => {
|
||||
const operation = paths[p][method];
|
||||
if (operation.tags && operation.tags.includes(tag.name)) {
|
||||
markdown += generateApiDoc(path, method, operation);
|
||||
markdown += generateApiDoc(p, method, operation);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -76,11 +92,11 @@ ${tag.description || `${moduleName} 相关接口`}
|
||||
/**
|
||||
* 生成单个 API 文档
|
||||
*/
|
||||
function generateApiDoc(path, method, operation) {
|
||||
function generateApiDoc(pathname, method, operation) {
|
||||
const summary = operation.summary || operation.operationId || '未命名接口';
|
||||
|
||||
let doc = `### ${summary}\n\n`;
|
||||
doc += `**接口**: \`${method.toUpperCase()} ${path}\`\n\n`;
|
||||
doc += `**接口**: \`${method.toUpperCase()} ${pathname}\`\n\n`;
|
||||
|
||||
if (operation.description) {
|
||||
doc += `**描述**: ${operation.description}\n\n`;
|
||||
@@ -92,7 +108,7 @@ function generateApiDoc(path, method, operation) {
|
||||
doc += `| 参数 | 类型 | 必填 | 说明 |\n`;
|
||||
doc += `|------|------|------|------|\n`;
|
||||
|
||||
operation.parameters.forEach(param => {
|
||||
operation.parameters.forEach((param) => {
|
||||
const required = param.required ? '是' : '否';
|
||||
const type = param.type || param.schema?.type || 'string';
|
||||
doc += `| ${param.name} | ${type} | ${required} | ${param.description || ''} |\n`;
|
||||
@@ -104,22 +120,14 @@ function generateApiDoc(path, method, operation) {
|
||||
if (operation.requestBody) {
|
||||
doc += `**请求体**:\n\n`;
|
||||
doc += `\`\`\`json\n`;
|
||||
doc += `{\n`;
|
||||
doc += ` // 请求数据\n`;
|
||||
doc += `}\n`;
|
||||
doc += `{}\n`;
|
||||
doc += `\`\`\`\n\n`;
|
||||
}
|
||||
|
||||
// 响应
|
||||
doc += `**响应**:\n\n`;
|
||||
doc += `\`\`\`json\n`;
|
||||
doc += `{\n`;
|
||||
doc += ` "code": 200,\n`;
|
||||
doc += ` "message": "success",\n`;
|
||||
doc += ` "data": {\n`;
|
||||
doc += ` // 响应数据\n`;
|
||||
doc += ` }\n`;
|
||||
doc += `}\n`;
|
||||
doc += `{"code":200,"message":"success","data":{}}\n`;
|
||||
doc += `\`\`\`\n\n`;
|
||||
doc += `---\n\n`;
|
||||
|
||||
@@ -148,7 +156,7 @@ function saveDocument(outputPath, content) {
|
||||
async function main() {
|
||||
console.log('开始自动生成 API 文档...\n');
|
||||
|
||||
for (const [key, group] of Object.entries(config.apiGroups)) {
|
||||
for (const [_key, group] of Object.entries(config.apiGroups)) {
|
||||
console.log(`处理 ${group.name}...`);
|
||||
|
||||
const swaggerData = await getSwaggerJson(group);
|
||||
@@ -164,7 +172,11 @@ async function main() {
|
||||
for (const tag of tags) {
|
||||
const moduleName = tag.name.toLowerCase().replace(/\s+/g, '-');
|
||||
const content = generateModuleDoc(group, tag, paths);
|
||||
const outputPath = path.join(config.docsDir, group.outputDir, `${moduleName}.md`);
|
||||
const outputPath = path.join(
|
||||
config.docsDir,
|
||||
group.outputDir,
|
||||
`${moduleName}.md`,
|
||||
);
|
||||
saveDocument(outputPath, content);
|
||||
}
|
||||
|
||||
@@ -177,7 +189,7 @@ async function main() {
|
||||
|
||||
// 运行
|
||||
if (require.main === module) {
|
||||
main().catch(error => {
|
||||
main().catch((error) => {
|
||||
console.error('执行失败:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user