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.
Files changed (235) hide show
  1. package/README.md +50 -0
  2. package/index.mjs +97 -0
  3. package/package.json +33 -0
  4. package/template/.env.development +9 -0
  5. package/template/.env.production +12 -0
  6. package/template/.github/workflows/ci-cd.yml +182 -0
  7. package/template/.trae/documents/controller_development_plan.md +386 -0
  8. package/template/.trae/skills/00-backend-core.skill.md +50 -0
  9. package/template/.trae/skills/01-backend-skill-router.skill.md +55 -0
  10. package/template/.trae/skills/10-backend-api.skill.md +54 -0
  11. package/template/.trae/skills/11-backend-controller-recipes.skill.md +107 -0
  12. package/template/.trae/skills/20-backend-repository.skill.md +25 -0
  13. package/template/.trae/skills/21-backend-service.skill.md +135 -0
  14. package/template/.trae/skills/25-backend-comments-and-doc.skill.md +97 -0
  15. package/template/.trae/skills/30-backend-validation.skill.md +320 -0
  16. package/template/.trae/skills/40-backend-error-logging.skill.md +21 -0
  17. package/template/.trae/skills/50-backend-bootstrap-lifecycle.skill.md +90 -0
  18. package/template/.trae/skills/60-backend-router-registration.skill.md +71 -0
  19. package/template/.trae/skills/70-backend-middleware.skill.md +98 -0
  20. package/template/.trae/skills/80-backend-utils-and-libs.skill.md +90 -0
  21. package/template/.trae/skills/85-backend-plugins.rule.md +64 -0
  22. package/template/.trae/skills/90-backend-testing.skill.md +29 -0
  23. package/template/.trae/skills/README.md +49 -0
  24. package/template/.vscode/launch.json +38 -0
  25. package/template/.vscode/settings.json +1 -0
  26. package/template/Dockerfile +36 -0
  27. package/template/README.md +229 -0
  28. package/template/docker-compose.yml +135 -0
  29. package/template/docs/API_DOCUMENTATION.md +837 -0
  30. package/template/docs/ARCHITECTURE_REFACTOR.md +109 -0
  31. package/template/docs/CACHE_MIGRATION_GUIDE.md +142 -0
  32. package/template/docs/DEPLOYMENT_GUIDE.md +1062 -0
  33. package/template/docs/DEVELOPMENT_GUIDE.md +1097 -0
  34. package/template/docs/DOCUMENTATION_CLEANUP_REPORT.md +166 -0
  35. package/template/docs/DOCUMENTATION_COMPLETION_REPORT.md +223 -0
  36. package/template/docs/DOCUMENTATION_INDEX.md +294 -0
  37. package/template/docs/DOCUMENTATION_STRUCTURE.md +221 -0
  38. package/template/docs/ENTERPRISE_ANNOTATION_SYSTEM_GUIDE.md +2069 -0
  39. package/template/docs/ENTERPRISE_DATABASE_ARCHITECTURE.md +318 -0
  40. package/template/docs/ENTERPRISE_DEPLOYMENT_GUIDE.md +547 -0
  41. package/template/docs/ENTERPRISE_ERROR_HANDLING_GUIDE.md +357 -0
  42. package/template/docs/ENTERPRISE_LOGGING_SYSTEM_GUIDE.md +494 -0
  43. package/template/docs/ENVIRONMENT_CONFIG_EXAMPLE.md +69 -0
  44. package/template/docs/FINAL_IMPLEMENTATION_SUMMARY.md +206 -0
  45. package/template/docs/HEALTH_CHECK_ROUTE_FIX.md +134 -0
  46. package/template/docs/IMPLEMENTATION_CHECKLIST.md +204 -0
  47. package/template/docs/INSTALLATION_GUIDE.md +611 -0
  48. package/template/docs/INTERCEPTOR_TESTING_REPORT.md +226 -0
  49. package/template/docs/INTERCEPTOR_TESTING_SCRIPTS.md +143 -0
  50. package/template/docs/LOGGING_OPTIMIZATION_GUIDE.md +126 -0
  51. package/template/docs/MEMORY_DATABASE_GUIDE.md +212 -0
  52. package/template/docs/NEW_ROUTER_INTEGRATION_GUIDE.md +345 -0
  53. package/template/docs/NEW_ROUTER_INTEGRATION_SUMMARY.md +259 -0
  54. package/template/docs/NEW_ROUTER_USAGE_GUIDE.md +364 -0
  55. package/template/docs/QUICK_START.md +268 -0
  56. package/template/docs/ROUTE_SLASH_COMPATIBILITY_FIX.md +191 -0
  57. package/template/docs/SERVICE_INTERCEPTOR_GUIDE.md +243 -0
  58. package/template/docs/SERVICE_LAYER_INDEX.md +205 -0
  59. package/template/docs/SERVICE_PATTERN_GUIDE.md +270 -0
  60. package/template/docs/SERVICE_RETURN_VALUE_SPECIFICATION.md +466 -0
  61. package/template/docs/SWAGGER_DEBUG_MODE_GUIDE.md +80 -0
  62. package/template/docs/SWAGGER_INTEGRATION_GUIDE.md +416 -0
  63. package/template/docs/TRANSACTION_MANAGER_USAGE.md +360 -0
  64. package/template/docs/TROUBLESHOOTING.md +869 -0
  65. package/template/env.production.example +62 -0
  66. package/template/jest.config.js +34 -0
  67. package/template/package-lock.json +13240 -0
  68. package/template/package.json +119 -0
  69. package/template/patches/typeorm+0.3.25.patch +22 -0
  70. package/template/scripts/sync-template.mjs +84 -0
  71. package/template/scripts/test-annotation-system.sh +48 -0
  72. package/template/scripts/test-core-functionality.sh +28 -0
  73. package/template/src/annotations/decorators/ConfigManagement.ts +9 -0
  74. package/template/src/annotations/decorators/DistributedTracing.ts +9 -0
  75. package/template/src/annotations/decorators/EnterprisePerformance.ts +9 -0
  76. package/template/src/annotations/decorators/PerformanceMonitor.ts +32 -0
  77. package/template/src/annotations/decorators/SecurityAudit.ts +9 -0
  78. package/template/src/annotations/index.ts +50 -0
  79. package/template/src/annotations/processors/ConfigManagementProcessor.ts +369 -0
  80. package/template/src/annotations/processors/DistributedTracingProcessor.ts +288 -0
  81. package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +189 -0
  82. package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +101 -0
  83. package/template/src/annotations/processors/SecurityAuditProcessor.ts +345 -0
  84. package/template/src/annotations/processors/SwaggerProcessor.ts +612 -0
  85. package/template/src/annotations/processors/index.ts +10 -0
  86. package/template/src/app.ts +123 -0
  87. package/template/src/controllers/base.controller.ts +41 -0
  88. package/template/src/controllers/cacheManagement.controller.ts +131 -0
  89. package/template/src/controllers/captcha.controller.ts +57 -0
  90. package/template/src/controllers/demo/AnnotationDemoController.ts +118 -0
  91. package/template/src/controllers/example/EnterpriseExampleController.ts +297 -0
  92. package/template/src/controllers/example/ExampleController.ts +110 -0
  93. package/template/src/controllers/example/NewAnnotationExampleController.ts +159 -0
  94. package/template/src/controllers/example/SwaggerExampleController.ts +205 -0
  95. package/template/src/controllers/example/TransactionExample.controller.ts +336 -0
  96. package/template/src/controllers/health.controller.ts +235 -0
  97. package/template/src/controllers/home/register.controller.ts +58 -0
  98. package/template/src/controllers/home/ytGoods.controller.ts +92 -0
  99. package/template/src/controllers/home/ytShop.controller.ts +135 -0
  100. package/template/src/controllers/home/ytUser.controller.ts +89 -0
  101. package/template/src/controllers/logManagement.controller.ts +396 -0
  102. package/template/src/controllers/public/emailSend.controller.ts +65 -0
  103. package/template/src/controllers/public/ytUserAuth.controller.ts +174 -0
  104. package/template/src/controllers/testData.controller.ts +253 -0
  105. package/template/src/dto/controller/example/NewAnnotationExampleController.dto.ts +73 -0
  106. package/template/src/dto/controller/home/emailSend.controller.dto.ts +40 -0
  107. package/template/src/dto/controller/home/register.controller.dto.ts +45 -0
  108. package/template/src/dto/controller/home/ytGoods.controller.dto.ts +55 -0
  109. package/template/src/dto/controller/home/ytShop.controller.dto.ts +69 -0
  110. package/template/src/dto/controller/home/ytUser.controller.dto.ts +44 -0
  111. package/template/src/dto/controller/public/ytUserAuth.controller.dto.ts +63 -0
  112. package/template/src/dto/goods.dto.ts +212 -0
  113. package/template/src/dto/service/ytService.dto.ts +13 -0
  114. package/template/src/dto/user.dto.ts +177 -0
  115. package/template/src/entity/base.entity.ts +13 -0
  116. package/template/src/entity/columnTypes.ts +13 -0
  117. package/template/src/entity/goodsImagesUnlockKey.entity.ts +33 -0
  118. package/template/src/entity/goodsUnlocker.entity.ts +34 -0
  119. package/template/src/entity/index.ts +15 -0
  120. package/template/src/entity/shop.entity.ts +52 -0
  121. package/template/src/entity/shopUser.entity.ts +41 -0
  122. package/template/src/entity/ytGoods.entity.ts +94 -0
  123. package/template/src/entity/ytUser.entity.ts +96 -0
  124. package/template/src/examples/InterceptorExampleRunner.ts +284 -0
  125. package/template/src/examples/ServiceInterceptorExample.ts +214 -0
  126. package/template/src/examples/SwaggerProcessorExample.ts +169 -0
  127. package/template/src/examples/TransactionManagerDemo.ts +377 -0
  128. package/template/src/examples/cacheExamples.ts +155 -0
  129. package/template/src/framework/decorator/controller.ts +311 -0
  130. package/template/src/framework/decorator/processor/AnnotationDecorators.ts +100 -0
  131. package/template/src/framework/decorator/processor/AnnotationProcessor.ts +156 -0
  132. package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +45 -0
  133. package/template/src/framework/decorator/processor/AnnotationRegistry.ts +117 -0
  134. package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +95 -0
  135. package/template/src/framework/decorator/processor/ProcessorManager.ts +76 -0
  136. package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +126 -0
  137. package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +207 -0
  138. package/template/src/framework/decorator/refactored/DecoratorFactory.ts +99 -0
  139. package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +125 -0
  140. package/template/src/framework/decorator/refactored/DecoratorValidator.ts +128 -0
  141. package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +139 -0
  142. package/template/src/framework/decorator/refactored/index.ts +98 -0
  143. package/template/src/framework/decorator/swagger.ts +150 -0
  144. package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +375 -0
  145. package/template/src/framework/interceptors/ServiceCallInterceptor.ts +348 -0
  146. package/template/src/framework/interceptors/index.ts +19 -0
  147. package/template/src/framework/plugins/registry.ts +63 -0
  148. package/template/src/framework/plugins/types.ts +15 -0
  149. package/template/src/framework/types/ServiceResult.ts +151 -0
  150. package/template/src/framework/types/index.ts +16 -0
  151. package/template/src/framework/utils/CacheManager.ts +430 -0
  152. package/template/src/framework/utils/CacheService.ts +248 -0
  153. package/template/src/framework/utils/DtoValidator.ts +164 -0
  154. package/template/src/framework/utils/MigrationHelper.ts +179 -0
  155. package/template/src/framework/utils/MigrationManager.ts +256 -0
  156. package/template/src/framework/utils/NewRouter.ts +207 -0
  157. package/template/src/framework/utils/TransactionManager.ts +172 -0
  158. package/template/src/framework/utils/bootstrap.ts +445 -0
  159. package/template/src/framework/utils/cache.ts +269 -0
  160. package/template/src/framework/utils/databaseConfig.ts +148 -0
  161. package/template/src/framework/utils/db.ts +39 -0
  162. package/template/src/framework/utils/dbMonitor.ts +106 -0
  163. package/template/src/framework/utils/dynamicSwagger.ts +410 -0
  164. package/template/src/framework/utils/function.ts +61 -0
  165. package/template/src/framework/utils/gracefulShutdown.ts +131 -0
  166. package/template/src/framework/utils/logger.ts +388 -0
  167. package/template/src/framework/utils/metrics.ts +182 -0
  168. package/template/src/framework/utils/router.ts +417 -0
  169. package/template/src/framework/utils/swagger.ts +184 -0
  170. package/template/src/framework/utils/testDb.ts +19 -0
  171. package/template/src/framework/utils/token.ts +23 -0
  172. package/template/src/framework/utils/transform.ts +17 -0
  173. package/template/src/libs/aokEmailSender.ts +42 -0
  174. package/template/src/libs/captcha.ts +37 -0
  175. package/template/src/libs/cos.ts +45 -0
  176. package/template/src/libs/mCache.ts +7 -0
  177. package/template/src/libs/serviceValidate.ts +3 -0
  178. package/template/src/libs/tecentSms.ts +51 -0
  179. package/template/src/middlewares/a.middleware.ts +6 -0
  180. package/template/src/middlewares/error.middleware.ts +14 -0
  181. package/template/src/middlewares/logging.middleware.ts +187 -0
  182. package/template/src/middlewares/static.middleware.ts +79 -0
  183. package/template/src/middlewares/swagger.middleware.ts +70 -0
  184. package/template/src/middlewares/token.middleware.ts +32 -0
  185. package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +172 -0
  186. package/template/src/migrations/index.ts +6 -0
  187. package/template/src/plugins/weboffice/core/context.ts +47 -0
  188. package/template/src/plugins/weboffice/core/errors.ts +51 -0
  189. package/template/src/plugins/weboffice/core/types.ts +63 -0
  190. package/template/src/plugins/weboffice/core/utils.ts +7 -0
  191. package/template/src/plugins/weboffice/entities/index.ts +3 -0
  192. package/template/src/plugins/weboffice/entities/webofficeFile.entity.ts +28 -0
  193. package/template/src/plugins/weboffice/entities/webofficeFileVersion.entity.ts +29 -0
  194. package/template/src/plugins/weboffice/http/routes.ts +179 -0
  195. package/template/src/plugins/weboffice/index.ts +23 -0
  196. package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +274 -0
  197. package/template/src/repository/UserRepository.ts +122 -0
  198. package/template/src/repository/base/BaseRepository.ts +124 -0
  199. package/template/src/repository/interfaces/IBaseRepository.ts +67 -0
  200. package/template/src/routers/index.ts +49 -0
  201. package/template/src/service/base.service.ts +116 -0
  202. package/template/src/service/paramValidateTest.service.ts +139 -0
  203. package/template/src/service/ytGoods.service.ts +42 -0
  204. package/template/src/service/ytShop.service.ts +90 -0
  205. package/template/src/service/ytUser.service.ts +451 -0
  206. package/template/src/test/swaggerParameterTest.ts +90 -0
  207. package/template/src/utils/testDataInitializer.ts +296 -0
  208. package/template/static/output.json +15203 -0
  209. package/template/test/controllers/controllers.test.ts +173 -0
  210. package/template/test/controllers/example/ExampleController.test.ts +222 -0
  211. package/template/test/controllers/example/NewAnnotationExampleController.test.ts +200 -0
  212. package/template/test/framework/TransactionManagerDemo.test.ts +363 -0
  213. package/template/test/framework/annotation/AnnotationDecorators.test.ts +222 -0
  214. package/template/test/framework/annotation/AnnotationExecutor.test.ts +246 -0
  215. package/template/test/framework/annotation/AnnotationProcessor.test.ts +179 -0
  216. package/template/test/framework/annotation/CustomProcessors.test.ts +313 -0
  217. package/template/test/framework/annotation/DefaultProcessors.test.ts +371 -0
  218. package/template/test/framework/annotation/NewRouter.test.ts +272 -0
  219. package/template/test/framework/annotation/ProcessorManager.test.ts +248 -0
  220. package/template/test/framework/annotation/setup.ts +26 -0
  221. package/template/test/framework/cache.test.ts +101 -0
  222. package/template/test/framework/databaseConfig.test.ts +142 -0
  223. package/template/test/integration/integration.test.ts +153 -0
  224. package/template/test/plugins/weboffice/http.routes.int.test.ts +61 -0
  225. package/template/test/service/business.test.ts +87 -0
  226. package/template/test/service/paramValidateTest.service.test.ts +184 -0
  227. package/template/test/service/ytUser.service.test.ts +566 -0
  228. package/template/test/setup.ts +20 -0
  229. package/template/test/setupAfterEnv.ts +14 -0
  230. package/template/test/utils/testHelpers.ts +220 -0
  231. package/template/test_output.txt +0 -0
  232. package/template/tsconfig.build.json +17 -0
  233. package/template/tsconfig.json +31 -0
  234. package/template/webpack.config.js +71 -0
  235. package/template/yarn.lock +7354 -0
