befly 3.13.7 → 3.13.8
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/dist/befly.js +23 -55
- package/dist/befly.min.js +12 -12
- package/dist/hooks/parser.d.ts +1 -1
- package/dist/hooks/parser.js +4 -21
- package/dist/hooks/validator.js +21 -5
- package/dist/lib/validator.js +1 -1
- package/package.json +2 -2
package/dist/befly.js
CHANGED
|
@@ -83,30 +83,6 @@ function normalizePositiveInt(value, fallback, min, max) {
|
|
|
83
83
|
return max;
|
|
84
84
|
return v;
|
|
85
85
|
}
|
|
86
|
-
function isEmpty(value) {
|
|
87
|
-
if (value === null || value === undefined) {
|
|
88
|
-
return true;
|
|
89
|
-
}
|
|
90
|
-
if (typeof value === "string") {
|
|
91
|
-
return value.trim().length === 0;
|
|
92
|
-
}
|
|
93
|
-
if (typeof value === "number") {
|
|
94
|
-
return value === 0 || Number.isNaN(value);
|
|
95
|
-
}
|
|
96
|
-
if (typeof value === "boolean") {
|
|
97
|
-
return value === false;
|
|
98
|
-
}
|
|
99
|
-
if (Array.isArray(value)) {
|
|
100
|
-
return value.length === 0;
|
|
101
|
-
}
|
|
102
|
-
if (value instanceof Map || value instanceof Set) {
|
|
103
|
-
return value.size === 0;
|
|
104
|
-
}
|
|
105
|
-
if (isPlainObject(value)) {
|
|
106
|
-
return Object.keys(value).length === 0;
|
|
107
|
-
}
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
86
|
function forOwn(obj, iteratee) {
|
|
111
87
|
if (typeof iteratee !== "function") {
|
|
112
88
|
return;
|
|
@@ -203,17 +179,6 @@ var keysToCamel = (obj) => {
|
|
|
203
179
|
if (!arr || !Array.isArray(arr))
|
|
204
180
|
return arr;
|
|
205
181
|
return arr.map((item) => keysToCamel(item));
|
|
206
|
-
}, pickFields = (obj, keys) => {
|
|
207
|
-
if (!obj || !isPlainObject(obj) && !Array.isArray(obj)) {
|
|
208
|
-
return {};
|
|
209
|
-
}
|
|
210
|
-
const result = {};
|
|
211
|
-
for (const key of keys) {
|
|
212
|
-
if (key in obj) {
|
|
213
|
-
result[key] = obj[key];
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
return result;
|
|
217
182
|
};
|
|
218
183
|
var init_util = () => {};
|
|
219
184
|
|
|
@@ -12127,7 +12092,6 @@ class XMLParser {
|
|
|
12127
12092
|
}
|
|
12128
12093
|
|
|
12129
12094
|
// hooks/parser.ts
|
|
12130
|
-
init_util();
|
|
12131
12095
|
var xmlParser = new XMLParser;
|
|
12132
12096
|
var parserHook = {
|
|
12133
12097
|
name: "parser",
|
|
@@ -12143,11 +12107,7 @@ var parserHook = {
|
|
|
12143
12107
|
if (ctx.req.method === "GET") {
|
|
12144
12108
|
const url = new URL(ctx.req.url);
|
|
12145
12109
|
const params = Object.fromEntries(url.searchParams);
|
|
12146
|
-
|
|
12147
|
-
ctx.body = pickFields(params, Object.keys(ctx.api.fields));
|
|
12148
|
-
} else {
|
|
12149
|
-
ctx.body = params;
|
|
12150
|
-
}
|
|
12110
|
+
ctx.body = params;
|
|
12151
12111
|
} else if (ctx.req.method === "POST") {
|
|
12152
12112
|
const contentType = ctx.req.headers.get("content-type") || "";
|
|
12153
12113
|
const url = new URL(ctx.req.url);
|
|
@@ -12156,22 +12116,14 @@ var parserHook = {
|
|
|
12156
12116
|
if (contentType.includes("application/json")) {
|
|
12157
12117
|
const body = await ctx.req.json();
|
|
12158
12118
|
const merged = { ...queryParams, ...body };
|
|
12159
|
-
|
|
12160
|
-
ctx.body = pickFields(merged, Object.keys(ctx.api.fields));
|
|
12161
|
-
} else {
|
|
12162
|
-
ctx.body = merged;
|
|
12163
|
-
}
|
|
12119
|
+
ctx.body = merged;
|
|
12164
12120
|
} else if (contentType.includes("application/xml") || contentType.includes("text/xml")) {
|
|
12165
12121
|
const text = await ctx.req.text();
|
|
12166
12122
|
const parsed = xmlParser.parse(text);
|
|
12167
12123
|
const rootKey = Object.keys(parsed)[0];
|
|
12168
12124
|
const body = rootKey && typeof parsed[rootKey] === "object" ? parsed[rootKey] : parsed;
|
|
12169
12125
|
const merged = { ...queryParams, ...body };
|
|
12170
|
-
|
|
12171
|
-
ctx.body = pickFields(merged, Object.keys(ctx.api.fields));
|
|
12172
|
-
} else {
|
|
12173
|
-
ctx.body = merged;
|
|
12174
|
-
}
|
|
12126
|
+
ctx.body = merged;
|
|
12175
12127
|
} else {
|
|
12176
12128
|
ctx.response = ErrorResponse(ctx, "\u65E0\u6548\u7684\u8BF7\u6C42\u53C2\u6570\u683C\u5F0F", 1, null, {
|
|
12177
12129
|
location: "content-type",
|
|
@@ -12326,7 +12278,7 @@ class Validator {
|
|
|
12326
12278
|
}
|
|
12327
12279
|
for (const field of required) {
|
|
12328
12280
|
const value = data[field];
|
|
12329
|
-
if (value === undefined || value === null
|
|
12281
|
+
if (value === undefined || value === null) {
|
|
12330
12282
|
const label = rules[field]?.name || field;
|
|
12331
12283
|
fieldErrors[field] = `${label}\u4E3A\u5FC5\u586B\u9879`;
|
|
12332
12284
|
}
|
|
@@ -12499,6 +12451,7 @@ class Validator {
|
|
|
12499
12451
|
}
|
|
12500
12452
|
|
|
12501
12453
|
// hooks/validator.ts
|
|
12454
|
+
init_util();
|
|
12502
12455
|
var validatorHook = {
|
|
12503
12456
|
name: "validator",
|
|
12504
12457
|
enable: true,
|
|
@@ -12509,10 +12462,25 @@ var validatorHook = {
|
|
|
12509
12462
|
if (!ctx.api.fields) {
|
|
12510
12463
|
return;
|
|
12511
12464
|
}
|
|
12512
|
-
|
|
12513
|
-
|
|
12514
|
-
|
|
12465
|
+
if (isPlainObject(ctx.api.fields)) {
|
|
12466
|
+
const rawBody = isPlainObject(ctx.body) ? ctx.body : {};
|
|
12467
|
+
const nextBody = {};
|
|
12468
|
+
for (const [field, fieldDef] of Object.entries(ctx.api.fields)) {
|
|
12469
|
+
let value = rawBody[field];
|
|
12470
|
+
if (value === undefined) {
|
|
12471
|
+
const snakeField = snakeCase(field);
|
|
12472
|
+
if (rawBody[snakeField] !== undefined) {
|
|
12473
|
+
value = rawBody[snakeField];
|
|
12474
|
+
}
|
|
12475
|
+
}
|
|
12476
|
+
if (value === undefined && fieldDef?.default !== undefined && fieldDef?.default !== null) {
|
|
12477
|
+
value = fieldDef.default;
|
|
12478
|
+
}
|
|
12479
|
+
if (value !== undefined) {
|
|
12480
|
+
nextBody[field] = value;
|
|
12481
|
+
}
|
|
12515
12482
|
}
|
|
12483
|
+
ctx.body = nextBody;
|
|
12516
12484
|
}
|
|
12517
12485
|
const result = Validator.validate(ctx.body, ctx.api.fields, ctx.api.required || []);
|
|
12518
12486
|
if (result.code !== 0) {
|