mm_expand 2.3.3 → 2.3.5

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/ret.js CHANGED
@@ -5,52 +5,52 @@ const { Base } = require('./base');
5
5
  * @class Ret
6
6
  */
7
7
  class Ret extends Base {
8
- static config = {
9
- /**
10
- * 响应格式
11
- * @type {string} "json-rpc" | "restful"
12
- * @default "json-rpc"
13
- */
14
- format: "json-rpc",
15
- /**
16
- * 默认错误码
17
- * @type {number}
18
- * @default 10000
19
- */
20
- error_code: 10000,
21
- /**
22
- * 默认错误提示
23
- * @type {string}
24
- * @default "错误"
25
- */
26
- error_message: "错误",
27
- /**
28
- * 默认成功码
29
- * @type {number}
30
- * @default 0
31
- */
32
- success_code: 0,
33
- /**
34
- * 默认成功提示
35
- * @type {string}
36
- * @default "成功"
37
- */
38
- success_message: "成功",
39
- /**
40
- * 是否自定义错误提示
41
- * @type {boolean}
42
- * @default false
43
- */
44
- message_diy: false
45
- };
46
- /**
47
- * 构造函数
48
- * @param {Object} config 配置项
49
- * @constructor
50
- */
51
- constructor(config) {
52
- super(config);
53
- }
8
+ static config = {
9
+ /**
10
+ * 响应格式
11
+ * @type {string} "json-rpc" | "restful"
12
+ * @default "json-rpc"
13
+ */
14
+ format: 'json-rpc',
15
+ /**
16
+ * 默认错误码
17
+ * @type {number}
18
+ * @default 10000
19
+ */
20
+ error_code: 10000,
21
+ /**
22
+ * 默认错误提示
23
+ * @type {string}
24
+ * @default "错误"
25
+ */
26
+ error_message: '错误',
27
+ /**
28
+ * 默认成功码
29
+ * @type {number}
30
+ * @default 0
31
+ */
32
+ success_code: 0,
33
+ /**
34
+ * 默认成功提示
35
+ * @type {string}
36
+ * @default "成功"
37
+ */
38
+ success_message: '成功',
39
+ /**
40
+ * 是否自定义错误提示
41
+ * @type {boolean}
42
+ * @default false
43
+ */
44
+ message_diy: false
45
+ };
46
+ /**
47
+ * 构造函数
48
+ * @param {object} config 配置项
49
+ * @class
50
+ */
51
+ constructor(config) {
52
+ super(config);
53
+ }
54
54
  }
55
55
 
56
56
  /**
@@ -59,125 +59,128 @@ class Ret extends Base {
59
59
  * @returns {string} 错误提示
60
60
  */
61
61
  Ret.prototype._getMessage = function (code) {
62
- var message;
63
- if ($.error) {
64
- message = $.error.get(code);
65
- }
62
+ var message;
63
+ if ($.error) {
64
+ message = $.error.get(code);
65
+ }
66
66
 
67
- if (!message) {
68
- if (code === this.config.success_code) {
69
- message = this.config.success_message;
70
- }
71
- else {
72
- message = this.config.error_message;
73
- }
74
- }
75
- return message;
76
- }
67
+ if (!message) {
68
+ if (code === this.config.success_code) {
69
+ message = this.config.success_message;
70
+ }
71
+ else {
72
+ message = this.config.error_message;
73
+ }
74
+ }
75
+ return message;
76
+ };
77
77
 
78
78
  /**
79
79
  * 生成JSON-RPC 2.0响应格式
80
- * @param {Object} result 返回的结果
81
- * @param {Object} error 返回的错误信息
82
- * @param {Object.code} error.code 返回的错误码
83
- * @param {Object.message} error.message 返回的错误提示
80
+ * @param {object} result 返回的结果
81
+ * @param {object} error 返回的错误信息
82
+ * @param {object.code} error.code 返回的错误码
83
+ * @param {object.message} error.message 返回的错误提示
84
84
  * @param {string} id 消息ID
85
85
  * @param {boolean} diy 是否自定义错误提示
86
- * @returns {Object} JSON-RPC 2.0响应格式
86
+ * @returns {object} JSON-RPC 2.0响应格式
87
87
  */