@@ -0,0 +1,386 @@
1
+ # 控制器(Controller)开发计划
2
+
3
+ ## 项目结构分析
4
+
5
+ * **控制器目录**:`src/controllers/`
6
+
7
+ * **基础控制器**:`base.controller.ts`
8
+
9
+ * **示例控制器**:位于 `example/`、`home/`、`public/` 等子目录
10
+
11
+ * **装饰器系统**:`framework/decorator/` 目录下的装饰器
12
+
13
+ ## 开发步骤
14
+
15
+ ### \[ ] 步骤 1:创建控制器文件
16
+
17
+ * **优先级**:P0
18
+
19
+ * **依赖**:无
20
+
21
+ * **描述**:
22
+
23
+ * 在 `src/controllers/` 目录下创建新的控制器文件
24
+
25
+ * 可以创建子目录来组织控制器
26
+
27
+ * 文件名使用 `controller.ts` 后缀
28
+
29
+ * **成功标准**:
30
+
31
+ * 控制器文件创建成功
32
+
33
+ * 文件结构符合项目规范
34
+
35
+ * **测试要求**:
36
+
37
+ * `programmatic` TR-1.1:文件存在且格式正确
38
+
39
+ * `human-judgement` TR-1.2:文件命名规范,目录结构合理
40
+
41
+ ### \[ ] 步骤 2:继承基础控制器
42
+
43
+ * **优先级**:P0
44
+
45
+ * **依赖**:步骤 1
46
+
47
+ * **描述**:
48
+
49
+ * 导入 `BaseController` 和 `ControllerResponse`
50
+
51
+ * 创建控制器类并继承 `BaseController`
52
+
53
+ * **成功标准**:
54
+
55
+ * 控制器类正确继承 `BaseController`
56
+
57
+ * 可以使用 `success` 和 `fail` 方法
58
+
59
+ * **测试要求**:
60
+
61
+ * `programmatic` TR-2.1:代码编译通过
62
+
63
+ * `human-judgement` TR-2.2:继承关系正确
64
+
65
+ ### \[ ] 步骤 3:注入服务(如果需要)
66
+
67
+ * **优先级**:P0
68
+
69
+ * **依赖**:步骤 2
70
+
71
+ * **描述**:
72
+
73
+ * 导入 `Inject` 装饰器
74
+
75
+ * 使用 `@Inject` 注入需要的服务
76
+
77
+ * **成功标准**:
78
+
79
+ * 服务注入正确
80
+
81
+ * 可以在控制器中使用服务
82
+
83
+ * **测试要求**:
84
+
85
+ * `programmatic` TR-3.1:代码编译通过
86
+
87
+ * `human-judgement` TR-3.2:依赖注入配置正确
88
+
89
+ ### \[ ] 步骤 4:定义路由
90
+
91
+ * **优先级**:P0
92
+
93
+ * **依赖**:步骤 3
94
+
95
+ * **描述**:
96
+
97
+ * 导入路由装饰器(`Get`、`Post`、`Put`、`Del`、`All`)
98
+
99
+ * 使用装饰器定义路由路径
100
+
101
+ * 为需要的接口添加 `@ResponseCode` 装饰器设置 HTTP 状态码
102
+
103
+ * **成功标准**:
104
+
105
+ * 路由装饰器使用正确
106
+
107
+ * 路由路径格式规范
108
+
109
+ * HTTP 状态码设置合理
110
+
111
+ * **测试要求**:
112
+
113
+ * `programmatic` TR-4.1:代码编译通过
114
+
115
+ * `human-judgement` TR-4.2:路由路径命名规范,状态码设置合理
116
+
117
+ ### \[ ] 步骤 5:处理请求参数
118
+
119
+ * **优先级**:P0
120
+
121
+ * **依赖**:步骤 4
122
+
123
+ * **描述**:
124
+
125
+ * 导入参数装饰器(`Query`、`Body`、`Params`、`Headers`、`State`)
126
+
127
+ * 在方法参数中使用装饰器获取请求参数
128
+
129
+ * 使用 DTO 类型定义参数结构,避免使用 `any` 类型
130
+
131
+ * **成功标准**:
132
+
133
+ * 参数装饰器使用正确
134
+
135
+ * 能够正确获取请求参数
136
+
137
+ * 参数类型定义清晰
138
+
139
+ * **测试要求**:
140
+
141
+ * `programmatic` TR-5.1:代码编译通过
142
+
143
+ * `human-judgement` TR-5.2:参数获取逻辑清晰,类型定义合理
144
+
145
+ ### \[ ] 步骤 6:实现业务逻辑编排
146
+
147
+ * **优先级**:P0
148
+
149
+ * **依赖**:步骤 5
150
+
151
+ * **描述**:
152
+
153
+ * 在控制器方法中编排业务逻辑
154
+
155
+ * 调用服务层方法
156
+
157
+ * 不直接访问数据库/Repository
158
+
159
+ * 使用 try/catch 捕获异常
160
+
161
+ * **成功标准**:
162
+
163
+ * 业务逻辑编排正确
164
+
165
+ * 代码结构清晰
166
+
167
+ * 异常处理完善
168
+
169
+ * **测试要求**:
170
+
171
+ * `programmatic` TR-6.1:代码编译通过
172
+
173
+ * `human-judgement` TR-6.2:业务逻辑编排合理,代码可读性好
174
+
175
+ ### \[ ] 步骤 7:返回响应
176
+
177
+ * **优先级**:P0
178
+
179
+ * **依赖**:步骤 6
180
+
181
+ * **描述**:
182
+
183
+ * 使用 `success` 方法返回成功响应
184
+
185
+ * 使用 `fail` 方法返回失败响应
186
+
187
+ * 确保响应格式统一
188
+
189
+ * 不直接返回 Service 原始结果,必须映射为统一响应格式
190
+
191
+ * **成功标准**:
192
+
193
+ * 响应格式符合项目规范
194
+
195
+ * 状态码和消息设置正确
196
+
197
+ * 响应数据结构清晰
198
+
199
+ * **测试要求**:
200
+
201
+ * `programmatic` TR-7.1:代码编译通过
202
+
203
+ * `human-judgement` TR-7.2:响应格式规范,信息完整
204
+
205
+ ### [ ] 步骤 8:使用其他注解
206
+
207
+ * **优先级**:P2
208
+
209
+ * **依赖**:步骤 7
210
+
211
+ * **描述**:
212
+
213
+ * 导入其他装饰器(`Logging`、`Permission`、`RateLimit`、`PerformanceMonitor`)
214
+
215
+ * 根据需要使用这些装饰器
216
+
217
+ * **成功标准**:
218
+
219
+ * 装饰器使用正确
220
+
221
+ * 功能正常生效
222
+
223
+ * **测试要求**:
224
+
225
+ * `programmatic` TR-8.1:代码编译通过
226
+
227
+ * `human-judgement` TR-8.2:装饰器使用合理
228
+
229
+ ### [ ] 步骤 9:注册控制器
230
+
231
+ * **优先级**:P0
232
+
233
+ * **依赖**:步骤 8
234
+
235
+ * **描述**:
236
+
237
+ * 确保控制器被正确注册到应用中
238
+
239
+ * 检查路由是否被正确加载
240
+
241
+ * **成功标准**:
242
+
243
+ * 控制器注册成功
244
+
245
+ * 路由可以访问
246
+
247
+ * **测试要求**:
248
+
249
+ * `programmatic` TR-9.1:应用启动无错误
250
+
251
+ * `human-judgement` TR-9.2:路由注册正确
252
+
253
+ ### [ ] 步骤 10:测试控制器
254
+
255
+ * **优先级**:P1
256
+
257
+ * **依赖**:步骤 9
258
+
259
+ * **描述**:
260
+
261
+ * 启动应用
262
+
263
+ * 测试控制器接口
264
+
265
+ * 验证功能是否正常
266
+
267
+ * **成功标准**:
268
+
269
+ * 接口响应正常
270
+
271
+ * 功能实现正确
272
+
273
+ * **测试要求**:
274
+
275
+ * `programmatic` TR-10.1:接口返回 200 状态码
276
+
277
+ * `human-judgement` TR-10.2:响应数据正确,功能符合预期
278
+
279
+ ## 示例控制器模板
280
+
281
+ ```typescript
282
+ import { Get, Post, Body, Query, Params, Headers, State, ResponseCode } from '@src/framework/decorator/controller';
283
+ import { BaseController, ControllerResponse } from '@src/controllers/base.controller';
284
+ import { Inject } from 'dp-ioc2';
285
+ import { logger } from '@src/framework/utils/logger';
286
+ import { Logging } from '@src/framework/decorator/processor/AnnotationDecorators';
287
+ import { SomeService } from '@src/service/some.service';
288
+ import { SomeQueryDto, SomeBodyDto } from '@src/dto/controller/some/some.controller.dto';
289
+ import { CommonServiceResultCode } from '@src/framework/types/ServiceResult';
290
+
291
+ /**
292
+ * 示例控制器
293
+ * 负责处理示例相关的接口请求
294
+ */
295
+ export class ExampleController extends BaseController {
296
+
297
+ @Inject(SomeService)
298
+ private someService: SomeService;
299
+
300
+ /**
301
+ * 获取数据
302
+ * - 来源:前端示例页面
303
+ * - 功能:根据查询参数获取示例数据
304
+ * - 依赖:需要登录态
305
+ */
306
+ @Get('/example/data')
307
+ @ResponseCode(200)
308
+ @Logging()
309
+ async getData(@Query() query: SomeQueryDto, @State('user') user: { userId: number; type?: number }): Promise<ControllerResponse<any>> {
310
+ try {
311
+ const result = await this.someService.getData(query, user.userId);
312
+ if (result.code !== CommonServiceResultCode.SUCCESS) {
313
+ return this.fail(-1, result.message);
314
+ }
315
+ return this.success(result.data);
316
+ } catch (error) {
317
+ logger.error('getData 失败', error as Error);
318
+ return this.fail(-1, '系统错误');
319
+ }
320
+ }
321
+
322
+ /**
323
+ * 创建数据
324
+ * - 来源:前端示例页面
325
+ * - 功能:创建新的示例数据
326
+ * - 入参:示例数据对象
327
+ */
328
+ @Post('/example/data')
329
+ @ResponseCode(201)
330
+ @Logging()
331
+ async createData(@Body() body: SomeBodyDto): Promise<ControllerResponse<any>> {
332
+ try {
333
+ const result = await this.someService.createData(body);
334
+ if (result.code !== CommonServiceResultCode.SUCCESS) {
335
+ return this.fail(-1, result.message);
336
+ }
337
+ return this.success(result.data);
338
+ } catch (error) {
339
+ logger.error('createData 失败', error as Error);
340
+ return this.fail(-1, '系统错误');
341
+ }
342
+ }
343
+ }
344
+ ```
345
+
346
+ ## 注意事项
347
+
348
+ 1. **命名规范**:控制器文件名使用 `controller.ts` 后缀,类名使用 `Controller` 后缀
349
+ 2. **路由路径**:使用小写字母和斜杠,避免使用下划线
350
+ 3. **响应格式**:统一使用 `success` 和 `fail` 方法返回响应
351
+ 4. **参数验证**:使用 DTO 类型定义参数结构,避免使用 `any` 类型
352
+ 5. **错误处理**:使用 try/catch 捕获异常,记录错误日志
353
+ 6. **性能优化**:根据需要使用 `RateLimit` 和 `PerformanceMonitor` 装饰器
354
+ 7. **安全考虑**:对于敏感接口,使用 `Permission` 装饰器进行权限控制
355
+ 8. **业务逻辑**:控制器不写业务逻辑,只做编排;不直接访问数据库/Repository
356
+ 9. **返回值**:不直接返回 Service 原始结果,必须映射为统一响应格式
357
+
358
+ ## 测试方法
359
+
360
+ 1. 启动应用:`npm run dev`
361
+ 2. 使用 Postman 或其他工具测试接口
362
+ 3. 检查日志输出
363
+ 4. 验证响应数据
364
+
365
+ ## 常见问题
366
+
367
+ 1. **路由未注册**:检查控制器是否被正确导入和注册
368
+ 2. **参数获取失败**:检查参数装饰器使用是否正确
369
+ 3. **响应格式错误**:确保使用 `success` 和 `fail` 方法
370
+ 4. **权限验证失败**:检查 `Permission` 装饰器配置是否正确
371
+ 5. **服务注入失败**:检查 `Inject` 装饰器使用是否正确
372
+ 6. **异常处理遗漏**:确保所有异步方法都有 try/catch
373
+
374
+ ## 最佳实践
375
+
376
+ 1. **单一职责**:每个控制器负责一个功能模块
377
+ 2. **代码组织**:使用子目录组织控制器
378
+ 3. **错误处理**:统一处理错误,返回清晰的错误信息
379
+ 4. **性能优化**:合理使用缓存和速率限制
380
+ 5. **安全措施**:对敏感接口进行权限控制
381
+ 6. **代码风格**:保持代码风格一致,使用 TypeScript 类型
382
+ 7. **测试覆盖**:为控制器编写单元测试
383
+ 8. **依赖注入**:使用 `Inject` 装饰器注入服务,避免硬编码依赖
384
+ 9. **响应规范**:使用 `ResponseCode` 装饰器设置合理的 HTTP 状态码
385
+ 10. **注释规范**:为控制器和方法添加清晰的中文注释,说明业务含义和使用场景
386
+
@@ -0,0 +1,50 @@
1
+ ---
2
+ alwaysApply: true
3
+ ---
4
+ # Trae 技能:后端核心开发规范(Backend Core)
5
+
6
+ ## 适用范围
7
+ - 本项目所有 TypeScript 后端代码
8
+ - 技术栈:Koa + TypeORM + dp-ioc2 + 自定义装饰器体系
9
+ - **包管理工具:必须使用 yarn(禁止使用 npm)**
10
+
11
+ ---
12
+
13
+ ## 一、分层架构(必须遵守)
14
+
15
+ ### 分层职责
16
+ - **Controller 层**
17
+ - 接收请求
18
+ - 参数校验
19
+ - 调用 Service
20
+ - 返回统一响应
21
+ - **Service 层**
22
+ - 编写业务逻辑
23
+ - 调用 Repository
24
+ - **Repository 层**
25
+ - 封装数据库访问逻辑(TypeORM)
26
+
27
+ ---
28
+
29
+ ## 二、基类与返回规范
30
+
31
+ ### 基类继承
32
+ - Controller **必须继承** `BaseController`
33
+ - Service **必须继承** `BaseService`
34
+
35
+ ### Service 返回值
36
+ - Service 方法必须返回 `CommonServiceResult<T>`
37
+
38
+ ### Controller 返回值
39
+ - Controller 必须通过:
40
+ - `this.success<T>(data, message?)`
41
+ - `this.fail<T>(code, message?)`
42
+
43
+ 返回 `ControllerResponse<T>`:
44
+ ```ts
45
+ {
46
+ code: number
47
+ data: T | null
48
+ message: string
49
+ }
50
+ ```
@@ -0,0 +1,55 @@
1
+ ---
2
+ alwaysApply: true
3
+ ---
4
+ # Trae 技能:Backend Skill Router(规则路由器)
5
+
6
+ 你现在的角色是【后端 Skill 路由器】:你的目标是帮助选择应启用的 rules/skills/commands,避免盲用规则。
7
+
8
+ ## 总原则(必须遵守)
9
+ - **优先使用 Commands** 来触发正确的 Skills,而不是把所有规则都写成 alwaysApply。
10
+ - 如果用户需求不清晰,必须先提问澄清(不要臆造不存在的接口/文件/配置)。
11
+
12
+ ---
13
+
14
+ ## 你必须做的事(每次用户提出开发/改动请求时)
15
+
16
+ ### 1) 识别任务类型(多选)
17
+ - Controller/API 开发
18
+ - DTO/参数校验
19
+ - Service/Repository/事务
20
+ - 错误处理/日志
21
+ - 启动流程(`app.ts`/`bootstrap.ts`,before/after)
22
+ - 路由注册(`routers/index.ts`、`bindRouter`)
23
+ - 中间件(`src/middlewares/*`)
24
+ - 测试(Jest)
25
+ - 目录结构与放置(libs/utils)
26
+
27
+ ### 2) 给出推荐启用的 Skills(只列文件名即可)
28
+ 按任务类型映射:
29
+ - **Controller/API**:
30
+ - `10-backend-api.skill.md`
31
+ -(需要范式/注解速查时)`11-backend-controller-recipes.skill.md`
32
+ - **Service**:
33
+ - `21-backend-service.skill.md`
34
+ - **DTO/校验**:`30-backend-validation.skill.md`
35
+ - **Service/Repository/事务**:`20-backend-repository.skill.md`
36
+ - **错误/日志**:`40-backend-error-logging.skill.md`
37
+ - **启动流程**:`50-backend-bootstrap-lifecycle.skill.md`
38
+ - **路由注册**:`60-backend-router-registration.skill.md`
39
+ - **中间件**:`70-backend-middleware.skill.md`
40
+ - **测试**:`90-backend-testing.skill.md`
41
+ - **libs/utils 放置**:`80-backend-utils-and-libs.skill.md`
42
+
43
+ 注意:
44
+ - `00-backend-core.skill.md` 已是 alwaysApply,无需重复声明
45
+
46
+ ### 3) 推荐使用的 Command(优先)
47
+ - 需要先规划:`.cursor/commands/plan-backend.md`
48
+ - 要实现/修改 Controller:`.cursor/commands/implement-backend-api-controller.md`
49
+ - 只要 Controller 速查:`.cursor/commands/cheatsheet-backend-controller.md`
50
+
51
+ ---
52
+
53
+ ## 输出要求(简短)
54
+ - 当用户让你“直接写代码”:直接进入实现,但先在心里完成上述路由选择
55
+ - 当用户不确定或信息不足:先问 2~5 个关键澄清问题
@@ -0,0 +1,54 @@
1
+ ---
2
+ alwaysApply: false
3
+ ---
4
+ # Trae 技能:后端 API 开发规范
5
+
6
+ ## 适用与触发(重要)
7
+ - **仅在**你要生成/修改 **Controller 路由接口** 时应用本 Skill(否则忽略)
8
+ - 触发口令建议:
9
+ - "按 `10-backend-api.skill.md` 实现/重构这个 Controller"
10
+ - "按 API Skill 写路由 + DTO + 统一响应"
11
+
12
+ ---
13
+
14
+ ## 一、路由与参数装饰器(必须)
15
+ - 路由装饰器:`@Get` / `@Post`
16
+ - 参数装饰器:`@Query` / `@Body` / `@State`
17
+ - 禁止在 Controller 中手动解析 ctx(除非该 Controller 明确以 ctx 作为参数且项目已有同类写法)
18
+
19
+ ---
20
+
21
+ ## 二、Controller 编排规范(必须)
22
+
23
+ ### 2.1 只做三件事
24
+ - **参数接收与校验**(DTO)
25
+ - **调用 Service**
26
+ - **映射为统一响应**(`BaseController.success/fail`)
27
+
28
+ ### 2.2 统一响应(必须)
29
+ - Controller 必须返回 `ControllerResponse<T>`(通过 `this.success()` / `this.fail()`)
30
+ - 禁止 `return result`(Service 原始结果):
31
+ - 成功:`return this.success(result.data, result.message?)`
32
+ - 失败:`return this.fail(result.code, result.message)`
33
+
34
+ ### 2.3 错误处理(必须)
35
+ - Controller 的 `async` 方法必须 `try/catch`
36
+ - catch:记录日志(见 `40-backend-error-logging.skill.md`)+ 返回通用失败响应
37
+
38
+ ### 2.4 DTO(必须)
39
+ - `@Query()` / `@Body()` 必须使用 **DTO 类型**(禁止 `any`)
40
+ - DTO 的定义与校验遵循 `30-backend-validation.skill.md`
41
+
42
+ ---
43
+
44
+ ## 三、接口文档与元信息(可选但优先)
45
+ - 对外接口优先补齐 Swagger:`@ApiTags` / `@Api` / `@ApiResponse`
46
+ - 需要显式状态码/响应头时使用:`@ResponseCode` / `@ResponseHeader`
47
+
48
+ ---
49
+
50
+ ## 四、禁止行为(必须)
51
+ - ❌ Controller 中编写业务逻辑(应下沉到 Service)
52
+ - ❌ Controller 中直接访问数据库/Repository
53
+ - ❌ Controller 中返回 Service 原始结果(必须映射为统一响应)
54
+ - ❌ `@Query()` / `@Body()` 使用 `any` 或未校验的入参对象
@@ -0,0 +1,107 @@
1
+ ---
2
+ alwaysApply: false
3
+ ---
4
+ # Trae 技能:Controller 推荐写法 + 常用注解速查(Recipes)
5
+
6
+ ## 适用与触发(重要)
7
+ - 仅当需要**新增/修改 Controller 路由接口**,且希望按本项目框架“推荐范式”实现时使用本 Skill。
8
+ - 触发口令建议:
9
+ - “请同时启用 `11-backend-controller-recipes.skill.md`,按项目范式实现 Controller”
10
+
11
+ ---
12
+
13
+ ## 一、写 Controller 的推荐范式(必须对齐)
14
+
15
+ ### 1.1 参考样例(写代码前先对齐)
16
+ - 推荐在实现前先对齐以下文件的写法(不要臆造新风格):
17
+ - `src/controllers/example/NewAnnotationExampleController.ts`
18
+ - `src/controllers/home/ytUser.controller.ts`
19
+ - `src/controllers/base.controller.ts`
20
+
21
+ ### 1.2 Controller 标准骨架(复制这个结构)
22
+ 目标:DTO 入参 → 调用 Service → 统一响应(`success/fail`)→ try/catch + logger
23
+
24
+ ```ts
25
+ import { Get, Post, Query, Body, State, ResponseCode, ResponseHeader } from '@src/framework/decorator/controller';
26
+ import { BaseController, ControllerResponse } from '@src/controllers/base.controller';
27
+ import { Inject } from 'dp-ioc2';
28
+ import { logger } from '@src/framework/utils/logger';
29
+ import { SomeService } from '@src/service/some.service';
30
+ import { SomeQueryDto, SomeBodyDto } from '@src/dto/controller/xxx/some.controller.dto';
31
+ import { CommonServiceResultCode } from '@src/framework/types/ServiceResult';
32
+
33
+ export class SomeController extends BaseController {
34
+ @Inject(SomeService)
35
+ private someService: SomeService;
36
+
37
+ @Get('/some/path')
38
+ @ResponseCode(200)
39
+ async getSomething(
40
+ @Query(SomeQueryDto) query: SomeQueryDto,
41
+ @State('user') user: { userId: number; type?: number },
42
+ ): Promise<ControllerResponse<any>> {
43
+ try {
44
+ const result = await this.someService.someMethod(query, user.userId);
45
+ if (result.code !== CommonServiceResultCode.SUCCESS) {
46
+ return this.fail(-1, result.message);
47
+ }
48
+ return this.success(result.data);
49
+ } catch (error) {
50
+ logger.error('getSomething 失败', error as Error);
51
+ return this.fail(-1, '系统错误');
52
+ }
53
+ }
54
+ }
55
+ ```
56
+
57
+ 硬约束:
58
+ - Controller **不写业务逻辑**,只做编排
59
+ - Controller **不直接访问数据库/Repository**
60
+ - Controller **不 return Service 原始结果**,必须映射 `success/fail`
61
+ - Controller `async` **必须 try/catch**
62
+
63
+ ---
64
+
65
+ ## 二、常用注解速查(按本项目实现理解)
66
+
67
+ ### 2.1 路由类注解
68
+ - **`@Get('/path')`**:注册 GET 路由;不传参数时 url 默认使用方法名
69
+ - **`@Post('/path')`**:注册 POST 路由;不传参数时 url 默认使用方法名
70
+
71
+ ### 2.2 参数注解(重点)
72
+ - **`@Query()`**:
73
+ - 读取 query 参数
74
+ - 推荐写法:`@Query(SomeQueryDto) query: XxxQueryDto`
75
+ - **`@Body()`**:
76
+ - 读取 body 参数
77
+ - 推荐写法:`@Body(SomeBodyDto) body: XxxBodyDto`
78
+ - **`@State()`**:
79
+ - 读取整个 `ctx.state`(样例:`ytUser.controller.ts`)
80
+ - 推荐写法:`@State() state: { user: { userId: number; type?: number } }`
81
+ - **`@State('user')`**:
82
+ - 读取 `ctx.state.user`(样例:`NewAnnotationExampleController.ts`)
83
+ - 推荐写法:`@State('user') user: { userId: number; type?: number }`
84
+
85
+ 禁止:
86
+ - ❌ `@Query() query: any` / `@Body() body: any` / `@State() state: any`(尽量不要)
87
+
88
+ ### 2.3 响应元信息注解
89
+ - **`@ResponseCode(200|201|...)`**:设置 HTTP 状态码(样例中 GET 用 200,POST 用 201)
90
+ - **`@ResponseHeader(key, value)`**:为响应添加 header
91
+
92
+ ### 2.4 响应校验注解(进阶,按需)
93
+ - **`@ResponseValidator(DtoClass, objectKey?)`**:对响应数据做校验
94
+ - **`@ResponseValidateIf(DtoClass, fn)`**:仅当 fn 返回真时才校验(样例:`ytUser.controller.ts`)
95
+
96
+ ---
97
+
98
+ ## 三、常见组合(直接套用)
99
+
100
+ ### 3.1 需要登录用户的 GET 接口
101
+ - `@Get('/xxx') + @State('user')`(或 `@State()` 取整段 state)
102
+
103
+ ### 3.2 带 body 的 POST 接口
104
+ - `@Post('/xxx') + @Body(SomeBodyDto) body: DtSomeBodyDtoo + @ResponseCode(201)`
105
+
106
+ ### 3.3 需要自定义 header
107
+ - `@ResponseHeader('X-XXX', 'value')`
@@ -0,0 +1,25 @@
1
+ ---
2
+ alwaysApply: false
3
+ ---
4
+ # Trae 技能:Repository 与数据访问规范
5
+
6
+ ## 一、Repository 使用方式
7
+
8
+ - Service 中通过:
9
+ - `getDataRepository<Entity>(EntityClass)`
10
+ - 或 `createLazyRepository()`(推荐)
11
+
12
+ ---
13
+
14
+ ## 二、事务管理
15
+
16
+ - 涉及多表操作时:
17
+ - 必须使用 `transactionManager.executeInTransaction()`
18
+ - 禁止手动管理事务
19
+
20
+ ---
21
+
22
+ ## 三、禁止行为
23
+
24
+ - ❌ Controller 中访问 Repository
25
+ - ❌ Service 中直接获取 DataSource