befly 2.3.3 → 3.0.0

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.
Files changed (93) hide show
  1. package/apis/health/info.ts +64 -0
  2. package/apis/tool/tokenCheck.ts +51 -0
  3. package/bin/befly.ts +202 -0
  4. package/checks/conflict.ts +408 -0
  5. package/checks/{table.js → table.ts} +139 -61
  6. package/config/env.ts +218 -0
  7. package/config/reserved.ts +96 -0
  8. package/main.ts +101 -0
  9. package/package.json +44 -8
  10. package/plugins/{db.js → db.ts} +24 -11
  11. package/plugins/logger.ts +28 -0
  12. package/plugins/redis.ts +51 -0
  13. package/plugins/tool.ts +34 -0
  14. package/scripts/syncDb/apply.ts +171 -0
  15. package/scripts/syncDb/constants.ts +70 -0
  16. package/scripts/syncDb/ddl.ts +182 -0
  17. package/scripts/syncDb/helpers.ts +172 -0
  18. package/scripts/syncDb/index.ts +215 -0
  19. package/scripts/syncDb/schema.ts +199 -0
  20. package/scripts/syncDb/sqlite.ts +50 -0
  21. package/scripts/syncDb/state.ts +104 -0
  22. package/scripts/syncDb/table.ts +204 -0
  23. package/scripts/syncDb/tableCreate.ts +142 -0
  24. package/scripts/syncDb/tests/constants.test.ts +104 -0
  25. package/scripts/syncDb/tests/ddl.test.ts +134 -0
  26. package/scripts/syncDb/tests/helpers.test.ts +70 -0
  27. package/scripts/syncDb/types.ts +92 -0
  28. package/scripts/syncDb/version.ts +73 -0
  29. package/scripts/syncDb.ts +9 -0
  30. package/scripts/{syncDev.js → syncDev.ts} +41 -25
  31. package/system.ts +149 -0
  32. package/tables/_common.json +21 -0
  33. package/tables/admin.json +10 -0
  34. package/tsconfig.json +58 -0
  35. package/types/api.d.ts +246 -0
  36. package/types/befly.d.ts +234 -0
  37. package/types/common.d.ts +215 -0
  38. package/types/context.ts +167 -0
  39. package/types/crypto.d.ts +23 -0
  40. package/types/database.d.ts +278 -0
  41. package/types/index.d.ts +16 -0
  42. package/types/index.ts +459 -0
  43. package/types/jwt.d.ts +99 -0
  44. package/types/logger.d.ts +43 -0
  45. package/types/plugin.d.ts +109 -0
  46. package/types/redis.d.ts +44 -0
  47. package/types/tool.d.ts +67 -0
  48. package/types/validator.d.ts +45 -0
  49. package/utils/addonHelper.ts +60 -0
  50. package/utils/api.ts +23 -0
  51. package/utils/{colors.js → colors.ts} +79 -21
  52. package/utils/crypto.ts +308 -0
  53. package/utils/datetime.ts +51 -0
  54. package/utils/dbHelper.ts +142 -0
  55. package/utils/errorHandler.ts +68 -0
  56. package/utils/index.ts +46 -0
  57. package/utils/jwt.ts +493 -0
  58. package/utils/logger.ts +284 -0
  59. package/utils/objectHelper.ts +68 -0
  60. package/utils/pluginHelper.ts +62 -0
  61. package/utils/redisHelper.ts +338 -0
  62. package/utils/response.ts +38 -0
  63. package/utils/{sqlBuilder.js → sqlBuilder.ts} +233 -97
  64. package/utils/sqlHelper.ts +447 -0
  65. package/utils/tableHelper.ts +167 -0
  66. package/utils/tool.ts +230 -0
  67. package/utils/typeHelper.ts +101 -0
  68. package/utils/validate.ts +451 -0
  69. package/utils/{xml.js → xml.ts} +100 -74
  70. package/.npmrc +0 -3
  71. package/.prettierignore +0 -2
  72. package/.prettierrc +0 -11
  73. package/apis/health/info.js +0 -49
  74. package/apis/tool/tokenCheck.js +0 -29
  75. package/bin/befly.js +0 -109
  76. package/config/env.js +0 -64
  77. package/main.js +0 -579
  78. package/plugins/logger.js +0 -14
  79. package/plugins/redis.js +0 -32
  80. package/plugins/tool.js +0 -8
  81. package/scripts/syncDb.js +0 -752
  82. package/system.js +0 -118
  83. package/tables/common.json +0 -16
  84. package/tables/tool.json +0 -6
  85. package/utils/api.js +0 -27
  86. package/utils/crypto.js +0 -260
  87. package/utils/index.js +0 -334
  88. package/utils/jwt.js +0 -387
  89. package/utils/logger.js +0 -143
  90. package/utils/redisHelper.js +0 -74
  91. package/utils/sqlManager.js +0 -471
  92. package/utils/tool.js +0 -31
  93. package/utils/validate.js +0 -226
