86 lines
2.7 KiB
TypeScript
86 lines
2.7 KiB
TypeScript
import { NestFactory } from '@nestjs/core';
|
||
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
||
import { AppModule } from './app.module';
|
||
import * as fs from 'fs';
|
||
|
||
async function generateSwaggerJson() {
|
||
const app = await NestFactory.create(AppModule, { logger: false });
|
||
|
||
// 设置API前缀(与main.ts保持一致)
|
||
app.setGlobalPrefix('api/v1', {
|
||
exclude: ['docs', 'docs/(.*)'],
|
||
});
|
||
|
||
const config = new DocumentBuilder()
|
||
.setTitle('多平台小程序统一后台API')
|
||
.setDescription(
|
||
`
|
||
## 功能特性
|
||
- 🔐 统一用户认证 (微信/支付宝/百度/字节跳动等)
|
||
- 🎨 AI模板系统 (图片/视频生成)
|
||
- 💰 积分系统 (积分获取、消耗、管理)
|
||
- 📱 多平台适配 (8大平台支持)
|
||
- 🧩 扩展数据存储 (灵活的JSON数据)
|
||
|
||
## 认证方式
|
||
使用JWT Bearer Token进行API认证
|
||
|
||
## 响应格式
|
||
所有API响应都遵循统一格式:
|
||
\`\`\`json
|
||
{
|
||
"code": 200,
|
||
"message": "success",
|
||
"data": {},
|
||
"timestamp": 1703001000000,
|
||
"traceId": "trace-uuid"
|
||
}
|
||
\`\`\`
|
||
`,
|
||
)
|
||
.setVersion('1.0.0')
|
||
.setContact('开发团队', 'https://example.com', 'dev@example.com')
|
||
.setLicense('MIT', 'https://opensource.org/licenses/MIT')
|
||
.addBearerAuth(
|
||
{
|
||
type: 'http',
|
||
scheme: 'bearer',
|
||
bearerFormat: 'JWT',
|
||
name: 'JWT',
|
||
description: 'Enter JWT token',
|
||
in: 'header',
|
||
},
|
||
'JWT-auth',
|
||
)
|
||
.addTag('用户管理', '用户注册、登录、信息管理')
|
||
.addTag('平台适配', '各平台特定接口和数据同步')
|
||
.addTag('AI模板系统', 'AI图片/视频生成模板管理')
|
||
.addTag('积分系统', '积分获取、消耗、查询管理')
|
||
.addTag('扩展服务', '预留的扩展功能接口')
|
||
.addServer(`http://localhost:${process.env.PORT || 3003}`, '开发环境')
|
||
.addServer(
|
||
process.env.TEST_SERVER_URL ||
|
||
'https://sd2s2bl25ni4n75na2bog.apigateway-cn-beijing.volceapi.com',
|
||
'测试环境',
|
||
)
|
||
.addServer(
|
||
process.env.PROD_SERVER_URL ||
|
||
'https://sd2s2bl25ni4n75na2bog.apigateway-cn-beijing.volceapi.com',
|
||
'生产环境',
|
||
)
|
||
.build();
|
||
|
||
const document = SwaggerModule.createDocument(app as any, config);
|
||
|
||
fs.writeFileSync('./swagger.json', JSON.stringify(document, null, 2));
|
||
fs.writeFileSync('./docs/api-spec.json', JSON.stringify(document, null, 2));
|
||
|
||
console.log('Swagger JSON文档已生成:');
|
||
console.log('- ./swagger.json');
|
||
console.log('- ./docs/api-spec.json');
|
||
|
||
await app.close();
|
||
}
|
||
|
||
generateSwaggerJson().catch(console.error);
|