waibu 2.12.0 → 2.14.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.
- package/extend/bajo/hook/waibu@on-request.js +2 -2
- package/extend/bajo/hook/waibu@on-response.js +3 -3
- package/index.js +9 -4
- package/lib/build-locals.js +32 -35
- package/lib/decorate.js +1 -0
- package/package.json +1 -1
- package/wiki/CHANGES.md +13 -0
|
@@ -18,10 +18,10 @@ const onRequest = {
|
|
|
18
18
|
const ip = plain ? this.getIp(req) : chalk.magenta(this.getIp(req))
|
|
19
19
|
let msg = this.app[ns].t('httpReq%s%s%s%s', arrow, method, url.replaceAll('%', '%%'), ip)
|
|
20
20
|
if (req.headers['content-length']) msg += this.app[ns].t('httpReqExt%s', req.headers['content-length'])
|
|
21
|
-
if (this.config.
|
|
21
|
+
if (this.config.log.defer) {
|
|
22
22
|
this.reqLog = this.reqLog ?? {}
|
|
23
23
|
this.reqLog[req.id] = msg
|
|
24
|
-
} else this.app[ns].log.info(msg)
|
|
24
|
+
} else if (!this.config.log.noReq) this.app[ns].log.info(msg)
|
|
25
25
|
if (Object.keys(this.config.paramsCharMap).length === 0) return
|
|
26
26
|
for (const key in req.params) {
|
|
27
27
|
let val = req.params[key]
|
|
@@ -36,12 +36,12 @@ const onResponse = {
|
|
|
36
36
|
if (elapsed < 1000) tc = 'yellow'
|
|
37
37
|
if (elapsed < 500) tc = 'green'
|
|
38
38
|
const time = plain ? elapsed.toFixed(2) : chalk[tc](elapsed.toFixed(2))
|
|
39
|
-
if (this.config.
|
|
39
|
+
if (this.config.log.defer) {
|
|
40
40
|
this.reqLog = this.reqLog ?? {}
|
|
41
|
-
if (this.reqLog[req.id]) this.app[ns].log.info(this.reqLog[req.id])
|
|
41
|
+
if (this.reqLog[req.id] && !this.config.log.noReq) this.app[ns].log.info(this.reqLog[req.id])
|
|
42
42
|
delete this.reqLog[req.id]
|
|
43
43
|
}
|
|
44
|
-
this.app[ns].log[level]('httpResp%s%s%s%s%s', arrow, method, url, state, time)
|
|
44
|
+
if (!this.config.log.noReply) this.app[ns].log[level]('httpResp%s%s%s%s%s', arrow, method, url, state, time)
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
package/index.js
CHANGED
|
@@ -91,7 +91,11 @@ async function factory (pkgName) {
|
|
|
91
91
|
intl: {
|
|
92
92
|
detectors: ['qs']
|
|
93
93
|
},
|
|
94
|
-
|
|
94
|
+
log: {
|
|
95
|
+
noReq: false,
|
|
96
|
+
noReply: false,
|
|
97
|
+
defer: false
|
|
98
|
+
},
|
|
95
99
|
prefixVirtual: '~',
|
|
96
100
|
qsKey: {
|
|
97
101
|
bbox: 'bbox',
|
|
@@ -215,9 +219,9 @@ async function factory (pkgName) {
|
|
|
215
219
|
* @param {string} name - ns based route name
|
|
216
220
|
* @returns {Object} Route object
|
|
217
221
|
*/
|
|
218
|
-
findRoute = (name) => {
|
|
222
|
+
findRoute = (name, method = 'GET') => {
|
|
219
223
|
const { outmatch } = this.app.lib
|
|
220
|
-
const { find } = this.app.lib._
|
|
224
|
+
const { find, isString } = this.app.lib._
|
|
221
225
|
const { breakNsPath } = this.app.bajo
|
|
222
226
|
let { ns, subNs = '', path } = breakNsPath(name)
|
|
223
227
|
const params = path.split('|')
|
|
@@ -227,7 +231,8 @@ async function factory (pkgName) {
|
|
|
227
231
|
r.config = r.config ?? {}
|
|
228
232
|
const match = outmatch(r.config.pathSrc ?? r.path, { separator: false })
|
|
229
233
|
if (!match(path)) return false
|
|
230
|
-
|
|
234
|
+
const methods = isString(r.method) ? [r.method] : r.method
|
|
235
|
+
return ns === r.config.ns && r.config.subNs === subNs && methods.includes(method)
|
|
231
236
|
})
|
|
232
237
|
}
|
|
233
238
|
|
package/lib/build-locals.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
const errs = {}
|
|
2
|
-
|
|
3
1
|
async function buildHomesMenu (req) {
|
|
4
2
|
const { callHandler } = this.app.bajo
|
|
5
3
|
const { get, find, orderBy } = this.app.lib._
|
|
@@ -52,49 +50,48 @@ async function buildPagesMenu (req) {
|
|
|
52
50
|
return orderBy(all, ['level', 'title'])
|
|
53
51
|
}
|
|
54
52
|
|
|
53
|
+
const omitted = ['createdAt', 'updatedAt', '_immutable']
|
|
54
|
+
|
|
55
55
|
async function buildLocals ({ tpl, params = {}, opts = {} } = {}) {
|
|
56
56
|
const { runHook } = this.app.bajo
|
|
57
|
-
const { set, merge, pick, get, isEmpty, find,
|
|
57
|
+
const { set, merge, pick, get, isEmpty, find, cloneDeep, omit } = this.app.lib._
|
|
58
58
|
const { req, reply } = opts
|
|
59
|
-
params.page = merge(params.page ?? {}, { ns: req.ns, features: [] })
|
|
60
|
-
params.sidebar = params.sidebar ?? []
|
|
61
59
|
|
|
62
|
-
const
|
|
63
|
-
const theme = pick(find(this.themes, { name: opts.theme ?? req.theme }) ?? {}, ['name', 'framework'])
|
|
64
|
-
const iconset = pick(find(this.iconsets, { name: opts.iconset ?? req.iconset }) ?? {}, ['name'])
|
|
65
|
-
const routeOpts = get(req, 'routeOptions.config', {})
|
|
66
|
-
const _meta = { theme, iconset, site, user, lang, darkMode, routeOpts }
|
|
67
|
-
_meta.site = _meta.site ?? {}
|
|
68
|
-
merge(_meta, pick(req, ['url', 'params', 'query']))
|
|
69
|
-
_meta.env = this.app.bajo.config.env
|
|
70
|
-
_meta.url = _meta.url.split('?')[0].split('#')[0]
|
|
71
|
-
if (req.session) _meta.prevUrl = req.session.prevUrl
|
|
72
|
-
_meta.route = get(req, 'routeOptions.url')
|
|
73
|
-
_meta.template = tpl
|
|
74
|
-
_meta.hostHeader = req.headers.host
|
|
75
|
-
_meta.statusCode = 200
|
|
76
|
-
_meta.isAdmin = _meta.user && find(_meta.user.teams, { alias: 'administrator' })
|
|
77
|
-
const pulled = []
|
|
78
|
-
for (const k in errs) {
|
|
79
|
-
if (Date.now() - errs[k] > 5000) pulled.push(k)
|
|
80
|
-
}
|
|
81
|
-
pullAt(errs, pulled)
|
|
60
|
+
const _meta = { template: tpl }
|
|
82
61
|
if (params.error) {
|
|
83
62
|
if (params.error.statusCode) _meta.statusCode = params.error.statusCode
|
|
84
63
|
_meta.errorMessage = params.error.message
|
|
85
64
|
if (params.error.ns) params.page.ns = params.error.ns
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
65
|
+
this.log.error('error%s', params.error.message)
|
|
66
|
+
if (this.app.bajo.config.env === 'dev') console.log(params.error)
|
|
67
|
+
}
|
|
68
|
+
if (!opts.partial) {
|
|
69
|
+
params.page = merge(params.page ?? {}, { ns: req.ns, features: [] })
|
|
70
|
+
params.sidebar = params.sidebar ?? []
|
|
71
|
+
merge(_meta, pick(req, ['site', 'user', 'lang', 'darkMode', 'url']))
|
|
72
|
+
_meta.params = cloneDeep(req.params)
|
|
73
|
+
_meta.query = cloneDeep(req.query)
|
|
74
|
+
_meta.site = omit(req.site, omitted)
|
|
75
|
+
_meta.user = omit(req.user, [...omitted, 'apiKey', 'salt', 'token'])
|
|
76
|
+
_meta.theme = pick(find(this.themes, { name: opts.theme ?? req.theme }) ?? {}, ['name', 'framework'])
|
|
77
|
+
_meta.iconset = pick(find(this.iconsets, { name: opts.iconset ?? req.iconset }) ?? {}, ['name'])
|
|
78
|
+
_meta.routeOpts = get(req, 'routeOptions.config', {})
|
|
79
|
+
set(params, 'menu.homes', await buildHomesMenu.call(this, req))
|
|
80
|
+
set(params, 'menu.pages', await buildPagesMenu.call(this, req))
|
|
81
|
+
_meta.env = this.app.bajo.config.env
|
|
82
|
+
_meta.url = _meta.url.split('?')[0].split('#')[0]
|
|
83
|
+
if (req.session) _meta.prevUrl = req.session.prevUrl
|
|
84
|
+
_meta.route = get(req, 'routeOptions.url')
|
|
85
|
+
_meta.hostHeader = req.headers.host
|
|
86
|
+
_meta.statusCode = 200
|
|
87
|
+
_meta.reqId = req.id
|
|
88
|
+
_meta.flash = reply && req.session && req.flash && !opts.noFlash ? reply.flash() : {}
|
|
91
89
|
}
|
|
92
|
-
if (reply && req.session && req.flash && !opts.partial) _meta.flash = reply.flash()
|
|
93
|
-
set(params, 'menu.homes', await buildHomesMenu.call(this, req))
|
|
94
|
-
set(params, 'menu.pages', await buildPagesMenu.call(this, req))
|
|
95
90
|
const merged = merge({}, params, { _meta })
|
|
96
|
-
|
|
97
|
-
|
|
91
|
+
if (!opts.partial) {
|
|
92
|
+
await runHook(`${this.ns}:afterBuildLocals`, merged, req, opts)
|
|
93
|
+
if (!isEmpty(merged._meta.routeOpts.ns)) await runHook(`${this.ns}.${merged._meta.routeOpts.ns}:afterBuildLocals`, merged, req, opts)
|
|
94
|
+
}
|
|
98
95
|
return merged
|
|
99
96
|
}
|
|
100
97
|
|
package/lib/decorate.js
CHANGED
|
@@ -4,6 +4,7 @@ async function decorate () {
|
|
|
4
4
|
const me = this
|
|
5
5
|
this.instance.decorateRequest('lang', null)
|
|
6
6
|
this.instance.decorateRequest('t', () => {})
|
|
7
|
+
this.instance.decorateRequest('te', () => {})
|
|
7
8
|
this.instance.decorateRequest('format', () => {})
|
|
8
9
|
this.instance.decorateRequest('langDetector', null)
|
|
9
10
|
this.instance.decorateRequest('site', null)
|
package/package.json
CHANGED
package/wiki/CHANGES.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# Changes
|
|
2
2
|
|
|
3
|
+
## 2026-05-24
|
|
4
|
+
|
|
5
|
+
- [2.14.0] Add auto detection of theme & iconset for all widgets incl the dynamic one
|
|
6
|
+
- [2.14.0] Bug fix in ```handle-error.js```
|
|
7
|
+
|
|
8
|
+
## 2026-05-22
|
|
9
|
+
|
|
10
|
+
- [2.13.0] Add ```config.log.noReq``` & ```config.log.noReply```
|
|
11
|
+
- [2.13.0] Change ```config.deferLog``` to ```config.log.defer```
|
|
12
|
+
- [2.13.0] Bug fix in ```build-Locals.js```
|
|
13
|
+
- [2.13.0] Add ```decorator.te```
|
|
14
|
+
- [2.13.0] Bug fix in ```handle-error.js```
|
|
15
|
+
|
|
3
16
|
## 2026-05-16
|
|
4
17
|
|
|
5
18
|
- [2.12.0] Change in ```routeDir()```
|