@airpower/transformer 0.0.1
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/LICENSE +21 -0
- package/README.md +78 -0
- package/assets/airpower-bg.svg +246 -0
- package/assets/airpower.svg +246 -0
- package/dist/decorator/Alias.d.ts +13 -0
- package/dist/decorator/IgnorePrefix.d.ts +12 -0
- package/dist/decorator/Prefix.d.ts +11 -0
- package/dist/decorator/ToClass.d.ts +13 -0
- package/dist/decorator/ToJson.d.ts +13 -0
- package/dist/decorator/Type.d.ts +22 -0
- package/dist/decorator/index.d.ts +6 -0
- package/dist/index.d.ts +2 -0
- package/dist/main.js +408 -0
- package/dist/transformer/DecoratorUtil.d.ts +65 -0
- package/dist/transformer/IJson.d.ts +11 -0
- package/dist/transformer/ITransformerConstructor.d.ts +9 -0
- package/dist/transformer/Transformer.d.ts +63 -0
- package/dist/transformer/index.d.ts +4 -0
- package/dist/type/type.d.ts +8 -0
- package/package.json +57 -0
package/dist/main.js
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
class DecoratorUtil {
|
|
5
|
+
/**
|
|
6
|
+
* ### 设置一个类配置项
|
|
7
|
+
* @param target 目标实体类
|
|
8
|
+
* @param classConfigKey 配置项索引键值
|
|
9
|
+
* @param classConfig 配置的参数
|
|
10
|
+
*/
|
|
11
|
+
static setClassConfig(target, classConfigKey, classConfig) {
|
|
12
|
+
this.setProperty(target.prototype, classConfigKey, classConfig);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* ### 递归获取指定类的配置项
|
|
16
|
+
* @param target 目标类
|
|
17
|
+
* @param classConfigKey 配置项的Key
|
|
18
|
+
* @param defaultValue `可选` 类装饰器请传入配置项实例
|
|
19
|
+
* @param isObject `可选` 是否是对象配置
|
|
20
|
+
*/
|
|
21
|
+
static getClassConfig(target, classConfigKey, defaultValue = void 0, isObject = false) {
|
|
22
|
+
let classConfig = Reflect.get(target, classConfigKey);
|
|
23
|
+
if (!isObject) {
|
|
24
|
+
if (classConfig) {
|
|
25
|
+
return classConfig;
|
|
26
|
+
}
|
|
27
|
+
const superClass2 = Reflect.getPrototypeOf(target);
|
|
28
|
+
if (!superClass2 || superClass2.constructor.name === Transformer.name) {
|
|
29
|
+
return void 0;
|
|
30
|
+
}
|
|
31
|
+
return this.getClassConfig(superClass2, classConfigKey);
|
|
32
|
+
}
|
|
33
|
+
classConfig = classConfig || {};
|
|
34
|
+
const superClass = Reflect.getPrototypeOf(target);
|
|
35
|
+
if (!superClass || superClass.constructor.name === Transformer.name) {
|
|
36
|
+
return defaultValue;
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
...this.getClassConfig(superClass, classConfigKey, defaultValue, isObject),
|
|
40
|
+
...classConfig
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* ### 设置一个属性配置项
|
|
45
|
+
* @param target 目标类
|
|
46
|
+
* @param key 属性
|
|
47
|
+
* @param fieldConfigKey 配置项索引键值
|
|
48
|
+
* @param fieldConfig 配置的参数
|
|
49
|
+
* @param fieldListKey `可选` 类配置项列表索引值
|
|
50
|
+
*/
|
|
51
|
+
static setFieldConfig(target, key, fieldConfigKey, fieldConfig, fieldListKey) {
|
|
52
|
+
if (fieldListKey) {
|
|
53
|
+
this.addFieldDecoratorKey(target, key, fieldListKey);
|
|
54
|
+
}
|
|
55
|
+
this.setProperty(target, `${fieldConfigKey}[${key}]`, fieldConfig);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* ### 获取类指定属性的指定类型的配置
|
|
59
|
+
* @param target 目标类
|
|
60
|
+
* @param key 属性
|
|
61
|
+
* @param fieldConfigKey FieldConfigKey
|
|
62
|
+
* @param isObject `可选` 是否对象配置
|
|
63
|
+
*/
|
|
64
|
+
static getFieldConfig(target, key, fieldConfigKey, isObject = false) {
|
|
65
|
+
if (typeof target !== "object") {
|
|
66
|
+
target = target.prototype;
|
|
67
|
+
}
|
|
68
|
+
let fieldConfig = Reflect.get(target, `${fieldConfigKey}[${key}]`);
|
|
69
|
+
if (!isObject) {
|
|
70
|
+
if (fieldConfig !== void 0) {
|
|
71
|
+
return fieldConfig;
|
|
72
|
+
}
|
|
73
|
+
const superClass2 = Reflect.getPrototypeOf(target);
|
|
74
|
+
if (!superClass2 || superClass2.constructor.name === Transformer.name) {
|
|
75
|
+
return void 0;
|
|
76
|
+
}
|
|
77
|
+
return this.getFieldConfig(superClass2, key, fieldConfigKey);
|
|
78
|
+
}
|
|
79
|
+
fieldConfig = fieldConfig || {};
|
|
80
|
+
const superClass = Reflect.getPrototypeOf(target);
|
|
81
|
+
if (!superClass || superClass.constructor.name === Transformer.name) {
|
|
82
|
+
return {};
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
...this.getFieldConfig(superClass, key, fieldConfigKey, true),
|
|
86
|
+
...fieldConfig
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* ### 获取类标记了装饰器的属性列表
|
|
91
|
+
* @param target 目标类
|
|
92
|
+
* @param fieldConfigKey FieldConfigKey
|
|
93
|
+
* @param list `递归参数` 无需传入
|
|
94
|
+
*/
|
|
95
|
+
static getFieldList(target, fieldConfigKey, list = []) {
|
|
96
|
+
const fieldList = Reflect.get(target, fieldConfigKey) || [];
|
|
97
|
+
fieldList.forEach((item) => list.includes(item) || list.push(item));
|
|
98
|
+
const superClass = Reflect.getPrototypeOf(target);
|
|
99
|
+
if (!superClass || superClass.constructor.name === Transformer.name) {
|
|
100
|
+
return list;
|
|
101
|
+
}
|
|
102
|
+
return this.getFieldList(superClass, fieldConfigKey, list);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* ### 反射添加属性
|
|
106
|
+
* @param target 目标类
|
|
107
|
+
* @param key 配置key
|
|
108
|
+
* @param value 配置值
|
|
109
|
+
*/
|
|
110
|
+
static setProperty(target, key, value) {
|
|
111
|
+
Reflect.defineProperty(target, key, {
|
|
112
|
+
enumerable: false,
|
|
113
|
+
value,
|
|
114
|
+
writable: false,
|
|
115
|
+
configurable: true
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* ### 设置一个属性的包含装饰器索引
|
|
120
|
+
* @param target 目标类
|
|
121
|
+
* @param key 属性
|
|
122
|
+
* @param fieldListKey 类配置项列表索引值
|
|
123
|
+
*/
|
|
124
|
+
static addFieldDecoratorKey(target, key, fieldListKey) {
|
|
125
|
+
const list = Reflect.get(target, fieldListKey) || [];
|
|
126
|
+
list.push(key);
|
|
127
|
+
this.setProperty(target, fieldListKey, list);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* ### 装饰器前缀
|
|
132
|
+
*/
|
|
133
|
+
__publicField(DecoratorUtil, "DecoratorKeyPrefix", "[AirPower]");
|
|
134
|
+
class Transformer {
|
|
135
|
+
/**
|
|
136
|
+
* ### 从 `JSON` 转换到当前类的对象
|
|
137
|
+
* 会自动进行数据别名转换
|
|
138
|
+
* @param json `JSON`
|
|
139
|
+
*/
|
|
140
|
+
static fromJson(json = {}) {
|
|
141
|
+
const instance = new this();
|
|
142
|
+
return Transformer.parse(instance, json);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* ### 从 `JSON` 数组转换到当前类的对象数组
|
|
146
|
+
* 会自动进行数据别名转换
|
|
147
|
+
* @param jsonArray `JSON`数组
|
|
148
|
+
*/
|
|
149
|
+
static fromJsonArray(jsonArray = []) {
|
|
150
|
+
const instanceList = [];
|
|
151
|
+
if (Array.isArray(jsonArray)) {
|
|
152
|
+
for (let i = 0; i < jsonArray.length; i += 1) {
|
|
153
|
+
const instance = new this();
|
|
154
|
+
instanceList.push(Transformer.parse(instance, jsonArray[i]));
|
|
155
|
+
}
|
|
156
|
+
} else {
|
|
157
|
+
const instance = new this();
|
|
158
|
+
instanceList.push(Transformer.parse(instance, jsonArray));
|
|
159
|
+
}
|
|
160
|
+
return instanceList;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* ### 创建一个当前类的实例
|
|
164
|
+
* @param recoverBy `可选` 初始化用于覆盖对象实例的 `JSON`
|
|
165
|
+
*/
|
|
166
|
+
static newInstance(recoverBy) {
|
|
167
|
+
const instance = new this();
|
|
168
|
+
if (recoverBy) {
|
|
169
|
+
return instance.recoverBy(recoverBy);
|
|
170
|
+
}
|
|
171
|
+
return instance;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* ### 转换 `JSON` 为实体
|
|
175
|
+
* 会自动进行数据别名转换
|
|
176
|
+
* @param instance 实体
|
|
177
|
+
* @param json `JSON`
|
|
178
|
+
*/
|
|
179
|
+
static parse(instance, json = {}) {
|
|
180
|
+
const fieldList = Object.keys(instance);
|
|
181
|
+
for (const field of fieldList) {
|
|
182
|
+
const jsonKey = this.getJsonKey(instance, field);
|
|
183
|
+
const fieldData = json[jsonKey];
|
|
184
|
+
instance[field] = fieldData;
|
|
185
|
+
const toClass = getToClass(instance, field);
|
|
186
|
+
if (toClass !== void 0) {
|
|
187
|
+
try {
|
|
188
|
+
;
|
|
189
|
+
instance[field] = toClass(json);
|
|
190
|
+
} catch (e) {
|
|
191
|
+
console.warn("ToClass Function Error", e);
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const FieldTypeClass = getType(instance, field);
|
|
196
|
+
const isArray = getArray(instance, field);
|
|
197
|
+
if (isArray) {
|
|
198
|
+
const fieldValueList = [];
|
|
199
|
+
if (typeof fieldData === "object" && Array.isArray(fieldData)) {
|
|
200
|
+
for (let i = 0; i < fieldData.length; i += 1) {
|
|
201
|
+
if (FieldTypeClass) {
|
|
202
|
+
fieldValueList[i] = this.parse(new FieldTypeClass(), fieldData[i]);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
instance[field] = fieldValueList;
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
if (fieldData === void 0 || fieldData === null) {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (!FieldTypeClass) {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
switch (FieldTypeClass.name) {
|
|
216
|
+
case "String":
|
|
217
|
+
instance[field] = fieldData.toString();
|
|
218
|
+
break;
|
|
219
|
+
case "Number":
|
|
220
|
+
instance[field] = Number.isNaN(Number.parseFloat(fieldData)) ? void 0 : Number.parseFloat(fieldData);
|
|
221
|
+
break;
|
|
222
|
+
case "Boolean":
|
|
223
|
+
instance[field] = !!fieldData;
|
|
224
|
+
break;
|
|
225
|
+
default:
|
|
226
|
+
instance[field] = this.parse(new FieldTypeClass(), fieldData);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
for (const fieldKey of fieldList) {
|
|
230
|
+
const alias = getAlias(instance, fieldKey) || fieldKey;
|
|
231
|
+
if (alias === fieldKey) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
delete instance[alias];
|
|
235
|
+
}
|
|
236
|
+
return instance;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* ### 获取 JSON 的 key
|
|
240
|
+
* @param instance 目标实例
|
|
241
|
+
* @param field 属性 key
|
|
242
|
+
*/
|
|
243
|
+
static getJsonKey(instance, field) {
|
|
244
|
+
const alias = getAlias(instance, field);
|
|
245
|
+
if (alias) {
|
|
246
|
+
return alias;
|
|
247
|
+
}
|
|
248
|
+
const prefix = getPrefix(instance);
|
|
249
|
+
if (!prefix) {
|
|
250
|
+
return field;
|
|
251
|
+
}
|
|
252
|
+
const ignorePrefix = getIgnorePrefix(instance, field);
|
|
253
|
+
if (!ignorePrefix) {
|
|
254
|
+
return prefix + field;
|
|
255
|
+
}
|
|
256
|
+
return field;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* ### 将当前实例复制到一个新实例上
|
|
260
|
+
*/
|
|
261
|
+
copy() {
|
|
262
|
+
const instance = Object.create(Object.getPrototypeOf(this));
|
|
263
|
+
return Object.assign(instance, this);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* ### 暴露部分类的属性
|
|
267
|
+
* @param fields 属性列表
|
|
268
|
+
*/
|
|
269
|
+
expose(...fields) {
|
|
270
|
+
const fieldList = Object.keys(this);
|
|
271
|
+
for (const field of fieldList) {
|
|
272
|
+
if (!fields.includes(field)) {
|
|
273
|
+
this[field] = void 0;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return this;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* ### 排除部分类的属性
|
|
280
|
+
* @param fields 属性列表
|
|
281
|
+
*/
|
|
282
|
+
exclude(...fields) {
|
|
283
|
+
const fieldList = Object.keys(this);
|
|
284
|
+
for (const field of fieldList) {
|
|
285
|
+
if (fields.includes(field)) {
|
|
286
|
+
this[field] = void 0;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return this;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* ### 用指定的数据对当前实例进行覆盖
|
|
293
|
+
* 相同属性才会覆盖上去
|
|
294
|
+
* @param obj 覆盖对象
|
|
295
|
+
*/
|
|
296
|
+
recoverBy(obj) {
|
|
297
|
+
return Object.assign(this, obj);
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* ### 转换到 `JSON`
|
|
301
|
+
* 会自动进行数据别名转换
|
|
302
|
+
*/
|
|
303
|
+
toJson() {
|
|
304
|
+
const fieldList = Object.keys(this);
|
|
305
|
+
const json = {};
|
|
306
|
+
for (const field of fieldList) {
|
|
307
|
+
const data = this[field];
|
|
308
|
+
if (data === null || data === void 0) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
const jsonKey = Transformer.getJsonKey(this, field);
|
|
312
|
+
json[jsonKey] = data;
|
|
313
|
+
const toJson = getToJson(this, field);
|
|
314
|
+
if (toJson !== void 0) {
|
|
315
|
+
try {
|
|
316
|
+
json[jsonKey] = toJson(this);
|
|
317
|
+
} catch (e) {
|
|
318
|
+
console.warn("ToJson Function Error", e);
|
|
319
|
+
}
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
if (typeof data === "object") {
|
|
323
|
+
if (Array.isArray(data)) {
|
|
324
|
+
const jsonArray = [];
|
|
325
|
+
for (let i = 0; i < data.length; i += 1) {
|
|
326
|
+
if (typeof data[i] === "object") {
|
|
327
|
+
jsonArray[i] = data[i].toJson();
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
jsonArray[i] = data[i];
|
|
331
|
+
}
|
|
332
|
+
json[jsonKey || field] = jsonArray;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
json[jsonKey || field] = data.toJson();
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return json;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
const KEY$5 = `${DecoratorUtil.DecoratorKeyPrefix}[Alias]`;
|
|
342
|
+
function Alias(alias) {
|
|
343
|
+
return (target, key) => DecoratorUtil.setFieldConfig(target, key, KEY$5, alias);
|
|
344
|
+
}
|
|
345
|
+
function getAlias(target, key) {
|
|
346
|
+
return (DecoratorUtil.getFieldConfig(target, key, KEY$5) || "").toString();
|
|
347
|
+
}
|
|
348
|
+
const KEY$4 = `${DecoratorUtil.DecoratorKeyPrefix}[IgnorePrefix]`;
|
|
349
|
+
function IgnorePrefix() {
|
|
350
|
+
return (target, key) => DecoratorUtil.setFieldConfig(target, key, KEY$4, true);
|
|
351
|
+
}
|
|
352
|
+
function getIgnorePrefix(target, key) {
|
|
353
|
+
return !!DecoratorUtil.getFieldConfig(target, key, KEY$4);
|
|
354
|
+
}
|
|
355
|
+
const KEY$3 = `${DecoratorUtil.DecoratorKeyPrefix}[Prefix]`;
|
|
356
|
+
function Prefix(prefix) {
|
|
357
|
+
return (target) => DecoratorUtil.setClassConfig(target, KEY$3, prefix);
|
|
358
|
+
}
|
|
359
|
+
function getPrefix(target) {
|
|
360
|
+
return DecoratorUtil.getClassConfig(target, KEY$3, "");
|
|
361
|
+
}
|
|
362
|
+
const KEY$2 = `${DecoratorUtil.DecoratorKeyPrefix}[ToClass]`;
|
|
363
|
+
function ToClass(func) {
|
|
364
|
+
return (target, key) => DecoratorUtil.setFieldConfig(target, key, KEY$2, func);
|
|
365
|
+
}
|
|
366
|
+
function getToClass(target, key) {
|
|
367
|
+
return DecoratorUtil.getFieldConfig(target, key, KEY$2);
|
|
368
|
+
}
|
|
369
|
+
const KEY$1 = `${DecoratorUtil.DecoratorKeyPrefix}[ToJson]`;
|
|
370
|
+
function ToJson(func) {
|
|
371
|
+
return (target, key) => DecoratorUtil.setFieldConfig(target, key, KEY$1, func);
|
|
372
|
+
}
|
|
373
|
+
function getToJson(target, key) {
|
|
374
|
+
return DecoratorUtil.getFieldConfig(target, key, KEY$1);
|
|
375
|
+
}
|
|
376
|
+
const KEY = `${DecoratorUtil.DecoratorKeyPrefix}[Type]`;
|
|
377
|
+
const KEY_ARRAY = `${DecoratorUtil.DecoratorKeyPrefix}[Array]`;
|
|
378
|
+
function Type(type, array = false) {
|
|
379
|
+
return (target, key) => {
|
|
380
|
+
DecoratorUtil.setFieldConfig(target, key, KEY, type);
|
|
381
|
+
if (array) {
|
|
382
|
+
DecoratorUtil.setFieldConfig(target, key, KEY_ARRAY, array);
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
function getType(target, key) {
|
|
387
|
+
return DecoratorUtil.getFieldConfig(target, key, KEY) || void 0;
|
|
388
|
+
}
|
|
389
|
+
function getArray(target, key) {
|
|
390
|
+
return !!DecoratorUtil.getFieldConfig(target, key, KEY_ARRAY);
|
|
391
|
+
}
|
|
392
|
+
export {
|
|
393
|
+
Alias,
|
|
394
|
+
DecoratorUtil,
|
|
395
|
+
IgnorePrefix,
|
|
396
|
+
Prefix,
|
|
397
|
+
ToClass,
|
|
398
|
+
ToJson,
|
|
399
|
+
Transformer,
|
|
400
|
+
Type,
|
|
401
|
+
getAlias,
|
|
402
|
+
getArray,
|
|
403
|
+
getIgnorePrefix,
|
|
404
|
+
getPrefix,
|
|
405
|
+
getToClass,
|
|
406
|
+
getToJson,
|
|
407
|
+
getType
|
|
408
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { DecoratorData, DecoratorTarget } from '../type/type';
|
|
2
|
+
/**
|
|
3
|
+
* # 装饰器工具类
|
|
4
|
+
*
|
|
5
|
+
* @author Hamm.cn
|
|
6
|
+
*/
|
|
7
|
+
export declare class DecoratorUtil {
|
|
8
|
+
/**
|
|
9
|
+
* ### 装饰器前缀
|
|
10
|
+
*/
|
|
11
|
+
static readonly DecoratorKeyPrefix = "[AirPower]";
|
|
12
|
+
/**
|
|
13
|
+
* ### 设置一个类配置项
|
|
14
|
+
* @param target 目标实体类
|
|
15
|
+
* @param classConfigKey 配置项索引键值
|
|
16
|
+
* @param classConfig 配置的参数
|
|
17
|
+
*/
|
|
18
|
+
static setClassConfig(target: DecoratorTarget, classConfigKey: string, classConfig: unknown): void;
|
|
19
|
+
/**
|
|
20
|
+
* ### 递归获取指定类的配置项
|
|
21
|
+
* @param target 目标类
|
|
22
|
+
* @param classConfigKey 配置项的Key
|
|
23
|
+
* @param defaultValue `可选` 类装饰器请传入配置项实例
|
|
24
|
+
* @param isObject `可选` 是否是对象配置
|
|
25
|
+
*/
|
|
26
|
+
static getClassConfig(target: DecoratorTarget, classConfigKey: string, defaultValue?: unknown, isObject?: boolean): DecoratorData;
|
|
27
|
+
/**
|
|
28
|
+
* ### 设置一个属性配置项
|
|
29
|
+
* @param target 目标类
|
|
30
|
+
* @param key 属性
|
|
31
|
+
* @param fieldConfigKey 配置项索引键值
|
|
32
|
+
* @param fieldConfig 配置的参数
|
|
33
|
+
* @param fieldListKey `可选` 类配置项列表索引值
|
|
34
|
+
*/
|
|
35
|
+
static setFieldConfig(target: DecoratorTarget, key: string, fieldConfigKey: string, fieldConfig: unknown, fieldListKey?: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* ### 获取类指定属性的指定类型的配置
|
|
38
|
+
* @param target 目标类
|
|
39
|
+
* @param key 属性
|
|
40
|
+
* @param fieldConfigKey FieldConfigKey
|
|
41
|
+
* @param isObject `可选` 是否对象配置
|
|
42
|
+
*/
|
|
43
|
+
static getFieldConfig(target: DecoratorTarget, key: string, fieldConfigKey: string, isObject?: boolean): DecoratorData;
|
|
44
|
+
/**
|
|
45
|
+
* ### 获取类标记了装饰器的属性列表
|
|
46
|
+
* @param target 目标类
|
|
47
|
+
* @param fieldConfigKey FieldConfigKey
|
|
48
|
+
* @param list `递归参数` 无需传入
|
|
49
|
+
*/
|
|
50
|
+
static getFieldList(target: DecoratorTarget, fieldConfigKey: string, list?: string[]): string[];
|
|
51
|
+
/**
|
|
52
|
+
* ### 反射添加属性
|
|
53
|
+
* @param target 目标类
|
|
54
|
+
* @param key 配置key
|
|
55
|
+
* @param value 配置值
|
|
56
|
+
*/
|
|
57
|
+
private static setProperty;
|
|
58
|
+
/**
|
|
59
|
+
* ### 设置一个属性的包含装饰器索引
|
|
60
|
+
* @param target 目标类
|
|
61
|
+
* @param key 属性
|
|
62
|
+
* @param fieldListKey 类配置项列表索引值
|
|
63
|
+
*/
|
|
64
|
+
private static addFieldDecoratorKey;
|
|
65
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { IJson, ITransformerConstructor } from './index.ts';
|
|
2
|
+
/**
|
|
3
|
+
* # 转换器
|
|
4
|
+
*
|
|
5
|
+
* @author Hamm.cn
|
|
6
|
+
*/
|
|
7
|
+
export declare class Transformer {
|
|
8
|
+
/**
|
|
9
|
+
* ### 从 `JSON` 转换到当前类的对象
|
|
10
|
+
* 会自动进行数据别名转换
|
|
11
|
+
* @param json `JSON`
|
|
12
|
+
*/
|
|
13
|
+
static fromJson<T extends Transformer>(this: ITransformerConstructor<T>, json?: IJson): T;
|
|
14
|
+
/**
|
|
15
|
+
* ### 从 `JSON` 数组转换到当前类的对象数组
|
|
16
|
+
* 会自动进行数据别名转换
|
|
17
|
+
* @param jsonArray `JSON`数组
|
|
18
|
+
*/
|
|
19
|
+
static fromJsonArray<T extends Transformer>(this: ITransformerConstructor<T>, jsonArray?: IJson | IJson[]): T[];
|
|
20
|
+
/**
|
|
21
|
+
* ### 创建一个当前类的实例
|
|
22
|
+
* @param recoverBy `可选` 初始化用于覆盖对象实例的 `JSON`
|
|
23
|
+
*/
|
|
24
|
+
static newInstance<T extends Transformer>(this: ITransformerConstructor<T>, recoverBy?: IJson): T;
|
|
25
|
+
/**
|
|
26
|
+
* ### 转换 `JSON` 为实体
|
|
27
|
+
* 会自动进行数据别名转换
|
|
28
|
+
* @param instance 实体
|
|
29
|
+
* @param json `JSON`
|
|
30
|
+
*/
|
|
31
|
+
private static parse;
|
|
32
|
+
/**
|
|
33
|
+
* ### 获取 JSON 的 key
|
|
34
|
+
* @param instance 目标实例
|
|
35
|
+
* @param field 属性 key
|
|
36
|
+
*/
|
|
37
|
+
private static getJsonKey;
|
|
38
|
+
/**
|
|
39
|
+
* ### 将当前实例复制到一个新实例上
|
|
40
|
+
*/
|
|
41
|
+
copy(): this;
|
|
42
|
+
/**
|
|
43
|
+
* ### 暴露部分类的属性
|
|
44
|
+
* @param fields 属性列表
|
|
45
|
+
*/
|
|
46
|
+
expose(...fields: string[]): this;
|
|
47
|
+
/**
|
|
48
|
+
* ### 排除部分类的属性
|
|
49
|
+
* @param fields 属性列表
|
|
50
|
+
*/
|
|
51
|
+
exclude(...fields: string[]): this;
|
|
52
|
+
/**
|
|
53
|
+
* ### 用指定的数据对当前实例进行覆盖
|
|
54
|
+
* 相同属性才会覆盖上去
|
|
55
|
+
* @param obj 覆盖对象
|
|
56
|
+
*/
|
|
57
|
+
recoverBy(obj: IJson | Transformer): this;
|
|
58
|
+
/**
|
|
59
|
+
* ### 转换到 `JSON`
|
|
60
|
+
* 会自动进行数据别名转换
|
|
61
|
+
*/
|
|
62
|
+
toJson(): IJson;
|
|
63
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@airpower/transformer",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "AirPower-Transformer 是一个基于 TypeScript 的数据转换器,提供了从 JSON 对象到 类实例 对象之间互相转换的系列装饰器和方法。",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "Hamm",
|
|
8
|
+
"email": "admin@hamm.cn",
|
|
9
|
+
"url": "https://github.com/HammCn"
|
|
10
|
+
},
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"homepage": "https://github.com/AirPowerTeam/AirPower-Transformer#readme",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/AirPowerTeam/AirPower-Transformer.git"
|
|
16
|
+
},
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/AirPowerTeam/AirPower-Transformer/issues"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"AirPower",
|
|
22
|
+
"class-transformer",
|
|
23
|
+
"typescript",
|
|
24
|
+
"transformer",
|
|
25
|
+
"bean",
|
|
26
|
+
"bean-copy",
|
|
27
|
+
"oop",
|
|
28
|
+
"class"
|
|
29
|
+
],
|
|
30
|
+
"main": "dist/main.js",
|
|
31
|
+
"module": "dist/main.js",
|
|
32
|
+
"types": "dist/index.d.ts",
|
|
33
|
+
"files": [
|
|
34
|
+
"dist",
|
|
35
|
+
"assets"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "tsc && vite build",
|
|
39
|
+
"lint": "eslint src --ext .ts",
|
|
40
|
+
"preview": "vite preview"
|
|
41
|
+
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"@types/crypto-js": "^4.2.2",
|
|
44
|
+
"crypto-js": "^4.2.0"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@antfu/eslint-config": "^4.11.0",
|
|
48
|
+
"@types/node": "^22.13.14",
|
|
49
|
+
"eslint": "^9.23.0",
|
|
50
|
+
"typescript": "^5.8.2",
|
|
51
|
+
"vite": "^6.2.0",
|
|
52
|
+
"vite-plugin-dts": "^4.5.3"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
}
|
|
57
|
+
}
|