create-dp-koa 1.1.2 → 1.1.4

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.
Files changed (104) hide show
  1. package/package.json +1 -1
  2. package/template/.cursor/rules/00-backend-core.skill.md +1 -1
  3. package/template/.cursor/rules/01-backend-skill-router.skill.md +8 -2
  4. package/template/.cursor/rules/10-backend-api.skill.md +8 -0
  5. package/template/.cursor/rules/11-backend-controller-recipes.skill.md +12 -9
  6. package/template/.cursor/rules/21-backend-service.skill.md +14 -0
  7. package/template/.cursor/rules/30-backend-validation.skill.md +1 -1
  8. package/template/.cursor/rules/40-backend-error-logging.skill.md +9 -5
  9. package/template/.cursor/rules/50-backend-bootstrap-lifecycle.skill.md +4 -4
  10. package/template/.cursor/rules/60-backend-router-registration.skill.md +16 -6
  11. package/template/.cursor/rules/70-backend-middleware.skill.md +2 -2
  12. package/template/.cursor/rules/80-backend-utils-and-libs.skill.md +71 -14
  13. package/template/.cursor/rules/85-backend-plugins.rule.md +4 -4
  14. package/template/.cursor/rules/90-backend-testing.skill.md +26 -0
  15. package/template/.cursor/rules/README.md +2 -2
  16. package/template/.trae/skills/11-backend-controller-recipes.skill.md +12 -9
  17. package/template/.trae/skills/21-backend-service.skill.md +15 -1
  18. package/template/.trae/skills/40-backend-error-logging.skill.md +9 -5
  19. package/template/.trae/skills/80-backend-utils-and-libs.skill.md +77 -8
  20. package/template/.trae/skills/90-backend-testing.skill.md +26 -0
  21. package/template/scripts/sync-template.mjs +20 -0
  22. package/template/src/app.ts +1 -2
  23. package/template/src/{framework/plugins → plugins}/registry.ts +2 -2
  24. package/template/src/plugins/weboffice/http/routes.ts +1 -1
  25. package/template/src/plugins/weboffice/index.ts +3 -3
  26. package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +3 -4
  27. package/template/src/types/ctxState.ts +9 -0
  28. package/template/src/utils/testDataInitializer.ts +1 -1
  29. package/template/tsconfig.json +6 -0
  30. package/template/src/annotations/decorators/ConfigManagement.ts +0 -9
  31. package/template/src/annotations/decorators/DistributedTracing.ts +0 -9
  32. package/template/src/annotations/decorators/EnterprisePerformance.ts +0 -9
  33. package/template/src/annotations/decorators/PerformanceMonitor.ts +0 -32
  34. package/template/src/annotations/decorators/SecurityAudit.ts +0 -9
  35. package/template/src/annotations/index.ts +0 -50
  36. package/template/src/annotations/processors/ConfigManagementProcessor.ts +0 -369
  37. package/template/src/annotations/processors/DistributedTracingProcessor.ts +0 -288
  38. package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +0 -189
  39. package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +0 -101
  40. package/template/src/annotations/processors/SecurityAuditProcessor.ts +0 -345
  41. package/template/src/annotations/processors/SwaggerProcessor.ts +0 -612
  42. package/template/src/annotations/processors/index.ts +0 -10
  43. package/template/src/examples/InterceptorExampleRunner.ts +0 -284
  44. package/template/src/examples/ServiceInterceptorExample.ts +0 -214
  45. package/template/src/examples/cacheExamples.ts +0 -155
  46. package/template/src/framework/decorator/controller.ts +0 -311
  47. package/template/src/framework/decorator/processor/AnnotationDecorators.ts +0 -100
  48. package/template/src/framework/decorator/processor/AnnotationProcessor.ts +0 -160
  49. package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +0 -45
  50. package/template/src/framework/decorator/processor/AnnotationRegistry.ts +0 -117
  51. package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +0 -95
  52. package/template/src/framework/decorator/processor/ProcessorManager.ts +0 -76
  53. package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +0 -126
  54. package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +0 -207
  55. package/template/src/framework/decorator/refactored/DecoratorFactory.ts +0 -99
  56. package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +0 -125
  57. package/template/src/framework/decorator/refactored/DecoratorValidator.ts +0 -128
  58. package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +0 -139
  59. package/template/src/framework/decorator/refactored/index.ts +0 -98
  60. package/template/src/framework/decorator/swagger.ts +0 -150
  61. package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +0 -375
  62. package/template/src/framework/interceptors/ServiceCallInterceptor.ts +0 -348
  63. package/template/src/framework/interceptors/index.ts +0 -19
  64. package/template/src/framework/plugins/types.ts +0 -15
  65. package/template/src/framework/types/ServiceResult.ts +0 -151
  66. package/template/src/framework/types/index.ts +0 -16
  67. package/template/src/framework/utils/CacheManager.ts +0 -430
  68. package/template/src/framework/utils/CacheService.ts +0 -248
  69. package/template/src/framework/utils/DtoValidator.ts +0 -164
  70. package/template/src/framework/utils/MigrationHelper.ts +0 -179
  71. package/template/src/framework/utils/MigrationManager.ts +0 -256
  72. package/template/src/framework/utils/NewRouter.ts +0 -207
  73. package/template/src/framework/utils/TransactionManager.ts +0 -172
  74. package/template/src/framework/utils/bootstrap.ts +0 -445
  75. package/template/src/framework/utils/cache.ts +0 -269
  76. package/template/src/framework/utils/databaseConfig.ts +0 -148
  77. package/template/src/framework/utils/db.ts +0 -39
  78. package/template/src/framework/utils/dbMonitor.ts +0 -106
  79. package/template/src/framework/utils/function.ts +0 -61
  80. package/template/src/framework/utils/gracefulShutdown.ts +0 -131
  81. package/template/src/framework/utils/logger.ts +0 -388
  82. package/template/src/framework/utils/metrics.ts +0 -182
  83. package/template/src/framework/utils/router.ts +0 -417
  84. package/template/src/framework/utils/swagger.ts +0 -184
  85. package/template/src/framework/utils/testDb.ts +0 -19
  86. package/template/src/framework/utils/token.ts +0 -23
  87. package/template/src/framework/utils/transform.ts +0 -17
  88. package/template/src/libs/aokEmailSender.ts +0 -42
  89. package/template/src/libs/captcha.ts +0 -37
  90. package/template/src/libs/cos.ts +0 -45
  91. package/template/src/libs/mCache.ts +0 -7
  92. package/template/src/libs/serviceValidate.ts +0 -3
  93. package/template/src/libs/tecentSms.ts +0 -51
  94. package/template/src/middlewares/a.middleware.ts +0 -6
  95. package/template/src/middlewares/error.middleware.ts +0 -14
  96. package/template/src/middlewares/logging.middleware.ts +0 -187
  97. package/template/src/middlewares/static.middleware.ts +0 -79
  98. package/template/src/middlewares/swagger.middleware.ts +0 -70
  99. package/template/src/middlewares/token.middleware.ts +0 -32
  100. package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +0 -172
  101. package/template/src/migrations/index.ts +0 -6
  102. package/template/src/repository/base/BaseRepository.ts +0 -124
  103. package/template/src/repository/interfaces/IBaseRepository.ts +0 -67
  104. package/template/src/service/base.service.ts +0 -116
