waibu 2.5.0 → 2.7.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
@@ -329,15 +329,16 @@ async function factory (pkgName) {
329
329
  *
330
330
  * @method
331
331
  * @param {string} prefix
332
+ * @param {boolean} nsOnly - Set ```true``` to return plugin's namespace only
332
333
  * @returns {Object}
333
334
  */
334
- getPluginByPrefix = (prefix) => {
335
+ getPluginByPrefix = (prefix, nsOnly) => {
335
336
  const { get, find } = this.app.lib._
336
- const item = find(this.app.waibu.routes, r => {
337
- return get(r, 'config.prefix') === prefix
337
+ const ns = find(this.app.getAllNs(), p => {
338
+ return get(this, `app.${p}.config.waibu.prefix`) === prefix
338
339
  })
339
- const ns = get(item, 'config.ns')
340
- if (ns) return this.app[ns]
340
+ if (!ns) return
341
+ return nsOnly ? ns : this.app[ns]
341
342
  }
342
343
 
343
344
  /**
@@ -568,6 +569,54 @@ async function factory (pkgName) {
568
569
  })
569
570
  return text
570
571
  }
572
+
573
+ arrayToAttr = (array = [], delimiter = ' ') => {
574
+ const { isPlainObject } = this.app.lib._
575
+ return array.map(item => {
576
+ if (isPlainObject(item)) return this.objectToAttr(item)
577
+ return item
578
+ }).join(delimiter)
579
+ }
580
+
581
+ attrToArray = (text = '', delimiter = ' ') => {
582
+ const { map, trim, without, isArray } = this.app.lib._
583
+ if (text === true) text = ''
584
+ if (isArray(text)) text = text.join(delimiter)
585
+ return without(map(text.split(delimiter), i => trim(i)), '', undefined, null).map(item => {
586
+ return item
587
+ })
588
+ }
589
+
590
+ attrToObject = (text = '', delimiter = ';', kvDelimiter = ':') => {
591
+ const { camelCase, isPlainObject } = this.app.lib._
592
+ const result = {}
593
+ if (isPlainObject(text)) text = this.objectToAttr(text)
594
+ if (typeof text !== 'string') return text
595
+ if (text.slice(1, 3) === '%=') return text
596
+ const array = this.attrToArray(text, delimiter)
597
+ array.forEach(item => {
598
+ const [key, val] = this.attrToArray(item, kvDelimiter)
599
+ result[camelCase(key)] = val
600
+ })
601
+ return result
602
+ }
603
+
604
+ base64JsonDecode = (data = 'e30=') => {
605
+ return JSON.parse(Buffer.from(data, 'base64'))
606
+ }
607
+
608
+ base64JsonEncode = (data) => {
609
+ return Buffer.from(JSON.stringify(data)).toString('base64')
610
+ }
611
+
612
+ objectToAttr = (obj = {}, delimiter = ';', kvDelimiter = ':') => {
613
+ const { forOwn, kebabCase } = this.app.lib._
614
+ const result = []
615
+ forOwn(obj, (v, k) => {
616
+ result.push(`${kebabCase(k)}${kvDelimiter}${v ?? ''}`)
617
+ })
618
+ return result.join(delimiter)
619
+ }
571
620
  }
572
621
 
573
622
  return Waibu
@@ -10,12 +10,16 @@ export function writeHtml (req, reply, tpl, payload) {
10
10
  return compiled(payload)
11
11
  }
12
12
 
13
- export async function interceptor (err, name, req, reply) {
14
- const { get } = this.app.lib._
15
- const webApp = get(req, 'routeOptions.config.webApp')
13
+ export async function interceptor (name, err, req, reply) {
14
+ const { get, trim } = this.app.lib._
15
+ let webApp = get(req, 'routeOptions.config.webApp')
16
+ if (!webApp) {
17
+ const [prefix] = trim(req.url, '/').split('/')
18
+ webApp = this.getPluginByPrefix(prefix, true)
19
+ }
16
20
  if (webApp) {
17
21
  const plugin = this.app[webApp]
18
- const handler = get(plugin, `waibuFactory.${name}`)
22
+ const handler = get(plugin, `webAppFactory.${name}`)
19
23
  if (handler) return await handler.call(plugin, err, req, reply)
20
24
  }
21
25
  }
@@ -57,6 +57,10 @@ async function attachIntl (detector = [], req, reply) {
57
57
  return this.app[defNs].t(text, ...args)
58
58
  // return result.replaceAll("'", ''').replaceAll('"', '"')
59
59
  }
60
+ req.te = (text, ...args) => {
61
+ args.push({ lang: req.lang })
62
+ return this.app[defNs].te(text, ...args)
63
+ }
60
64
  req.format = (value, type = 'auto', opts = {}) => {
61
65
  opts.lang = opts.lang ?? req.lang
62
66
  const timeZone = get(req, 'site.setting.sumba.timeZone', this.app.bajo.config.intl.format.datetime.timeZone)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu",
3
- "version": "2.5.0",
3
+ "version": "2.7.0",
4
4
  "description": "Web Framework for Bajo",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/wiki/CHANGES.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-02-20
4
+
5
+ - [2.7.0] Add ```req.te()``` decorator
6
+ - [2.7.0] Bug fix on ```getPluginByPrefix()```
7
+ - [2.7.0] Bug fix on ```notFoundHandler.interceptor()```
8
+
9
+ ## 2026-02-18
10
+
11
+ - [2.6.0] Move attribute functions from ```waibu-mpa```
12
+
3
13
  ## 2026-02-17
4
14
 
5
15
  - [2.5.0] Add ```getHostname()```