mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-22 12:20:34 +00:00

* chore: extract the type and comment from apis * chore: template code * feat: openapi * pref: openapi generator. send into public/openapi folder
55 lines
1.3 KiB
TypeScript
55 lines
1.3 KiB
TypeScript
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);
|