create-dp-koa 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -0
- package/index.mjs +97 -0
- package/package.json +33 -0
- package/template/.env.development +9 -0
- package/template/.env.production +12 -0
- package/template/.github/workflows/ci-cd.yml +182 -0
- package/template/.trae/documents/controller_development_plan.md +386 -0
- package/template/.trae/skills/00-backend-core.skill.md +50 -0
- package/template/.trae/skills/01-backend-skill-router.skill.md +55 -0
- package/template/.trae/skills/10-backend-api.skill.md +54 -0
- package/template/.trae/skills/11-backend-controller-recipes.skill.md +107 -0
- package/template/.trae/skills/20-backend-repository.skill.md +25 -0
- package/template/.trae/skills/21-backend-service.skill.md +135 -0
- package/template/.trae/skills/25-backend-comments-and-doc.skill.md +97 -0
- package/template/.trae/skills/30-backend-validation.skill.md +320 -0
- package/template/.trae/skills/40-backend-error-logging.skill.md +21 -0
- package/template/.trae/skills/50-backend-bootstrap-lifecycle.skill.md +90 -0
- package/template/.trae/skills/60-backend-router-registration.skill.md +71 -0
- package/template/.trae/skills/70-backend-middleware.skill.md +98 -0
- package/template/.trae/skills/80-backend-utils-and-libs.skill.md +90 -0
- package/template/.trae/skills/85-backend-plugins.rule.md +64 -0
- package/template/.trae/skills/90-backend-testing.skill.md +29 -0
- package/template/.trae/skills/README.md +49 -0
- package/template/.vscode/launch.json +38 -0
- package/template/.vscode/settings.json +1 -0
- package/template/Dockerfile +36 -0
- package/template/README.md +229 -0
- package/template/docker-compose.yml +135 -0
- package/template/docs/API_DOCUMENTATION.md +837 -0
- package/template/docs/ARCHITECTURE_REFACTOR.md +109 -0
- package/template/docs/CACHE_MIGRATION_GUIDE.md +142 -0
- package/template/docs/DEPLOYMENT_GUIDE.md +1062 -0
- package/template/docs/DEVELOPMENT_GUIDE.md +1097 -0
- package/template/docs/DOCUMENTATION_CLEANUP_REPORT.md +166 -0
- package/template/docs/DOCUMENTATION_COMPLETION_REPORT.md +223 -0
- package/template/docs/DOCUMENTATION_INDEX.md +294 -0
- package/template/docs/DOCUMENTATION_STRUCTURE.md +221 -0
- package/template/docs/ENTERPRISE_ANNOTATION_SYSTEM_GUIDE.md +2069 -0
- package/template/docs/ENTERPRISE_DATABASE_ARCHITECTURE.md +318 -0
- package/template/docs/ENTERPRISE_DEPLOYMENT_GUIDE.md +547 -0
- package/template/docs/ENTERPRISE_ERROR_HANDLING_GUIDE.md +357 -0
- package/template/docs/ENTERPRISE_LOGGING_SYSTEM_GUIDE.md +494 -0
- package/template/docs/ENVIRONMENT_CONFIG_EXAMPLE.md +69 -0
- package/template/docs/FINAL_IMPLEMENTATION_SUMMARY.md +206 -0
- package/template/docs/HEALTH_CHECK_ROUTE_FIX.md +134 -0
- package/template/docs/IMPLEMENTATION_CHECKLIST.md +204 -0
- package/template/docs/INSTALLATION_GUIDE.md +611 -0
- package/template/docs/INTERCEPTOR_TESTING_REPORT.md +226 -0
- package/template/docs/INTERCEPTOR_TESTING_SCRIPTS.md +143 -0
- package/template/docs/LOGGING_OPTIMIZATION_GUIDE.md +126 -0
- package/template/docs/MEMORY_DATABASE_GUIDE.md +212 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_GUIDE.md +345 -0
- package/template/docs/NEW_ROUTER_INTEGRATION_SUMMARY.md +259 -0
- package/template/docs/NEW_ROUTER_USAGE_GUIDE.md +364 -0
- package/template/docs/QUICK_START.md +268 -0
- package/template/docs/ROUTE_SLASH_COMPATIBILITY_FIX.md +191 -0
- package/template/docs/SERVICE_INTERCEPTOR_GUIDE.md +243 -0
- package/template/docs/SERVICE_LAYER_INDEX.md +205 -0
- package/template/docs/SERVICE_PATTERN_GUIDE.md +270 -0
- package/template/docs/SERVICE_RETURN_VALUE_SPECIFICATION.md +466 -0
- package/template/docs/SWAGGER_DEBUG_MODE_GUIDE.md +80 -0
- package/template/docs/SWAGGER_INTEGRATION_GUIDE.md +416 -0
- package/template/docs/TRANSACTION_MANAGER_USAGE.md +360 -0
- package/template/docs/TROUBLESHOOTING.md +869 -0
- package/template/env.production.example +62 -0
- package/template/jest.config.js +34 -0
- package/template/package-lock.json +13240 -0
- package/template/package.json +119 -0
- package/template/patches/typeorm+0.3.25.patch +22 -0
- package/template/scripts/sync-template.mjs +84 -0
- package/template/scripts/test-annotation-system.sh +48 -0
- package/template/scripts/test-core-functionality.sh +28 -0
- package/template/src/annotations/decorators/ConfigManagement.ts +9 -0
- package/template/src/annotations/decorators/DistributedTracing.ts +9 -0
- package/template/src/annotations/decorators/EnterprisePerformance.ts +9 -0
- package/template/src/annotations/decorators/PerformanceMonitor.ts +32 -0
- package/template/src/annotations/decorators/SecurityAudit.ts +9 -0
- package/template/src/annotations/index.ts +50 -0
- package/template/src/annotations/processors/ConfigManagementProcessor.ts +369 -0
- package/template/src/annotations/processors/DistributedTracingProcessor.ts +288 -0
- package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +189 -0
- package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +101 -0
- package/template/src/annotations/processors/SecurityAuditProcessor.ts +345 -0
- package/template/src/annotations/processors/SwaggerProcessor.ts +612 -0
- package/template/src/annotations/processors/index.ts +10 -0
- package/template/src/app.ts +123 -0
- package/template/src/controllers/base.controller.ts +41 -0
- package/template/src/controllers/cacheManagement.controller.ts +131 -0
- package/template/src/controllers/captcha.controller.ts +57 -0
- package/template/src/controllers/demo/AnnotationDemoController.ts +118 -0
- package/template/src/controllers/example/EnterpriseExampleController.ts +297 -0
- package/template/src/controllers/example/ExampleController.ts +110 -0
- package/template/src/controllers/example/NewAnnotationExampleController.ts +159 -0
- package/template/src/controllers/example/SwaggerExampleController.ts +205 -0
- package/template/src/controllers/example/TransactionExample.controller.ts +336 -0
- package/template/src/controllers/health.controller.ts +235 -0
- package/template/src/controllers/home/register.controller.ts +58 -0
- package/template/src/controllers/home/ytGoods.controller.ts +92 -0
- package/template/src/controllers/home/ytShop.controller.ts +135 -0
- package/template/src/controllers/home/ytUser.controller.ts +89 -0
- package/template/src/controllers/logManagement.controller.ts +396 -0
- package/template/src/controllers/public/emailSend.controller.ts +65 -0
- package/template/src/controllers/public/ytUserAuth.controller.ts +174 -0
- package/template/src/controllers/testData.controller.ts +253 -0
- package/template/src/dto/controller/example/NewAnnotationExampleController.dto.ts +73 -0
- package/template/src/dto/controller/home/emailSend.controller.dto.ts +40 -0
- package/template/src/dto/controller/home/register.controller.dto.ts +45 -0
- package/template/src/dto/controller/home/ytGoods.controller.dto.ts +55 -0
- package/template/src/dto/controller/home/ytShop.controller.dto.ts +69 -0
- package/template/src/dto/controller/home/ytUser.controller.dto.ts +44 -0
- package/template/src/dto/controller/public/ytUserAuth.controller.dto.ts +63 -0
- package/template/src/dto/goods.dto.ts +212 -0
- package/template/src/dto/service/ytService.dto.ts +13 -0
- package/template/src/dto/user.dto.ts +177 -0
- package/template/src/entity/base.entity.ts +13 -0
- package/template/src/entity/columnTypes.ts +13 -0
- package/template/src/entity/goodsImagesUnlockKey.entity.ts +33 -0
- package/template/src/entity/goodsUnlocker.entity.ts +34 -0
- package/template/src/entity/index.ts +15 -0
- package/template/src/entity/shop.entity.ts +52 -0
- package/template/src/entity/shopUser.entity.ts +41 -0
- package/template/src/entity/ytGoods.entity.ts +94 -0
- package/template/src/entity/ytUser.entity.ts +96 -0
- package/template/src/examples/InterceptorExampleRunner.ts +284 -0
- package/template/src/examples/ServiceInterceptorExample.ts +214 -0
- package/template/src/examples/SwaggerProcessorExample.ts +169 -0
- package/template/src/examples/TransactionManagerDemo.ts +377 -0
- package/template/src/examples/cacheExamples.ts +155 -0
- package/template/src/framework/decorator/controller.ts +311 -0
- package/template/src/framework/decorator/processor/AnnotationDecorators.ts +100 -0
- package/template/src/framework/decorator/processor/AnnotationProcessor.ts +156 -0
- package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +45 -0
- package/template/src/framework/decorator/processor/AnnotationRegistry.ts +117 -0
- package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +95 -0
- package/template/src/framework/decorator/processor/ProcessorManager.ts +76 -0
- package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +126 -0
- package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +207 -0
- package/template/src/framework/decorator/refactored/DecoratorFactory.ts +99 -0
- package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +125 -0
- package/template/src/framework/decorator/refactored/DecoratorValidator.ts +128 -0
- package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +139 -0
- package/template/src/framework/decorator/refactored/index.ts +98 -0
- package/template/src/framework/decorator/swagger.ts +150 -0
- package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +375 -0
- package/template/src/framework/interceptors/ServiceCallInterceptor.ts +348 -0
- package/template/src/framework/interceptors/index.ts +19 -0
- package/template/src/framework/plugins/registry.ts +63 -0
- package/template/src/framework/plugins/types.ts +15 -0
- package/template/src/framework/types/ServiceResult.ts +151 -0
- package/template/src/framework/types/index.ts +16 -0
- package/template/src/framework/utils/CacheManager.ts +430 -0
- package/template/src/framework/utils/CacheService.ts +248 -0
- package/template/src/framework/utils/DtoValidator.ts +164 -0
- package/template/src/framework/utils/MigrationHelper.ts +179 -0
- package/template/src/framework/utils/MigrationManager.ts +256 -0
- package/template/src/framework/utils/NewRouter.ts +207 -0
- package/template/src/framework/utils/TransactionManager.ts +172 -0
- package/template/src/framework/utils/bootstrap.ts +445 -0
- package/template/src/framework/utils/cache.ts +269 -0
- package/template/src/framework/utils/databaseConfig.ts +148 -0
- package/template/src/framework/utils/db.ts +39 -0
- package/template/src/framework/utils/dbMonitor.ts +106 -0
- package/template/src/framework/utils/dynamicSwagger.ts +410 -0
- package/template/src/framework/utils/function.ts +61 -0
- package/template/src/framework/utils/gracefulShutdown.ts +131 -0
- package/template/src/framework/utils/logger.ts +388 -0
- package/template/src/framework/utils/metrics.ts +182 -0
- package/template/src/framework/utils/router.ts +417 -0
- package/template/src/framework/utils/swagger.ts +184 -0
- package/template/src/framework/utils/testDb.ts +19 -0
- package/template/src/framework/utils/token.ts +23 -0
- package/template/src/framework/utils/transform.ts +17 -0
- package/template/src/libs/aokEmailSender.ts +42 -0
- package/template/src/libs/captcha.ts +37 -0
- package/template/src/libs/cos.ts +45 -0
- package/template/src/libs/mCache.ts +7 -0
- package/template/src/libs/serviceValidate.ts +3 -0
- package/template/src/libs/tecentSms.ts +51 -0
- package/template/src/middlewares/a.middleware.ts +6 -0
- package/template/src/middlewares/error.middleware.ts +14 -0
- package/template/src/middlewares/logging.middleware.ts +187 -0
- package/template/src/middlewares/static.middleware.ts +79 -0
- package/template/src/middlewares/swagger.middleware.ts +70 -0
- package/template/src/middlewares/token.middleware.ts +32 -0
- package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +172 -0
- package/template/src/migrations/index.ts +6 -0
- package/template/src/plugins/weboffice/core/context.ts +47 -0
- package/template/src/plugins/weboffice/core/errors.ts +51 -0
- package/template/src/plugins/weboffice/core/types.ts +63 -0
- package/template/src/plugins/weboffice/core/utils.ts +7 -0
- package/template/src/plugins/weboffice/entities/index.ts +3 -0
- package/template/src/plugins/weboffice/entities/webofficeFile.entity.ts +28 -0
- package/template/src/plugins/weboffice/entities/webofficeFileVersion.entity.ts +29 -0
- package/template/src/plugins/weboffice/http/routes.ts +179 -0
- package/template/src/plugins/weboffice/index.ts +23 -0
- package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +274 -0
- package/template/src/repository/UserRepository.ts +122 -0
- package/template/src/repository/base/BaseRepository.ts +124 -0
- package/template/src/repository/interfaces/IBaseRepository.ts +67 -0
- package/template/src/routers/index.ts +49 -0
- package/template/src/service/base.service.ts +116 -0
- package/template/src/service/paramValidateTest.service.ts +139 -0
- package/template/src/service/ytGoods.service.ts +42 -0
- package/template/src/service/ytShop.service.ts +90 -0
- package/template/src/service/ytUser.service.ts +451 -0
- package/template/src/test/swaggerParameterTest.ts +90 -0
- package/template/src/utils/testDataInitializer.ts +296 -0
- package/template/static/output.json +15203 -0
- package/template/test/controllers/controllers.test.ts +173 -0
- package/template/test/controllers/example/ExampleController.test.ts +222 -0
- package/template/test/controllers/example/NewAnnotationExampleController.test.ts +200 -0
- package/template/test/framework/TransactionManagerDemo.test.ts +363 -0
- package/template/test/framework/annotation/AnnotationDecorators.test.ts +222 -0
- package/template/test/framework/annotation/AnnotationExecutor.test.ts +246 -0
- package/template/test/framework/annotation/AnnotationProcessor.test.ts +179 -0
- package/template/test/framework/annotation/CustomProcessors.test.ts +313 -0
- package/template/test/framework/annotation/DefaultProcessors.test.ts +371 -0
- package/template/test/framework/annotation/NewRouter.test.ts +272 -0
- package/template/test/framework/annotation/ProcessorManager.test.ts +248 -0
- package/template/test/framework/annotation/setup.ts +26 -0
- package/template/test/framework/cache.test.ts +101 -0
- package/template/test/framework/databaseConfig.test.ts +142 -0
- package/template/test/integration/integration.test.ts +153 -0
- package/template/test/plugins/weboffice/http.routes.int.test.ts +61 -0
- package/template/test/service/business.test.ts +87 -0
- package/template/test/service/paramValidateTest.service.test.ts +184 -0
- package/template/test/service/ytUser.service.test.ts +566 -0
- package/template/test/setup.ts +20 -0
- package/template/test/setupAfterEnv.ts +14 -0
- package/template/test/utils/testHelpers.ts +220 -0
- package/template/test_output.txt +0 -0
- package/template/tsconfig.build.json +17 -0
- package/template/tsconfig.json +31 -0
- package/template/webpack.config.js +71 -0
- package/template/yarn.lock +7354 -0
|
@@ -0,0 +1,612 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Swagger注解处理器
|
|
3
|
+
* 负责处理Swagger相关的注解,包括元数据收集、验证和规范生成
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { logger } from '@src/framework/utils/logger';
|
|
7
|
+
import { getSwaggerMetadata } from '@src/framework/decorator/swagger';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Swagger注解处理器接口
|
|
11
|
+
*/
|
|
12
|
+
export interface SwaggerProcessor {
|
|
13
|
+
/**
|
|
14
|
+
* 处理控制器的Swagger注解
|
|
15
|
+
*/
|
|
16
|
+
processController(target: any): SwaggerControllerMetadata;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 处理方法级别的Swagger注解
|
|
20
|
+
*/
|
|
21
|
+
processMethod(target: any, methodName: string): SwaggerMethodMetadata;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 验证Swagger注解的完整性
|
|
25
|
+
*/
|
|
26
|
+
validateSwaggerAnnotations(target: any): ValidationResult;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 生成OpenAPI规范
|
|
30
|
+
*/
|
|
31
|
+
generateOpenAPISpec(controllers: any[]): OpenAPISpec;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Swagger控制器元数据
|
|
36
|
+
*/
|
|
37
|
+
export interface SwaggerControllerMetadata {
|
|
38
|
+
tags: string[];
|
|
39
|
+
description?: string;
|
|
40
|
+
basePath?: string;
|
|
41
|
+
methods: Map<string, SwaggerMethodMetadata>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Swagger方法元数据
|
|
46
|
+
*/
|
|
47
|
+
export interface SwaggerMethodMetadata {
|
|
48
|
+
operationId: string;
|
|
49
|
+
summary: string;
|
|
50
|
+
description: string;
|
|
51
|
+
tags: string[];
|
|
52
|
+
parameters: SwaggerParameter[];
|
|
53
|
+
requestBody?: SwaggerRequestBody;
|
|
54
|
+
responses: SwaggerResponse[];
|
|
55
|
+
deprecated?: boolean;
|
|
56
|
+
security?: SwaggerSecurity[];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Swagger参数定义
|
|
61
|
+
*/
|
|
62
|
+
export interface SwaggerParameter {
|
|
63
|
+
name: string;
|
|
64
|
+
in: 'path' | 'query' | 'header' | 'cookie';
|
|
65
|
+
description?: string;
|
|
66
|
+
required: boolean;
|
|
67
|
+
schema: any;
|
|
68
|
+
example?: any;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Swagger请求体定义
|
|
73
|
+
*/
|
|
74
|
+
export interface SwaggerRequestBody {
|
|
75
|
+
description?: string;
|
|
76
|
+
required: boolean;
|
|
77
|
+
content: {
|
|
78
|
+
[mediaType: string]: {
|
|
79
|
+
schema: any;
|
|
80
|
+
examples?: Record<string, any>;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Swagger响应定义
|
|
87
|
+
*/
|
|
88
|
+
export interface SwaggerResponse {
|
|
89
|
+
status: number;
|
|
90
|
+
description: string;
|
|
91
|
+
content?: {
|
|
92
|
+
[mediaType: string]: {
|
|
93
|
+
schema: any;
|
|
94
|
+
examples?: Record<string, any>;
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Swagger安全定义
|
|
101
|
+
*/
|
|
102
|
+
export interface SwaggerSecurity {
|
|
103
|
+
name: string;
|
|
104
|
+
scopes?: string[];
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 验证结果
|
|
109
|
+
*/
|
|
110
|
+
export interface ValidationResult {
|
|
111
|
+
isValid: boolean;
|
|
112
|
+
errors: string[];
|
|
113
|
+
warnings: string[];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* OpenAPI规范
|
|
118
|
+
*/
|
|
119
|
+
export interface OpenAPISpec {
|
|
120
|
+
openapi: string;
|
|
121
|
+
info: {
|
|
122
|
+
title: string;
|
|
123
|
+
version: string;
|
|
124
|
+
description?: string;
|
|
125
|
+
};
|
|
126
|
+
servers: Array<{
|
|
127
|
+
url: string;
|
|
128
|
+
description?: string;
|
|
129
|
+
}>;
|
|
130
|
+
paths: Record<string, any>;
|
|
131
|
+
components?: {
|
|
132
|
+
schemas?: Record<string, any>;
|
|
133
|
+
securitySchemes?: Record<string, any>;
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Swagger注解处理器实现
|
|
139
|
+
*/
|
|
140
|
+
class SwaggerProcessorImpl implements SwaggerProcessor {
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* 处理控制器的Swagger注解
|
|
144
|
+
*/
|
|
145
|
+
processController(target: any): SwaggerControllerMetadata {
|
|
146
|
+
try {
|
|
147
|
+
const swaggerMetadata = getSwaggerMetadata(target);
|
|
148
|
+
|
|
149
|
+
const controllerMetadata: SwaggerControllerMetadata = {
|
|
150
|
+
tags: swaggerMetadata.tags || [],
|
|
151
|
+
methods: new Map()
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
// 处理所有方法的Swagger注解
|
|
155
|
+
const httpMethods = this.getHttpMethods(target);
|
|
156
|
+
httpMethods.forEach(methodName => {
|
|
157
|
+
const methodMetadata = this.processMethod(target, methodName);
|
|
158
|
+
controllerMetadata.methods.set(methodName, methodMetadata);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
logger.debug(`处理控制器 ${target.name} 的Swagger注解完成`);
|
|
162
|
+
return controllerMetadata;
|
|
163
|
+
|
|
164
|
+
} catch (error) {
|
|
165
|
+
logger.error(`处理控制器 ${target.name} 的Swagger注解失败:`, error as Error);
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 处理方法级别的Swagger注解
|
|
172
|
+
*/
|
|
173
|
+
processMethod(target: any, methodName: string): SwaggerMethodMetadata {
|
|
174
|
+
try {
|
|
175
|
+
const swaggerMetadata = getSwaggerMetadata(target);
|
|
176
|
+
|
|
177
|
+
// 获取方法的基本信息
|
|
178
|
+
const methodInfo = swaggerMetadata.methods.get(methodName) || {};
|
|
179
|
+
const params = swaggerMetadata.params.get(methodName) || [];
|
|
180
|
+
const body = swaggerMetadata.bodies.get(methodName);
|
|
181
|
+
const responses = swaggerMetadata.responses.get(methodName) || [];
|
|
182
|
+
|
|
183
|
+
// 自动推断路径参数
|
|
184
|
+
const autoInferredParams = this.autoInferParameters(target, methodName);
|
|
185
|
+
const allParams = [...this.processParameters(params), ...autoInferredParams];
|
|
186
|
+
|
|
187
|
+
const methodMetadata: SwaggerMethodMetadata = {
|
|
188
|
+
operationId: methodInfo.operationId || methodName,
|
|
189
|
+
summary: methodInfo.summary || this.generateDefaultSummary(methodName),
|
|
190
|
+
description: methodInfo.description || this.generateDefaultDescription(methodName),
|
|
191
|
+
tags: methodInfo.tags || [],
|
|
192
|
+
parameters: allParams,
|
|
193
|
+
responses: this.processResponses(responses)
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// 处理请求体
|
|
197
|
+
if (body) {
|
|
198
|
+
methodMetadata.requestBody = this.processRequestBody(body);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
logger.debug(`处理方法 ${methodName} 的Swagger注解完成`);
|
|
202
|
+
return methodMetadata;
|
|
203
|
+
|
|
204
|
+
} catch (error) {
|
|
205
|
+
logger.error(`处理方法 ${methodName} 的Swagger注解失败:`, error as Error);
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* 验证Swagger注解的完整性
|
|
212
|
+
*/
|
|
213
|
+
validateSwaggerAnnotations(target: any): ValidationResult {
|
|
214
|
+
const errors: string[] = [];
|
|
215
|
+
const warnings: string[] = [];
|
|
216
|
+
|
|
217
|
+
try {
|
|
218
|
+
const swaggerMetadata = getSwaggerMetadata(target);
|
|
219
|
+
const httpMethods = this.getHttpMethods(target);
|
|
220
|
+
|
|
221
|
+
// 验证每个方法的注解
|
|
222
|
+
httpMethods.forEach(methodName => {
|
|
223
|
+
const methodInfo = swaggerMetadata.methods.get(methodName);
|
|
224
|
+
const responses = swaggerMetadata.responses.get(methodName) || [];
|
|
225
|
+
|
|
226
|
+
// 检查是否有基本的方法信息
|
|
227
|
+
if (!methodInfo) {
|
|
228
|
+
warnings.push(`方法 ${methodName} 缺少 @Api 注解`);
|
|
229
|
+
} else {
|
|
230
|
+
// 检查summary
|
|
231
|
+
if (!methodInfo.summary) {
|
|
232
|
+
warnings.push(`方法 ${methodName} 缺少 summary 描述`);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// 检查description
|
|
236
|
+
if (!methodInfo.description) {
|
|
237
|
+
warnings.push(`方法 ${methodName} 缺少 description 描述`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// 检查响应定义
|
|
242
|
+
if (responses.length === 0) {
|
|
243
|
+
warnings.push(`方法 ${methodName} 缺少 @ApiResponse 注解`);
|
|
244
|
+
} else {
|
|
245
|
+
// 检查是否有200响应
|
|
246
|
+
const has200Response = responses.some((r: any) => r.status === 200);
|
|
247
|
+
if (!has200Response) {
|
|
248
|
+
warnings.push(`方法 ${methodName} 缺少200状态码的响应定义`);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
// 检查控制器标签
|
|
254
|
+
if (!swaggerMetadata.tags || swaggerMetadata.tags.length === 0) {
|
|
255
|
+
warnings.push(`控制器 ${target.name} 缺少 @ApiTags 注解`);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
logger.debug(`验证控制器 ${target.name} 的Swagger注解完成`);
|
|
259
|
+
|
|
260
|
+
return {
|
|
261
|
+
isValid: errors.length === 0,
|
|
262
|
+
errors,
|
|
263
|
+
warnings
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
} catch (error) {
|
|
267
|
+
logger.error(`验证控制器 ${target.name} 的Swagger注解失败:`, error as Error);
|
|
268
|
+
return {
|
|
269
|
+
isValid: false,
|
|
270
|
+
errors: [`验证失败: ${(error as Error).message}`],
|
|
271
|
+
warnings: []
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* 生成OpenAPI规范
|
|
278
|
+
*/
|
|
279
|
+
generateOpenAPISpec(controllers: any[]): OpenAPISpec {
|
|
280
|
+
try {
|
|
281
|
+
const spec: OpenAPISpec = {
|
|
282
|
+
openapi: '3.0.0',
|
|
283
|
+
info: {
|
|
284
|
+
title: 'DP Koa Framework API',
|
|
285
|
+
version: '1.0.0',
|
|
286
|
+
description: '基于Koa的企业级框架API文档'
|
|
287
|
+
},
|
|
288
|
+
servers: [
|
|
289
|
+
{
|
|
290
|
+
url: 'http://localhost:3000',
|
|
291
|
+
description: '开发环境'
|
|
292
|
+
}
|
|
293
|
+
],
|
|
294
|
+
paths: {},
|
|
295
|
+
components: {
|
|
296
|
+
schemas: {},
|
|
297
|
+
securitySchemes: {}
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
// 处理每个控制器
|
|
302
|
+
controllers.forEach(controller => {
|
|
303
|
+
const controllerMetadata = this.processController(controller);
|
|
304
|
+
|
|
305
|
+
// 获取控制器的路由信息
|
|
306
|
+
const httpMethods = this.getHttpMethods(controller);
|
|
307
|
+
|
|
308
|
+
httpMethods.forEach(methodName => {
|
|
309
|
+
const methodMetadata = controllerMetadata.methods.get(methodName);
|
|
310
|
+
if (methodMetadata) {
|
|
311
|
+
// 获取路由路径
|
|
312
|
+
const routePath = this.getRoutePath(controller, methodName);
|
|
313
|
+
if (routePath) {
|
|
314
|
+
const swaggerPath = routePath.replace(/:(\w+)/g, '{$1}');
|
|
315
|
+
const httpMethod = this.getHttpMethod(controller, methodName);
|
|
316
|
+
|
|
317
|
+
if (!spec.paths[swaggerPath]) {
|
|
318
|
+
spec.paths[swaggerPath] = {};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
spec.paths[swaggerPath][httpMethod] = {
|
|
322
|
+
operationId: methodMetadata.operationId,
|
|
323
|
+
summary: methodMetadata.summary,
|
|
324
|
+
description: methodMetadata.description,
|
|
325
|
+
tags: methodMetadata.tags,
|
|
326
|
+
parameters: methodMetadata.parameters,
|
|
327
|
+
responses: this.convertResponsesToOpenAPI(methodMetadata.responses)
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
if (methodMetadata.requestBody) {
|
|
331
|
+
spec.paths[swaggerPath][httpMethod].requestBody = methodMetadata.requestBody;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
logger.info(`生成OpenAPI规范完成,包含 ${Object.keys(spec.paths).length} 个路径`);
|
|
339
|
+
return spec;
|
|
340
|
+
|
|
341
|
+
} catch (error) {
|
|
342
|
+
logger.error('生成OpenAPI规范失败:', error as Error);
|
|
343
|
+
throw error;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* 处理参数
|
|
349
|
+
*/
|
|
350
|
+
private processParameters(params: any[]): SwaggerParameter[] {
|
|
351
|
+
return params.map(param => ({
|
|
352
|
+
name: param.name,
|
|
353
|
+
in: param.in,
|
|
354
|
+
description: param.description,
|
|
355
|
+
required: param.required || false,
|
|
356
|
+
schema: param.schema || { type: 'string' },
|
|
357
|
+
example: param.example
|
|
358
|
+
}));
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* 自动推断路径参数
|
|
363
|
+
*/
|
|
364
|
+
private autoInferParameters(controller: any, methodName: string): SwaggerParameter[] {
|
|
365
|
+
const parameters: SwaggerParameter[] = [];
|
|
366
|
+
|
|
367
|
+
try {
|
|
368
|
+
// 获取方法的URL路径
|
|
369
|
+
const urlPath = this.getMethodUrlPath(controller, methodName);
|
|
370
|
+
if (urlPath) {
|
|
371
|
+
// 提取路径参数 (:paramName)
|
|
372
|
+
const pathParamMatches = urlPath.match(/:(\w+)/g);
|
|
373
|
+
if (pathParamMatches) {
|
|
374
|
+
pathParamMatches.forEach(paramMatch => {
|
|
375
|
+
const paramName = paramMatch.substring(1); // 移除冒号
|
|
376
|
+
|
|
377
|
+
// 检查是否已经通过@ApiParam定义了
|
|
378
|
+
const swaggerMetadata = getSwaggerMetadata(controller);
|
|
379
|
+
const existingParams = swaggerMetadata.params.get(methodName) || [];
|
|
380
|
+
const alreadyDefined = existingParams.some((p: any) => p.name === paramName && p.in === 'path');
|
|
381
|
+
|
|
382
|
+
if (!alreadyDefined) {
|
|
383
|
+
parameters.push({
|
|
384
|
+
name: paramName,
|
|
385
|
+
in: 'path',
|
|
386
|
+
description: this.generateParameterDescription(paramName),
|
|
387
|
+
required: true,
|
|
388
|
+
schema: { type: 'string' }
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
logger.debug(`为方法 ${methodName} 自动推断出 ${parameters.length} 个路径参数`);
|
|
396
|
+
return parameters;
|
|
397
|
+
|
|
398
|
+
} catch (error) {
|
|
399
|
+
logger.warn(`自动推断方法 ${methodName} 的参数失败:`, error as Error);
|
|
400
|
+
return [];
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* 获取方法的URL路径
|
|
406
|
+
*/
|
|
407
|
+
private getMethodUrlPath(controller: any, methodName: string): string | null {
|
|
408
|
+
// 检查GET方法
|
|
409
|
+
if (controller.$_GetMethods) {
|
|
410
|
+
const getMethods = Array.from(controller.$_GetMethods) as Array<{methodName: string, url: string}>;
|
|
411
|
+
const method = getMethods.find(m => m.methodName === methodName);
|
|
412
|
+
if (method) return method.url;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// 检查POST方法
|
|
416
|
+
if (controller.$_PostMethods) {
|
|
417
|
+
const postMethods = Array.from(controller.$_PostMethods) as Array<{methodName: string, url: string}>;
|
|
418
|
+
const method = postMethods.find(m => m.methodName === methodName);
|
|
419
|
+
if (method) return method.url;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// 检查PUT方法
|
|
423
|
+
if (controller.$_PutMethods) {
|
|
424
|
+
const putMethods = Array.from(controller.$_PutMethods) as Array<{methodName: string, url: string}>;
|
|
425
|
+
const method = putMethods.find(m => m.methodName === methodName);
|
|
426
|
+
if (method) return method.url;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// 检查DELETE方法
|
|
430
|
+
if (controller.$_DelMethods) {
|
|
431
|
+
const delMethods = Array.from(controller.$_DelMethods) as Array<{methodName: string, url: string}>;
|
|
432
|
+
const method = delMethods.find(m => m.methodName === methodName);
|
|
433
|
+
if (method) return method.url;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
return null;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* 生成参数描述
|
|
441
|
+
*/
|
|
442
|
+
private generateParameterDescription(paramName: string): string {
|
|
443
|
+
const descriptions: Record<string, string> = {
|
|
444
|
+
'id': 'ID',
|
|
445
|
+
'userId': '用户ID',
|
|
446
|
+
'productId': '产品ID',
|
|
447
|
+
'orderId': '订单ID',
|
|
448
|
+
'categoryId': '分类ID',
|
|
449
|
+
'page': '页码',
|
|
450
|
+
'size': '每页数量',
|
|
451
|
+
'limit': '限制数量',
|
|
452
|
+
'offset': '偏移量'
|
|
453
|
+
};
|
|
454
|
+
|
|
455
|
+
return descriptions[paramName] || `${paramName}参数`;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/**
|
|
459
|
+
* 处理请求体
|
|
460
|
+
*/
|
|
461
|
+
private processRequestBody(body: any): SwaggerRequestBody {
|
|
462
|
+
return {
|
|
463
|
+
description: body.description,
|
|
464
|
+
required: body.required || false,
|
|
465
|
+
content: {
|
|
466
|
+
'application/json': {
|
|
467
|
+
schema: body.schema,
|
|
468
|
+
examples: body.examples
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* 处理响应
|
|
476
|
+
*/
|
|
477
|
+
private processResponses(responses: any[]): SwaggerResponse[] {
|
|
478
|
+
return responses.map(response => ({
|
|
479
|
+
status: response.status,
|
|
480
|
+
description: response.description,
|
|
481
|
+
content: response.schema ? {
|
|
482
|
+
'application/json': {
|
|
483
|
+
schema: response.schema,
|
|
484
|
+
examples: response.examples
|
|
485
|
+
}
|
|
486
|
+
} : undefined
|
|
487
|
+
}));
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* 转换响应为OpenAPI格式
|
|
492
|
+
*/
|
|
493
|
+
private convertResponsesToOpenAPI(responses: SwaggerResponse[]): Record<string, any> {
|
|
494
|
+
const openAPIResponses: Record<string, any> = {};
|
|
495
|
+
|
|
496
|
+
responses.forEach(response => {
|
|
497
|
+
openAPIResponses[response.status.toString()] = {
|
|
498
|
+
description: response.description,
|
|
499
|
+
content: response.content
|
|
500
|
+
};
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
return openAPIResponses;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* 获取HTTP方法
|
|
508
|
+
*/
|
|
509
|
+
private getHttpMethods(target: any): string[] {
|
|
510
|
+
const methods: string[] = [];
|
|
511
|
+
|
|
512
|
+
if (target.$_GetMethods) {
|
|
513
|
+
const getMethods = Array.from(target.$_GetMethods) as Array<{methodName: string}>;
|
|
514
|
+
methods.push(...getMethods.map(m => m.methodName));
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
if (target.$_PostMethods) {
|
|
518
|
+
const postMethods = Array.from(target.$_PostMethods) as Array<{methodName: string}>;
|
|
519
|
+
methods.push(...postMethods.map(m => m.methodName));
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
if (target.$_PutMethods) {
|
|
523
|
+
const putMethods = Array.from(target.$_PutMethods) as Array<{methodName: string}>;
|
|
524
|
+
methods.push(...putMethods.map(m => m.methodName));
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (target.$_DelMethods) {
|
|
528
|
+
const delMethods = Array.from(target.$_DelMethods) as Array<{methodName: string}>;
|
|
529
|
+
methods.push(...delMethods.map(m => m.methodName));
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
return methods;
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
/**
|
|
536
|
+
* 获取路由路径
|
|
537
|
+
*/
|
|
538
|
+
private getRoutePath(target: any, methodName: string): string | null {
|
|
539
|
+
// 这里需要根据实际的路由注册逻辑来获取路径
|
|
540
|
+
// 暂时返回null,需要与路由系统集成
|
|
541
|
+
return null;
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* 获取HTTP方法
|
|
546
|
+
*/
|
|
547
|
+
private getHttpMethod(target: any, methodName: string): string {
|
|
548
|
+
if (target.$_GetMethods) {
|
|
549
|
+
const getMethods = Array.from(target.$_GetMethods) as Array<{methodName: string}>;
|
|
550
|
+
if (getMethods.some(m => m.methodName === methodName)) return 'get';
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
if (target.$_PostMethods) {
|
|
554
|
+
const postMethods = Array.from(target.$_PostMethods) as Array<{methodName: string}>;
|
|
555
|
+
if (postMethods.some(m => m.methodName === methodName)) return 'post';
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
if (target.$_PutMethods) {
|
|
559
|
+
const putMethods = Array.from(target.$_PutMethods) as Array<{methodName: string}>;
|
|
560
|
+
if (putMethods.some(m => m.methodName === methodName)) return 'put';
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
if (target.$_DelMethods) {
|
|
564
|
+
const delMethods = Array.from(target.$_DelMethods) as Array<{methodName: string}>;
|
|
565
|
+
if (delMethods.some(m => m.methodName === methodName)) return 'delete';
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
return 'get'; // 默认
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* 生成默认摘要
|
|
573
|
+
*/
|
|
574
|
+
private generateDefaultSummary(methodName: string): string {
|
|
575
|
+
const methodMap: Record<string, string> = {
|
|
576
|
+
'get': '获取',
|
|
577
|
+
'post': '创建',
|
|
578
|
+
'put': '更新',
|
|
579
|
+
'delete': '删除'
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
const httpMethod = this.getHttpMethod({}, methodName);
|
|
583
|
+
const action = methodMap[httpMethod] || '操作';
|
|
584
|
+
|
|
585
|
+
return `${action}${methodName}`;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* 生成默认描述
|
|
590
|
+
*/
|
|
591
|
+
private generateDefaultDescription(methodName: string): string {
|
|
592
|
+
const httpMethod = this.getHttpMethod({}, methodName);
|
|
593
|
+
const descriptions: Record<string, string> = {
|
|
594
|
+
'get': '获取数据',
|
|
595
|
+
'post': '创建数据',
|
|
596
|
+
'put': '更新数据',
|
|
597
|
+
'delete': '删除数据'
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
return descriptions[httpMethod] || 'API操作';
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* 创建Swagger处理器实例
|
|
606
|
+
*/
|
|
607
|
+
export const swaggerProcessor = new SwaggerProcessorImpl();
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* 导出Swagger处理器
|
|
611
|
+
*/
|
|
612
|
+
export { SwaggerProcessorImpl };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 注解处理器导出文件
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export { PerformanceMonitorProcessor } from './PerformanceMonitorProcessor';
|
|
6
|
+
export { EnterprisePerformanceProcessor } from './EnterprisePerformanceProcessor';
|
|
7
|
+
export { SecurityAuditProcessor } from './SecurityAuditProcessor';
|
|
8
|
+
export { DistributedTracingProcessor } from './DistributedTracingProcessor';
|
|
9
|
+
export { ConfigManagementProcessor } from './ConfigManagementProcessor';
|
|
10
|
+
export { SwaggerProcessor, swaggerProcessor } from './SwaggerProcessor';
|