@@ -1,207 +0,0 @@
1
- import { Context } from 'koa';
2
- import { AnnotationExecutor } from '@src/framework/decorator/processor/AnnotationProcessor';
3
- import { ProcessorManager } from '@src/framework/decorator/processor/ProcessorManager';
4
- import { logger } from '@src/framework/utils/logger';
5
- import { isPromise } from '@src/framework/utils/function';
6
- import { createCache, CacheType } from '@src/framework/utils/cache';
7
-
8
- // 使用统一的缓存管理创建控制器结果缓存
9
- const controllerResultCacheStore = createCache('controller-result', CacheType.CONTROLLER, {
10
- stdTTL: 20, // 20秒过期
11
- maxKeys: 2000 // 控制器缓存可以更多
12
- });
13
-
14
- /**
15
- * 新的控制器调用函数
16
- * 使用注解处理器系统
17
- */
18
- export async function callControllerWithProcessors(
19
- ctx: Context,
20
- controller: any,
21
- methodName: string
22
- ): Promise<void> {
23
- // 确保处理器已初始化
24
- ProcessorManager.initialize();
25
-
26
- // 构建调用参数
27
- const callParams = await buildCallParams(ctx, controller, methodName);
28
-
29
- // 执行注解处理器
30
- const shouldContinue = await AnnotationExecutor.execute(
31
- ctx,
32
- controller,
33
- methodName,
34
- callParams
35
- );
36
-
37
- if (!shouldContinue) {
38
- // 如果处理器返回false,说明已经处理了响应,直接返回
39
- return;
40
- }
41
-
42
- try {
43
- // 调用控制器方法
44
- const callResult = controller[methodName].apply(controller, callParams);
45
-
46
- // 处理响应
47
- let response: any;
48
- if (isPromise(callResult)) {
49
- response = await callResult;
50
- } else {
51
- response = callResult;
52
- }
53
-
54
- // 执行响应验证
55
- await executeResponseValidation(ctx, controller, methodName, response);
56
-
57
- // 处理缓存
58
- await handleCache(controller, methodName, callParams, response);
59
-
60
- // 执行日志记录
61
- await executeLogging(ctx, controller, methodName, response);
62
-
63
- // 执行后置处理器回调
64
- await executePostProcessors(ctx, controller, methodName, response);
65
-
66
- ctx.body = response;
67
-
68
- } catch (err) {
69
- logger.error(`控制器方法 ${controller.constructor.name}.${methodName} 执行失败:`, err as Error);
70
- throw err;
71
- }
72
- }
73
-
74
- /**
75
- * 执行响应验证
76
- * 通过接口调用,避免硬编码依赖
77
- */
78
- async function executeResponseValidation(ctx: Context, controller: any, methodName: string, response: any): Promise<void> {
79
- const responseValidationProcessor = ProcessorManager.getProcessor('ResponseValidation');
80
- if (responseValidationProcessor && typeof responseValidationProcessor.validateResponse === 'function') {
81
- try {
82
- const validationErrors = await responseValidationProcessor.validateResponse(
83
- controller,
84
- methodName,
85
- response
86
- );
87
-
88
- if (validationErrors && validationErrors.length > 0) {
89
- ctx.status = 400;
90
- logger.error(validationErrors.join(', '));
91
- ctx.body = `接口${methodName}返回数据参数校验异常`;
92
- return;
93
- }
94
- } catch (error) {
95
- logger.error(`响应验证处理器执行失败:`, error as Error);
96
- }
97
- }
98
- }
99
-
100
- /**
101
- * 执行日志记录
102
- * 通过接口调用,避免硬编码依赖
103
- */
104
- async function executeLogging(ctx: Context, controller: any, methodName: string, response: any): Promise<void> {
105
- const loggingProcessor = ProcessorManager.getProcessor('Logging');
106
- if (loggingProcessor && typeof loggingProcessor.logResponse === 'function') {
107
- try {
108
- await loggingProcessor.logResponse(ctx, controller, methodName, response);
109
- } catch (error) {
110
- logger.error(`日志处理器执行失败:`, error as Error);
111
- }
112
- }
113
- }
114
-
115
- /**
116
- * 执行后置处理器回调
117
- * 框架层通过回调机制调用业务层的后置处理方法
118
- */
119
- async function executePostProcessors(ctx: Context, controller: any, methodName: string, response: any): Promise<void> {
120
- // 获取所有已注册的处理器
121
- const processors = ProcessorManager.getAllProcessors();
122
-
123
- for (const processor of processors) {
124
- // 检查处理器是否有后置处理方法
125
- if (typeof processor.postProcess === 'function') {
126
- try {
127
- await processor.postProcess(ctx, controller, methodName, response);
128
- } catch (error) {
129
- logger.error(`后置处理器 ${processor.name} 执行失败:`, error as Error);
130
- }
131
- }
132
- }
133
- }
134
-
135
- /**
136
- * 构建调用参数
137
- */
138
- async function buildCallParams(ctx: Context, controller: any, methodName: string): Promise<any[]> {
139
- const callParams: any[] = [];
140
-
141
- if (controller.$_MethdosParamInfo) {
142
- const paramsInfo = controller.$_MethdosParamInfo.get(methodName);
143
- if (paramsInfo && paramsInfo.length > 0) {
144
- for (let paramInfo of paramsInfo) {
145
- let data: any;
146
-
147
- switch (paramInfo.type) {
148
- case 'Body':
149
- data = ctx.request.body;
150
- break;
151
- case 'Query':
152
- data = ctx.query;
153
- break;
154
- case 'Params':
155
- data = ctx.params;
156
- break;
157
- case 'Headers':
158
- data = ctx.request.headers;
159
- break;
160
- case 'State':
161
- data = ctx.state;
162
- break;
163
- default:
164
- data = undefined;
165
- }
166
-
167
- callParams.push(data);
168
- }
169
- }
170
- }
171
-
172
- return callParams;
173
- }
174
-
175
- /**
176
- * 处理缓存逻辑
177
- */
178
- async function handleCache(
179
- controller: any,
180
- methodName: string,
181
- callParams: any[],
182
- response: any
183
- ): Promise<void> {
184
- if (!controller.$_ControllerCache || !controller.$_ControllerCache.get(methodName)) {
185
- return;
186
- }
187
-
188
- const { cacheyFn, options } = controller.$_ControllerCache.get(methodName);
189
- if (!options || (options.enable !== undefined && !options.enable)) {
190
- return;
191
- }
192
-
193
- const cacheKey = cacheyFn(...callParams);
194
- if (cacheKey) {
195
- let ttl = 20;
196
- if (options.ttl) {
197
- if (typeof options.ttl === 'object') {
198
- // 随机TTL
199
- ttl = Math.floor(Math.random() * (options.ttl.max - options.ttl.min + 1)) + options.ttl.min;
200
- } else {
201
- ttl = options.ttl;
202
- }
203
- }
204
-
205
- controllerResultCacheStore.set(cacheKey, response, ttl);
206
- }
207
- }
@@ -1,172 +0,0 @@
1
- import { EntityManager, DataSource } from 'typeorm';
2
- import { logger } from '@src/framework/utils/logger';
3
-
4
- /**
5
- * 事务管理器
6
- * 提供企业级事务管理功能
7
- */
8
- export class TransactionManager {
9
- private static instance: TransactionManager;
10
- private dataSource: DataSource | null = null;
11
-
12
- private constructor() {}
13
-
14
- static getInstance(): TransactionManager {
15
- if (!TransactionManager.instance) {
16
- TransactionManager.instance = new TransactionManager();
17
- }
18
- return TransactionManager.instance;
19
- }
20
-
21
- /**
22
- * 设置数据源
23
- */
24
- setDataSource(dataSource: DataSource): void {
25
- this.dataSource = dataSource;
26
- }
27
-
28
- /**
29
- * 执行事务
30
- * @param operation 事务操作函数
31
- * @param isolationLevel 隔离级别
32
- */
33
- async executeInTransaction<T>(
34
- operation: (manager: EntityManager) => Promise<T>,
35
- isolationLevel?: 'READ_UNCOMMITTED' | 'READ_COMMITTED' | 'REPEATABLE_READ' | 'SERIALIZABLE'
36
- ): Promise<T> {
37
- if (!this.dataSource) {
38
- throw new Error('数据源未初始化');
39
- }
40
-
41
- const queryRunner = this.dataSource.createQueryRunner();
42
- await queryRunner.connect();
43
- await queryRunner.startTransaction();
44
-
45
- try {
46
- // 设置隔离级别
47
- if (isolationLevel) {
48
- await queryRunner.query(`SET TRANSACTION ISOLATION LEVEL ${isolationLevel}`);
49
- }
50
-
51
- const result = await operation(queryRunner.manager);
52
- await queryRunner.commitTransaction();
53
-
54
- logger.info('事务执行成功');
55
- return result;
56
- } catch (error) {
57
- await queryRunner.rollbackTransaction();
58
- logger.error('事务执行失败,已回滚:', error as Error);
59
- throw error;
60
- } finally {
61
- await queryRunner.release();
62
- }
63
- }
64
-
65
- /**
66
- * 执行只读事务
67
- * @param operation 只读操作函数
68
- */
69
- async executeInReadOnlyTransaction<T>(
70
- operation: (manager: EntityManager) => Promise<T>
71
- ): Promise<T> {
72
- if (!this.dataSource) {
73
- throw new Error('数据源未初始化');
74
- }
75
-
76
- const queryRunner = this.dataSource.createQueryRunner();
77
- await queryRunner.connect();
78
- await queryRunner.startTransaction('READ COMMITTED');
79
-
80
- try {
81
- // 设置为只读事务
82
- await queryRunner.query('SET TRANSACTION READ ONLY');
83
-
84
- const result = await operation(queryRunner.manager);
85
- await queryRunner.commitTransaction();
86
-
87
- logger.debug('只读事务执行成功');
88
- return result;
89
- } catch (error) {
90
- await queryRunner.rollbackTransaction();
91
- logger.error('只读事务执行失败:', error as Error);
92
- throw error;
93
- } finally {
94
- await queryRunner.release();
95
- }
96
- }
97
-
98
- /**
99
- * 批量操作事务
100
- * @param operations 批量操作数组
101
- */
102
- async executeBatchTransaction<T>(
103
- operations: Array<(manager: EntityManager) => Promise<T>>
104
- ): Promise<T[]> {
105
- return await this.executeInTransaction(async (manager) => {
106
- const results: T[] = [];
107
- for (const operation of operations) {
108
- const result = await operation(manager);
109
- results.push(result);
110
- }
111
- return results;
112
- });
113
- }
114
-
115
- /**
116
- * 嵌套事务支持
117
- * @param operation 嵌套事务操作函数
118
- */
119
- async executeNestedTransaction<T>(
120
- operation: (manager: EntityManager) => Promise<T>
121
- ): Promise<T> {
122
- if (!this.dataSource) {
123
- throw new Error('数据源未初始化');
124
- }
125
-
126
- const queryRunner = this.dataSource.createQueryRunner();
127
- await queryRunner.connect();
128
-
129
- // 检查是否已经在事务中
130
- const isInTransaction = queryRunner.isTransactionActive;
131
-
132
- let shouldRelease = false;
133
-
134
- try {
135
- if (!isInTransaction) {
136
- // 开始新事务
137
- await queryRunner.startTransaction();
138
- shouldRelease = true;
139
- }
140
-
141
- const result = await operation(queryRunner.manager);
142
-
143
- if (shouldRelease) {
144
- await queryRunner.commitTransaction();
145
- }
146
-
147
- return result;
148
- } catch (error) {
149
- if (shouldRelease) {
150
- await queryRunner.rollbackTransaction();
151
- }
152
- logger.error('嵌套事务执行失败:', error as Error);
153
- throw error;
154
- } finally {
155
- if (shouldRelease) {
156
- await queryRunner.release();
157
- }
158
- }
159
- }
160
-
161
- /**
162
- * 清理资源
163
- * 在应用关闭时调用
164
- */
165
- cleanup(): void {
166
- this.dataSource = null;
167
- logger.info('事务管理器资源已清理');
168
- }
169
- }
170
-
171
- // 导出单例实例
172
- export const transactionManager = TransactionManager.getInstance();