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,311 @@
|
|
|
1
|
+
|
|
2
|
+
export function Post(url?: string): MethodDecorator {
|
|
3
|
+
return (target: any,
|
|
4
|
+
methodName: any,
|
|
5
|
+
propertyDescriptor: PropertyDescriptor) => {
|
|
6
|
+
if (!target.$_PostMethods) {
|
|
7
|
+
target.$_PostMethods = new Set();
|
|
8
|
+
}
|
|
9
|
+
target.$_PostMethods.add({
|
|
10
|
+
methodName,
|
|
11
|
+
url: url ?? methodName
|
|
12
|
+
})
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function Get(url?: string): MethodDecorator {
|
|
17
|
+
return (target: any,
|
|
18
|
+
methodName: any,
|
|
19
|
+
propertyDescriptor: PropertyDescriptor) => {
|
|
20
|
+
if (!target.$_GetMethods) {
|
|
21
|
+
target.$_GetMethods = new Set();
|
|
22
|
+
}
|
|
23
|
+
target.$_GetMethods.add({
|
|
24
|
+
methodName,
|
|
25
|
+
url: url ?? methodName
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function Put(url?: string): MethodDecorator {
|
|
31
|
+
return (target: any,
|
|
32
|
+
methodName: any,
|
|
33
|
+
propertyDescriptor: PropertyDescriptor) => {
|
|
34
|
+
if (!target.$_PutMethods) {
|
|
35
|
+
target.$_PutMethods = new Set();
|
|
36
|
+
}
|
|
37
|
+
target.$_PutMethods.add({
|
|
38
|
+
methodName,
|
|
39
|
+
url: url ?? methodName
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function Del(url?: string): MethodDecorator {
|
|
45
|
+
return (target: any,
|
|
46
|
+
methodName: any,
|
|
47
|
+
propertyDescriptor: PropertyDescriptor) => {
|
|
48
|
+
if (!target.$_DelMethods) {
|
|
49
|
+
target.$_DelMethods = new Set();
|
|
50
|
+
}
|
|
51
|
+
target.$_DelMethods.add({
|
|
52
|
+
methodName,
|
|
53
|
+
url: url ?? methodName
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function All(url?: string): MethodDecorator {
|
|
59
|
+
return (target: any,
|
|
60
|
+
methodName: any,
|
|
61
|
+
propertyDescriptor: PropertyDescriptor) => {
|
|
62
|
+
if (!target.$_AllMethods) {
|
|
63
|
+
target.$_AllMethods = new Set();
|
|
64
|
+
}
|
|
65
|
+
target.$_AllMethods.add({
|
|
66
|
+
methodName,
|
|
67
|
+
url: url ?? methodName
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function ResponseCode(code = 200): MethodDecorator {
|
|
73
|
+
return (target: any,
|
|
74
|
+
methodName: any,
|
|
75
|
+
propertyDescriptor: PropertyDescriptor) => {
|
|
76
|
+
if (!target.$_ResponseCode) {
|
|
77
|
+
target.$_ResponseCode = new Map<string, number>();
|
|
78
|
+
}
|
|
79
|
+
target.$_ResponseCode.set(methodName, code);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function ResponseHeader(type: string, value: any): MethodDecorator {
|
|
84
|
+
return (target: any,
|
|
85
|
+
methodName: any,
|
|
86
|
+
propertyDescriptor: PropertyDescriptor) => {
|
|
87
|
+
if (!target.$_ResponseHeader) {
|
|
88
|
+
target.$_ResponseHeader = new Map<string, Map<string, any>>();
|
|
89
|
+
}
|
|
90
|
+
let headerMap = target.$_ResponseHeader.get(methodName);
|
|
91
|
+
if (!headerMap) {
|
|
92
|
+
headerMap = new Map<string, any>()
|
|
93
|
+
}
|
|
94
|
+
headerMap.set(type, value)
|
|
95
|
+
target.$_ResponseHeader.set(methodName, headerMap);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
*
|
|
101
|
+
* @param clas
|
|
102
|
+
* @param objectKey 验证的数据对象所处的key
|
|
103
|
+
* @returns
|
|
104
|
+
*/
|
|
105
|
+
export function ResponseValidator(clas: any, objectKey: string = ""): MethodDecorator {
|
|
106
|
+
return (target: any,
|
|
107
|
+
methodName: any,
|
|
108
|
+
propertyDescriptor: PropertyDescriptor) => {
|
|
109
|
+
if (!target.$_ResponseValidator) {
|
|
110
|
+
target.$_ResponseValidator = new Map<string, any>();
|
|
111
|
+
}
|
|
112
|
+
target.$_ResponseValidator.set(methodName, {
|
|
113
|
+
clas,
|
|
114
|
+
objectKey
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
*
|
|
122
|
+
* @param fn 只有这里的函数有返回数据才会进入校验环节
|
|
123
|
+
* @returns
|
|
124
|
+
*/
|
|
125
|
+
export function ResponseValidateIf(clas: any, fn: (data: any) => any,): MethodDecorator {
|
|
126
|
+
return (target: any,
|
|
127
|
+
methodName: any,
|
|
128
|
+
propertyDescriptor: PropertyDescriptor) => {
|
|
129
|
+
if (!target.$_ResponseValidateIf) {
|
|
130
|
+
target.$_ResponseValidateIf = new Map<string, any>();
|
|
131
|
+
}
|
|
132
|
+
target.$_ResponseValidateIf.set(methodName, {
|
|
133
|
+
clas,
|
|
134
|
+
fn,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 控制器的缓存
|
|
141
|
+
* @param cacheyFn
|
|
142
|
+
* @param options
|
|
143
|
+
* @returns
|
|
144
|
+
*/
|
|
145
|
+
export function ControllerCache(cacheyFn: (...params: any[]) => any, options: {
|
|
146
|
+
ttl: number | { max: number, min: number },
|
|
147
|
+
enable?: boolean, // true 表示立即启用,默认是true
|
|
148
|
+
} = { ttl: 20, enable: true }): MethodDecorator {
|
|
149
|
+
return (target: any,
|
|
150
|
+
methodName: any,
|
|
151
|
+
propertyDescriptor: PropertyDescriptor) => {
|
|
152
|
+
if (!target.$_ControllerCache) {
|
|
153
|
+
target.$_ControllerCache = new Map<string, any>();
|
|
154
|
+
}
|
|
155
|
+
target.$_ControllerCache.set(methodName, {
|
|
156
|
+
cacheyFn,
|
|
157
|
+
options,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
export enum IParamTypeEnum {
|
|
165
|
+
Body = "Body",
|
|
166
|
+
Query = "Query",
|
|
167
|
+
Params = "Params",
|
|
168
|
+
Headers = "Headers",
|
|
169
|
+
State = "State",
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* body 参数注解
|
|
174
|
+
* @param validate
|
|
175
|
+
* @returns
|
|
176
|
+
*/
|
|
177
|
+
export function Body(validate?: any) {
|
|
178
|
+
|
|
179
|
+
return (target: any, methodName: string, index: number) => {
|
|
180
|
+
|
|
181
|
+
if (!target.$_MethdosParamInfo) {
|
|
182
|
+
target.$_MethdosParamInfo = new Map<string, { type: IParamTypeEnum, validate: any }[]>();
|
|
183
|
+
}
|
|
184
|
+
let paramsInfo = target.$_MethdosParamInfo.get(methodName);
|
|
185
|
+
paramsInfo = paramsInfo ?? [];
|
|
186
|
+
paramsInfo[index] = {
|
|
187
|
+
type: IParamTypeEnum.Body,
|
|
188
|
+
validate,
|
|
189
|
+
}
|
|
190
|
+
target.$_MethdosParamInfo.set(methodName, paramsInfo);
|
|
191
|
+
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* query 参数
|
|
198
|
+
* @param validate
|
|
199
|
+
* @returns
|
|
200
|
+
*/
|
|
201
|
+
export function Query(validate?: any) {
|
|
202
|
+
|
|
203
|
+
return (target: any, methodName: string, index: number) => {
|
|
204
|
+
|
|
205
|
+
if (!target.$_MethdosParamInfo) {
|
|
206
|
+
target.$_MethdosParamInfo = new Map<string, { type: IParamTypeEnum, validate: any }[]>();
|
|
207
|
+
// console.log("query MethodsParamInfo set ",index)
|
|
208
|
+
}
|
|
209
|
+
let paramsInfo = target.$_MethdosParamInfo.get(methodName);
|
|
210
|
+
paramsInfo = paramsInfo ?? [];
|
|
211
|
+
paramsInfo[index] = {
|
|
212
|
+
type: IParamTypeEnum.Query,
|
|
213
|
+
validate,
|
|
214
|
+
}
|
|
215
|
+
target.$_MethdosParamInfo.set(methodName, paramsInfo)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* @param validate
|
|
222
|
+
* @returns
|
|
223
|
+
*/
|
|
224
|
+
export function Params(validate?: any) {
|
|
225
|
+
|
|
226
|
+
return (target: any, methodName: string, index: number) => {
|
|
227
|
+
|
|
228
|
+
if (!target.$_MethdosParamInfo) {
|
|
229
|
+
target.$_MethdosParamInfo = new Map<string, { type: IParamTypeEnum, validate: any }[]>();
|
|
230
|
+
// console.log("Params MethodsParamInfo set ",index)
|
|
231
|
+
}
|
|
232
|
+
let paramsInfo = target.$_MethdosParamInfo.get(methodName);
|
|
233
|
+
paramsInfo = paramsInfo ?? [];
|
|
234
|
+
paramsInfo[index] = {
|
|
235
|
+
type: IParamTypeEnum.Params,
|
|
236
|
+
validate,
|
|
237
|
+
}
|
|
238
|
+
target.$_MethdosParamInfo.set(methodName, paramsInfo)
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* @param validate
|
|
245
|
+
* @returns
|
|
246
|
+
*/
|
|
247
|
+
export function Headers(validate?: any) {
|
|
248
|
+
|
|
249
|
+
return (target: any, methodName: string, index: number) => {
|
|
250
|
+
|
|
251
|
+
if (!target.$_MethdosParamInfo) {
|
|
252
|
+
target.$_MethdosParamInfo = new Map<string, { type: IParamTypeEnum, validate: any }[]>();
|
|
253
|
+
// console.log("Params MethodsParamInfo set ",index)
|
|
254
|
+
}
|
|
255
|
+
let paramsInfo = target.$_MethdosParamInfo.get(methodName);
|
|
256
|
+
paramsInfo = paramsInfo ?? [];
|
|
257
|
+
paramsInfo[index] = {
|
|
258
|
+
type: IParamTypeEnum.Headers,
|
|
259
|
+
validate,
|
|
260
|
+
}
|
|
261
|
+
target.$_MethdosParamInfo.set(methodName, paramsInfo)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* 读取ctx.state上的用户信息
|
|
268
|
+
* @param validate
|
|
269
|
+
* @returns
|
|
270
|
+
*/
|
|
271
|
+
export function State(validate?: any) {
|
|
272
|
+
|
|
273
|
+
return (target: any, methodName: string, index: number) => {
|
|
274
|
+
|
|
275
|
+
if (!target.$_MethdosParamInfo) {
|
|
276
|
+
target.$_MethdosParamInfo = new Map<string, { type: IParamTypeEnum, validate: any }[]>();
|
|
277
|
+
// console.log("Params MethodsParamInfo set ",index)
|
|
278
|
+
}
|
|
279
|
+
let paramsInfo = target.$_MethdosParamInfo.get(methodName);
|
|
280
|
+
paramsInfo = paramsInfo ?? [];
|
|
281
|
+
paramsInfo[index] = {
|
|
282
|
+
type: IParamTypeEnum.State,
|
|
283
|
+
validate,
|
|
284
|
+
}
|
|
285
|
+
target.$_MethdosParamInfo.set(methodName, paramsInfo)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* 此注解实现 class-validator的 数据预处理,可以直接传一个自定义的处理函数即可
|
|
295
|
+
* @param func
|
|
296
|
+
*/
|
|
297
|
+
export function Transform(func: (data: any) => any) {
|
|
298
|
+
return (target: any, propertyKey: string) => {
|
|
299
|
+
|
|
300
|
+
if (!target.$_Transform) {
|
|
301
|
+
target.$_Transform = new Map();
|
|
302
|
+
}
|
|
303
|
+
target.$_Transform.set(propertyKey, func);
|
|
304
|
+
// Object.defineProperty(target, propertyKey, {
|
|
305
|
+
// get: () => target[propertyKey],
|
|
306
|
+
// set: (newValue) => {
|
|
307
|
+
// target[propertyKey] = newValue;
|
|
308
|
+
// },
|
|
309
|
+
// });
|
|
310
|
+
}
|
|
311
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 注解数据存储接口
|
|
3
|
+
*/
|
|
4
|
+
export interface AnnotationData {
|
|
5
|
+
processorName: string;
|
|
6
|
+
data: any;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 注解注册器
|
|
11
|
+
* 用于注册注解数据到控制器
|
|
12
|
+
*/
|
|
13
|
+
export class AnnotationRegistry {
|
|
14
|
+
/**
|
|
15
|
+
* 注册注解数据
|
|
16
|
+
*/
|
|
17
|
+
static register(
|
|
18
|
+
target: any,
|
|
19
|
+
methodName: string,
|
|
20
|
+
processorName: string,
|
|
21
|
+
data: any
|
|
22
|
+
): void {
|
|
23
|
+
if (!target.$_Annotations) {
|
|
24
|
+
target.$_Annotations = new Map<string, Map<string, any>>();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (!target.$_Annotations.has(methodName)) {
|
|
28
|
+
target.$_Annotations.set(methodName, new Map<string, any>());
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
target.$_Annotations.get(methodName).set(processorName, data);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 获取注解数据
|
|
36
|
+
*/
|
|
37
|
+
static get(
|
|
38
|
+
target: any,
|
|
39
|
+
methodName: string,
|
|
40
|
+
processorName: string
|
|
41
|
+
): any {
|
|
42
|
+
if (!target.$_Annotations) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const methodAnnotations = target.$_Annotations.get(methodName);
|
|
47
|
+
if (!methodAnnotations) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const result = methodAnnotations.get(processorName);
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 获取方法的所有注解
|
|
57
|
+
*/
|
|
58
|
+
static getAll(target: any, methodName: string): Map<string, any> {
|
|
59
|
+
if (!target.$_Annotations) {
|
|
60
|
+
return new Map();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return target.$_Annotations.get(methodName) || new Map();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 通用注解装饰器工厂
|
|
69
|
+
*/
|
|
70
|
+
export function createAnnotationDecorator(processorName: string) {
|
|
71
|
+
return function(data?: any) {
|
|
72
|
+
return function(target: any, methodName: string, descriptor: PropertyDescriptor) {
|
|
73
|
+
AnnotationRegistry.register(target, methodName, processorName, data);
|
|
74
|
+
return descriptor;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 参数注解装饰器工厂
|
|
81
|
+
*/
|
|
82
|
+
export function createParameterDecorator(processorName: string) {
|
|
83
|
+
return function(data?: any) {
|
|
84
|
+
return function(target: any, methodName: string, index: number) {
|
|
85
|
+
AnnotationRegistry.register(target, methodName, processorName, {
|
|
86
|
+
...data,
|
|
87
|
+
parameterIndex: index
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 导出一些常用的注解装饰器
|
|
94
|
+
export const Logging = createAnnotationDecorator('Logging');
|
|
95
|
+
export const Permission = createAnnotationDecorator('Permission');
|
|
96
|
+
export const RateLimit = createAnnotationDecorator('RateLimit');
|
|
97
|
+
|
|
98
|
+
// 参数注解装饰器
|
|
99
|
+
export const LoggedParam = createParameterDecorator('Logging');
|
|
100
|
+
export const ValidatedParam = createParameterDecorator('ParameterValidation');
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { Context } from 'koa';
|
|
2
|
+
import { annotationRegistry } from '@src/framework/decorator/processor/AnnotationRegistry';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 注解处理器接口
|
|
6
|
+
* 所有注解处理器都需要实现这个接口
|
|
7
|
+
*/
|
|
8
|
+
export interface AnnotationProcessor {
|
|
9
|
+
/**
|
|
10
|
+
* 处理器名称
|
|
11
|
+
*/
|
|
12
|
+
readonly name: string;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 处理器优先级,数字越小优先级越高
|
|
16
|
+
*/
|
|
17
|
+
readonly priority: number;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 处理注解逻辑(前置处理)
|
|
21
|
+
* @param ctx Koa上下文
|
|
22
|
+
* @param controller 控制器实例
|
|
23
|
+
* @param methodName 方法名
|
|
24
|
+
* @param annotationData 注解数据
|
|
25
|
+
* @param callParams 当前调用参数
|
|
26
|
+
* @returns 处理结果,如果返回false则中断后续处理
|
|
27
|
+
*/
|
|
28
|
+
process(
|
|
29
|
+
ctx: Context,
|
|
30
|
+
controller: any,
|
|
31
|
+
methodName: string,
|
|
32
|
+
annotationData: any,
|
|
33
|
+
callParams: any[]
|
|
34
|
+
): Promise<boolean | any>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 后置处理方法(可选)
|
|
38
|
+
* 在控制器方法执行完成后调用
|
|
39
|
+
* @param ctx Koa上下文
|
|
40
|
+
* @param controller 控制器实例
|
|
41
|
+
* @param methodName 方法名
|
|
42
|
+
* @param response 控制器方法返回的响应
|
|
43
|
+
*/
|
|
44
|
+
postProcess?(
|
|
45
|
+
ctx: Context,
|
|
46
|
+
controller: any,
|
|
47
|
+
methodName: string,
|
|
48
|
+
response: any
|
|
49
|
+
): Promise<void>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 响应验证方法(可选)
|
|
53
|
+
* 验证控制器方法返回的响应数据
|
|
54
|
+
* @param controller 控制器实例
|
|
55
|
+
* @param methodName 方法名
|
|
56
|
+
* @param response 控制器方法返回的响应
|
|
57
|
+
* @returns 验证错误数组,空数组表示验证通过
|
|
58
|
+
*/
|
|
59
|
+
validateResponse?(
|
|
60
|
+
controller: any,
|
|
61
|
+
methodName: string,
|
|
62
|
+
response: any
|
|
63
|
+
): Promise<string[]>;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 日志记录方法(可选)
|
|
67
|
+
* 记录控制器方法的执行日志
|
|
68
|
+
* @param ctx Koa上下文
|
|
69
|
+
* @param controller 控制器实例
|
|
70
|
+
* @param methodName 方法名
|
|
71
|
+
* @param response 控制器方法返回的响应
|
|
72
|
+
*/
|
|
73
|
+
logResponse?(
|
|
74
|
+
ctx: Context,
|
|
75
|
+
controller: any,
|
|
76
|
+
methodName: string,
|
|
77
|
+
response: any
|
|
78
|
+
): Promise<void>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 注解处理器执行器
|
|
83
|
+
*/
|
|
84
|
+
export class AnnotationExecutor {
|
|
85
|
+
/**
|
|
86
|
+
* 执行所有相关的注解处理器
|
|
87
|
+
*/
|
|
88
|
+
static async execute(
|
|
89
|
+
ctx: Context,
|
|
90
|
+
controller: any,
|
|
91
|
+
methodName: string,
|
|
92
|
+
callParams: any[]
|
|
93
|
+
): Promise<boolean> {
|
|
94
|
+
const processors = annotationRegistry.getAllProcessors();
|
|
95
|
+
|
|
96
|
+
for (const processor of processors) {
|
|
97
|
+
// 检查控制器是否有该注解的数据
|
|
98
|
+
const annotationData = this.getAnnotationData(controller, methodName, processor.name);
|
|
99
|
+
if (annotationData !== null) {
|
|
100
|
+
try {
|
|
101
|
+
const result = await processor.process(
|
|
102
|
+
ctx,
|
|
103
|
+
controller,
|
|
104
|
+
methodName,
|
|
105
|
+
annotationData,
|
|
106
|
+
callParams
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
// 如果处理器返回false,中断后续处理
|
|
110
|
+
if (result === false) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
} catch (error) {
|
|
114
|
+
console.error(`注解处理器 ${processor.name} 执行失败:`, error);
|
|
115
|
+
// 可以选择是否继续执行其他处理器
|
|
116
|
+
// 这里选择继续执行
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 获取注解数据
|
|
126
|
+
* 支持新的注解系统和旧的注解系统
|
|
127
|
+
*/
|
|
128
|
+
private static getAnnotationData(controller: any, methodName: string, processorName: string): any {
|
|
129
|
+
// 首先检查新的注解系统
|
|
130
|
+
if (controller.$_Annotations) {
|
|
131
|
+
const methodAnnotations = controller.$_Annotations.get(methodName);
|
|
132
|
+
if (methodAnnotations) {
|
|
133
|
+
const annotationData = methodAnnotations.get(processorName);
|
|
134
|
+
if (methodAnnotations.has(processorName)) {
|
|
135
|
+
return annotationData;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// 兼容旧的注解系统
|
|
141
|
+
const legacyMappings: { [key: string]: string } = {
|
|
142
|
+
'ParameterValidation': '$_MethdosParamInfo',
|
|
143
|
+
'ResponseValidation': '$_ResponseValidator',
|
|
144
|
+
'Cache': '$_ControllerCache',
|
|
145
|
+
'ResponseCode': '$_ResponseCode',
|
|
146
|
+
'ResponseHeader': '$_ResponseHeader'
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const legacyMapName = legacyMappings[processorName];
|
|
150
|
+
if (legacyMapName && controller[legacyMapName]) {
|
|
151
|
+
return controller[legacyMapName].get(methodName);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 注解处理器注册配置
|
|
3
|
+
* 用于在应用启动时注册所有注解处理器
|
|
4
|
+
* 这种方式对 webpack 打包友好
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// 框架层注解处理器
|
|
8
|
+
import {
|
|
9
|
+
LoggingProcessor,
|
|
10
|
+
PermissionProcessor,
|
|
11
|
+
RateLimitProcessor
|
|
12
|
+
} from './processors/CustomProcessors';
|
|
13
|
+
|
|
14
|
+
// 业务层注解处理器
|
|
15
|
+
import { PerformanceMonitorProcessor } from '@src/annotations/processors/PerformanceMonitorProcessor';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 获取所有框架层注解处理器
|
|
19
|
+
*/
|
|
20
|
+
export function getFrameworkAnnotationProcessors() {
|
|
21
|
+
return [
|
|
22
|
+
new LoggingProcessor(),
|
|
23
|
+
new PermissionProcessor(),
|
|
24
|
+
new RateLimitProcessor()
|
|
25
|
+
];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 获取所有业务层注解处理器
|
|
30
|
+
*/
|
|
31
|
+
export function getBusinessAnnotationProcessors() {
|
|
32
|
+
return [
|
|
33
|
+
new PerformanceMonitorProcessor()
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 获取所有注解处理器(框架层 + 业务层)
|
|
39
|
+
*/
|
|
40
|
+
export function getAllAnnotationProcessors() {
|
|
41
|
+
return [
|
|
42
|
+
...getFrameworkAnnotationProcessors(),
|
|
43
|
+
...getBusinessAnnotationProcessors()
|
|
44
|
+
];
|
|
45
|
+
}
|