create-dp-koa 1.1.2 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/template/.cursor/rules/00-backend-core.skill.md +1 -1
- package/template/.cursor/rules/01-backend-skill-router.skill.md +8 -2
- package/template/.cursor/rules/10-backend-api.skill.md +8 -0
- package/template/.cursor/rules/11-backend-controller-recipes.skill.md +12 -9
- package/template/.cursor/rules/21-backend-service.skill.md +14 -0
- package/template/.cursor/rules/30-backend-validation.skill.md +1 -1
- package/template/.cursor/rules/40-backend-error-logging.skill.md +9 -5
- package/template/.cursor/rules/50-backend-bootstrap-lifecycle.skill.md +4 -4
- package/template/.cursor/rules/60-backend-router-registration.skill.md +16 -6
- package/template/.cursor/rules/70-backend-middleware.skill.md +2 -2
- package/template/.cursor/rules/80-backend-utils-and-libs.skill.md +71 -14
- package/template/.cursor/rules/85-backend-plugins.rule.md +4 -4
- package/template/.cursor/rules/90-backend-testing.skill.md +26 -0
- package/template/.cursor/rules/README.md +2 -2
- package/template/.trae/skills/11-backend-controller-recipes.skill.md +12 -9
- package/template/.trae/skills/21-backend-service.skill.md +15 -1
- package/template/.trae/skills/40-backend-error-logging.skill.md +9 -5
- package/template/.trae/skills/80-backend-utils-and-libs.skill.md +77 -8
- package/template/.trae/skills/90-backend-testing.skill.md +26 -0
- package/template/scripts/sync-template.mjs +20 -0
- package/template/src/app.ts +1 -2
- package/template/src/{framework/plugins → plugins}/registry.ts +2 -2
- package/template/src/plugins/weboffice/http/routes.ts +1 -1
- package/template/src/plugins/weboffice/index.ts +3 -3
- package/template/src/plugins/weboffice/services/webofficeCallback.service.ts +3 -4
- package/template/src/types/ctxState.ts +9 -0
- package/template/src/utils/testDataInitializer.ts +1 -1
- package/template/tsconfig.json +6 -0
- package/template/src/annotations/decorators/ConfigManagement.ts +0 -9
- package/template/src/annotations/decorators/DistributedTracing.ts +0 -9
- package/template/src/annotations/decorators/EnterprisePerformance.ts +0 -9
- package/template/src/annotations/decorators/PerformanceMonitor.ts +0 -32
- package/template/src/annotations/decorators/SecurityAudit.ts +0 -9
- package/template/src/annotations/index.ts +0 -50
- package/template/src/annotations/processors/ConfigManagementProcessor.ts +0 -369
- package/template/src/annotations/processors/DistributedTracingProcessor.ts +0 -288
- package/template/src/annotations/processors/EnterprisePerformanceProcessor.ts +0 -189
- package/template/src/annotations/processors/PerformanceMonitorProcessor.ts +0 -101
- package/template/src/annotations/processors/SecurityAuditProcessor.ts +0 -345
- package/template/src/annotations/processors/SwaggerProcessor.ts +0 -612
- package/template/src/annotations/processors/index.ts +0 -10
- package/template/src/examples/InterceptorExampleRunner.ts +0 -284
- package/template/src/examples/ServiceInterceptorExample.ts +0 -214
- package/template/src/examples/cacheExamples.ts +0 -155
- package/template/src/framework/decorator/controller.ts +0 -311
- package/template/src/framework/decorator/processor/AnnotationDecorators.ts +0 -100
- package/template/src/framework/decorator/processor/AnnotationProcessor.ts +0 -160
- package/template/src/framework/decorator/processor/AnnotationProcessorConfig.ts +0 -45
- package/template/src/framework/decorator/processor/AnnotationRegistry.ts +0 -117
- package/template/src/framework/decorator/processor/AnnotationSystemInitializer.ts +0 -95
- package/template/src/framework/decorator/processor/ProcessorManager.ts +0 -76
- package/template/src/framework/decorator/processor/processors/CustomProcessors.ts +0 -126
- package/template/src/framework/decorator/processor/processors/DefaultProcessors.ts +0 -207
- package/template/src/framework/decorator/refactored/DecoratorFactory.ts +0 -99
- package/template/src/framework/decorator/refactored/DecoratorMetadataManager.ts +0 -125
- package/template/src/framework/decorator/refactored/DecoratorValidator.ts +0 -128
- package/template/src/framework/decorator/refactored/TypeSafeDecorators.ts +0 -139
- package/template/src/framework/decorator/refactored/index.ts +0 -98
- package/template/src/framework/decorator/swagger.ts +0 -150
- package/template/src/framework/interceptors/AdvancedServiceCallInterceptor.ts +0 -375
- package/template/src/framework/interceptors/ServiceCallInterceptor.ts +0 -348
- package/template/src/framework/interceptors/index.ts +0 -19
- package/template/src/framework/plugins/types.ts +0 -15
- package/template/src/framework/types/ServiceResult.ts +0 -151
- package/template/src/framework/types/index.ts +0 -16
- package/template/src/framework/utils/CacheManager.ts +0 -430
- package/template/src/framework/utils/CacheService.ts +0 -248
- package/template/src/framework/utils/DtoValidator.ts +0 -164
- package/template/src/framework/utils/MigrationHelper.ts +0 -179
- package/template/src/framework/utils/MigrationManager.ts +0 -256
- package/template/src/framework/utils/NewRouter.ts +0 -207
- package/template/src/framework/utils/TransactionManager.ts +0 -172
- package/template/src/framework/utils/bootstrap.ts +0 -445
- package/template/src/framework/utils/cache.ts +0 -269
- package/template/src/framework/utils/databaseConfig.ts +0 -148
- package/template/src/framework/utils/db.ts +0 -39
- package/template/src/framework/utils/dbMonitor.ts +0 -106
- package/template/src/framework/utils/function.ts +0 -61
- package/template/src/framework/utils/gracefulShutdown.ts +0 -131
- package/template/src/framework/utils/logger.ts +0 -388
- package/template/src/framework/utils/metrics.ts +0 -182
- package/template/src/framework/utils/router.ts +0 -417
- package/template/src/framework/utils/swagger.ts +0 -184
- package/template/src/framework/utils/testDb.ts +0 -19
- package/template/src/framework/utils/token.ts +0 -23
- package/template/src/framework/utils/transform.ts +0 -17
- package/template/src/libs/aokEmailSender.ts +0 -42
- package/template/src/libs/captcha.ts +0 -37
- package/template/src/libs/cos.ts +0 -45
- package/template/src/libs/mCache.ts +0 -7
- package/template/src/libs/serviceValidate.ts +0 -3
- package/template/src/libs/tecentSms.ts +0 -51
- package/template/src/middlewares/a.middleware.ts +0 -6
- package/template/src/middlewares/error.middleware.ts +0 -14
- package/template/src/middlewares/logging.middleware.ts +0 -187
- package/template/src/middlewares/static.middleware.ts +0 -79
- package/template/src/middlewares/swagger.middleware.ts +0 -70
- package/template/src/middlewares/token.middleware.ts +0 -32
- package/template/src/migrations/1700000000000-InitialDatabaseStructure.ts +0 -172
- package/template/src/migrations/index.ts +0 -6
- package/template/src/repository/base/BaseRepository.ts +0 -124
- package/template/src/repository/interfaces/IBaseRepository.ts +0 -67
- package/template/src/service/base.service.ts +0 -116
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 装饰器系统重构方案
|
|
3
|
-
* 统一装饰器接口和实现
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
// 1. 基础装饰器接口
|
|
7
|
-
export interface BaseDecorator {
|
|
8
|
-
readonly name: string;
|
|
9
|
-
readonly type: 'method' | 'parameter' | 'class';
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// 2. HTTP方法装饰器接口
|
|
13
|
-
export interface HttpMethodDecorator extends BaseDecorator {
|
|
14
|
-
readonly type: 'method';
|
|
15
|
-
readonly httpMethod: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'ALL';
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// 3. 参数装饰器接口
|
|
19
|
-
export interface ParameterDecorator extends BaseDecorator {
|
|
20
|
-
readonly type: 'parameter';
|
|
21
|
-
readonly parameterType: 'Body' | 'Query' | 'Params' | 'Headers' | 'State';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// 4. 统一的装饰器工厂
|
|
25
|
-
export class DecoratorFactory {
|
|
26
|
-
/**
|
|
27
|
-
* 创建HTTP方法装饰器
|
|
28
|
-
*/
|
|
29
|
-
static createHttpMethodDecorator(
|
|
30
|
-
httpMethod: HttpMethodDecorator['httpMethod'],
|
|
31
|
-
url?: string
|
|
32
|
-
): MethodDecorator {
|
|
33
|
-
return (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
|
|
34
|
-
const methodName = String(propertyKey);
|
|
35
|
-
const metadataKey = `$_${httpMethod}Methods`;
|
|
36
|
-
|
|
37
|
-
if (!target[metadataKey]) {
|
|
38
|
-
target[metadataKey] = new Set();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
target[metadataKey].add({
|
|
42
|
-
methodName,
|
|
43
|
-
url: url ?? methodName
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
return descriptor;
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* 创建参数装饰器
|
|
52
|
-
*/
|
|
53
|
-
static createParameterDecorator(
|
|
54
|
-
parameterType: ParameterDecorator['parameterType'],
|
|
55
|
-
validate?: any
|
|
56
|
-
): (target: any, propertyKey: string | symbol | undefined, parameterIndex: number) => void {
|
|
57
|
-
return (target: any, propertyKey: string | symbol | undefined, parameterIndex: number) => {
|
|
58
|
-
const methodName = String(propertyKey);
|
|
59
|
-
const metadataKey = '$_MethodsParamInfo';
|
|
60
|
-
|
|
61
|
-
if (!target[metadataKey]) {
|
|
62
|
-
target[metadataKey] = new Map<string, ParameterInfo[]>();
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
let paramsInfo = target[metadataKey].get(methodName) ?? [];
|
|
66
|
-
paramsInfo[parameterIndex] = {
|
|
67
|
-
type: parameterType,
|
|
68
|
-
validate
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
target[metadataKey].set(methodName, paramsInfo);
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// 5. 类型定义
|
|
77
|
-
export interface ParameterInfo {
|
|
78
|
-
type: ParameterDecorator['parameterType'];
|
|
79
|
-
validate?: any;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export interface MethodInfo {
|
|
83
|
-
methodName: string;
|
|
84
|
-
url: string;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// 6. 统一的装饰器导出
|
|
88
|
-
export const Get = (url?: string) => DecoratorFactory.createHttpMethodDecorator('GET', url);
|
|
89
|
-
export const Post = (url?: string) => DecoratorFactory.createHttpMethodDecorator('POST', url);
|
|
90
|
-
export const Put = (url?: string) => DecoratorFactory.createHttpMethodDecorator('PUT', url);
|
|
91
|
-
export const Delete = (url?: string) => DecoratorFactory.createHttpMethodDecorator('DELETE', url);
|
|
92
|
-
export const Patch = (url?: string) => DecoratorFactory.createHttpMethodDecorator('PATCH', url);
|
|
93
|
-
export const All = (url?: string) => DecoratorFactory.createHttpMethodDecorator('ALL', url);
|
|
94
|
-
|
|
95
|
-
export const Body = (validate?: any) => DecoratorFactory.createParameterDecorator('Body', validate);
|
|
96
|
-
export const Query = (validate?: any) => DecoratorFactory.createParameterDecorator('Query', validate);
|
|
97
|
-
export const Params = (validate?: any) => DecoratorFactory.createParameterDecorator('Params', validate);
|
|
98
|
-
export const Headers = (validate?: any) => DecoratorFactory.createParameterDecorator('Headers', validate);
|
|
99
|
-
export const State = (validate?: any) => DecoratorFactory.createParameterDecorator('State', validate);
|
|
@@ -1,125 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 装饰器元数据管理器
|
|
3
|
-
* 统一管理所有装饰器的元数据
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
export class DecoratorMetadataManager {
|
|
7
|
-
private static readonly METADATA_KEYS = {
|
|
8
|
-
HTTP_METHODS: '$_HttpMethods',
|
|
9
|
-
PARAMETERS: '$_Parameters',
|
|
10
|
-
RESPONSE_CODE: '$_ResponseCode',
|
|
11
|
-
RESPONSE_HEADER: '$_ResponseHeader',
|
|
12
|
-
RESPONSE_VALIDATOR: '$_ResponseValidator',
|
|
13
|
-
CONTROLLER_CACHE: '$_ControllerCache',
|
|
14
|
-
TRANSFORM: '$_Transform',
|
|
15
|
-
ANNOTATIONS: '$_Annotations'
|
|
16
|
-
} as const;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* 获取HTTP方法元数据
|
|
20
|
-
*/
|
|
21
|
-
static getHttpMethods(target: any): Map<string, MethodInfo[]> {
|
|
22
|
-
return target[this.METADATA_KEYS.HTTP_METHODS] || new Map();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 设置HTTP方法元数据
|
|
27
|
-
*/
|
|
28
|
-
static setHttpMethod(
|
|
29
|
-
target: any,
|
|
30
|
-
methodName: string,
|
|
31
|
-
httpMethod: string,
|
|
32
|
-
url: string
|
|
33
|
-
): void {
|
|
34
|
-
if (!target[this.METADATA_KEYS.HTTP_METHODS]) {
|
|
35
|
-
target[this.METADATA_KEYS.HTTP_METHODS] = new Map();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const methods = target[this.METADATA_KEYS.HTTP_METHODS];
|
|
39
|
-
if (!methods.has(methodName)) {
|
|
40
|
-
methods.set(methodName, []);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
methods.get(methodName).push({ httpMethod, url });
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* 获取参数元数据
|
|
48
|
-
*/
|
|
49
|
-
static getParameters(target: any, methodName: string): ParameterInfo[] {
|
|
50
|
-
const params = target[this.METADATA_KEYS.PARAMETERS];
|
|
51
|
-
return params?.get(methodName) || [];
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 设置参数元数据
|
|
56
|
-
*/
|
|
57
|
-
static setParameter(
|
|
58
|
-
target: any,
|
|
59
|
-
methodName: string,
|
|
60
|
-
index: number,
|
|
61
|
-
parameterInfo: ParameterInfo
|
|
62
|
-
): void {
|
|
63
|
-
if (!target[this.METADATA_KEYS.PARAMETERS]) {
|
|
64
|
-
target[this.METADATA_KEYS.PARAMETERS] = new Map();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const params = target[this.METADATA_KEYS.PARAMETERS];
|
|
68
|
-
if (!params.has(methodName)) {
|
|
69
|
-
params.set(methodName, []);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const methodParams = params.get(methodName);
|
|
73
|
-
methodParams[index] = parameterInfo;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* 获取注解元数据
|
|
78
|
-
*/
|
|
79
|
-
static getAnnotations(target: any, methodName: string): Map<string, any> {
|
|
80
|
-
const annotations = target[this.METADATA_KEYS.ANNOTATIONS];
|
|
81
|
-
return annotations?.get(methodName) || new Map();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* 设置注解元数据
|
|
86
|
-
*/
|
|
87
|
-
static setAnnotation(
|
|
88
|
-
target: any,
|
|
89
|
-
methodName: string,
|
|
90
|
-
processorName: string,
|
|
91
|
-
data: any
|
|
92
|
-
): void {
|
|
93
|
-
if (!target[this.METADATA_KEYS.ANNOTATIONS]) {
|
|
94
|
-
target[this.METADATA_KEYS.ANNOTATIONS] = new Map();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const annotations = target[this.METADATA_KEYS.ANNOTATIONS];
|
|
98
|
-
if (!annotations.has(methodName)) {
|
|
99
|
-
annotations.set(methodName, new Map());
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
annotations.get(methodName).set(processorName, data);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* 清理元数据
|
|
107
|
-
*/
|
|
108
|
-
static clear(target: any): void {
|
|
109
|
-
Object.values(this.METADATA_KEYS).forEach(key => {
|
|
110
|
-
delete target[key];
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export interface MethodInfo {
|
|
116
|
-
httpMethod: string;
|
|
117
|
-
url: string;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export interface ParameterInfo {
|
|
121
|
-
type: 'Body' | 'Query' | 'Params' | 'Headers' | 'State';
|
|
122
|
-
validate?: any;
|
|
123
|
-
required?: boolean;
|
|
124
|
-
defaultValue?: any;
|
|
125
|
-
}
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 装饰器验证系统
|
|
3
|
-
* 提供装饰器配置的验证和错误检查
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { DecoratorMetadataManager } from '@src/framework/decorator/refactored/DecoratorMetadataManager';
|
|
7
|
-
|
|
8
|
-
export class DecoratorValidator {
|
|
9
|
-
/**
|
|
10
|
-
* 验证控制器装饰器配置
|
|
11
|
-
*/
|
|
12
|
-
static validateController(target: any): ValidationResult {
|
|
13
|
-
const errors: string[] = [];
|
|
14
|
-
const warnings: string[] = [];
|
|
15
|
-
|
|
16
|
-
// 检查HTTP方法装饰器
|
|
17
|
-
const httpMethods = DecoratorMetadataManager.getHttpMethods(target);
|
|
18
|
-
if (httpMethods.size === 0) {
|
|
19
|
-
warnings.push('控制器没有定义任何HTTP方法');
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// 检查重复的URL
|
|
23
|
-
const urlMap = new Map<string, string[]>();
|
|
24
|
-
httpMethods.forEach((methods, methodName) => {
|
|
25
|
-
methods.forEach(method => {
|
|
26
|
-
const key = `${method.httpMethod}:${method.url}`;
|
|
27
|
-
if (!urlMap.has(key)) {
|
|
28
|
-
urlMap.set(key, []);
|
|
29
|
-
}
|
|
30
|
-
urlMap.get(key)!.push(methodName);
|
|
31
|
-
});
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
urlMap.forEach((methods, url) => {
|
|
35
|
-
if (methods.length > 1) {
|
|
36
|
-
errors.push(`URL冲突: ${url} 被多个方法使用: ${methods.join(', ')}`);
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
return {
|
|
41
|
-
isValid: errors.length === 0,
|
|
42
|
-
errors,
|
|
43
|
-
warnings
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 验证方法装饰器配置
|
|
49
|
-
*/
|
|
50
|
-
static validateMethod(target: any, methodName: string): ValidationResult {
|
|
51
|
-
const errors: string[] = [];
|
|
52
|
-
const warnings: string[] = [];
|
|
53
|
-
|
|
54
|
-
// 检查参数装饰器
|
|
55
|
-
const parameters = DecoratorMetadataManager.getParameters(target, methodName);
|
|
56
|
-
const gaps = parameters.findIndex((param, index) => param === undefined);
|
|
57
|
-
if (gaps !== -1) {
|
|
58
|
-
errors.push(`方法 ${methodName} 的参数装饰器存在间隙,索引 ${gaps} 未定义`);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// 检查注解装饰器
|
|
62
|
-
const annotations = DecoratorMetadataManager.getAnnotations(target, methodName);
|
|
63
|
-
if (annotations.size === 0) {
|
|
64
|
-
warnings.push(`方法 ${methodName} 没有使用任何注解装饰器`);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return {
|
|
68
|
-
isValid: errors.length === 0,
|
|
69
|
-
errors,
|
|
70
|
-
warnings
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 验证整个控制器类
|
|
76
|
-
*/
|
|
77
|
-
static validateClass(target: any): ValidationResult {
|
|
78
|
-
const errors: string[] = [];
|
|
79
|
-
const warnings: string[] = [];
|
|
80
|
-
|
|
81
|
-
// 验证控制器级别
|
|
82
|
-
const controllerResult = this.validateController(target);
|
|
83
|
-
errors.push(...controllerResult.errors);
|
|
84
|
-
warnings.push(...controllerResult.warnings);
|
|
85
|
-
|
|
86
|
-
// 验证所有方法
|
|
87
|
-
const methods = Object.getOwnPropertyNames(target.prototype)
|
|
88
|
-
.filter(name => name !== 'constructor' && typeof target.prototype[name] === 'function');
|
|
89
|
-
|
|
90
|
-
methods.forEach(methodName => {
|
|
91
|
-
const methodResult = this.validateMethod(target, methodName);
|
|
92
|
-
errors.push(...methodResult.errors);
|
|
93
|
-
warnings.push(...methodResult.warnings);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
return {
|
|
97
|
-
isValid: errors.length === 0,
|
|
98
|
-
errors,
|
|
99
|
-
warnings
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export interface ValidationResult {
|
|
105
|
-
isValid: boolean;
|
|
106
|
-
errors: string[];
|
|
107
|
-
warnings: string[];
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 装饰器验证装饰器
|
|
112
|
-
* 在类加载时自动验证装饰器配置
|
|
113
|
-
*/
|
|
114
|
-
export function ValidateDecorators(): ClassDecorator {
|
|
115
|
-
return (target: any) => {
|
|
116
|
-
const result = DecoratorValidator.validateClass(target);
|
|
117
|
-
|
|
118
|
-
if (!result.isValid) {
|
|
119
|
-
console.error(`装饰器验证失败 - ${target.name}:`, result.errors);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (result.warnings.length > 0) {
|
|
123
|
-
console.warn(`装饰器验证警告 - ${target.name}:`, result.warnings);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
return target;
|
|
127
|
-
};
|
|
128
|
-
}
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 类型安全的装饰器系统
|
|
3
|
-
* 提供完整的类型支持和智能提示
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { DecoratorMetadataManager, ParameterInfo } from '@src/framework/decorator/refactored/DecoratorMetadataManager';
|
|
7
|
-
|
|
8
|
-
// 1. 类型定义
|
|
9
|
-
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'ALL';
|
|
10
|
-
export type ParameterType = 'Body' | 'Query' | 'Params' | 'Headers' | 'State';
|
|
11
|
-
|
|
12
|
-
// 2. 装饰器配置接口
|
|
13
|
-
export interface HttpMethodConfig {
|
|
14
|
-
url?: string;
|
|
15
|
-
middleware?: any[];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface ParameterConfig {
|
|
19
|
-
validate?: any;
|
|
20
|
-
required?: boolean;
|
|
21
|
-
defaultValue?: any;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// 3. 类型安全的HTTP方法装饰器
|
|
25
|
-
export function createHttpMethodDecorator<T extends HttpMethod>(
|
|
26
|
-
method: T,
|
|
27
|
-
config?: HttpMethodConfig
|
|
28
|
-
): MethodDecorator {
|
|
29
|
-
return (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
|
|
30
|
-
const methodName = String(propertyKey);
|
|
31
|
-
// HTTP方法装饰器的 target 是类的构造函数
|
|
32
|
-
const metadataKey = `$_${method}Methods`;
|
|
33
|
-
|
|
34
|
-
if (!target[metadataKey]) {
|
|
35
|
-
target[metadataKey] = new Set();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
target[metadataKey].add({
|
|
39
|
-
methodName,
|
|
40
|
-
url: config?.url ?? methodName
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
console.log(`装饰器执行: ${method} ${methodName} -> ${config?.url ?? methodName}`);
|
|
44
|
-
|
|
45
|
-
return descriptor;
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// 4. 类型安全的参数装饰器
|
|
50
|
-
export function createParameterDecorator<T extends ParameterType>(
|
|
51
|
-
type: T,
|
|
52
|
-
config?: ParameterConfig
|
|
53
|
-
): (target: any, propertyKey: string | symbol | undefined, parameterIndex: number) => void {
|
|
54
|
-
return (target: any, propertyKey: string | symbol | undefined, parameterIndex: number) => {
|
|
55
|
-
const methodName = String(propertyKey);
|
|
56
|
-
// 参数装饰器的 target 是类的构造函数
|
|
57
|
-
const metadataKey = '$_MethodsParamInfo';
|
|
58
|
-
|
|
59
|
-
if (!target[metadataKey]) {
|
|
60
|
-
target[metadataKey] = new Map<string, ParameterInfo[]>();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
let paramsInfo = target[metadataKey].get(methodName) ?? [];
|
|
64
|
-
paramsInfo[parameterIndex] = {
|
|
65
|
-
type,
|
|
66
|
-
validate: config?.validate,
|
|
67
|
-
required: config?.required ?? true,
|
|
68
|
-
defaultValue: config?.defaultValue
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
target[metadataKey].set(methodName, paramsInfo);
|
|
72
|
-
|
|
73
|
-
console.log(`参数装饰器执行: ${type} ${methodName}[${parameterIndex}]`);
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// 5. 导出的装饰器(类型安全)
|
|
78
|
-
export const Get = (config?: HttpMethodConfig) => createHttpMethodDecorator('GET', config);
|
|
79
|
-
export const Post = (config?: HttpMethodConfig) => createHttpMethodDecorator('POST', config);
|
|
80
|
-
export const Put = (config?: HttpMethodConfig) => createHttpMethodDecorator('PUT', config);
|
|
81
|
-
export const Delete = (config?: HttpMethodConfig) => createHttpMethodDecorator('DELETE', config);
|
|
82
|
-
export const Patch = (config?: HttpMethodConfig) => createHttpMethodDecorator('PATCH', config);
|
|
83
|
-
export const All = (config?: HttpMethodConfig) => createHttpMethodDecorator('ALL', config);
|
|
84
|
-
|
|
85
|
-
export const Body = (config?: ParameterConfig) => createParameterDecorator('Body', config);
|
|
86
|
-
export const Query = (config?: ParameterConfig) => createParameterDecorator('Query', config);
|
|
87
|
-
export const Params = (config?: ParameterConfig) => createParameterDecorator('Params', config);
|
|
88
|
-
export const Headers = (config?: ParameterConfig) => createParameterDecorator('Headers', config);
|
|
89
|
-
export const State = (config?: ParameterConfig) => createParameterDecorator('State', config);
|
|
90
|
-
|
|
91
|
-
// 6. 高级装饰器
|
|
92
|
-
export function ResponseCode(code: number = 200): MethodDecorator {
|
|
93
|
-
return (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
|
|
94
|
-
const methodName = String(propertyKey);
|
|
95
|
-
if (!target.$_ResponseCode) {
|
|
96
|
-
target.$_ResponseCode = new Map<string, number>();
|
|
97
|
-
}
|
|
98
|
-
target.$_ResponseCode.set(methodName, code);
|
|
99
|
-
return descriptor;
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function ResponseHeader(type: string, value: any): MethodDecorator {
|
|
104
|
-
return (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
|
|
105
|
-
const methodName = String(propertyKey);
|
|
106
|
-
if (!target.$_ResponseHeader) {
|
|
107
|
-
target.$_ResponseHeader = new Map<string, Map<string, any>>();
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
let headerMap = target.$_ResponseHeader.get(methodName);
|
|
111
|
-
if (!headerMap) {
|
|
112
|
-
headerMap = new Map<string, any>();
|
|
113
|
-
}
|
|
114
|
-
headerMap.set(type, value);
|
|
115
|
-
target.$_ResponseHeader.set(methodName, headerMap);
|
|
116
|
-
|
|
117
|
-
return descriptor;
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function ControllerCache(
|
|
122
|
-
cacheKeyFn: (...params: any[]) => string,
|
|
123
|
-
options: {
|
|
124
|
-
ttl: number | { max: number; min: number };
|
|
125
|
-
enable?: boolean;
|
|
126
|
-
} = { ttl: 20, enable: true }
|
|
127
|
-
): MethodDecorator {
|
|
128
|
-
return (target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
|
|
129
|
-
const methodName = String(propertyKey);
|
|
130
|
-
if (!target.$_ControllerCache) {
|
|
131
|
-
target.$_ControllerCache = new Map<string, any>();
|
|
132
|
-
}
|
|
133
|
-
target.$_ControllerCache.set(methodName, {
|
|
134
|
-
cacheKeyFn,
|
|
135
|
-
options
|
|
136
|
-
});
|
|
137
|
-
return descriptor;
|
|
138
|
-
};
|
|
139
|
-
}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 统一的装饰器导出文件
|
|
3
|
-
* 重构后的装饰器系统入口
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
// 1. 核心装饰器
|
|
7
|
-
export {
|
|
8
|
-
Get,
|
|
9
|
-
Post,
|
|
10
|
-
Put,
|
|
11
|
-
Delete,
|
|
12
|
-
Patch,
|
|
13
|
-
All,
|
|
14
|
-
Body,
|
|
15
|
-
Query,
|
|
16
|
-
Params,
|
|
17
|
-
Headers,
|
|
18
|
-
State,
|
|
19
|
-
ResponseCode,
|
|
20
|
-
ResponseHeader,
|
|
21
|
-
ControllerCache
|
|
22
|
-
} from './TypeSafeDecorators';
|
|
23
|
-
|
|
24
|
-
// 2. 元数据管理
|
|
25
|
-
export { DecoratorMetadataManager } from './DecoratorMetadataManager';
|
|
26
|
-
|
|
27
|
-
// 3. 验证系统
|
|
28
|
-
export { DecoratorValidator, ValidateDecorators } from './DecoratorValidator';
|
|
29
|
-
|
|
30
|
-
// 4. 类型定义
|
|
31
|
-
export type {
|
|
32
|
-
HttpMethod,
|
|
33
|
-
ParameterType,
|
|
34
|
-
HttpMethodConfig,
|
|
35
|
-
ParameterConfig
|
|
36
|
-
} from './TypeSafeDecorators';
|
|
37
|
-
|
|
38
|
-
export type {
|
|
39
|
-
MethodInfo,
|
|
40
|
-
ParameterInfo
|
|
41
|
-
} from './DecoratorMetadataManager';
|
|
42
|
-
|
|
43
|
-
export type {
|
|
44
|
-
ValidationResult
|
|
45
|
-
} from './DecoratorValidator';
|
|
46
|
-
|
|
47
|
-
// 5. 兼容性导出(保持向后兼容)
|
|
48
|
-
export {
|
|
49
|
-
// 保持原有的导出名称
|
|
50
|
-
Get as GetDecorator,
|
|
51
|
-
Post as PostDecorator,
|
|
52
|
-
Put as PutDecorator,
|
|
53
|
-
Delete as DeleteDecorator,
|
|
54
|
-
Body as BodyDecorator,
|
|
55
|
-
Query as QueryDecorator,
|
|
56
|
-
Params as ParamsDecorator,
|
|
57
|
-
Headers as HeadersDecorator,
|
|
58
|
-
State as StateDecorator
|
|
59
|
-
} from './TypeSafeDecorators';
|
|
60
|
-
|
|
61
|
-
// 6. 工具函数
|
|
62
|
-
import { DecoratorMetadataManager } from '@src/framework/decorator/refactored/DecoratorMetadataManager';
|
|
63
|
-
import { DecoratorValidator } from '@src/framework/decorator/refactored/DecoratorValidator';
|
|
64
|
-
|
|
65
|
-
export const DecoratorUtils = {
|
|
66
|
-
/**
|
|
67
|
-
* 获取控制器的所有HTTP方法
|
|
68
|
-
*/
|
|
69
|
-
getHttpMethods: (target: any) => DecoratorMetadataManager.getHttpMethods(target),
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* 获取方法的参数信息
|
|
73
|
-
*/
|
|
74
|
-
getMethodParameters: (target: any, methodName: string) =>
|
|
75
|
-
DecoratorMetadataManager.getParameters(target, methodName),
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* 获取方法的注解信息
|
|
79
|
-
*/
|
|
80
|
-
getMethodAnnotations: (target: any, methodName: string) =>
|
|
81
|
-
DecoratorMetadataManager.getAnnotations(target, methodName),
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* 验证控制器装饰器配置
|
|
85
|
-
*/
|
|
86
|
-
validateController: (target: any) => DecoratorValidator.validateController(target),
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* 验证方法装饰器配置
|
|
90
|
-
*/
|
|
91
|
-
validateMethod: (target: any, methodName: string) =>
|
|
92
|
-
DecoratorValidator.validateMethod(target, methodName),
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* 清理装饰器元数据
|
|
96
|
-
*/
|
|
97
|
-
clearMetadata: (target: any) => DecoratorMetadataManager.clear(target)
|
|
98
|
-
};
|