mm_ret 1.4.1 → 1.4.3

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 ADDED
@@ -0,0 +1,352 @@
1
+ require('mm_expand');
2
+ /**
3
+ * @file 响应管理类
4
+ * @class Ret
5
+ */
6
+ class Ret {
7
+ /**
8
+ * @description 构造函数
9
+ * @param {Object} config 配置项
10
+ * @constructor
11
+ */
12
+ constructor(config) {
13
+ this._config = {
14
+ /**
15
+ * @description 响应格式
16
+ * @type {string} "json-rpc" | "restful"
17
+ * @default "json-rpc"
18
+ */
19
+ format: "json-rpc",
20
+ /**
21
+ * @description 默认错误码
22
+ * @type {number}
23
+ * @default 10000
24
+ */
25
+ error_code: 10000,
26
+ /**
27
+ * @description 默认错误提示
28
+ * @type {string}
29
+ * @default "错误"
30
+ */
31
+ error_message: "错误",
32
+ /**
33
+ * @description 默认成功码
34
+ * @type {number}
35
+ * @default 0
36
+ */
37
+ success_code: 0,
38
+ /**
39
+ * @description 默认成功提示
40
+ * @type {string}
41
+ * @default "成功"
42
+ */
43
+ success_message: "成功",
44
+ /**
45
+ * @description
46
+ * @type {boolean}
47
+ * @default false
48
+ */
49
+ message_diy: false
50
+ };
51
+
52
+ this.setConfig(config);
53
+ }
54
+ }
55
+
56
+ /**
57
+ * @description 设置配置项
58
+ * @param {Object} config 配置项
59
+ */
60
+ Ret.prototype.setConfig = function (config) {
61
+ Object.assign(this._config, config);
62
+ }
63
+
64
+ /**
65
+ * @description 获取错误提示
66
+ * @param {number} code 错误码
67
+ * @returns {string} 错误提示
68
+ */
69
+ Ret.prototype._getMessage = function (code) {
70
+ var message;
71
+ if ($.error) {
72
+ message = $.error.get(code);
73
+ }
74
+
75
+ if (!message) {
76
+ if (code === this._config.success_code) {
77
+ message = this._config.success_message;
78
+ }
79
+ else {
80
+ message = this._config.error_message;
81
+ }
82
+ }
83
+ return message;
84
+ }
85
+
86
+ /**
87
+ * @description 生成JSON-RPC 2.0响应格式
88
+ * @param {Object} result 返回的结果
89
+ * @param {Object} error 返回的错误信息
90
+ * @param {Object.code} error.code 返回的错误码
91
+ * @param {Object.message} error.message 返回的错误提示
92
+ * @param {string} id 消息ID
93
+ * @param {boolean} diy 是否自定义错误提示
94
+ * @returns {Object} JSON-RPC 2.0响应格式
95
+ */
96
+ Ret.prototype._toJsonRPC = function (result, error, id, diy = false) {
97
+ const ret = {
98
+ id
99
+ };
100
+
101
+ if (result) {
102
+ ret.result = result;
103
+ }
104
+ else if (error) {
105
+ if (!error.code) {
106
+ error.code = this._config.error_code;
107
+ }
108
+ diy = diy || this._config.message_diy;
109
+ if (!diy) {
110
+ error.message = this._getMessage(error.code) || error.message;
111
+ }
112
+ else if (!error.message) {
113
+ error.message = this._getMessage(error.code);
114
+ }
115
+ ret.error = error;
116
+ }
117
+ else {
118
+ ret.error = {
119
+ code: this._config.error_code,
120
+ message: this._getMessage(this._config.error_code)
121
+ };
122
+ }
123
+ return ret;
124
+ }
125
+
126
+ /**
127
+ * @description 生成RESTful响应格式
128
+ * @param {*} result 返回的结果
129
+ * @param {Object} error 返回的错误信息
130
+ * @param {Object.code} error.code 返回的错误码
131
+ * @param {Object.message} error.message 返回的错误提示
132
+ * @param {string} id 消息ID
133
+ * @param {boolean} diy 是否自定义错误提示
134
+ * @returns {Object} RESTful响应格式
135
+ */
136
+ Ret.prototype._toRESTful = function (result, error, id, diy = false) {
137
+ var code;
138
+ var msg;
139
+ if (result) {
140
+ code = this._config.success_code;
141
+ msg = this._getMessage(this._config.success_code);
142
+ }
143
+ else if (error) {
144
+ code = error.code || this._config.error_code;
145
+ diy = diy || this._config.message_diy;
146
+ if (!diy) {
147
+ msg = this._getMessage(code) || error.message || error.msg;
148
+ }
149
+ else if (!error.message && !error.msg) {
150
+ msg = this._getMessage(code);
151
+ }
152
+ else {
153
+ msg = error.message || error.msg || this._getMessage(code);
154
+ }
155
+ }
156
+ else {
157
+ code = this._config.error_code;
158
+ msg = this._getMessage(code);
159
+ }
160
+ return {
161
+ id,
162
+ code,
163
+ msg,
164
+ data: result
165
+ };
166
+ }
167
+
168
+ /**
169
+ * @description 生成响应体
170
+ * @param {Object} result 返回的结果
171
+ * @param {Object} error 返回的错误信息
172
+ * @param {string} id 消息ID
173
+ * @param {boolean} diy 是否自定义错误提示
174
+ * @returns {Object} 响应格式
175
+ */
176
+ Ret.prototype.body = function (result, error, id, diy = false) {
177
+ if (this._config.format === "json-rpc") {
178
+ return this._toJsonRPC(result, error, id, diy);
179
+ }
180
+ else {
181
+ return this._toRESTful(result, error, id, diy);
182
+ }
183
+ };
184
+
185
+ /**
186
+ * @description 生成错误响应
187
+ * @param {number} code 错误码
188
+ * @param {string} message 错误信息
189
+ * @param {string} id 消息ID
190
+ * @param {boolean} diy 是否自定义错误提示
191
+ * @returns {Object} 错误响应格式
192
+ */
193
+ Ret.prototype.error = function (code, message, id, diy = false) {
194
+ const error = {
195
+ code,
196
+ message
197
+ };
198
+ return this.body(null, error, id, diy);
199
+ };
200
+
201
+ /**
202
+ * @description 生成列表响应
203
+ * @param {Array} list 返回列表
204
+ * @param {number} count 查询结果数
205
+ * @param {string} id 消息ID
206
+ * @param {boolean} diy 是否自定义错误提示
207
+ * @returns {Object} 列表响应格式
208
+ */
209
+ Ret.prototype.list = function (list, count, id, diy = false) {
210
+ if (!list) {
211
+ list = [];
212
+ }
213
+ const result = {
214
+ list: list
215
+ };
216
+ if (count !== undefined) {
217
+ result.count = count;
218
+ }
219
+ return this.body(result, null, id, diy);
220
+ };
221
+
222
+ /**
223
+ * @description 生成对象响应
224
+ * @param {Object} obj 返回对象
225
+ * @param {string} id 消息ID
226
+ * @returns {Object} 对象响应格式
227
+ */
228
+ Ret.prototype.obj = function (obj, id) {
229
+ return this.body(obj, null, id);
230
+ };
231
+
232
+ /**
233
+ * @description 生成布尔型响应
234
+ * @param {boolean} bl 布尔结果
235
+ * @param {string} tip 提示信息
236
+ * @param {string} id 消息ID
237
+ * @returns {Object} 布尔响应格式
238
+ */
239
+ Ret.prototype.bl = function (bl, tip, id) {
240
+ if (!tip) {
241
+ tip = bl ? this._config.success_message : this._config.error_message;
242
+ }
243
+ return this.body({
244
+ bl,
245
+ tip
246
+ }, null, id);
247
+ };
248
+
249
+ /**
250
+ * @description 添加数据项到响应结果
251
+ * @param {Object} res 响应对象
252
+ * @param {string} key 数据项键
253
+ * @param {*} value 数据项值
254
+ */
255
+ Ret.prototype.add = function (res, key, value) {
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
+ }
269
+
270
+ /**
271
+ * @description 解析响应
272
+ * @param {Object} res 响应对象
273
+ * @returns {Object} 解析后的格式
274
+ */
275
+ Ret.prototype.parse = function (res) {
276
+ let error;
277
+ let result;
278
+
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
+ }
293
+
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
+ }
321
+
322
+ /**
323
+ * @description 获取当前配置
324
+ * @returns {Object} 配置对象
325
+ */
326
+ Ret.prototype.getConfig = function () {
327
+ return this._config;
328
+ };
329
+
330
+ /**
331
+ * @description 设置响应格式
332
+ * @param {string} format 响应格式
333
+ */
334
+ Ret.prototype.setFormat = function (format) {
335
+ switch (format?.toLowerCase()) {
336
+ case "json":
337
+ case "json-rpc":
338
+ this._config.format = "json-rpc";
339
+ break;
340
+ case "json3":
341
+ case "json-rpc3":
342
+ this._config.format = "json-rpc3";
343
+ break;
344
+ default:
345
+ this._config.format = "restful";
346
+ break;
347
+ }
348
+ }
349
+
350
+ module.exports = {
351
+ Ret
352
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_ret",
3
- "version": "1.4.1",
3
+ "version": "1.4.3",
4
4
  "description": "这是超级美眉http请求结果输出类函数模块,用于输出json-rpc2.0标准结果",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -30,6 +30,6 @@
30
30
  },
