mm_expand 2.3.4 → 2.3.6
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/lib/array.js +36 -36
- package/lib/base.js +15 -15
- package/lib/date.js +9 -8
- package/lib/errors.js +99 -99
- package/lib/event.js +45 -45
- package/lib/eventer.js +166 -165
- package/lib/file.js +127 -15
- package/lib/global.js +51 -53
- package/lib/lang.js +10 -10
- package/lib/logger.js +2 -2
- package/lib/number.js +10 -10
- package/lib/object.js +72 -75
- package/lib/req.js +42 -42
- package/lib/ret.js +247 -248
- package/lib/string.js +87 -93
- package/lib/timer.js +15 -15
- package/lib/validator.js +11 -11
- package/package.json +6 -6
package/lib/object.js
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
const {
|
|
2
|
-
inspect
|
|
3
|
-
} = require('util');
|
|
4
|
-
const {
|
|
5
|
-
XMLBuilder
|
|
6
|
-
} = require('fast-xml-parser');
|
|
1
|
+
const { inspect } = require('util');
|
|
7
2
|
/* == 基础函数 == */
|
|
8
3
|
/**
|
|
9
|
-
*
|
|
4
|
+
* 尝试将值转换为目标类型
|
|
10
5
|
* @param {any} value 要转换的值
|
|
11
6
|
* @param {string} target_type 目标类型
|
|
12
7
|
* @returns {any} 转换后的值,转换失败返回原值
|
|
@@ -33,7 +28,7 @@ function _tryConvertType(value, target_type) {
|
|
|
33
28
|
}
|
|
34
29
|
|
|
35
30
|
/**
|
|
36
|
-
*
|
|
31
|
+
* 根据目标类型转换值
|
|
37
32
|
* @param {any} value 要转换的值
|
|
38
33
|
* @param {string} target_type 目标类型
|
|
39
34
|
* @returns {any} 转换后的值
|
|
@@ -41,32 +36,32 @@ function _tryConvertType(value, target_type) {
|
|
|
41
36
|
*/
|
|
42
37
|
function _convertValueByType(value, target_type) {
|
|
43
38
|
switch (target_type) {
|
|
44
|
-
|
|
39
|
+
case 'number':
|
|
45
40
|
// 尝试转换为数字
|
|
46
|
-
|
|
47
|
-
|
|
41
|
+
var num = Number(value);
|
|
42
|
+
return isNaN(num) ? value : num;
|
|
48
43
|
|
|
49
|
-
|
|
44
|
+
case 'string':
|
|
50
45
|
// 转换为字符串,但对象转字符串视为失败
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
46
|
+
if (typeof value === 'object' && value !== null) {
|
|
47
|
+
return value; // 对象转字符串失败,返回原值
|
|
48
|
+
}
|
|
49
|
+
return String(value);
|
|
55
50
|
|
|
56
|
-
|
|
51
|
+
case 'boolean':
|
|
57
52
|
// 转换为布尔值
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
53
|
+
if (value === 'false' || value === '0' || value === 0) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
return Boolean(value);
|
|
62
57
|
|
|
63
|
-
|
|
58
|
+
default:
|
|
64
59
|
// 其他类型无法转换,返回原值
|
|
65
|
-
|
|
60
|
+
return value;
|
|
66
61
|
}
|
|
67
62
|
}
|
|
68
63
|
/**
|
|
69
|
-
*
|
|
64
|
+
* 查询对象属性明细
|
|
70
65
|
* @param {object} obj 目标对象
|
|
71
66
|
* @param {string|Array} prop_path 属性名或路径数组
|
|
72
67
|
* @returns {string} 属性详细信息
|
|
@@ -88,7 +83,7 @@ function getInfo(obj, prop_path) {
|
|
|
88
83
|
}
|
|
89
84
|
|
|
90
85
|
/**
|
|
91
|
-
*
|
|
86
|
+
* 获取基本类型信息
|
|
92
87
|
* @param {any} obj 目标对象
|
|
93
88
|
* @param {string|Array} prop_path 属性路径
|
|
94
89
|
* @returns {string|null} 基本类型信息或null
|
|
@@ -111,7 +106,7 @@ function _getBasicInfo(obj, prop_path) {
|
|
|
111
106
|
}
|
|
112
107
|
|
|
113
108
|
/**
|
|
114
|
-
*
|
|
109
|
+
* 获取属性路径信息
|
|
115
110
|
* @param {object} obj 目标对象
|
|
116
111
|
* @param {string|Array} prop_path 属性路径
|
|
117
112
|
* @returns {string} 属性信息
|
|
@@ -137,7 +132,7 @@ function _getPropertyInfo(obj, prop_path) {
|
|
|
137
132
|
return inspect(current, { depth: 1, colors: false });
|
|
138
133
|
}
|
|
139
134
|
/**
|
|
140
|
-
*
|
|
135
|
+
* 判断对象是否相似
|
|
141
136
|
* @param {object} source 被判断对象
|
|
142
137
|
* @param {object} target 用作判断的对象
|
|
143
138
|
* @param {boolean} exact 是否完全相同
|
|
@@ -166,7 +161,7 @@ function as(source, target, exact, depth = 0) {
|
|
|
166
161
|
return processObject(source, target, exact, depth);
|
|
167
162
|
}
|
|
168
163
|
/**
|
|
169
|
-
*
|
|
164
|
+
* 处理数组相似性比较
|
|
170
165
|
* @param {Array} source_arr 源数组
|
|
171
166
|
* @param {Array} target_arr 目标数组
|
|
172
167
|
* @param {boolean} exact 是否精确匹配
|
|
@@ -186,7 +181,7 @@ function processArray(source_arr, target_arr, exact, depth) {
|
|
|
186
181
|
return true;
|
|
187
182
|
}
|
|
188
183
|
/**
|
|
189
|
-
*
|
|
184
|
+
* 处理对象相似性比较
|
|
190
185
|
* @param {object} source_obj 源对象
|
|
191
186
|
* @param {object} target_obj 目标对象
|
|
192
187
|
* @param {boolean} exact 是否精确匹配
|
|
@@ -206,7 +201,7 @@ function processObject(source_obj, target_obj, exact, depth) {
|
|
|
206
201
|
return true;
|
|
207
202
|
}
|
|
208
203
|
/**
|
|
209
|
-
*
|
|
204
|
+
* 添加对象属性
|
|
210
205
|
* @param {object} target 被添加的对象
|
|
211
206
|
* @param {object} source 用作添加的对象
|
|
212
207
|
* @param {boolean} supplement 是否补充没有的对象
|
|
@@ -244,7 +239,7 @@ function push(target, source, supplement) {
|
|
|
244
239
|
}
|
|
245
240
|
|
|
246
241
|
/**
|
|
247
|
-
*
|
|
242
|
+
* 转换值类型
|
|
248
243
|
* @param {any} target_val 目标值
|
|
249
244
|
* @param {any} source_val 源值
|
|
250
245
|
* @returns {any} 转换后的值
|
|
@@ -271,7 +266,7 @@ function convertValue(target_val, source_val) {
|
|
|
271
266
|
|
|
272
267
|
|
|
273
268
|
/**
|
|
274
|
-
*
|
|
269
|
+
* 清空对象中的空值属性
|
|
275
270
|
* @param {any} target 目标对象或值
|
|
276
271
|
* @param {boolean} zero 是否清空0值
|
|
277
272
|
* @returns {any} 清空后的对象或值
|
|
@@ -310,7 +305,7 @@ function clear(target, zero = false) {
|
|
|
310
305
|
}
|
|
311
306
|
|
|
312
307
|
/**
|
|
313
|
-
*
|
|
308
|
+
* 判断值是否为空值
|
|
314
309
|
* @param {any} value 要判断的值
|
|
315
310
|
* @param {boolean} zero 是否将0视为空值
|
|
316
311
|
* @returns {boolean} 是否为空值
|
|
@@ -334,7 +329,7 @@ function isEmptyValue(value, zero = false) {
|
|
|
334
329
|
return false;
|
|
335
330
|
}
|
|
336
331
|
/**
|
|
337
|
-
*
|
|
332
|
+
* 转为json字符串
|
|
338
333
|
* @param {object} source 被转换的对象
|
|
339
334
|
* @param {boolean} format 是否格式化
|
|
340
335
|
* @returns {string} json格式字符串
|
|
@@ -347,7 +342,7 @@ function toJson(source, format) {
|
|
|
347
342
|
}
|
|
348
343
|
}
|
|
349
344
|
/**
|
|
350
|
-
*
|
|
345
|
+
* 将内容转换为CDATA格式
|
|
351
346
|
* @param {any} content 要转换的内容
|
|
352
347
|
* @returns {string|any} CDATA格式的字符串或原始内容
|
|
353
348
|
*/
|
|
@@ -359,7 +354,7 @@ function toCDATA(content) {
|
|
|
359
354
|
}
|
|
360
355
|
}
|
|
361
356
|
/**
|
|
362
|
-
*
|
|
357
|
+
* 转为xml字符串
|
|
363
358
|
* @param {object} source 被转换的对象
|
|
364
359
|
* @param {boolean} format 是否格式化
|
|
365
360
|
* @param {boolean} attributes 是否使用属性格式
|
|
@@ -378,23 +373,23 @@ function toXml(source, format, attributes, cdata = false) {
|
|
|
378
373
|
if (attributes) {
|
|
379
374
|
// 合并属性选项,不覆盖格式化选项
|
|
380
375
|
Object.assign(options, {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
376
|
+
ignore_attr: false,
|
|
377
|
+
suppress_bool_attrs: false,
|
|
378
|
+
suppress_unpaired: false,
|
|
379
|
+
process_entities: false // 禁用实体转换
|
|
385
380
|
});
|
|
386
381
|
}
|
|
387
382
|
|
|
388
383
|
if (cdata) {
|
|
389
384
|
options.cdataPropName = cdata ? '_cdata' : undefined;
|
|
390
|
-
|
|
385
|
+
var source_xml = toCDATA({...source});
|
|
391
386
|
}
|
|
392
387
|
|
|
393
|
-
const builder = new
|
|
394
|
-
return builder.build(source);
|
|
388
|
+
const builder = new Xml(options);
|
|
389
|
+
return builder.build(source_xml || source);
|
|
395
390
|
};
|
|
396
391
|
/**
|
|
397
|
-
*
|
|
392
|
+
* 转url字符串
|
|
398
393
|
* @param {object} source 被转换的对象
|
|
399
394
|
* @param {string} base 基础url地址
|
|
400
395
|
* @returns {string} url参数格式字符串
|
|
@@ -425,7 +420,7 @@ function toUrl(source, base) {
|
|
|
425
420
|
};
|
|
426
421
|
|
|
427
422
|
/**
|
|
428
|
-
*
|
|
423
|
+
* 将对象保存为JSON文件
|
|
429
424
|
* @param {object} source 源对象
|
|
430
425
|
* @param {string|object} target 目标路径或可写流
|
|
431
426
|
* @param {boolean} format 是否格式化
|
|
@@ -441,7 +436,7 @@ function saveJson(source, target, format = true) {
|
|
|
441
436
|
return json_str;
|
|
442
437
|
};
|
|
443
438
|
/**
|
|
444
|
-
*
|
|
439
|
+
* 将对象保存为XML文件
|
|
445
440
|
* @param {object} source 源对象
|
|
446
441
|
* @param {string|object} target 目标路径或可写流
|
|
447
442
|
* @param {object} options 选项
|
|
@@ -450,8 +445,9 @@ function saveJson(source, target, format = true) {
|
|
|
450
445
|
* @param {boolean} options.cdata 是否使用CDATA
|
|
451
446
|
* @returns {string} XML字符串
|
|
452
447
|
*/
|
|
453
|
-
function saveXml(source, target,
|
|
448
|
+
function saveXml(source, target, _options = {}) {
|
|
454
449
|
// 设置默认选项
|
|
450
|
+
var options = _options;
|
|
455
451
|
if (typeof options !== 'object' || options === null) {
|
|
456
452
|
options = {};
|
|
457
453
|
}
|
|
@@ -470,7 +466,7 @@ function saveXml(source, target, options = {}) {
|
|
|
470
466
|
return xml_str;
|
|
471
467
|
};
|
|
472
468
|
/**
|
|
473
|
-
*
|
|
469
|
+
* 复制对象或值
|
|
474
470
|
* @param {any} source 源对象或值
|
|
475
471
|
* @param {boolean} deep 是否深拷贝,默认true
|
|
476
472
|
* @param {number} depth 递归深度,防止无限递归
|
|
@@ -503,7 +499,7 @@ function copy(source, deep = true, depth = 0) {
|
|
|
503
499
|
}
|
|
504
500
|
|
|
505
501
|
/**
|
|
506
|
-
*
|
|
502
|
+
* 复制数组
|
|
507
503
|
* @param {Array} source 源数组
|
|
508
504
|
* @param {boolean} deep 是否深拷贝
|
|
509
505
|
* @param {number} depth 递归深度
|
|
@@ -526,7 +522,7 @@ function _copyArray(source, deep, depth) {
|
|
|
526
522
|
}
|
|
527
523
|
|
|
528
524
|
/**
|
|
529
|
-
*
|
|
525
|
+
* 复制对象
|
|
530
526
|
* @param {object} source 源对象
|
|
531
527
|
* @param {boolean} deep 是否深拷贝
|
|
532
528
|
* @param {number} depth 递归深度
|
|
@@ -552,7 +548,7 @@ function _copyObject(source, deep, depth) {
|
|
|
552
548
|
}
|
|
553
549
|
|
|
554
550
|
/**
|
|
555
|
-
*
|
|
551
|
+
* 获取对象的属性名数组
|
|
556
552
|
* @param {object} source 源对象
|
|
557
553
|
* @param {boolean} include_proto 是否包含原型链属性,默认false
|
|
558
554
|
* @param {boolean} include_private 是否包含私有方法,默认false
|
|
@@ -575,7 +571,7 @@ function keys(source, include_proto = false, include_private = false) {
|
|
|
575
571
|
}
|
|
576
572
|
|
|
577
573
|
/**
|
|
578
|
-
*
|
|
574
|
+
* 收集原型链属性
|
|
579
575
|
* @param {object} source 源对象
|
|
580
576
|
* @param {Array} result 结果数组
|
|
581
577
|
* @param {boolean} include_private 是否包含私有方法
|
|
@@ -590,7 +586,7 @@ function _collectProtoKeys(source, result, include_private) {
|
|
|
590
586
|
}
|
|
591
587
|
|
|
592
588
|
/**
|
|
593
|
-
*
|
|
589
|
+
* 收集自身属性
|
|
594
590
|
* @param {object} source 源对象
|
|
595
591
|
* @param {Array} result 结果数组
|
|
596
592
|
* @param {boolean} include_private 是否包含私有方法
|
|
@@ -605,7 +601,7 @@ function _collectOwnKeys(source, result, include_private) {
|
|
|
605
601
|
}
|
|
606
602
|
|
|
607
603
|
/**
|
|
608
|
-
*
|
|
604
|
+
* 判断是否应该包含该键
|
|
609
605
|
* @param {string} key 键名
|
|
610
606
|
* @param {boolean} include_private 是否包含私有方法
|
|
611
607
|
* @returns {boolean} 是否包含
|
|
@@ -615,7 +611,7 @@ function _shouldIncludeKey(key, include_private) {
|
|
|
615
611
|
return include_private || !key.startsWith('_');
|
|
616
612
|
}
|
|
617
613
|
/**
|
|
618
|
-
*
|
|
614
|
+
* 添加对象属性
|
|
619
615
|
* @param {object} target 目标对象(被添加的对象)
|
|
620
616
|
* @param {object} source 源对象(提供要添加的属性)
|
|
621
617
|
* @param {boolean} overwrite 是否覆盖目标对象,true则修改原对象,false则返回新对象
|
|
@@ -644,7 +640,7 @@ function add(target, source, overwrite = true, depth = 0) {
|
|
|
644
640
|
}
|
|
645
641
|
|
|
646
642
|
/**
|
|
647
|
-
*
|
|
643
|
+
* 添加单个属性
|
|
648
644
|
* @param {object} result 结果对象
|
|
649
645
|
* @param {string} key 属性键
|
|
650
646
|
* @param {any} source_val 源值
|
|
@@ -672,7 +668,7 @@ function _addProperty(result, key, source_val, options) {
|
|
|
672
668
|
}
|
|
673
669
|
|
|
674
670
|
/**
|
|
675
|
-
*
|
|
671
|
+
* 判断是否应该进行深层合并
|
|
676
672
|
* @param {any} target_val 目标值
|
|
677
673
|
* @param {any} source_val 源值
|
|
678
674
|
* @returns {boolean} 是否应该深层合并
|
|
@@ -687,7 +683,7 @@ function _shouldDeepMerge(target_val, source_val) {
|
|
|
687
683
|
}
|
|
688
684
|
|
|
689
685
|
/**
|
|
690
|
-
*
|
|
686
|
+
* 删除对象属性(ECS组件删除)
|
|
691
687
|
* @param {object} target 目标对象
|
|
692
688
|
* @param {string|Array} key_path 组件路径(支持点分隔符或数组)
|
|
693
689
|
* @param {boolean} overwrite 是否覆盖目标对象,true则修改原对象,false则返回新对象
|
|
@@ -733,7 +729,7 @@ function del(target, key_path, overwrite = true) {
|
|
|
733
729
|
|
|
734
730
|
|
|
735
731
|
/**
|
|
736
|
-
*
|
|
732
|
+
* 修改对象属性
|
|
737
733
|
* @param {object} target 目标对象(被修改的对象)
|
|
738
734
|
* @param {object} source 源对象(提供要设置的属性)
|
|
739
735
|
* @param {boolean} overwrite 是否覆盖目标对象,true则修改原对象,false则返回新对象
|
|
@@ -762,7 +758,7 @@ function set(target, source, overwrite = true, depth = 0) {
|
|
|
762
758
|
}
|
|
763
759
|
|
|
764
760
|
/**
|
|
765
|
-
*
|
|
761
|
+
* 设置单个属性
|
|
766
762
|
* @param {object} result 结果对象
|
|
767
763
|
* @param {string} key 属性键
|
|
768
764
|
* @param {any} source_val 源值
|
|
@@ -797,7 +793,7 @@ function _setProperty(result, key, source_val, options) {
|
|
|
797
793
|
}
|
|
798
794
|
|
|
799
795
|
/**
|
|
800
|
-
*
|
|
796
|
+
* 设置相同类型的属性
|
|
801
797
|
* @param {object} result 结果对象
|
|
802
798
|
* @param {string} key 属性键
|
|
803
799
|
* @param {object} values 值对象
|
|
@@ -825,7 +821,7 @@ function _setSameTypeProperty(result, key, values, options) {
|
|
|
825
821
|
}
|
|
826
822
|
|
|
827
823
|
/**
|
|
828
|
-
*
|
|
824
|
+
* 设置不同类型的属性
|
|
829
825
|
* @param {object} result 结果对象
|
|
830
826
|
* @param {string} key 属性键
|
|
831
827
|
* @param {object} values 值对象
|
|
@@ -836,19 +832,20 @@ function _setSameTypeProperty(result, key, values, options) {
|
|
|
836
832
|
*/
|
|
837
833
|
function _setDiffTypeProp(result, key, values) {
|
|
838
834
|
// 类型不一致时尝试转换
|
|
839
|
-
var
|
|
835
|
+
var conv_val = _tryConvertType(values.source_val, values.target_type);
|
|
840
836
|
|
|
841
837
|
// 检查转换后的类型是否与目标类型一致
|
|
842
|
-
if (typeof
|
|
838
|
+
if (typeof conv_val === values.target_type) {
|
|
843
839
|
// 转换成功,赋值
|
|
844
|
-
result[key] =
|
|
840
|
+
result[key] = conv_val;
|
|
845
841
|
}
|
|
846
842
|
// 转换失败时不赋值(保持原值)
|
|
847
843
|
}
|
|
848
844
|
|
|
849
845
|
/**
|
|
850
|
-
*
|
|
846
|
+
* 将数组转换为对象
|
|
851
847
|
* @param {Array} source 源数组
|
|
848
|
+
* @param key
|
|
852
849
|
* @returns {object} 转换后的对象
|
|
853
850
|
*/
|
|
854
851
|
function arrToObj(source, key = 'key') {
|
|
@@ -866,13 +863,13 @@ function arrToObj(source, key = 'key') {
|
|
|
866
863
|
return result;
|
|
867
864
|
}
|
|
868
865
|
/**
|
|
869
|
-
*
|
|
866
|
+
* 查询对象属性
|
|
870
867
|
* @param {object} source 对象
|
|
871
868
|
* @param {string|Array} condition 查询条件或路径数组
|
|
872
869
|
* @returns {any} 返回查询结果
|
|
873
870
|
*/
|
|
874
871
|
/**
|
|
875
|
-
*
|
|
872
|
+
* 获取对象属性(ECS组件获取)
|
|
876
873
|
* @param {object} target 目标对象
|
|
877
874
|
* @param {string|Array} key_path 组件路径(支持点分隔符或数组)
|
|
878
875
|
* @param {any} default_val 组件不存在时的默认值
|
|
@@ -901,7 +898,7 @@ function get(target, key_path, default_val = undefined, overwrite = true) {
|
|
|
901
898
|
}
|
|
902
899
|
|
|
903
900
|
/**
|
|
904
|
-
*
|
|
901
|
+
* 解析键路径
|
|
905
902
|
* @param {string|Array} key_path 键路径
|
|
906
903
|
* @returns {Array|null} 路径数组或null
|
|
907
904
|
* @private
|
|
@@ -917,7 +914,7 @@ function _parseKeyPath(key_path) {
|
|
|
917
914
|
}
|
|
918
915
|
|
|
919
916
|
/**
|
|
920
|
-
*
|
|
917
|
+
* 遍历路径查找值
|
|
921
918
|
* @param {object} target 目标对象
|
|
922
919
|
* @param {Array} path_arr 路径数组
|
|
923
920
|
* @returns {any} 找到的值或undefined
|
|
@@ -938,7 +935,7 @@ function _traversePath(target, path_arr) {
|
|
|
938
935
|
}
|
|
939
936
|
|
|
940
937
|
/**
|
|
941
|
-
*
|
|
938
|
+
* 处理默认值
|
|
942
939
|
* @param {object} target 目标对象
|
|
943
940
|
* @param {Array} path_arr 路径数组
|
|
944
941
|
* @param {any} default_val 默认值
|
|
@@ -957,7 +954,7 @@ function _handleDefaultValue(target, path_arr, default_val, overwrite) {
|
|
|
957
954
|
}
|
|
958
955
|
|
|
959
956
|
/**
|
|
960
|
-
*
|
|
957
|
+
* 补全对象路径
|
|
961
958
|
* @param {object} target 目标对象
|
|
962
959
|
* @param {Array} path_arr 路径数组
|
|
963
960
|
* @param {object} default_val 默认值对象
|
|
@@ -993,7 +990,7 @@ function _completeObjectPath(target, path_arr, default_val, overwrite) {
|
|
|
993
990
|
}
|
|
994
991
|
|
|
995
992
|
/**
|
|
996
|
-
*
|
|
993
|
+
* 根据路径获取对象属性值
|
|
997
994
|
* @param {object} source 源对象
|
|
998
995
|
* @param {string|Array} path 属性路径
|
|
999
996
|
* @returns {any} 属性值
|
|
@@ -1015,7 +1012,7 @@ function prop(source, path) {
|
|
|
1015
1012
|
return result;
|
|
1016
1013
|
}
|
|
1017
1014
|
/**
|
|
1018
|
-
*
|
|
1015
|
+
* 将对象转换为数组
|
|
1019
1016
|
* @param {object} source 源对象
|
|
1020
1017
|
* @param {string} key 键名
|
|
1021
1018
|
* @returns {Array} 转换后的数组
|
|
@@ -1034,7 +1031,7 @@ function objToArr(source, key = 'key') {
|
|
|
1034
1031
|
return result;
|
|
1035
1032
|
}
|
|
1036
1033
|
/**
|
|
1037
|
-
*
|
|
1034
|
+
* 数值增减操作
|
|
1038
1035
|
* @param {object} source 源对象
|
|
1039
1036
|
* @param {string} key 属性键名
|
|
1040
1037
|
* @param {number} value 增减值
|
package/lib/req.js
CHANGED
|
@@ -11,18 +11,18 @@ class Req extends Base {
|
|
|
11
11
|
* @type {string}
|
|
12
12
|
* @default "sys"
|
|
13
13
|
*/
|
|
14
|
-
scope:
|
|
14
|
+
scope: 'sys',
|
|
15
15
|
/**
|
|
16
16
|
* 响应格式
|
|
17
17
|
* @type {string} "json-rpc" | "json-rpc3" | "restful"
|
|
18
18
|
* @default "json-rpc"
|
|
19
19
|
*/
|
|
20
|
-
format:
|
|
20
|
+
format: 'json-rpc'
|
|
21
21
|
};
|
|
22
22
|
/**
|
|
23
23
|
* 构造函数
|
|
24
|
-
* @
|
|
25
|
-
* @param {
|
|
24
|
+
* @class
|
|
25
|
+
* @param {object} config 配置项
|
|
26
26
|
*/
|
|
27
27
|
constructor(config) {
|
|
28
28
|
super(config);
|
|
@@ -43,16 +43,16 @@ Req.prototype.genId = function () {
|
|
|
43
43
|
* 发送请求
|
|
44
44
|
* @param {string} method 方法名
|
|
45
45
|
* @param {...any} args 参数
|
|
46
|
-
* @returns {
|
|
46
|
+
* @returns {object} 发送的JSON格式
|
|
47
47
|
*/
|
|
48
48
|
Req.prototype.send = function (method, ...args) {
|
|
49
49
|
if (this._methods[method]) {
|
|
50
50
|
return this._methods[method](...args);
|
|
51
51
|
}
|
|
52
|
-
else if (this.config.format ===
|
|
52
|
+
else if (this.config.format === 'json-rpc') {
|
|
53
53
|
return this._sendJsonRPC(method, args);
|
|
54
54
|
}
|
|
55
|
-
else if (this.config.format ===
|
|
55
|
+
else if (this.config.format === 'json-rpc3') {
|
|
56
56
|
return this._sendJsonRPC3(method, ...args);
|
|
57
57
|
}
|
|
58
58
|
else {
|
|
@@ -65,7 +65,7 @@ Req.prototype.send = function (method, ...args) {
|
|
|
65
65
|
* @param {string} method 方法名
|
|
66
66
|
* @param {Array} params 参数数组
|
|
67
67
|
* @param {string} [id] 请求ID
|
|
68
|
-
* @returns {
|
|
68
|
+
* @returns {object} JSON-RPC 2.0格式
|
|
69
69
|
*/
|
|
70
70
|
Req.prototype._sendJsonRPC = function (method, params, id) {
|
|
71
71
|
return {
|
|
@@ -73,16 +73,16 @@ Req.prototype._sendJsonRPC = function (method, params, id) {
|
|
|
73
73
|
method,
|
|
74
74
|
params
|
|
75
75
|
};
|
|
76
|
-
}
|
|
76
|
+
};
|
|
77
77
|
|
|
78
78
|
/**
|
|
79
79
|
* 发送JSON-RPC 3.0请求
|
|
80
80
|
* @param {string} method 方法名
|
|
81
|
-
* @param {
|
|
82
|
-
* @param {
|
|
81
|
+
* @param {object} [query] 查询参数
|
|
82
|
+
* @param {object} [body] 请求体
|
|
83
83
|
* @param {Array} [meta] 元数据数组
|
|
84
84
|
* @param {string} [id] 请求ID
|
|
85
|
-
* @returns {
|
|
85
|
+
* @returns {object} JSON-RPC 3.0格式
|
|
86
86
|
*/
|
|
87
87
|
Req.prototype._sendJsonRPC3 = function (method, query, body, meta, id) {
|
|
88
88
|
return {
|
|
@@ -94,16 +94,16 @@ Req.prototype._sendJsonRPC3 = function (method, query, body, meta, id) {
|
|
|
94
94
|
meta: meta || []
|
|
95
95
|
}
|
|
96
96
|
};
|
|
97
|
-
}
|
|
97
|
+
};
|
|
98
98
|
|
|
99
99
|
/**
|
|
100
100
|
* 发送RESTful请求
|
|
101
101
|
* @param {string} method 方法名
|
|
102
|
-
* @param {
|
|
103
|
-
* @param {
|
|
102
|
+
* @param {object} [query] 查询参数
|
|
103
|
+
* @param {object} [body] 请求体
|
|
104
104
|
* @param {Array} [meta] 元数据数组
|
|
105
105
|
* @param {string} [id] 请求ID
|
|
106
|
-
* @returns {
|
|
106
|
+
* @returns {object} RESTful格式
|
|
107
107
|
*/
|
|
108
108
|
Req.prototype._sendRESTful = function (method, query, body, meta, id) {
|
|
109
109
|
return {
|
|
@@ -113,16 +113,16 @@ Req.prototype._sendRESTful = function (method, query, body, meta, id) {
|
|
|
113
113
|
body: body || {},
|
|
114
114
|
meta: meta || []
|
|
115
115
|
};
|
|
116
|
-
}
|
|
116
|
+
};
|
|
117
117
|
|
|
118
118
|
/**
|
|
119
119
|
* 解析请求
|
|
120
|
-
* @param {
|
|
121
|
-
* @returns {
|
|
120
|
+
* @param {object} req 请求对象
|
|
121
|
+
* @returns {object} 解析后的对象
|
|
122
122
|
*/
|
|
123
123
|
Req.prototype.parse = function (req) {
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
let id = req.id;
|
|
125
|
+
let method = req.method;
|
|
126
126
|
let params = [];
|
|
127
127
|
|
|
128
128
|
if (req.params) {
|
|
@@ -130,14 +130,14 @@ Req.prototype.parse = function (req) {
|
|
|
130
130
|
params = req.params;
|
|
131
131
|
}
|
|
132
132
|
else {
|
|
133
|
-
if (typeof req.params ===
|
|
134
|
-
let ctx =
|
|
133
|
+
if (typeof req.params === 'object') {
|
|
134
|
+
let ctx = { ...req.params};
|
|
135
135
|
if (ctx.query || ctx.body) {
|
|
136
136
|
params.push(ctx.query || null);
|
|
137
137
|
params.push(ctx.body || null);
|
|
138
138
|
delete ctx.query;
|
|
139
139
|
delete ctx.body;
|
|
140
|
-
for (
|
|
140
|
+
for (let key in ctx) {
|
|
141
141
|
params.push(ctx[key]);
|
|
142
142
|
}
|
|
143
143
|
}
|
|
@@ -159,25 +159,25 @@ Req.prototype.parse = function (req) {
|
|
|
159
159
|
params.push(req.meta || null);
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
|
-
if (this.config.format ===
|
|
162
|
+
if (this.config.format === 'json-rpc') {
|
|
163
163
|
if (req.params) {
|
|
164
164
|
params = req.params;
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
|
-
else if (this.config.format ===
|
|
167
|
+
else if (this.config.format === 'json-rpc3') {
|
|
168
168
|
var pm = {};
|
|
169
169
|
pm.query = params.length > 0 ? params[0] || null : null;
|
|
170
170
|
pm.body = params.length > 1 ? params[1] || null : null;
|
|
171
171
|
pm.meta = params.length > 2 ? params[2] || null : null;
|
|
172
172
|
params = pm;
|
|
173
173
|
}
|
|
174
|
-
else if (this.config.format ===
|
|
174
|
+
else if (this.config.format === 'restful') {
|
|
175
175
|
var ret = {
|
|
176
176
|
id,
|
|
177
177
|
method,
|
|
178
178
|
query: params.length > 0 ? params[0] || null : null,
|
|
179
179
|
body: params.length > 1 ? params[1] || null : null
|
|
180
|
-
}
|
|
180
|
+
};
|
|
181
181
|
if (params.length > 2) {
|
|
182
182
|
ret.meta = params[2];
|
|
183
183
|
}
|
|
@@ -188,7 +188,7 @@ Req.prototype.parse = function (req) {
|
|
|
188
188
|
method,
|
|
189
189
|
params
|
|
190
190
|
};
|
|
191
|
-
}
|
|
191
|
+
};
|
|
192
192
|
|
|
193
193
|
/**
|
|
194
194
|
* 设置自定义方法
|
|
@@ -196,8 +196,8 @@ Req.prototype.parse = function (req) {
|
|
|
196
196
|
* @param {Function} func 处理方法
|
|
197
197
|
*/
|
|
198
198
|
Req.prototype.setMethod = function (method, func) {
|
|
199
|
-
if (typeof func !==
|
|
200
|
-
throw new Error(
|
|
199
|
+
if (typeof func !== 'function') {
|
|
200
|
+
throw new Error('func必须是一个函数类型');
|
|
201
201
|
}
|
|
202
202
|
this._methods[method] = func;
|
|
203
203
|
};
|
|
@@ -221,7 +221,7 @@ Req.prototype.getMethod = function (method) {
|
|
|
221
221
|
|
|
222
222
|
/**
|
|
223
223
|
* 获取所有自定义方法
|
|
224
|
-
* @returns {
|
|
224
|
+
* @returns {object} 所有自定义方法
|
|
225
225
|
*/
|
|
226
226
|
Req.prototype.getMethods = function () {
|
|
227
227
|
return this._methods;
|
|
@@ -231,15 +231,15 @@ Req.prototype.getMethods = function () {
|
|
|
231
231
|
* 运行自定义方法
|
|
232
232
|
* @param {string} method 方法名
|
|
233
233
|
* @param {Array} params 参数数组
|
|
234
|
-
* @returns {
|
|
234
|
+
* @returns {object} 处理结果
|
|
235
235
|
*/
|
|
236
236
|
Req.prototype.run = function (method, params) {
|
|
237
|
-
|
|
237
|
+
let func = this._methods[method];
|
|
238
238
|
if (func) {
|
|
239
239
|
return func.apply(this, params);
|
|
240
240
|
}
|
|
241
241
|
else {
|
|
242
|
-
throw new Error(
|
|
242
|
+
throw new Error('方法不存在');
|
|
243
243
|
}
|
|
244
244
|
};
|
|
245
245
|
|
|
@@ -249,16 +249,16 @@ Req.prototype.run = function (method, params) {
|
|
|
249
249
|
*/
|
|
250
250
|
Req.prototype.setFormat = function (format) {
|
|
251
251
|
switch (format?.toLowerCase()) {
|
|
252
|
-
case
|
|
253
|
-
case
|
|
254
|
-
this.config.format =
|
|
252
|
+
case 'json':
|
|
253
|
+
case 'json-rpc':
|
|
254
|
+
this.config.format = 'json-rpc';
|
|
255
255
|
break;
|
|
256
|
-
case
|
|
257
|
-
case
|
|
258
|
-
this.config.format =
|
|
256
|
+
case 'json3':
|
|
257
|
+
case 'json-rpc3':
|
|
258
|
+
this.config.format = 'json-rpc3';
|
|
259
259
|
break;
|
|
260
260
|
default:
|
|
261
|
-
this.config.format =
|
|
261
|
+
this.config.format = 'restful';
|
|
262
262
|
break;
|
|
263
263
|
}
|
|
264
264
|
};
|