88
- Ret.prototype._toJsonRPC = function (result, error, id, diy = true) {
89
- const ret = {
90
- };
91
- if (id) {
92
- ret.id = id;
93
- }
94
-
95
- if (result) {
96
- ret.result = result;
97
- }
98
- else if (error) {
99
- if (!error.code) {
100
- error.code = this.config.error_code;
101
- }
102
- diy = diy || this.config.message_diy;
103
- if (!diy) {
104
- error.message = this._getMessage(error.code) || error.message;
105
- }
106
- else if (!error.message) {
107
- error.message = this._getMessage(error.code);
108
- }
109
- ret.error = error;
110
- }
111
- else {
112
- ret.error = {
113
- code: this.config.error_code,
114
- message: this._getMessage(this.config.error_code)
115
- };
116
- }
117
- return ret;
118
- }
88
+ Ret.prototype._toJsonRPC = function (result, error, id, _diy = true) {
89
+ const ret = {
90
+ };
91
+ if (id) {
92
+ ret.id = id;
93
+ }
94
+
95
+ if (result) {
96
+ ret.result = result;
97
+ }
98
+ else if (error) {
99
+ if (!error.code) {
100
+ error.code = this.config.error_code;
101
+ }
102
+ var diy = _diy || this.config.message_diy;
103
+ if (!diy) {
104
+ error.message = this._getMessage(error.code) || error.message;
105
+ }
106
+ else if (!error.message) {
107
+ error.message = this._getMessage(error.code);
108
+ }
109
+ ret.error = error;
110
+ }
111
+ else {
112
+ ret.error = {
113
+ code: this.config.error_code,
114
+ message: this._getMessage(this.config.error_code)
115
+ };
116
+ }
117
+ if (this.config.fs) {
118
+ ret.fs = this.config.fs;
119
+ }
120
+ return ret;
121
+ };
119
122
 
120
123
  /**
121
124
  * 生成RESTful响应格式
122
125
  * @param {*} result 返回的结果
123
- * @param {Object} error 返回的错误信息
124
- * @param {Object.code} error.code 返回的错误码
125
- * @param {Object.message} error.message 返回的错误提示
126
+ * @param {object} error 返回的错误信息
127
+ * @param {object.code} error.code 返回的错误码
128
+ * @param {object.message} error.message 返回的错误提示
126
129
  * @param {string} id 消息ID
127
130
  * @param {boolean} diy 是否自定义错误提示
128
- * @returns {Object} RESTful响应格式
131
+ * @returns {object} RESTful响应格式
129
132
  */