31
31
  "homepage": "https://gitee.com/qiuwenwu91/mm_ret#readme",
32
32
  "dependencies": {
33
- "mm_expand": "^1.8.3"
33
+ "mm_expand": "^1.9.6"
34
34
  }
35
35
  }
package/test.js CHANGED
@@ -1,36 +1,104 @@
1
- require('./index.js');
1
+ const { Ret, Req } = require('./index.js');
2
2
 
3
3
  /* 调用示例 */
4
- async function test() {
4
+ async function retTest(out) {
5
+ if (!out) {
6
+ out = $.ret;
7
+ }
5
8
  var body;
6
- body = $.ret.error(10000, '错误');
7
- console.log(JSON.stringify(body));
8
-
9
+ body = $.ret.error(10000, "这是一个错误", null, true);
10
+ $.log.debug("封装:", JSON.stringify(body));
11
+ $.log.debug("解析结果:", out.parse(body));
12
+
9
13
  body = $.ret.body("这是一个结果");
10
- console.log(JSON.stringify(body));
11
-
12
- body = $.ret.obj({ "name": "张三", age: 18});
13
- console.log(JSON.stringify(body));
14
-
15
- body = $.ret.list([{ "name": "张三", age: 18}, { "name": "李四", age: 24}]);
16
- console.log(JSON.stringify(body));
17
-
14
+ $.log.debug("封装:", JSON.stringify(body));
15
+ $.log.debug("解析结果:", out.parse(body));
16
+
17
+ body = $.ret.obj({ "name": "张三", age: 18 });
18
+ $.log.debug("封装:", JSON.stringify(body));
19
+ $.log.debug("解析结果:", out.parse(body));
20
+
21
+ body = $.ret.list([{ "name": "张三", age: 18 }, { "name": "李四", age: 24 }], 35);
22
+ $.log.debug("封装:", JSON.stringify(body));
23
+ $.log.debug("解析结果:", out.parse(body));
24
+
18
25
  body = $.ret.bl(true, "修改成功");
19
- console.log(JSON.stringify(body));
26
+ $.log.debug("封装:", JSON.stringify(body));
27
+ $.log.debug("解析结果:", out.parse(body));
20
28
  }
21
29
 
22
- test();
23
-
24
- async function test2() {
25
- var body = $.req.send('test', "你好吗");
26
- console.log(body);
27
-
28
- $.req.tpl.message = {
29
- "to_user": "",
30
- "from_user": "",
31
- "content": ""
32
- };
33
- body = $.req.send('message', { content: "你好吗?", "media": { "title": "牛" } });
34
- console.log(body);
30
+ function retDemo() {
31
+ $.log.debug("=== 测试JSON-RPC风格 进出 === ");
32
+ retTest();
33
+
34
+ $.log.debug("=== 测试RESTful风格 进出 === ");
35
+ $.ret.setFormat("restful");
36
+ retTest();
37
+
38
+ $.log.debug("=== 测试RESTful风格 进,JSON-RPC风格 出 === ");
39
+ retTest(new Ret());
40
+
41
+ $.log.debug("=== 测试JSON-RPC风格 进,RESTful风格 === ");
42
+ $.ret.setFormat("json-rpc");
43
+ retTest(new Ret({ format: "restful" }));
35
44
  }
36
- test2();
45
+
46
+ retDemo();
47
+
48
+ /* 调用示例 */
49
+ async function reqTest(out) {
50
+ if (!out) {
51
+ out = $.req;
52
+ }
53
+ var body;
54
+ body = $.req.send("test_method", "param1", "param2", "param3");
55
+ $.log.debug("封装:", JSON.stringify(body));
56
+ $.log.debug("解析结果:", out.parse(body));
57
+
58
+ body = $.req.send("set", {
59
+ user_id: 1
60
+ }, {
61
+ "name": "张三",
62
+ "age": 18
63
+ }, {
64
+ "token": "xxxxx"
65
+ });
66
+ $.log.debug("封装:", JSON.stringify(body));
67
+ $.log.debug("解析结果:", out.parse(body));
68
+ }
69
+
70
+ function reqDemo() {
71
+ $.log.debug("=== 测试JSON-RPC风格请求 进出 === ");
72
+ reqTest();
73
+
74
+ $.log.debug("=== 测试JSON-RPC3风格请求 进出 === ");
75
+ $.req.setFormat("json-rpc3");
76
+ reqTest();
77
+
78
+ $.log.debug("=== 测试RESTful风格请求 进出 === ");
79
+ $.req.setFormat("restful");
80
+ reqTest();
81
+
82
+ $.log.debug("=== 测试RESTful风格请求 进,JSON-RPC风格请求 出 === ");
83
+ reqTest(new Req());
84
+
85
+ $.log.debug("=== 测试RESTful风格请求 进,JSON-RPC3风格请求 出 === ");
86
+ reqTest(new Req({ format: "json-rpc3" }));
87
+
88
+ $.log.debug("=== 测试JSON-RPC风格请求 进,RESTful风格请求 出 === ");
89
+ $.req.setFormat("json-rpc");
90
+ reqTest(new Req({ format: "restful" }));
91
+ }
92
+
93
+ reqDemo();
94
+
95
+ // var reqCS = new Req();
96
+ // const request = reqCS.send('getProfile', { userId: 123 });
97
+ // $.log.debug("封装:", JSON.stringify(request));
98
+
99
+ // var model = reqCS.parse({
100
+ // id: "1231233",
101
+ // method: 'getProfile',
102
+ // params: [{ userId: 123 }]
103
+ // });
104
+ // $.log.debug("解析:", model);