@airpower/transformer 0.1.8 → 0.1.10
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/dist/index.d.ts +1 -0
- package/dist/main.js +92 -108
- package/dist/transformer/index.d.ts +0 -1
- package/dist/{transformer → util}/DecoratorUtil.d.ts +2 -10
- package/dist/util/index.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/main.js
CHANGED
|
@@ -1,110 +1,3 @@
|
|
|
1
|
-
class DecoratorUtil {
|
|
2
|
-
/**
|
|
3
|
-
* ### 设置一个类配置项
|
|
4
|
-
* @param Class 目标类
|
|
5
|
-
* @param classConfigKey 配置项索引键值
|
|
6
|
-
* @param classConfig 配置的参数
|
|
7
|
-
*/
|
|
8
|
-
static setClassConfig(Class, classConfigKey, classConfig) {
|
|
9
|
-
this.setProperty(Class.prototype, classConfigKey, classConfig);
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* ### 递归获取指定类的配置项
|
|
13
|
-
* @param Class 目标类
|
|
14
|
-
* @param classConfigKey 配置项的Key
|
|
15
|
-
* @param defaultValue `可选` 类装饰器请传入配置项实例
|
|
16
|
-
* @param isObject `可选` 是否是对象配置
|
|
17
|
-
*/
|
|
18
|
-
static getClassConfig(Class, classConfigKey, defaultValue = void 0, isObject = false) {
|
|
19
|
-
let classConfig = Reflect.get(Class, classConfigKey);
|
|
20
|
-
if (!isObject) {
|
|
21
|
-
if (classConfig) {
|
|
22
|
-
return classConfig;
|
|
23
|
-
}
|
|
24
|
-
const SuperClass2 = Reflect.getPrototypeOf(Class);
|
|
25
|
-
if (!SuperClass2 || SuperClass2.prototype.constructor.name === Transformer.name) {
|
|
26
|
-
return void 0;
|
|
27
|
-
}
|
|
28
|
-
return this.getClassConfig(SuperClass2, classConfigKey);
|
|
29
|
-
}
|
|
30
|
-
classConfig = classConfig || {};
|
|
31
|
-
const SuperClass = Reflect.getPrototypeOf(Class);
|
|
32
|
-
if (!SuperClass || SuperClass.constructor.name === Transformer.name) {
|
|
33
|
-
return defaultValue;
|
|
34
|
-
}
|
|
35
|
-
return {
|
|
36
|
-
...this.getClassConfig(SuperClass, classConfigKey, defaultValue, isObject),
|
|
37
|
-
...classConfig
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* ### 设置一个属性配置项
|
|
42
|
-
* @param instance 目标实例
|
|
43
|
-
* @param field 属性
|
|
44
|
-
* @param fieldConfigKey 配置项索引键值
|
|
45
|
-
* @param fieldConfig 配置的参数
|
|
46
|
-
*/
|
|
47
|
-
static setFieldConfig(instance, field, fieldConfigKey, fieldConfig) {
|
|
48
|
-
this.setProperty(instance, `${fieldConfigKey}[${field.toString()}]`, fieldConfig);
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* ### 获取类指定属性的指定类型的配置
|
|
52
|
-
* @param Class 目标类
|
|
53
|
-
* @param field 属性
|
|
54
|
-
* @param fieldConfigKey FieldConfigKey
|
|
55
|
-
* @param isObject `可选` 是否对象配置
|
|
56
|
-
*/
|
|
57
|
-
static getFieldConfig(Class, field, fieldConfigKey, isObject = false) {
|
|
58
|
-
let fieldConfig = Reflect.get(new Class(), `${fieldConfigKey}[${field.toString()}]`);
|
|
59
|
-
const SuperClass = Reflect.getPrototypeOf(Class);
|
|
60
|
-
if (!isObject) {
|
|
61
|
-
if (fieldConfig !== void 0) {
|
|
62
|
-
return fieldConfig;
|
|
63
|
-
}
|
|
64
|
-
if (!SuperClass || SuperClass.prototype.constructor.name === Transformer.name) {
|
|
65
|
-
return void 0;
|
|
66
|
-
}
|
|
67
|
-
return this.getFieldConfig(SuperClass, field, fieldConfigKey);
|
|
68
|
-
}
|
|
69
|
-
fieldConfig = fieldConfig || {};
|
|
70
|
-
if (!SuperClass || SuperClass.prototype.constructor.name === Transformer.name) {
|
|
71
|
-
return {};
|
|
72
|
-
}
|
|
73
|
-
return {
|
|
74
|
-
...this.getFieldConfig(SuperClass, field, fieldConfigKey, true),
|
|
75
|
-
...fieldConfig
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* ### 获取类标记了装饰器的属性列表
|
|
80
|
-
* @param Class 目标类
|
|
81
|
-
* @param fieldConfigKey FieldConfigKey
|
|
82
|
-
* @param list `递归参数` 无需传入
|
|
83
|
-
*/
|
|
84
|
-
static getFieldList(Class, fieldConfigKey, list = []) {
|
|
85
|
-
const fieldList = Reflect.get(new Class(), fieldConfigKey) || [];
|
|
86
|
-
fieldList.forEach((item) => list.includes(item) || list.push(item));
|
|
87
|
-
const SuperClass = Reflect.getPrototypeOf(Class);
|
|
88
|
-
if (!SuperClass || SuperClass.prototype.constructor.name === Transformer.name) {
|
|
89
|
-
return list;
|
|
90
|
-
}
|
|
91
|
-
return this.getFieldList(SuperClass, fieldConfigKey, list);
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* ### 反射添加属性
|
|
95
|
-
* @param instance 目标属性
|
|
96
|
-
* @param propertyKey 配置key
|
|
97
|
-
* @param value 配置值
|
|
98
|
-
*/
|
|
99
|
-
static setProperty(instance, propertyKey, value) {
|
|
100
|
-
Reflect.defineProperty(instance, propertyKey, {
|
|
101
|
-
enumerable: false,
|
|
102
|
-
value,
|
|
103
|
-
writable: false,
|
|
104
|
-
configurable: true
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
1
|
class Transformer {
|
|
109
2
|
/**
|
|
110
3
|
* ### 从 `JSON` 转换到当前类的对象
|
|
@@ -312,6 +205,97 @@ class Transformer {
|
|
|
312
205
|
return json;
|
|
313
206
|
}
|
|
314
207
|
}
|
|
208
|
+
class DecoratorUtil {
|
|
209
|
+
/**
|
|
210
|
+
* ### 设置一个类配置项
|
|
211
|
+
* @param Class 目标类
|
|
212
|
+
* @param classConfigKey 配置项索引键值
|
|
213
|
+
* @param classConfig 配置的参数
|
|
214
|
+
*/
|
|
215
|
+
static setClassConfig(Class, classConfigKey, classConfig) {
|
|
216
|
+
this.setProperty(Class.prototype, classConfigKey, classConfig);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* ### 递归获取指定类的配置项
|
|
220
|
+
* @param Class 目标类
|
|
221
|
+
* @param classConfigKey 配置项的Key
|
|
222
|
+
* @param isObject `可选` 是否是对象配置
|
|
223
|
+
*/
|
|
224
|
+
static getClassConfig(Class, classConfigKey, isObject = false) {
|
|
225
|
+
let classConfig = Reflect.get(Class, classConfigKey);
|
|
226
|
+
if (!isObject) {
|
|
227
|
+
if (classConfig) {
|
|
228
|
+
return classConfig;
|
|
229
|
+
}
|
|
230
|
+
const SuperClass2 = Reflect.getPrototypeOf(Class);
|
|
231
|
+
if (!SuperClass2 || SuperClass2.prototype.constructor.name === Transformer.name) {
|
|
232
|
+
return void 0;
|
|
233
|
+
}
|
|
234
|
+
return this.getClassConfig(SuperClass2, classConfigKey);
|
|
235
|
+
}
|
|
236
|
+
classConfig = classConfig || {};
|
|
237
|
+
const SuperClass = Reflect.getPrototypeOf(Class);
|
|
238
|
+
if (!SuperClass || SuperClass.constructor.name === Transformer.name) {
|
|
239
|
+
return classConfig;
|
|
240
|
+
}
|
|
241
|
+
return {
|
|
242
|
+
...this.getClassConfig(SuperClass, classConfigKey, isObject),
|
|
243
|
+
...classConfig
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* ### 设置一个属性配置项
|
|
248
|
+
* @param instance 目标实例
|
|
249
|
+
* @param field 属性
|
|
250
|
+
* @param fieldConfigKey 配置项索引键值
|
|
251
|
+
* @param fieldConfig 配置的参数
|
|
252
|
+
*/
|
|
253
|
+
static setFieldConfig(instance, field, fieldConfigKey, fieldConfig) {
|
|
254
|
+
this.setProperty(instance, `${fieldConfigKey}[${field.toString()}]`, fieldConfig);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* ### 获取类指定属性的指定类型的配置
|
|
258
|
+
* @param Class 目标类
|
|
259
|
+
* @param field 属性
|
|
260
|
+
* @param fieldConfigKey FieldConfigKey
|
|
261
|
+
* @param isObject `可选` 是否对象配置
|
|
262
|
+
*/
|
|
263
|
+
static getFieldConfig(Class, field, fieldConfigKey, isObject = false) {
|
|
264
|
+
let fieldConfig = Reflect.get(new Class(), `${fieldConfigKey}[${field.toString()}]`);
|
|
265
|
+
const SuperClass = Reflect.getPrototypeOf(Class);
|
|
266
|
+
if (!isObject) {
|
|
267
|
+
if (fieldConfig !== void 0) {
|
|
268
|
+
return fieldConfig;
|
|
269
|
+
}
|
|
270
|
+
if (!SuperClass || SuperClass.prototype.constructor.name === Transformer.name) {
|
|
271
|
+
return void 0;
|
|
272
|
+
}
|
|
273
|
+
return this.getFieldConfig(SuperClass, field, fieldConfigKey);
|
|
274
|
+
}
|
|
275
|
+
fieldConfig = fieldConfig || {};
|
|
276
|
+
if (!SuperClass || SuperClass.prototype.constructor.name === Transformer.name) {
|
|
277
|
+
return fieldConfig;
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
...this.getFieldConfig(SuperClass, field, fieldConfigKey, true),
|
|
281
|
+
...fieldConfig
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* ### 反射添加属性
|
|
286
|
+
* @param instance 目标属性
|
|
287
|
+
* @param propertyKey 配置key
|
|
288
|
+
* @param value 配置值
|
|
289
|
+
*/
|
|
290
|
+
static setProperty(instance, propertyKey, value) {
|
|
291
|
+
Reflect.defineProperty(instance, propertyKey, {
|
|
292
|
+
enumerable: false,
|
|
293
|
+
value,
|
|
294
|
+
writable: false,
|
|
295
|
+
configurable: true
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
315
299
|
const KEY$5 = "[Alias]";
|
|
316
300
|
function Alias(alias) {
|
|
317
301
|
return (instance, field) => {
|
|
@@ -333,7 +317,7 @@ function Prefix(prefix) {
|
|
|
333
317
|
return (Class) => DecoratorUtil.setClassConfig(Class, KEY$3, prefix);
|
|
334
318
|
}
|
|
335
319
|
function getPrefix(Class) {
|
|
336
|
-
return DecoratorUtil.getClassConfig(Class, KEY$3
|
|
320
|
+
return DecoratorUtil.getClassConfig(Class, KEY$3) || "";
|
|
337
321
|
}
|
|
338
322
|
const KEY$2 = "[ToClass]";
|
|
339
323
|
function ToClass(func) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { ITransformerConstructor, Transformer } from '../transformer';
|
|
1
2
|
import { DecoratorData, TransformerField } from '../type';
|
|
2
|
-
import { ITransformerConstructor, Transformer } from './index';
|
|
3
3
|
/**
|
|
4
4
|
* # 装饰器工具类
|
|
5
5
|
*
|
|
@@ -17,10 +17,9 @@ export declare class DecoratorUtil {
|
|
|
17
17
|
* ### 递归获取指定类的配置项
|
|
18
18
|
* @param Class 目标类
|
|
19
19
|
* @param classConfigKey 配置项的Key
|
|
20
|
-
* @param defaultValue `可选` 类装饰器请传入配置项实例
|
|
21
20
|
* @param isObject `可选` 是否是对象配置
|
|
22
21
|
*/
|
|
23
|
-
static getClassConfig<T extends Transformer>(Class: ITransformerConstructor<T>, classConfigKey: string,
|
|
22
|
+
static getClassConfig<T extends Transformer>(Class: ITransformerConstructor<T>, classConfigKey: string, isObject?: boolean): DecoratorData;
|
|
24
23
|
/**
|
|
25
24
|
* ### 设置一个属性配置项
|
|
26
25
|
* @param instance 目标实例
|
|
@@ -37,13 +36,6 @@ export declare class DecoratorUtil {
|
|
|
37
36
|
* @param isObject `可选` 是否对象配置
|
|
38
37
|
*/
|
|
39
38
|
static getFieldConfig<T extends Transformer>(Class: ITransformerConstructor<T>, field: TransformerField<T>, fieldConfigKey: string, isObject?: boolean): DecoratorData;
|
|
40
|
-
/**
|
|
41
|
-
* ### 获取类标记了装饰器的属性列表
|
|
42
|
-
* @param Class 目标类
|
|
43
|
-
* @param fieldConfigKey FieldConfigKey
|
|
44
|
-
* @param list `递归参数` 无需传入
|
|
45
|
-
*/
|
|
46
|
-
static getFieldList<T extends Transformer>(Class: ITransformerConstructor<T>, fieldConfigKey: string, list?: string[]): string[];
|
|
47
39
|
/**
|
|
48
40
|
* ### 反射添加属性
|
|
49
41
|
* @param instance 目标属性
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './DecoratorUtil';
|