waibu 2.7.1 → 2.8.1
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
CHANGED
|
@@ -12,8 +12,8 @@ import handleFavicon from './lib/handle-favicon.js'
|
|
|
12
12
|
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
|
-
import buildLocals from './lib/build-locals.js'
|
|
16
15
|
import queryString from 'query-string'
|
|
16
|
+
import handleBody from './lib/handle-body.js'
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* @typedef TEscapeChars
|
|
@@ -200,6 +200,7 @@ async function factory (pkgName) {
|
|
|
200
200
|
await handleAppHook.call(this)
|
|
201
201
|
await handleError.call(this)
|
|
202
202
|
await routeHook.call(this, this.ns)
|
|
203
|
+
await handleBody.call(this)
|
|
203
204
|
await webApp.call(this)
|
|
204
205
|
await handleHome.call(this)
|
|
205
206
|
await handleNotFound.call(this)
|
|
@@ -502,36 +503,6 @@ async function factory (pkgName) {
|
|
|
502
503
|
return url
|
|
503
504
|
}
|
|
504
505
|
|
|
505
|
-
/**
|
|
506
|
-
* Method to send mail through Masohi Messaging System. It is a thin wrapper
|
|
507
|
-
* for {@link https://github.com/ardhi/masohi-mail|masohi-mail} send method.
|
|
508
|
-
*
|
|
509
|
-
* If masohi is not loaded, nothing is delivered.
|
|
510
|
-
*
|
|
511
|
-
* @method
|
|
512
|
-
* @async
|
|
513
|
-
* @param {(string|Array)} tpl - Mail's template to use. If a string is given, the same template will be used for html & plaintext versions. Otherwise, the first template will be used for html mail, and the second one is for it's plaintext version
|
|
514
|
-
* @param {Object} [params={}] - {@link https://github.com/ardhi/masohi-mail|masohi-mail}'s params object.
|
|
515
|
-
* @returns
|
|
516
|
-
*/
|
|
517
|
-
sendMail = async (tpl, { to, cc, bcc, from, subject, data = {}, conn, source, options = {} }) => {
|
|
518
|
-
conn = conn ?? 'masohiMail:default'
|
|
519
|
-
if (!this.app.masohi || !this.app.masohiMail) return
|
|
520
|
-
const { get, isString } = this.app.lib._
|
|
521
|
-
const { generateId } = this.app.lib.aneka
|
|
522
|
-
const { render } = this.app.bajoTemplate
|
|
523
|
-
if (isString(tpl)) tpl = [tpl]
|
|
524
|
-
const locals = await buildLocals.call(this, { tpl, params: data, opts: options })
|
|
525
|
-
const opts = {
|
|
526
|
-
lang: get(options, 'req.lang'),
|
|
527
|
-
groupId: get(options, 'req.id', generateId())
|
|
528
|
-
}
|
|
529
|
-
const message = await render(tpl[0], locals, opts)
|
|
530
|
-
if (tpl[1]) opts.messageText = await render(tpl[1], locals, opts)
|
|
531
|
-
const payload = { type: 'object', data: { to, cc, bcc, from, subject, message, options: opts } }
|
|
532
|
-
await this.app.masohi.send({ payload, source: source ?? this.ns, conn }) // mail sent through worker
|
|
533
|
-
}
|
|
534
|
-
|
|
535
506
|
/**
|
|
536
507
|
* Recursively unescape block of texts
|
|
537
508
|
*
|
|
@@ -0,0 +1,32 @@
|
|
|
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 me = this
|
|
23
|
+
this.instance.addHook('preValidation', async function (req, reply) {
|
|
24
|
+
if (req.body) {
|
|
25
|
+
for (const key in req.body) {
|
|
26
|
+
req.body[key] = normalizeValue.call(me, req.body[key])
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
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
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changes
|
|
2
2
|
|
|
3
|
+
## 2026-03-06
|
|
4
|
+
|
|
5
|
+
- [2.8.1] Bug fix on ```req.body``` parsing
|
|
6
|
+
|
|
7
|
+
## 2026-03-02
|
|
8
|
+
|
|
9
|
+
- [2.8.0] Remove ```sendMail()``` as from now on it will be using sumba's ```sendMail()```
|
|
10
|
+
- [2.8.0] Remove mail templates
|
|
11
|
+
|
|
3
12
|
## 2026-02-21
|
|
4
13
|
|
|
5
14
|
- [2.7.1] Bug fix on ```errorHandler```
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<head lang="<%= _meta.lang %>">
|
|
3
|
-
<meta charset="UTF-8">
|
|
4
|
-
<title><%= page.fullTitle %></title>
|
|
5
|
-
<style type="text/css">
|
|
6
|
-
</style>
|
|
7
|
-
</head>
|
|
8
|
-
<body>
|
|
9
|
-
<% if (arguments[0].firstName && arguments[0].lastName) { %>
|
|
10
|
-
<p>Dear <%= firstName %> <%= lastName %>,</p>
|
|
11
|
-
<% } %>
|
|
12
|
-
<!-- body -->
|
|
13
|
-
<p>Sincerely,</p>
|
|
14
|
-
|
|
15
|
-
<p><%= _meta.site.title %></p>
|
|
16
|
-
</body>
|
|
17
|
-
</html>
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="<%= _meta.lang %>">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8">
|
|
5
|
-
<title><%= page.fullTitle %></title>
|
|
6
|
-
<style type="text/css">
|
|
7
|
-
</style>
|
|
8
|
-
</head>
|
|
9
|
-
<body>
|
|
10
|
-
<% if (arguments[0].firstName && arguments[0].lastName) { %>
|
|
11
|
-
<p>Yth <%= firstName %> <%= lastName %>,</p>
|
|
12
|
-
<% } %>
|
|
13
|
-
<!-- body -->
|
|
14
|
-
<p>Hormat kami,</p>
|
|
15
|
-
|
|
16
|
-
<p><%= _meta.site.title %></p>
|
|
17
|
-
</body>
|
|
18
|
-
</html>
|