130
- Ret.prototype._toRESTful = function (result, error, id, diy = true) {
131
- let ret = {};
132
- if (id) {
133
- ret.id = id;
134
- }
135
- var code;
136
- var msg;
137
- if (result) {
138
- code = this.config.success_code;
139
- msg = this._getMessage(this.config.success_code);
140
- }
141
- else if (error) {
142
- code = error.code || this.config.error_code;
143
- diy = diy || this.config.message_diy;
144
- if (!diy) {
145
- msg = this._getMessage(code) || error.message || error.msg;
146
- }
147
- else if (!error.message && !error.msg) {
148
- msg = this._getMessage(code);
149
- }
150
- else {
151
- msg = error.message || error.msg || this._getMessage(code);
152
- }
153
- }
154
- else {
155
- code = this.config.error_code;
156
- msg = this._getMessage(code);
157
- }
158
- ret.code = code;
159
- ret.msg = msg;
160
- if (result) {
161
- ret.data = result;
162
- }
163
- return ret;
133
+ Ret.prototype._toRESTful = function (result, error, id, _diy = true) {
134
+ let ret = {};
135
+ if (id) {
136
+ ret.id = id;
137
+ }
138
+ var code;
139
+ var msg;
140
+ if (result) {
141
+ code = this.config.success_code;
142
+ msg = this._getMessage(this.config.success_code);
143
+ }
144
+ else if (error) {
145
+ code = error.code || this.config.error_code;
146
+ var diy = _diy || this.config.message_diy;
147
+ if (!diy) {
148
+ msg = this._getMessage(code) || error.message || error.msg;
149
+ }
150
+ else if (!error.message && !error.msg) {
151
+ msg = this._getMessage(code);
152
+ }
153
+ else {
154
+ msg = error.message || error.msg || this._getMessage(code);
155
+ }
156
+ }
157
+ else {
158
+ code = this.config.error_code;
159
+ msg = this._getMessage(code);
160
+ }
161
+ ret.code = code;
162
+ ret.msg = msg;
163
+ if (result) {
164
+ ret.data = result;
165
+ }
166
+ return ret;
164
167
  };
165
168
 
166
169
  /**
167
170
  * 生成响应体
168
- * @param {Object} result 返回的结果
169
- * @param {Object} error 返回的错误信息
171
+ * @param {object} result 返回的结果
172
+ * @param {object} error 返回的错误信息
170
173
  * @param {string} id 消息ID
171
174
  * @param {boolean} diy 是否自定义错误提示
172
- * @returns {Object} 响应格式
175
+ * @returns {object} 响应格式
173
176
  */
174
177
  Ret.prototype.body = function (result, error, id, diy = true) {
175
- if (this.config.format === "json-rpc") {
176
- return this._toJsonRPC(result, error, id, diy);
177
- }
178
- else {
179
- return this._toRESTful(result, error, id, diy);
180
- }
178
+ if (this.config.format === 'json-rpc') {
179
+ return this._toJsonRPC(result, error, id, diy);
180
+ }
181
+ else {
182
+ return this._toRESTful(result, error, id, diy);
183
+ }
181
184
  };
182
185
 
183
186
  /**
@@ -186,14 +189,14 @@ Ret.prototype.body = function (result, error, id, diy = true) {
186
189
  * @param {string} message 错误信息
187
190
  * @param {string} id 消息ID
188
191
  * @param {boolean} diy 是否自定义错误提示
189
- * @returns {Object} 错误响应格式
192
+ * @returns {object} 错误响应格式
190
193
  */
191
194
  Ret.prototype.error = function (code, message, id, diy = true) {
192
- const error = {
193
- code,
194
- message
195
- };
196
- return this.body(null, error, id, diy);
195
+ const error = {
196
+ code,
197
+ message
198
+ };
199
+ return this.body(null, error, id, diy);
197
200
  };
198
201
 
199
202
  /**
@@ -202,32 +205,30 @@ Ret.prototype.error = function (code, message, id, diy = true) {
202
205
  * @param {number} count 查询结果数
203
206
  * @param {string} id 消息ID
204
207
  * @param {boolean} diy 是否自定义错误提示
205
- * @returns {Object} 列表响应格式
208
+ * @returns {object} 列表响应格式
206
209
  */
207
210
  Ret.prototype.list = function (list, count, id, diy = true) {
208
- if (!list) {
209
- list = [];
210
- }
211
- const result = {
212
- list: list
213
- };
214
- if (count !== undefined) {
215
- result.count = count;
216
- }
217
- return this.body(result, null, id, diy);
211
+ var list_val = list || [];
212
+ const result = {
213
+ list: list_val
214
+ };
215
+ if (count !== undefined) {
216
+ result.count = count;
217
+ }
218
+ return this.body(result, null, id, diy);
218
219
  };
219
220
 
220
221
  /**
221
222
  * 生成对象响应
222
- * @param {Object} obj 返回对象
223
+ * @param {object} obj 返回对象
223
224
  * @param {string} id 消息ID
224
- * @returns {Object} 对象响应格式
225
+ * @returns {object} 对象响应格式
225
226
  */
226
227
  Ret.prototype.obj = function (obj, id) {
227
- const result = {
228
- obj: obj
229
- };
230
- return this.body(result, null, id);
228
+ const result = {
229
+ obj: obj
230
+ };
231
+ return this.body(result, null, id);
231
232
  };
232
233
 
233
234
  /**
@@ -235,112 +236,110 @@ Ret.prototype.obj = function (obj, id) {
235
236
  * @param {boolean} bl 布尔结果
236
237
  * @param {string} tip 提示信息
237
238
  * @param {string} id 消息ID
238
- * @returns {Object} 布尔响应格式
239
+ * @returns {object} 布尔响应格式
239
240
  */
240
- Ret.prototype.bl = function (bl, tip, id) {
241
- if (!tip) {
242
- tip = bl ? this.config.success_message : this.config.error_message;
243
- }
244
- return this.body({
245
- bl,
246
- tip
247
- }, null, id);
241
+ Ret.prototype.bl = function (bl, _tip, id) {
242
+ var tip = _tip || (bl ? this.config.success_message : this.config.error_message);
243
+ return this.body({
244
+ bl,
245
+ tip
246
+ }, null, id);
248
247
  };
249
248
 
250
249
  /**
251
250
  * 添加数据项到响应结果
252
- * @param {Object} res 响应对象
251
+ * @param {object} res 响应对象
253
252
  * @param {string} key 数据项键
254
253
  * @param {*} value 数据项值
255
254
  */
256
255
  Ret.prototype.add = function (res, key, value) {
257
- if (this.config.format === "json-rpc") {
258
- if (!res.result) {
259
- res.result = {};
260
- }
261
- res.result[key] = value;
262
- }
263
- else {
264
- if (!res.data) {
265
- res.data = {};
266
- }
267
- res.data[key] = value;
268
- }
269
- }
256
+ if (this.config.format === 'json-rpc') {
257
+ if (!res.result) {
258
+ res.result = {};
259
+ }
260
+ res.result[key] = value;
261
+ }
262
+ else {
263
+ if (!res.data) {
264
+ res.data = {};
265
+ }
266
+ res.data[key] = value;
267
+ }
268
+ };
270
269
 
271
270
  /**
272
271
  * 解析响应
273
- * @param {Object} res 响应对象
274
- * @returns {Object} 解析后的格式
272
+ * @param {object} res 响应对象
273
+ * @returns {object} 解析后的格式
275
274
  */
276
275
  Ret.prototype.parse = function (res) {
277
- let error;
278
- let result;
276
+ let error;
277
+ let result;
279
278
 
280
- if (res.error) {
281
- error = res.error;
282
- }
283
- else if (res.result) {
284
- result = res.result;
285
- } else if (res.data) {
286
- result = res.data;
287
- }
288
- else if (res.msg || res.message) {
289
- error = {
290
- code: res.code || this.config.error_code,
291
- message: res.msg || res.message || this.config.error_message
292
- };
293
- }
279
+ if (res.error) {
280
+ error = res.error;
281
+ }
282
+ else if (res.result) {
283
+ result = res.result;
284
+ } else if (res.data) {
285
+ result = res.data;
286
+ }
287
+ else if (res.msg || res.message) {
288
+ error = {
289
+ code: res.code || this.config.error_code,
290
+ message: res.msg || res.message || this.config.error_message
291
+ };
292
+ }
294
293
 
295
- const ret = {};
296
- if (this.config.format === "json-rpc") {
297
- if (error) {
298
- ret.error = error;
299
- }
300
- else if (result) {
301
- ret.result = result;
302
- }
303
- }
304
- else {
305
- if (error) {
306
- ret.code = error.code || this.config.error_code;
307
- ret.msg = error.message || this.config.error_message;
308
- ret.data = null;
309
- }
310
- else if (result) {
311
- ret.code = this.config.success_code;
312
- ret.msg = this._getMessage(this.config.success_code);
313
- ret.data = result;
314
- }
315
- else {
316
- ret.code = this.config.success_code;
317
- ret.msg = this._getMessage(this.config.success_code);
318
- }
319
- }
320
- return ret;
321
- }
294
+ const ret = {};
295
+ if (this.config.format === 'json-rpc') {
296
+ if (error) {
297
+ ret.error = error;
298
+ }
299
+ else if (result) {
300
+ ret.result = result;
301
+ }
302
+ }
303
+ else {
304
+ if (error) {
305
+ ret.code = error.code || this.config.error_code;
306
+ ret.msg = error.message || this.config.error_message;
307
+ ret.data = null;
308
+ }
309
+ else if (result) {
310
+ ret.code = this.config.success_code;
311
+ ret.msg = this._getMessage(this.config.success_code);
312
+ ret.data = result;
313
+ }
314
+ else {
315
+ ret.code = this.config.success_code;
316
+ ret.msg = this._getMessage(this.config.success_code);
317
+ }
318
+ }
319
+ return ret;
320
+ };
322
321
 
323
322
  /**
324
323
  * 设置响应格式
325
324
  * @param {string} format 响应格式
326
325
  */
327
326
  Ret.prototype.setFormat = function (format) {
328
- switch (format?.toLowerCase()) {
329
- case "json":
330
- case "json-rpc":
331
- this.config.format = "json-rpc";
332
- break;
333
- case "json3":
334
- case "json-rpc3":
335
- this.config.format = "json-rpc3";
336
- break;
337
- default:
338
- this.config.format = "restful";
339
- break;
340
- }
341
- return this;
327
+ switch (format?.toLowerCase()) {
328
+ case 'json':
329
+ case 'json-rpc':
330
+ this.config.format = 'json-rpc';
331
+ break;
332
+ case 'json3':
333
+ case 'json-rpc3':
334
+ this.config.format = 'json-rpc3';
335
+ break;
336
+ default:
337
+ this.config.format = 'restful';
338
+ break;
339
+ }
340
+ return this;
342
341
  };
343
342
 
344
343
  module.exports = {
345
- Ret
344
+ Ret
346
345
  };