mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-20 02:34:52 +00:00
chore: openapi doc generator (#2644)
* chore: extract the type and comment from apis * chore: template code * feat: openapi * pref: openapi generator. send into public/openapi folder
This commit is contained in:
54
scripts/openapi/index.ts
Normal file
54
scripts/openapi/index.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { parseAPI } from './utils';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { convertOpenApi } from './openapi';
|
||||
|
||||
const rootPath = 'projects/app/src/pages/api';
|
||||
const exclude = ['/admin', '/proApi'];
|
||||
|
||||
function getAllFiles(dir: string) {
|
||||
let files: string[] = [];
|
||||
const stat = fs.statSync(dir);
|
||||
if (stat.isDirectory()) {
|
||||
const list = fs.readdirSync(dir);
|
||||
list.forEach((item) => {
|
||||
const fullPath = path.join(dir, item);
|
||||
if (!exclude.some((excluded) => fullPath.includes(excluded))) {
|
||||
files = files.concat(getAllFiles(fullPath));
|
||||
}
|
||||
});
|
||||
} else {
|
||||
files.push(dir);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
const searchPath = process.env.SEARCH_PATH || '';
|
||||
|
||||
const files = getAllFiles(path.join(rootPath, searchPath));
|
||||
// console.log(files)
|
||||
const apis = files.map((file) => {
|
||||
return parseAPI({ path: file, rootPath });
|
||||
});
|
||||
|
||||
const openapi = convertOpenApi({
|
||||
apis,
|
||||
openapi: '3.0.0',
|
||||
info: {
|
||||
title: 'FastGPT OpenAPI',
|
||||
version: '1.0.0',
|
||||
author: 'FastGPT'
|
||||
},
|
||||
servers: [
|
||||
{
|
||||
url: 'http://localhost:4000'
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
const json = JSON.stringify(openapi, null, 2);
|
||||
|
||||
fs.writeFileSync('./scripts/openapi/openapi.json', json);
|
||||
fs.writeFileSync('./scripts/openapi/openapi.out', JSON.stringify(apis, null, 2));
|
||||
|
||||
console.log('Total APIs:', files.length);
|
Reference in New Issue
Block a user