waibu 2.8.0 → 2.8.2
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/index.js +2 -0
- package/lib/handle-body.js +33 -0
- package/lib/webapp-scope/handle-multipart-body.js +7 -24
- package/package.json +1 -1
- package/wiki/CHANGES.md +5 -0
package/index.js
CHANGED
|
@@ -13,6 +13,7 @@ import handleError from './lib/handle-error.js'
|
|
|
13
13
|
import handleNotFound from './lib/handle-not-found.js'
|
|
14
14
|
import handleHome from './lib/handle-home.js'
|
|
15
15
|
import queryString from 'query-string'
|
|
16
|
+
import handleBody from './lib/handle-body.js'
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* @typedef TEscapeChars
|
|
@@ -199,6 +200,7 @@ async function factory (pkgName) {
|
|
|
199
200
|
await handleAppHook.call(this)
|
|
200
201
|
await handleError.call(this)
|
|
201
202
|
await routeHook.call(this, this.ns)
|
|
203
|
+
await handleBody.call(this)
|
|
202
204
|
await webApp.call(this)
|
|
203
205
|
await handleHome.call(this)
|
|
204
206
|
await handleNotFound.call(this)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function normalizeValue (value) {
|
|
2
|
+
const { isSet } = this.app.lib.aneka
|
|
3
|
+
const { isArray, trim, isPlainObject } = this.app.lib._
|
|
4
|
+
if (!isSet(value)) return
|
|
5
|
+
if (value === 'null') value = null
|
|
6
|
+
else if (value === 'undefined') value = undefined
|
|
7
|
+
else {
|
|
8
|
+
const val = trim(value)
|
|
9
|
+
if (['{', '['].includes(val[0])) {
|
|
10
|
+
try {
|
|
11
|
+
const parsed = JSON.parse(val)
|
|
12
|
+
if (isPlainObject(parsed) || isArray(parsed)) value = parsed
|
|
13
|
+
} catch (err) {
|
|
14
|
+
value = val
|
|
15
|
+
}
|
|
16
|
+
} else value = val
|
|
17
|
+
}
|
|
18
|
+
return value
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function handleBody (options = {}) {
|
|
22
|
+
const { isPlainObject } = this.app.lib._
|
|
23
|
+
const me = this
|
|
24
|
+
this.instance.addHook('preValidation', async function (req, reply) {
|
|
25
|
+
if (req.body && isPlainObject(req.body)) {
|
|
26
|
+
for (const key in req.body) {
|
|
27
|
+
req.body[key] = normalizeValue.call(me, req.body[key])
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default handleBody
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import multipart from '@fastify/multipart'
|
|
2
2
|
import { promisify } from 'util'
|
|
3
3
|
import { pipeline } from 'stream'
|
|
4
|
+
import { normalizeValue } from '../handle-body.js'
|
|
4
5
|
import path from 'path'
|
|
5
6
|
const pump = promisify(pipeline)
|
|
6
7
|
|
|
@@ -17,34 +18,16 @@ async function onFileHandler () {
|
|
|
17
18
|
}
|
|
18
19
|
|
|
19
20
|
async function handleMultipartBody (options = {}) {
|
|
20
|
-
const {
|
|
21
|
-
const {
|
|
22
|
-
const
|
|
23
|
-
|
|
21
|
+
const { defaultsDeep } = this.app.lib.aneka
|
|
22
|
+
const { isArray, map, isEmpty } = this.app.lib._
|
|
23
|
+
const me = this
|
|
24
|
+
|
|
24
25
|
if (options === false) return this.log.warn('middlewareDisabled%s', 'multipart')
|
|
25
26
|
const opts = defaultsDeep(options, this.app.waibu.config.multipart)
|
|
26
27
|
const onFile = await onFileHandler.call(this)
|
|
27
28
|
opts.onFile = onFile
|
|
28
29
|
await this.webAppCtx.register(multipart, opts)
|
|
29
30
|
|
|
30
|
-
function normalizeValue (value) {
|
|
31
|
-
if (!isSet(value)) return
|
|
32
|
-
if (value === 'null') value = null
|
|
33
|
-
else if (value === 'undefined') value = undefined
|
|
34
|
-
else {
|
|
35
|
-
const val = trim(value)
|
|
36
|
-
if (['{', '['].includes(val[0])) {
|
|
37
|
-
try {
|
|
38
|
-
const parsed = JSON.parse(val)
|
|
39
|
-
if (isPlainObject(parsed) || isArray(parsed)) value = parsed
|
|
40
|
-
} catch (err) {
|
|
41
|
-
value = val
|
|
42
|
-
}
|
|
43
|
-
} else value = parseVar({ item: value }).item
|
|
44
|
-
}
|
|
45
|
-
return value
|
|
46
|
-
}
|
|
47
|
-
|
|
48
31
|
this.webAppCtx.addHook('preValidation', async function (req, reply) {
|
|
49
32
|
if (req.isMultipart() && opts.attachFieldsToBody === true) {
|
|
50
33
|
const body = Object.fromEntries(
|
|
@@ -53,9 +36,9 @@ async function handleMultipartBody (options = {}) {
|
|
|
53
36
|
let value
|
|
54
37
|
if (key.endsWith('[]') && !isArray(item)) item = [item]
|
|
55
38
|
if (isArray(item)) {
|
|
56
|
-
value = map(item, i => normalizeValue(i.value))
|
|
39
|
+
value = map(item, i => normalizeValue.call(me, i.value))
|
|
57
40
|
} else {
|
|
58
|
-
value = normalizeValue(item.value)
|
|
41
|
+
value = normalizeValue.call(me, item.value)
|
|
59
42
|
}
|
|
60
43
|
key = key.replace('[]', '')
|
|
61
44
|
return [key, value]
|
package/package.json
CHANGED
package/wiki/CHANGES.md
CHANGED