package/utils/validate.js DELETED
@@ -1,226 +0,0 @@
1
- import { isType, parseRule } from './index.js';
2
-
3
- /**
4
- * 验证器类
5
- */
6
- export class Validator {
7
- /**
8
- * 验证数据
9
- * @param {Object} data - 要验证的数据对象
10
- * @param {Object} rules - 验证规则对象
11
- * @param {Array} required - 必传字段数组
12
- * @returns {Object} { code: 0|1, fields: {} }
13
- */
14
- validate(data, rules, required = []) {
15
- const result = {
16
- code: 0,
17
- fields: {}
18
- };
19
-
20
- // 参数检查
21
- if (!this.checkParams(data, rules, required, result)) {
22
- return result;
23
- }
24
-
25
- // 检查必传字段
26
- this.checkRequiredFields(data, rules, required, result);
27
-
28
- // 验证所有在规则中定义的字段
29
- this.validateFields(data, rules, required, result);
30
-
31
- return result;
32
- }
33
-
34
- /**
35
- * 检查参数有效性
36
- */
37
- checkParams(data, rules, required, result) {
38
- if (!data || typeof data !== 'object') {
39
- result.code = 1;
40
- result.fields.error = '数据必须是对象格式';
41
- return false;
42
- }
43
-
44
- if (!rules || typeof rules !== 'object') {
45
- result.code = 1;
46
- result.fields.error = '验证规则必须是对象格式';
47
- return false;
48
- }
49
-
50
- if (!Array.isArray(required)) {
51
- result.code = 1;
52
- result.fields.error = '必传字段必须是数组格式';
53
- return false;
54
- }
55
-
56
- return true;
57
- }
58
-
59
- /**
60
- * 检查必传字段
61
- */
62
- checkRequiredFields(data, rules, required, result) {
63
- for (const fieldName of required) {
64
- if (!(fieldName in data) || data[fieldName] === undefined || data[fieldName] === null || data[fieldName] === '') {
65
- result.code = 1;
66
- const ruleParts = parseRule(rules[fieldName] || '');
67
- const fieldLabel = (ruleParts && ruleParts[0]) || fieldName;
68
- result.fields[fieldName] = `${fieldLabel}(${fieldName})为必填项`;
69
- }
70
- }
71
- }
72
-
73
- /**
74
- * 验证所有字段
75
- */
76
- validateFields(data, rules, required, result) {
77
- for (const [fieldName, rule] of Object.entries(rules)) {
78
- // 如果字段不存在且不是必传字段,跳过验证
79
- if (!(fieldName in data) && !required.includes(fieldName)) {
80
- continue;
81
- }
82
-
83
- // 如果必传验证已经失败,跳过后续验证
84
- if (result.fields[fieldName]) {
85
- continue;
86
- }
87
-
88
- const value = data[fieldName];
89
- const error = this.validateFieldValue(value, rule, fieldName);
90
-
91
- if (error) {
92
- result.code = 1;
93
- result.fields[fieldName] = error;
94
- }
95
- }
96
- }
97
-
98
- /**
99
- * 验证单个字段的值
100
- */
101
- validateFieldValue(value, rule, fieldName) {
102
- const [name, type, minRaw, maxRaw, defaultValue, isIndexRaw, regexConstraint] = parseRule(rule);
103
- const min = minRaw === 'null' ? null : /** @type {number} */ (typeof minRaw === 'number' ? minRaw : Number(minRaw));
104
- const max = maxRaw === 'null' ? null : /** @type {number} */ (typeof maxRaw === 'number' ? maxRaw : Number(maxRaw));
105
- const spec = regexConstraint === 'null' ? null : String(regexConstraint).trim();
106
-
107
- switch (type.toLowerCase()) {
108
- case 'number':
109
- return this.validateNumber(value, name, min, max, spec, fieldName);
110
- case 'string':
111
- return this.validateString(value, name, min, max, spec, fieldName);
112
- case 'text':
113
- return this.validateString(value, name, min, max, spec, fieldName);
114
- case 'array':
115
- return this.validateArray(value, name, min, max, spec, fieldName);
116
- default:
117
- return `字段 ${fieldName} 的类型 ${type} 不支持`;
118
- }
119
- }
120
-
121
- /**
122
- * 验证数字类型
123
- */
124
- validateNumber(value, name, min, max, spec, fieldName) {
125
- try {
126
- if (isType(value, 'number') === false) {
127
- return `${name}(${fieldName})必须是数字`;
128
- }
129
-
130
- if (min !== null && value < min) {
131
- return `${name}(${fieldName})不能小于${min}`;
132
- }
133
-
134
- if (max !== null && max > 0 && value > max) {
135
- return `${name}(${fieldName})不能大于${max}`;
136
- }
137
-
138
- if (spec && spec.trim() !== '') {
139
- try {
140
- const regExp = new RegExp(spec);
141
- if (!regExp.test(String(value))) {
142
- return `${name}(${fieldName})格式不正确`;
143
- }
144
- } catch (error) {
145
- return `${name}(${fieldName})的正则表达式格式错误`;
146
- }
147
- }
148
-
149
- return null;
150
- } catch (error) {
151
- return `${name}(${fieldName})验证出错: ${error.message}`;
152
- }
153
- }
154
-
155
- /**
156
- * 验证字符串类型
157
- */
158
- validateString(value, name, min, max, spec, fieldName) {
159
- try {
160
- if (isType(value, 'string') === false) {
161
- return `${name}(${fieldName})必须是字符串`;
162
- }
163
-
164
- if (min !== null && value.length < min) {
165
- return `${name}(${fieldName})长度不能少于${min}个字符`;
166
- }
167
-
168
- if (max !== null && max > 0 && value.length > max) {
169
- return `${name}(${fieldName})长度不能超过${max}个字符`;
170
- }
171
-
172
- if (spec && spec.trim() !== '') {
173
- try {
174
- const regExp = new RegExp(spec);
175
- if (!regExp.test(value)) {
176
- return `${name}(${fieldName})格式不正确`;
177
- }
178
- } catch (error) {
179
- return `${name}(${fieldName})的正则表达式格式错误`;
180
- }
181
- }
182
-
183
- return null;
184
- } catch (error) {
185
- return `${name}(${fieldName})验证出错: ${error.message}`;
186
- }
187
- }
188
-
189
- /**
190
- * 验证数组类型
191
- */
192
- validateArray(value, name, min, max, spec, fieldName) {
193
- try {
194
- if (!Array.isArray(value)) {
195
- return `${name}(${fieldName})必须是数组`;
196
- }
197
-
198
- if (min !== null && value.length < min) {
199
- return `${name}(${fieldName})至少需要${min}个元素`;
200
- }
201
-
202
- if (max !== null && max > 0 && value.length > max) {
203
- return `${name}(${fieldName})最多只能有${max}个元素`;
204
- }
205
-
206
- if (spec && spec.trim() !== '') {
207
- try {
208
- const regExp = new RegExp(spec);
209
- for (const item of value) {
210
- if (!regExp.test(String(item))) {
211
- return `${name}(${fieldName})中的元素"${item}"格式不正确`;
212
- }
213
- }
214
- } catch (error) {
215
- return `${name}(${fieldName})的正则表达式格式错误`;
216
- }
217
- }
218
-
219
- return null;
220
- } catch (error) {
221
- return `${name}(${fieldName})验证出错: ${error.message}`;
222
- }
223
- }
224
- }
225
-
226
- export const validator = new Validator();