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,246 @@
|
|
|
1
|
+
import { AnnotationExecutor, AnnotationProcessorRegistry } from '../../../src/framework/decorator/processor/AnnotationProcessor';
|
|
2
|
+
import { Context } from 'koa';
|
|
3
|
+
|
|
4
|
+
// 模拟 Koa Context
|
|
5
|
+
const createMockContext = (): Context => ({
|
|
6
|
+
status: 200,
|
|
7
|
+
body: null,
|
|
8
|
+
request: {
|
|
9
|
+
body: { test: 'body' },
|
|
10
|
+
headers: { 'content-type': 'application/json' },
|
|
11
|
+
query: { id: '123' }
|
|
12
|
+
},
|
|
13
|
+
params: { userId: '456' },
|
|
14
|
+
state: { user: { id: 1, name: 'test' } },
|
|
15
|
+
set: jest.fn(),
|
|
16
|
+
throw: jest.fn()
|
|
17
|
+
} as any);
|
|
18
|
+
|
|
19
|
+
// 测试用的注解处理器
|
|
20
|
+
class TestProcessor implements AnnotationProcessor {
|
|
21
|
+
readonly name = 'TestProcessor';
|
|
22
|
+
readonly priority = 100;
|
|
23
|
+
|
|
24
|
+
public processCallCount = 0;
|
|
25
|
+
public lastAnnotationData: any = null;
|
|
26
|
+
public shouldReturn = true;
|
|
27
|
+
|
|
28
|
+
async process(
|
|
29
|
+
ctx: Context,
|
|
30
|
+
controller: any,
|
|
31
|
+
methodName: string,
|
|
32
|
+
annotationData: any,
|
|
33
|
+
callParams: any[]
|
|
34
|
+
): Promise<boolean> {
|
|
35
|
+
this.processCallCount++;
|
|
36
|
+
this.lastAnnotationData = annotationData;
|
|
37
|
+
return this.shouldReturn;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe('注解执行器测试', () => {
|
|
42
|
+
let testProcessor: TestProcessor;
|
|
43
|
+
let mockContext: Context;
|
|
44
|
+
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
testProcessor = new TestProcessor();
|
|
47
|
+
mockContext = createMockContext();
|
|
48
|
+
AnnotationProcessorRegistry.clear();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('AnnotationExecutor.execute', () => {
|
|
52
|
+
test('应该能够执行所有相关处理器', async () => {
|
|
53
|
+
AnnotationProcessorRegistry.register(testProcessor);
|
|
54
|
+
|
|
55
|
+
const mockController = {
|
|
56
|
+
$_Annotations: new Map([
|
|
57
|
+
['testMethod', new Map([
|
|
58
|
+
['TestProcessor', { testData: 'value' }]
|
|
59
|
+
])]
|
|
60
|
+
])
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const result = await AnnotationExecutor.execute(
|
|
64
|
+
mockContext,
|
|
65
|
+
mockController,
|
|
66
|
+
'testMethod',
|
|
67
|
+
[]
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
expect(result).toBe(true);
|
|
71
|
+
expect(testProcessor.processCallCount).toBe(1);
|
|
72
|
+
expect(testProcessor.lastAnnotationData).toEqual({ testData: 'value' });
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('应该跳过没有注解数据的方法', async () => {
|
|
76
|
+
AnnotationProcessorRegistry.register(testProcessor);
|
|
77
|
+
|
|
78
|
+
const mockController = {
|
|
79
|
+
$_Annotations: new Map([
|
|
80
|
+
['testMethod', new Map([
|
|
81
|
+
['TestProcessor', { testData: 'value' }]
|
|
82
|
+
])]
|
|
83
|
+
])
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const result = await AnnotationExecutor.execute(
|
|
87
|
+
mockContext,
|
|
88
|
+
mockController,
|
|
89
|
+
'otherMethod', // 没有注解数据的方法
|
|
90
|
+
[]
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
expect(result).toBe(true);
|
|
94
|
+
expect(testProcessor.processCallCount).toBe(0);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('处理器返回false应该中断执行', async () => {
|
|
98
|
+
testProcessor.shouldReturn = false;
|
|
99
|
+
AnnotationProcessorRegistry.register(testProcessor);
|
|
100
|
+
|
|
101
|
+
const mockController = {
|
|
102
|
+
$_Annotations: new Map([
|
|
103
|
+
['testMethod', new Map([
|
|
104
|
+
['TestProcessor', { testData: 'value' }]
|
|
105
|
+
])]
|
|
106
|
+
])
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const result = await AnnotationExecutor.execute(
|
|
110
|
+
mockContext,
|
|
111
|
+
mockController,
|
|
112
|
+
'testMethod',
|
|
113
|
+
[]
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
expect(result).toBe(false);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('应该支持多个处理器', async () => {
|
|
120
|
+
const processor1 = new TestProcessor();
|
|
121
|
+
processor1.name = 'Processor1';
|
|
122
|
+
processor1.priority = 50;
|
|
123
|
+
|
|
124
|
+
const processor2 = new TestProcessor();
|
|
125
|
+
processor2.name = 'Processor2';
|
|
126
|
+
processor2.priority = 100;
|
|
127
|
+
|
|
128
|
+
AnnotationProcessorRegistry.register(processor1);
|
|
129
|
+
AnnotationProcessorRegistry.register(processor2);
|
|
130
|
+
|
|
131
|
+
const mockController = {
|
|
132
|
+
$_Annotations: new Map([
|
|
133
|
+
['testMethod', new Map([
|
|
134
|
+
['Processor1', { data1: 'value1' }],
|
|
135
|
+
['Processor2', { data2: 'value2' }]
|
|
136
|
+
])]
|
|
137
|
+
])
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const result = await AnnotationExecutor.execute(
|
|
141
|
+
mockContext,
|
|
142
|
+
mockController,
|
|
143
|
+
'testMethod',
|
|
144
|
+
[]
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
expect(result).toBe(true);
|
|
148
|
+
expect(processor1.processCallCount).toBe(1);
|
|
149
|
+
expect(processor2.processCallCount).toBe(1);
|
|
150
|
+
expect(processor1.lastAnnotationData).toEqual({ data1: 'value1' });
|
|
151
|
+
expect(processor2.lastAnnotationData).toEqual({ data2: 'value2' });
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test('应该按优先级顺序执行处理器', async () => {
|
|
155
|
+
const executionOrder: string[] = [];
|
|
156
|
+
|
|
157
|
+
const processor1 = new TestProcessor();
|
|
158
|
+
processor1.name = 'Processor1';
|
|
159
|
+
processor1.priority = 100;
|
|
160
|
+
processor1.process = jest.fn().mockImplementation(async () => {
|
|
161
|
+
executionOrder.push('Processor1');
|
|
162
|
+
return true;
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const processor2 = new TestProcessor();
|
|
166
|
+
processor2.name = 'Processor2';
|
|
167
|
+
processor2.priority = 50;
|
|
168
|
+
processor2.process = jest.fn().mockImplementation(async () => {
|
|
169
|
+
executionOrder.push('Processor2');
|
|
170
|
+
return true;
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
AnnotationProcessorRegistry.register(processor1);
|
|
174
|
+
AnnotationProcessorRegistry.register(processor2);
|
|
175
|
+
|
|
176
|
+
const mockController = {
|
|
177
|
+
$_Annotations: new Map([
|
|
178
|
+
['testMethod', new Map([
|
|
179
|
+
['Processor1', { data1: 'value1' }],
|
|
180
|
+
['Processor2', { data2: 'value2' }]
|
|
181
|
+
])]
|
|
182
|
+
])
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
await AnnotationExecutor.execute(
|
|
186
|
+
mockContext,
|
|
187
|
+
mockController,
|
|
188
|
+
'testMethod',
|
|
189
|
+
[]
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
expect(executionOrder).toEqual(['Processor2', 'Processor1']); // 优先级高的先执行
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test('应该兼容旧的注解系统', async () => {
|
|
196
|
+
AnnotationProcessorRegistry.register(testProcessor);
|
|
197
|
+
|
|
198
|
+
const mockController = {
|
|
199
|
+
$_MethdosParamInfo: new Map([
|
|
200
|
+
['testMethod', [{ type: 'Body', validate: null }]]
|
|
201
|
+
])
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const result = await AnnotationExecutor.execute(
|
|
205
|
+
mockContext,
|
|
206
|
+
mockController,
|
|
207
|
+
'testMethod',
|
|
208
|
+
[]
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
expect(result).toBe(true);
|
|
212
|
+
expect(testProcessor.processCallCount).toBe(1);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test('处理器抛出异常不应该中断执行', async () => {
|
|
216
|
+
const errorProcessor = new TestProcessor();
|
|
217
|
+
errorProcessor.name = 'ErrorProcessor';
|
|
218
|
+
errorProcessor.process = jest.fn().mockRejectedValue(new Error('Test error'));
|
|
219
|
+
|
|
220
|
+
const normalProcessor = new TestProcessor();
|
|
221
|
+
normalProcessor.name = 'NormalProcessor';
|
|
222
|
+
|
|
223
|
+
AnnotationProcessorRegistry.register(errorProcessor);
|
|
224
|
+
AnnotationProcessorRegistry.register(normalProcessor);
|
|
225
|
+
|
|
226
|
+
const mockController = {
|
|
227
|
+
$_Annotations: new Map([
|
|
228
|
+
['testMethod', new Map([
|
|
229
|
+
['ErrorProcessor', { data: 'value' }],
|
|
230
|
+
['NormalProcessor', { data: 'value' }]
|
|
231
|
+
])]
|
|
232
|
+
])
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const result = await AnnotationExecutor.execute(
|
|
236
|
+
mockContext,
|
|
237
|
+
mockController,
|
|
238
|
+
'testMethod',
|
|
239
|
+
[]
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
expect(result).toBe(true);
|
|
243
|
+
expect(normalProcessor.processCallCount).toBe(1);
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
});
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { AnnotationProcessor, AnnotationProcessorRegistry } from '../../../src/framework/decorator/processor/AnnotationProcessor';
|
|
2
|
+
import { Context } from 'koa';
|
|
3
|
+
|
|
4
|
+
// 模拟 Koa Context
|
|
5
|
+
const createMockContext = (): Context => ({
|
|
6
|
+
status: 200,
|
|
7
|
+
body: null,
|
|
8
|
+
request: {
|
|
9
|
+
body: {},
|
|
10
|
+
headers: {},
|
|
11
|
+
query: {}
|
|
12
|
+
},
|
|
13
|
+
params: {},
|
|
14
|
+
state: {},
|
|
15
|
+
set: jest.fn(),
|
|
16
|
+
throw: jest.fn()
|
|
17
|
+
} as any);
|
|
18
|
+
|
|
19
|
+
// 测试用的注解处理器
|
|
20
|
+
class TestProcessor implements AnnotationProcessor {
|
|
21
|
+
readonly name = 'TestProcessor';
|
|
22
|
+
readonly priority = 100;
|
|
23
|
+
|
|
24
|
+
public processCallCount = 0;
|
|
25
|
+
public lastAnnotationData: any = null;
|
|
26
|
+
public shouldReturn = true;
|
|
27
|
+
|
|
28
|
+
async process(
|
|
29
|
+
ctx: Context,
|
|
30
|
+
controller: any,
|
|
31
|
+
methodName: string,
|
|
32
|
+
annotationData: any,
|
|
33
|
+
callParams: any[]
|
|
34
|
+
): Promise<boolean> {
|
|
35
|
+
this.processCallCount++;
|
|
36
|
+
this.lastAnnotationData = annotationData;
|
|
37
|
+
return this.shouldReturn;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 可配置的测试处理器
|
|
42
|
+
class ConfigurableTestProcessor implements AnnotationProcessor {
|
|
43
|
+
readonly name: string;
|
|
44
|
+
readonly priority: number;
|
|
45
|
+
|
|
46
|
+
public processCallCount = 0;
|
|
47
|
+
public lastAnnotationData: any = null;
|
|
48
|
+
public shouldReturn = true;
|
|
49
|
+
|
|
50
|
+
constructor(name: string, priority: number) {
|
|
51
|
+
this.name = name;
|
|
52
|
+
this.priority = priority;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async process(
|
|
56
|
+
ctx: Context,
|
|
57
|
+
controller: any,
|
|
58
|
+
methodName: string,
|
|
59
|
+
annotationData: any,
|
|
60
|
+
callParams: any[]
|
|
61
|
+
): Promise<boolean> {
|
|
62
|
+
this.processCallCount++;
|
|
63
|
+
this.lastAnnotationData = annotationData;
|
|
64
|
+
return this.shouldReturn;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
describe('注解处理器系统测试', () => {
|
|
69
|
+
let testProcessor: TestProcessor;
|
|
70
|
+
let mockContext: Context;
|
|
71
|
+
|
|
72
|
+
beforeEach(() => {
|
|
73
|
+
testProcessor = new TestProcessor();
|
|
74
|
+
mockContext = createMockContext();
|
|
75
|
+
AnnotationProcessorRegistry.clear();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('AnnotationProcessorRegistry', () => {
|
|
79
|
+
test('应该能够注册处理器', () => {
|
|
80
|
+
AnnotationProcessorRegistry.register(testProcessor);
|
|
81
|
+
|
|
82
|
+
const retrievedProcessor = AnnotationProcessorRegistry.getProcessor('TestProcessor');
|
|
83
|
+
expect(retrievedProcessor).toBe(testProcessor);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('应该能够获取所有处理器', () => {
|
|
87
|
+
const processor1 = new ConfigurableTestProcessor('Processor1', 50);
|
|
88
|
+
const processor2 = new ConfigurableTestProcessor('Processor2', 100);
|
|
89
|
+
|
|
90
|
+
AnnotationProcessorRegistry.register(processor2);
|
|
91
|
+
AnnotationProcessorRegistry.register(processor1);
|
|
92
|
+
|
|
93
|
+
const allProcessors = AnnotationProcessorRegistry.getAllProcessors();
|
|
94
|
+
expect(allProcessors).toHaveLength(2);
|
|
95
|
+
expect(allProcessors[0]).toBe(processor1); // 优先级高的在前面
|
|
96
|
+
expect(allProcessors[1]).toBe(processor2);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('应该能够移除处理器', () => {
|
|
100
|
+
AnnotationProcessorRegistry.register(testProcessor);
|
|
101
|
+
expect(AnnotationProcessorRegistry.getProcessor('TestProcessor')).toBe(testProcessor);
|
|
102
|
+
|
|
103
|
+
const removed = AnnotationProcessorRegistry.unregister('TestProcessor');
|
|
104
|
+
expect(removed).toBe(true);
|
|
105
|
+
expect(AnnotationProcessorRegistry.getProcessor('TestProcessor')).toBeUndefined();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('应该能够清空所有处理器', () => {
|
|
109
|
+
AnnotationProcessorRegistry.register(testProcessor);
|
|
110
|
+
expect(AnnotationProcessorRegistry.getAllProcessors()).toHaveLength(1);
|
|
111
|
+
|
|
112
|
+
AnnotationProcessorRegistry.clear();
|
|
113
|
+
expect(AnnotationProcessorRegistry.getAllProcessors()).toHaveLength(0);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test('移除不存在的处理器应该返回false', () => {
|
|
117
|
+
const removed = AnnotationProcessorRegistry.unregister('NonExistentProcessor');
|
|
118
|
+
expect(removed).toBe(false);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('处理器优先级', () => {
|
|
123
|
+
test('应该按优先级排序处理器', () => {
|
|
124
|
+
const processor1 = new ConfigurableTestProcessor('Processor1', 200);
|
|
125
|
+
const processor2 = new ConfigurableTestProcessor('Processor2', 50);
|
|
126
|
+
const processor3 = new ConfigurableTestProcessor('Processor3', 100);
|
|
127
|
+
|
|
128
|
+
AnnotationProcessorRegistry.register(processor1);
|
|
129
|
+
AnnotationProcessorRegistry.register(processor2);
|
|
130
|
+
AnnotationProcessorRegistry.register(processor3);
|
|
131
|
+
|
|
132
|
+
const allProcessors = AnnotationProcessorRegistry.getAllProcessors();
|
|
133
|
+
expect(allProcessors[0]).toBe(processor2); // 优先级 50
|
|
134
|
+
expect(allProcessors[1]).toBe(processor3); // 优先级 100
|
|
135
|
+
expect(allProcessors[2]).toBe(processor1); // 优先级 200
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
describe('处理器执行', () => {
|
|
140
|
+
test('应该能够执行处理器', async () => {
|
|
141
|
+
AnnotationProcessorRegistry.register(testProcessor);
|
|
142
|
+
|
|
143
|
+
const mockController = {
|
|
144
|
+
$_Annotations: new Map([
|
|
145
|
+
['testMethod', new Map([
|
|
146
|
+
['TestProcessor', { testData: 'value' }]
|
|
147
|
+
])]
|
|
148
|
+
])
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const result = await AnnotationProcessorRegistry.getAllProcessors()[0].process(
|
|
152
|
+
mockContext,
|
|
153
|
+
mockController,
|
|
154
|
+
'testMethod',
|
|
155
|
+
{ testData: 'value' },
|
|
156
|
+
[]
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
expect(result).toBe(true);
|
|
160
|
+
expect(testProcessor.processCallCount).toBe(1);
|
|
161
|
+
expect(testProcessor.lastAnnotationData).toEqual({ testData: 'value' });
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
test('处理器返回false应该中断执行', async () => {
|
|
165
|
+
testProcessor.shouldReturn = false;
|
|
166
|
+
AnnotationProcessorRegistry.register(testProcessor);
|
|
167
|
+
|
|
168
|
+
const result = await testProcessor.process(
|
|
169
|
+
mockContext,
|
|
170
|
+
{},
|
|
171
|
+
'testMethod',
|
|
172
|
+
{},
|
|
173
|
+
[]
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
expect(result).toBe(false);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
});
|