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/error.js ADDED
@@ -0,0 +1,163 @@
1
+ /**
2
+ * @file 错误代码管理类
3
+ * @class Error
4
+ */
5
+ class Error {
6
+ /**
7
+ * @description 构造函数
8
+ * @constructor
9
+ * @param {Object} config 配置项
10
+ */
11
+ constructor(config) {
12
+ /**
13
+ * @description 错误码配置项
14
+ */
15
+ this._config = {
16
+ lang: "zh-CN"
17
+ };
18
+ this.setConfig(config);
19
+ /**
20
+ * @description 错误码语言包
21
+ */
22
+ this._langs = {
23
+ "zh-CN": {
24
+ "10000": "业务逻辑错误",
25
+ "0": "成功",
26
+ "‌100": "继续",
27
+ "‌101": "切换协议‌",
28
+ "‌200": "确定",
29
+ "‌201": "已创建",
30
+ "‌202": "已接受",
31
+ "‌204": "无内容",
32
+ "‌301": "已永久移动",
33
+ "‌302": "已找到",
34
+ "‌303": "见其他",
35
+ "‌304": "未修改",
36
+ "‌307": "临时重定向‌",
37
+ "‌400": "错误请求",
38
+ "‌401": "未授权",
39
+ "‌403": "禁止访问",
40
+ "‌404": "未找到",
41
+ "‌405": "方法不允许",
42
+ "‌408": "请求超时",
43
+ "‌413": "Payload Too Large",
44
+ "‌415": "不支持的媒体类型",
45
+ "‌429": "请求过多",
46
+ "‌500": "内部服务器错误",
47
+ "‌501": "未实现",
48
+ "‌502": "错误网关",
49
+ "‌503": "服务不可用",
50
+ "‌504": "网关超时",
51
+ "-32700": "格式错误",
52
+ "-32701": "不支持的编码",
53
+ "-32702": "编码中包含无效字符",
54
+ "-32600": "无效的json-rpc. 不符合规范",
55
+ "-32601": "请求的方法未找到",
56
+ "-32602": "无效的方法参数",
57
+ "-32603": "内部json-rpc错误",
58
+ "-32500": "应用错误",
59
+ "-32400": "系统错误",
60
+ "-32300": "传输错误",
61
+ "30000": "身份验证失败",
62
+ "40000": "数据库执行错误",
63
+ "50000": "服务端执行错误",
64
+ "70000": "参数不正确"
65
+ },
66
+ "en-US": {
67
+ "10000": "business logic error",
68
+ "0": "success",
69
+ "‌100": "Continue",
70
+ "‌101": "Switching Protocols‌",
71
+ "‌200": "OK",
72
+ "‌201": "Created",
73
+ "‌202": "Accepted",
74
+ "‌204": "No Content",
75
+ "‌301": "Moved Permanently",
76
+ "‌302": "Found",
77
+ "‌303": "See Other",
78
+ "‌304": "Not Modified‌",
79
+ "‌307": "Temporary Redirect‌",
80
+ "‌400": "Bad Request",
81
+ "‌401": "Unauthorized",
82
+ "‌403": "Forbidden",
83
+ "‌404": "Not Found",
84
+ "‌405": "Method Not Allowed",
85
+ "‌408": "Request Timeout",
86
+ "‌413": "Payload Too Large",
87
+ "‌415": "Unsupported Media Type",
88
+ "‌429": "Too Many Requests‌",
89
+ "‌500": "Internal Server Error",
90
+ "‌501": "Not Implemented",
91
+ "‌502": "Bad Gateway",
92
+ "‌503": "Service Unavailable",
93
+ "‌504": "Gateway Timeout",
94
+ "-32700": "not well formed",
95
+ "-32701": "unsupported encoding",
96
+ "-32702": "invalid character for encoding",
97
+ "-32600": "invalid json-rpc. not conforming to spec",
98
+ "-32601": "requested method not found",
99
+ "-32602": "invalid method parameters",
100
+ "-32603": "internal json-rpc error",
101
+ "-32500": "application error",
102
+ "-32400": "system error",
103
+ "-32300": "transport error",
104
+ "30000": "authentication failed",
105
+ "40000": "database execution error",
106
+ "50000": "server execution error",
107
+ "70000": "parameter is incorrect"
108
+ }
109
+ };
110
+ }
111
+ }
112
+
113
+ /**
114
+ * @description 设置配置项
115
+ * @param {Object} config 配置项
116
+ */
117
+ Error.prototype.setConfig = function (config) {
118
+ Object.assign(this._config, config);
119
+ }
120
+
121
+ /**
122
+ * @description 根据错误码获取错误信息
123
+ * @param {string|number} code 错误码
124
+ * @returns {string|null} 错误信息
125
+ */
126
+ Error.prototype.get = function (code) {
127
+ const lang = this._config.lang;
128
+ const lang_pack = this._langs[lang];
129
+
130
+ if (lang_pack && lang_pack[code]) {
131
+ return lang_pack[code];
132
+ }
133
+
134
+ // 如果当前语言包没有,尝试英文包
135
+ if (lang !== "en-US" && this._langs["en-US"][code]) {
136
+ return this._langs["en-US"][code];
137
+ }
138
+
139
+ return null;
140
+ };
141
+
142
+ /**
143
+ * @description 设置语言
144
+ * @param {string} lang 语言代码
145
+ */
146
+ Error.prototype.setLang = function (lang) {
147
+ this._config.lang = lang;
148
+ };
149
+
150
+ /**
151
+ * @description 获取当前语言
152
+ * @returns {string} 当前语言代码
153
+ */
154
+ Error.prototype.getLang = function () {
155
+ return this._config.lang;
156
+ };
157
+
158
+ /**
159
+ * @description 导出错误管理类
160
+ */
161
+ module.exports = {
162
+ Error
163
+ }
package/lib/req.js ADDED
@@ -0,0 +1,241 @@
1
+ /**
2
+ * @file 请求管理类
3
+ * @class Req
4
+ */
5
+ class Req {
6
+ /**
7
+ * @description 构造函数
8
+ * @constructor
9
+ * @param {Object} config 配置项
10
+ */
11
+ constructor(config) {
12
+ // 配置项
13
+ this._config = {
14
+ /**
15
+ * @description 作用域
16
+ * @type {string}
17
+ * @default "sys"
18
+ */
19
+ scope: "sys",
20
+ /**
21
+ * @description 响应格式
22
+ * @type {string} "json-rpc" | "json-rpc3" | "restful"
23
+ * @default "json-rpc"
24
+ */
25
+ format: "json-rpc"
26
+ };
27
+ this.setConfig(config);
28
+
29
+ // 方法集合
30
+ this._methods = {};
31
+ }
32
+ }
33
+
34
+ /**
35
+ * @description 设置配置项
36
+ * @param {Object} config 配置项
37
+ */
38
+ Req.prototype.setConfig = function (config) {
39
+ Object.assign(this._config, config);
40
+ this._config.format = this._config.format?.toLowerCase() || "json-rpc";
41
+ }
42
+
43
+ /**
44
+ * @description 生成新的ID
45
+ * @returns {string} 新的ID
46
+ */
47
+ Req.prototype.newId = function () {
48
+ return this._config.scope + Date.now();
49
+ }
50
+
51
+ /**
52
+ * @description 发送请求
53
+ * @param {string} method 方法名
54
+ * @param {...any} args 参数
55
+ * @returns {Object} 发送的JSON格式
56
+ */
57
+ Req.prototype.send = function (method, ...args) {
58
+ if (this._methods[method]) {
59
+ return this._methods[method](...args);
60
+ }
61
+ else if (this._config.format === "json-rpc") {
62
+ return this._sendJsonRPC(method, args);
63
+ }
64
+ else if (this._config.format === "json-rpc3") {
65
+ return this._sendJsonRPC3(method, ...args);
66
+ }
67
+ else {
68
+ return this._sendRESTful(method, ...args);
69
+ }
70
+ };
71
+
72
+ /**
73
+ * @description 发送JSON-RPC 2.0请求
74
+ * @param {string} method 方法名
75
+ * @param {Array} params 参数数组
76
+ * @param {string} [id] 请求ID
77
+ * @returns {Object} JSON-RPC 2.0格式
78
+ */
79
+ Req.prototype._sendJsonRPC = function (method, params, id) {
80
+ return {
81
+ id: id || this.newId(),
82
+ method,
83
+ params
84
+ };
85
+ }
86
+
87
+ /**
88
+ * @description 发送JSON-RPC 3.0请求
89
+ * @param {string} method 方法名
90
+ * @param {Object} [query] 查询参数
91
+ * @param {Object} [body] 请求体
92
+ * @param {Array} [meta] 元数据数组
93
+ * @param {string} [id] 请求ID
94
+ * @returns {Object} JSON-RPC 3.0格式
95
+ */
96
+ Req.prototype._sendJsonRPC3 = function (method, query, body, meta, id) {
97
+ return {
98
+ id: id || this.newId(),
99
+ method,
100
+ params: {
101
+ query: query || {},
102
+ body: body || {},
103
+ meta: meta || []
104
+ }
105
+ };
106
+ }
107
+
108
+ /**
109
+ * @description 发送RESTful请求
110
+ * @param {string} method 方法名
111
+ * @param {Object} [query] 查询参数
112
+ * @param {Object} [body] 请求体
113
+ * @param {Array} [meta] 元数据数组
114
+ * @param {string} [id] 请求ID
115
+ * @returns {Object} RESTful格式
116
+ */
117
+ Req.prototype._sendRESTful = function (method, query, body, meta, id) {
118
+ return {
119
+ id: id || this.newId(),
120
+ method,
121
+ query: query || {},
122
+ body: body || {},
123
+ meta: meta || []
124
+ };
125
+ }
126
+
127
+ /**
128
+ * @description 解析请求
129
+ * @param {Object} req 请求对象
130
+ * @returns {Object} 解析后的对象
131
+ */
132
+ Req.prototype.parse = function (req) {
133
+ const id = req.id;
134
+ const method = req.method;
135
+ let params = [];
136
+
137
+ if (req.params) {
138
+ if (Array.isArray(req.params)) {
139
+ params = req.params;
140
+ }
141
+ else {
142
+ if (typeof req.params === "object") {
143
+ let ctx = Object.assign({}, req.params);
144
+ if (ctx.query || ctx.body) {
145
+ params.push(ctx.query || null);
146
+ params.push(ctx.body || null);
147
+ delete ctx.query;
148
+ delete ctx.body;
149
+ for (const key in ctx) {
150
+ params.push(ctx[key]);
151
+ }
152
+ }
153
+ else {
154
+ params = [
155
+ req.params
156
+ ];
157
+ }
158
+ }
159
+ else {
160
+ params = [req.params];
161
+ }
162
+ }
163
+ }
164
+ else if (req.query || req.body) {
165
+ params.push(req.query || null);
166
+ params.push(req.body || null);
167
+ if (req.meta) {
168
+ params.push(req.meta || null);
169
+ }
170
+ }
171
+ if (this._config.format === "json-rpc") {
172
+ if (req.params) {
173
+ params = req.params;
174
+ }
175
+ }
176
+ else if (this._config.format === "json-rpc3") {
177
+ var pm = {};
178
+ pm.query = params.length > 0 ? params[0] || null : null;
179
+ pm.body = params.length > 1 ? params[1] || null : null;
180
+ pm.meta = params.length > 2 ? params[2] || null : null;
181
+ params = pm;
182
+ }
183
+ else if (this._config.format === "restful") {
184
+ var ret = {
185
+ id,
186
+ method,
187
+ query: params.length > 0 ? params[0] || null : null,
188
+ body: params.length > 1 ? params[1] || null : null
189
+ }
190
+ if (params.length > 2) {
191
+ ret.meta = params[2];
192
+ }
193
+ return ret;
194
+ }
195
+ return {
196
+ id,
197
+ method,
198
+ params
199
+ };
200
+ }
201
+
202
+ /**
203
+ * @description 注册自定义方法
204
+ * @param {string} method 方法名
205
+ * @param {Function} handler 处理方法
206
+ */
207
+ Req.prototype.registerMethod = function (method, handler) {
208
+ this._methods[method] = handler;
209
+ };
210
+
211
+ /**
212
+ * @description 注销自定义方法
213
+ * @param {string} method 方法名
214
+ */
215
+ Req.prototype.unregisterMethod = function (method) {
216
+ delete this._methods[method];
217
+ };
218
+
219
+ /**
220
+ * @description 设置请求格式
221
+ * @param {string} format 请求格式
222
+ */
223
+ Req.prototype.setFormat = function (format) {
224
+ switch (format?.toLowerCase()) {
225
+ case "json":
226
+ case "json-rpc":
227
+ this._config.format = "json-rpc";
228
+ break;
229
+ case "json3":
230
+ case "json-rpc3":
231
+ this._config.format = "json-rpc3";
232
+ break;
233
+ default:
234
+ this._config.format = "restful";
235
+ break;
236
+ }
237
+ }
238
+
239
+ module.exports = {
240
+ Req
241
+ };