111 lines
3.5 KiB
JavaScript
111 lines
3.5 KiB
JavaScript
import { generate } from 'openapi-typescript-codegen';
|
||
import { readFileSync, writeFileSync, existsSync, readdirSync } from 'fs';
|
||
import { join } from 'path';
|
||
import axios from 'axios';
|
||
|
||
// 配置
|
||
const config = {
|
||
input: process.env.OPENAPI_URL || 'https://bowongai--glam-api-fastapi-app.modal.run/openapi.json', // OpenAPI规范URL
|
||
output: './src/api', // 生成的API代码输出目录
|
||
client: 'axios', // 使用axios作为HTTP客户端
|
||
useOptions: true,
|
||
useUnionTypes: true,
|
||
exportSchemas: true,
|
||
exportServices: true,
|
||
exportCore: true,
|
||
exportModels: true,
|
||
exportClient: true,
|
||
};
|
||
|
||
async function fetchOpenApiSpec(url) {
|
||
try {
|
||
console.log(`📡 正在获取OpenAPI规范: ${url}`);
|
||
const response = await axios.get(url);
|
||
return response.data;
|
||
} catch (error) {
|
||
console.error(`❌ 获取OpenAPI规范失败: ${error.message}`);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
function createIndexFiles() {
|
||
const apiDir = config.output;
|
||
|
||
// 创建services/index.ts
|
||
const servicesDir = join(apiDir, 'services');
|
||
if (existsSync(servicesDir)) {
|
||
const serviceFiles = readdirSync(servicesDir)
|
||
.filter(file => file.endsWith('.ts') && file !== 'index.ts' && file !== 'Service.ts')
|
||
.map(file => file.replace('.ts', ''));
|
||
|
||
const servicesIndexContent = serviceFiles
|
||
.map(service => `export * from './${service}';`)
|
||
.join('\n');
|
||
|
||
writeFileSync(join(servicesDir, 'index.ts'), servicesIndexContent);
|
||
console.log('📝 创建services/index.ts完成');
|
||
}
|
||
|
||
// 创建models/index.ts
|
||
const modelsDir = join(apiDir, 'models');
|
||
if (existsSync(modelsDir)) {
|
||
const modelFiles = readdirSync(modelsDir)
|
||
.filter(file => file.endsWith('.ts') && file !== 'index.ts')
|
||
.map(file => file.replace('.ts', ''));
|
||
|
||
const modelsIndexContent = modelFiles
|
||
.map(model => `export * from './${model}';`)
|
||
.join('\n');
|
||
|
||
writeFileSync(join(modelsDir, 'index.ts'), modelsIndexContent);
|
||
console.log('📝 创建models/index.ts完成');
|
||
}
|
||
}
|
||
|
||
async function generateApi() {
|
||
try {
|
||
console.log('🚀 开始生成API代码...');
|
||
|
||
let openApiSpec;
|
||
|
||
// 检查输入是URL还是本地文件
|
||
if (config.input.startsWith('http://') || config.input.startsWith('https://')) {
|
||
// 从URL获取OpenAPI规范
|
||
openApiSpec = await fetchOpenApiSpec(config.input);
|
||
} else {
|
||
// 从本地文件读取
|
||
if (!existsSync(config.input)) {
|
||
console.error(`❌ OpenAPI文件不存在: ${config.input}`);
|
||
console.log('请确保openapi.json文件存在于项目根目录,或设置OPENAPI_URL环境变量');
|
||
return;
|
||
}
|
||
openApiSpec = JSON.parse(readFileSync(config.input, 'utf8'));
|
||
}
|
||
|
||
// 生成API代码
|
||
await generate({
|
||
input: openApiSpec, // 直接传入规范对象
|
||
output: config.output,
|
||
client: config.client,
|
||
useOptions: config.useOptions,
|
||
useUnionTypes: config.useUnionTypes,
|
||
exportSchemas: config.exportSchemas,
|
||
exportServices: config.exportServices,
|
||
exportCore: config.exportCore,
|
||
exportModels: config.exportModels,
|
||
exportClient: config.exportClient,
|
||
});
|
||
|
||
console.log('✅ API代码生成完成!');
|
||
console.log(`📁 输出目录: ${config.output}`);
|
||
|
||
// 创建索引文件
|
||
createIndexFiles();
|
||
|
||
} catch (error) {
|
||
console.error('❌ 生成API代码时出错:', error);
|
||
}
|
||
}
|
||
|
||
// 运行生成器
|
||
generateApi();
|