waibu 2.3.3 → 2.4.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/index.js CHANGED
@@ -458,7 +458,7 @@ async function factory (pkgName) {
458
458
  const { defaultsDeep } = this.app.lib.aneka
459
459
  const { isEmpty, get, trimEnd, trimStart } = this.app.lib._
460
460
  const { breakNsPath } = this.app.bajo
461
- const { query = {}, base = this.ns, params = {}, guessHost } = options
461
+ const { query = {}, base = this.ns, params = {}, guessHost, defaults = {} } = options
462
462
 
463
463
  const plugin = getPlugin(base)
464
464
  const cfg = plugin.config ?? {}
@@ -474,7 +474,12 @@ async function factory (pkgName) {
474
474
  if (info.path.includes('//')) return info.path
475
475
 
476
476
  info.path = info.path.split('/').map(p => {
477
- return p[0] === ':' && params[p.slice(1)] ? params[p.slice(1)] : p
477
+ if (!(p[0] === ':' || (p[0] === '{' && p[p.length - 1] === '}'))) return p
478
+ const _p = p
479
+ p = p.replace(':', '').replace('{', '').replace('}', '')
480
+ if (params[p]) return params[p]
481
+ if (defaults[p]) return defaults[p]
482
+ return _p
478
483
  }).join('/')
479
484
  let url = info.path
480
485
  const langDetector = get(cfg, 'intl.detectors', [])
@@ -1,24 +1,19 @@
1
1
  import { redirect } from './handle-redirect.js'
2
- import { notFound, writeHtml } from './handle-not-found.js'
2
+ import { notFound, writeHtml, interceptor } from './handle-not-found.js'
3
3
 
4
- async function error (req, reply, err = {}) {
5
- const { get } = this.app.lib._
6
- const webApp = get(req, 'routeOptions.config.webApp')
4
+ async function error (err, req, reply) {
7
5
  this.log.error(err)
8
- if (webApp) {
9
- const plugin = this.app[webApp]
10
- const errorHandler = get(plugin, 'waibuFactory.errorHandler')
11
- if (errorHandler) return await errorHandler.call(plugin, err, req, reply)
12
- }
6
+ const resp = await interceptor.call(this, 'errorHandler', err, req, reply)
7
+ if (resp) return resp
13
8
  return writeHtml.call(this, req, reply, `${this.ns}:/extend/bajoTemplate/template/500.html`, { text: this.app.log.getErrorMessage(err) })
14
9
  }
15
10
 
16
11
  async function handleError () {
17
12
  const me = this
18
13
  this.instance.setErrorHandler(async function (err, req, reply) {
19
- if (err.message === '_notFound' || err.statusCode === 404) return await notFound.call(me, req, reply, err)
20
- if (err.message === '_redirect' && err.redirect) return redirect.call(me, reply, err.redirect, err.options)
21
- return await error.call(me, req, reply, err)
14
+ if (err.message === '_notFound' || err.statusCode === 404) return await notFound.call(me, err, req, reply)
15
+ if (err.message === '_redirect' && err.redirect) return redirect.call(me, err, req, reply)
16
+ return await error.call(me, err, req, reply)
22
17
  })
23
18
  }
24
19
 
@@ -10,15 +10,51 @@ export function writeHtml (req, reply, tpl, payload) {
10
10
  return compiled(payload)
11
11
  }
12
12
 
13
- export async function notFound (req, reply, err = {}) {
13
+ export async function interceptor (err, name, req, reply) {
14
14
  const { get } = this.app.lib._
15
15
  const webApp = get(req, 'routeOptions.config.webApp')
16
- reply.code(404)
17
16
  if (webApp) {
18
17
  const plugin = this.app[webApp]
19
- const errorHandler = get(plugin, 'waibuFactory.errorHandler')
20
- if (errorHandler) return await errorHandler.call(plugin, req, reply)
18
+ const handler = get(plugin, `waibuFactory.${name}`)
19
+ if (handler) return await handler.call(plugin, err, req, reply)
20
+ }
21
+ }
22
+
23
+ function redirSvc (req) {
24
+ const { trim, find, get } = this.app.lib._
25
+ const { outmatch } = this.app.lib
26
+ let match = false
27
+ let [prefix, ...args] = trim(req.url, '/').split('/')
28
+ args = '/' + args.join('/')
29
+ let plugin = find(this.app.getAllNs(), p => {
30
+ return get(this, `app.${p}.config.waibu.prefix`) === prefix
31
+ })
32
+ if (!plugin) {
33
+ plugin = 'main'
34
+ args = `/${prefix}`
21
35
  }
36
+ const items = get(this, `app.${plugin}.config.waibuMpa.redirect`, {})
37
+ for (const k in items) {
38
+ const isMatch = outmatch(k)
39
+ if (isMatch(args)) {
40
+ match = items[k]
41
+ break
42
+ }
43
+ }
44
+ return match
45
+ }
46
+
47
+ export async function notFound (err, req, reply) {
48
+ const { getMethod } = this.app.bajo
49
+ let redirectTo = await redirSvc.call(this, req, reply)
50
+ if (redirectTo !== false) {
51
+ const fn = getMethod(redirectTo, false)
52
+ if (fn) redirectTo = await fn(req)
53
+ if (redirectTo) return reply.redirectTo(redirectTo)
54
+ }
55
+ reply.code(404)
56
+ const resp = await interceptor.call(this, 'notFoundHandler', err, req, reply)
57
+ if (resp) return resp
22
58
  const text = req.t('notFound%s%s', req.t('route'), req.url)
23
59
  return writeHtml.call(this, req, reply, `${this.ns}:/extend/bajoTemplate/template/400.html`, { text })
24
60
  }
@@ -26,7 +62,7 @@ export async function notFound (req, reply, err = {}) {
26
62
  async function handleNotFound () {
27
63
  const me = this
28
64
  me.instance.setNotFoundHandler(async function (req, reply) {
29
- return await notFound.call(me, req, reply)
65
+ return await notFound.call(me, null, req, reply)
30
66
  })
31
67
  }
32
68
 
@@ -1,15 +1,15 @@
1
1
  import path from 'path'
2
2
 
3
- export function redirect (reply, url, options = {}) {
4
- if (url.startsWith('http') || path.isAbsolute(url)) reply.redirect(url)
5
- else reply.redirect(this.routePath(url, options))
3
+ export function redirect (err = {}, req, reply) {
4
+ if (err.redirect.startsWith('http') || path.isAbsolute(err.redirect)) reply.redirect(err.redirect)
5
+ else reply.redirect(this.routePath(err.redirect, err.options))
6
6
  return reply
7
7
  }
8
8
 
9
9
  async function handleRedirect (options) {
10
10
  const me = this
11
11
  this.instance.decorateReply('redirectTo', function (url, options = {}) {
12
- return redirect.call(me, this, url, options)
12
+ return redirect.call(me, { redirect: url, options }, null, this, url, options)
13
13
  })
14
14
  }
15
15
 
package/lib/web-app.js CHANGED
@@ -24,10 +24,10 @@ export async function collect (glob = 'boot.js', baseNs) {
24
24
 
25
25
  async function webApp () {
26
26
  const { runHook } = this.app.bajo
27
- const mods = await collect.call(this)
27
+ this.webApps = await collect.call(this)
28
28
  await runHook(`${this.ns}:beforeAppBoot`)
29
29
  // build routes
30
- for (const m of mods) {
30
+ for (const m of this.webApps) {
31
31
  const disabled = this.app[m.ns].config.disabled
32
32
  if (Array.isArray(disabled) && disabled.length === 1 && ['*', 'all'].includes(disabled[0])) {
33
33
  this.log.warn('allRoutesConfigDisabled%s', m.ns)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu",
3
- "version": "2.3.3",
3
+ "version": "2.4.0",
4
4
  "description": "Web Framework for Bajo",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/wiki/CHANGES.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-02-09
4
+
5
+ - [2.3.4] Bug fix on error handling
6
+ - [2.3.4] Bug fix on not found handling
7
+ - [2.3.4] Bug fix on redirection handling
8
+ - [2.4.0] Accept path parameter as in ```{param}``` to complement ```:param``` in ```routePath()```
9
+
3
10
  ## 2026-02-08
4
11
 
5
12
  - [2.3.0] Simplify all common handler calls