update add openapi api
This commit is contained in:
105
docs/split.ts
Normal file
105
docs/split.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
const swaggerPath = 'docs/openapi/swagger-2.0.json';
|
||||
const outputDir = 'docs/api-groups';
|
||||
|
||||
// 读取 swagger JSON
|
||||
const swagger = JSON.parse(fs.readFileSync(swaggerPath, 'utf8'));
|
||||
|
||||
// 收集所有 tag 及其对应的 API
|
||||
const tagApis: Record<string, any> = {};
|
||||
|
||||
// 遍历所有路径和方法
|
||||
for (const [apiPath, methods] of Object.entries(swagger.paths)) {
|
||||
for (const [method, operation] of Object.entries(methods as Record<string, any>)) {
|
||||
const tags = operation.tags || ['default'];
|
||||
for (const tag of tags) {
|
||||
if (!tagApis[tag]) {
|
||||
tagApis[tag] = {};
|
||||
}
|
||||
if (!tagApis[tag][apiPath]) {
|
||||
tagApis[tag][apiPath] = {};
|
||||
}
|
||||
tagApis[tag][apiPath][method] = operation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 收集每个 tag 需要的 definitions
|
||||
function getUsedDefinitions(apis: Record<string, any>): Set<string> {
|
||||
const usedDefs = new Set<string>();
|
||||
const jsonStr = JSON.stringify(apis);
|
||||
const refMatches = jsonStr.match(/#\/definitions\/[^"]+/g) || [];
|
||||
for (const ref of refMatches) {
|
||||
const defName = ref.replace('#/definitions/', '');
|
||||
usedDefs.add(defName);
|
||||
}
|
||||
return usedDefs;
|
||||
}
|
||||
|
||||
// 递归收集依赖的 definitions
|
||||
function getAllRequiredDefinitions(startDefs: Set<string>, allDefs: Record<string, any>): Set<string> {
|
||||
const result = new Set<string>(startDefs);
|
||||
let added = true;
|
||||
while (added) {
|
||||
added = false;
|
||||
for (const defName of Array.from(result)) {
|
||||
const def = allDefs[defName];
|
||||
if (def && def.properties) {
|
||||
for (const prop of Object.values(def.properties) as any[]) {
|
||||
if (prop.$ref) {
|
||||
const refName = prop.$ref.replace('#/definitions/', '');
|
||||
if (!result.has(refName)) {
|
||||
result.add(refName);
|
||||
added = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 检查数组类型的 items
|
||||
if (def && def.items && def.items.$ref) {
|
||||
const refName = def.items.$ref.replace('#/definitions/', '');
|
||||
if (!result.has(refName)) {
|
||||
result.add(refName);
|
||||
added = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// 为每个 tag 创建输出目录和文件
|
||||
for (const [tag, apis] of Object.entries(tagApis)) {
|
||||
const tagDir = path.join(outputDir, tag.toLowerCase());
|
||||
if (!fs.existsSync(tagDir)) {
|
||||
fs.mkdirSync(tagDir, { recursive: true });
|
||||
}
|
||||
|
||||
// 收集使用的 definitions
|
||||
const usedDefs = getUsedDefinitions(apis);
|
||||
const allRequiredDefs = getAllRequiredDefinitions(usedDefs, swagger.definitions || {});
|
||||
|
||||
// 构建输出的 swagger 片段
|
||||
const outputSwagger: any = {
|
||||
swagger: swagger.swagger,
|
||||
info: swagger.info,
|
||||
paths: apis,
|
||||
definitions: {}
|
||||
};
|
||||
|
||||
// 只包含需要的 definitions
|
||||
for (const defName of allRequiredDefs) {
|
||||
if (swagger.definitions && swagger.definitions[defName]) {
|
||||
outputSwagger.definitions[defName] = swagger.definitions[defName];
|
||||
}
|
||||
}
|
||||
|
||||
// 写入文件
|
||||
const outputPath = path.join(tagDir, 'api.json');
|
||||
fs.writeFileSync(outputPath, JSON.stringify(outputSwagger, null, 2));
|
||||
console.log(`Created: ${outputPath}`);
|
||||
}
|
||||
|
||||
console.log(`\nTotal tags processed: ${Object.keys(tagApis).length}`);
|
||||
Reference in New Issue
Block a user