waibu-rest-api 2.15.0 → 2.16.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.
@@ -9,7 +9,7 @@ const boot = {
9
9
  handler: async function (prefix) {
10
10
  const { importPkg, eachPlugins, importModule, runHook } = this.app.bajo
11
11
  const { fastGlob } = this.app.lib
12
- const { getPluginPrefix, isRouteDisabled } = this.app.waibu
12
+ const { getPluginPrefix, isRouteDisabled, cleanPath } = this.app.waibu
13
13
  const [bodyParser, accepts] = await importPkg('waibu:@fastify/formbody', 'waibu:@fastify/accepts')
14
14
  const {
15
15
  routeHook, handleMultipartBody, handleXmlBody, handleCors, handleHelmet, handleCompress,
@@ -37,35 +37,33 @@ const boot = {
37
37
 
38
38
  await eachPlugins(async function ({ dir }) {
39
39
  const { ns, alias } = this
40
- const appPrefix = '/' + (ns === me.app.mainNs ? '' : getPluginPrefix(ns, 'waibuRestApi'))
40
+ const appPrefix = getPluginPrefix(ns, 'waibuRestApi')
41
41
  const pattern = [
42
42
  `${dir}/extend/${pathPrefix}/**/{${actions.join(',')}}.js`,
43
43
  `${dir}/extend/${pathPrefix}/**/model-builder.*`
44
44
  ]
45
45
  const files = await fastGlob(pattern)
46
46
  if (files.length === 0) return undefined
47
- await me.webAppCtx.register(async (appCtx) => {
48
- for (const file of files) {
49
- const base = path.basename(file, path.extname(file))
50
- let mods
51
- if (base === 'model-builder') mods = await me._routeByModelBuilder({ file, dir, pathPrefix, ns, parent: me.ns })
52
- else mods = await me._routeByVerb({ file, appCtx, dir, pathPrefix, ns, alias, parent: me.ns })
47
+ for (const file of files) {
48
+ const base = path.basename(file, path.extname(file))
49
+ let mods
50
+ if (base === 'model-builder') mods = await me._routeByModelBuilder({ file, dir, pathPrefix, ns, parent: me.ns })
51
+ else mods = await me._routeByVerb({ file, dir, pathPrefix, ns, alias, parent: me.ns })
53
52
 
54
- if (!Array.isArray(mods)) mods = [mods]
55
- for (const mod of mods) {
56
- const fullPath = appPrefix === '/' ? mod.url : (appPrefix + mod.url)
57
- if (isRouteDisabled(`${prefix === '' ? '' : `/${prefix}`}${fullPath}`)) continue
58
- const rpath = await reroutedPath.call(this, fullPath, me.config.rerouted)
59
- if (me.config.format.asExt) mod.url = formatExt(mod.url)
60
- if (rpath) {
61
- mod.config.pathReroutedTo = rpath
62
- this.log.warn('rerouted%s%s', `${prefix}${fullPath}`, `${prefix}${rpath}`)
63
- mod.url = me.config.format.asExt ? formatExt(rpath) : rpath
64
- me.webAppCtx.route(mod)
65
- } else appCtx.route(mod)
66
- }
53
+ if (!Array.isArray(mods)) mods = [mods]
54
+ for (const mod of mods) {
55
+ mod.url = cleanPath(`/${appPrefix}${mod.url}`)
56
+ if (isRouteDisabled(prefix === '' ? '' : mod.url)) continue
57
+ const rpath = await reroutedPath.call(this, mod.url, me.config.rerouted)
58
+ if (me.config.format.asExt) mod.url = formatExt(mod.url)
59
+ if (rpath) {
60
+ mod.config.pathReroutedTo = rpath
61
+ this.log.warn('rerouted%s%s', mod.url, `${prefix}${rpath}`)
62
+ mod.url = me.config.format.asExt ? formatExt(rpath) : rpath
63
+ me.webAppCtx.route(mod)
64
+ } else me.webAppCtx.route(mod)
67
65
  }
68
- }, { prefix: appPrefix })
66
+ }
69
67
  })
70
68
  await runHook(`${this.ns}:afterCreateRoutes`, this.webAppCtx)
71
69
  await this._handleSubApp()
package/index.js CHANGED
@@ -57,13 +57,13 @@ async function factory (pkgName) {
57
57
  * @returns {string} The generated route path
58
58
  */
59
59
  routePath = (name, { defFormat = '.json', uriEncoded = true } = {}) => {
60
- const { get, trimStart } = this.app.lib._
61
60
  const { fullPath, ns } = this.app.bajo.breakNsPath(name)
62
- const prefix = get(this.app[ns], 'config.waibu.prefix', this.app[ns].alias)
61
+ const { getPluginPrefix, cleanPath } = this.app.waibu
62
+ const prefix = getPluginPrefix(ns, 'waibuRestApi')
63
63
  const format = defFormat === false ? '' : (this.config.format.asExt ? defFormat : '')
64
64
  let path = `/${this.config.waibu.prefix}/${prefix}${fullPath}${format}`
65
65
  if (uriEncoded) path = path.split('/').map(p => encodeURI(p)).join('/')
66
- return '/' + trimStart(path, '/')
66
+ return cleanPath(path)
67
67
  }
68
68
 
69
69
  /**
@@ -577,7 +577,6 @@ async function factory (pkgName) {
577
577
  * @method
578
578
  * @param {Object} options - The options for building the route
579
579
  * @param {string} options.file - The file path of the route configuration
580
- * @param {Object} options.appCtx - The application context
581
580
  * @param {string} options.dir - The directory of the route configuration
582
581
  * @param {string} options.pathPrefix - The path prefix for the route
583
582
  * @param {string} options.ns - The namespace
@@ -586,7 +585,7 @@ async function factory (pkgName) {
586
585
  * @returns {Object} The built route
587
586
  */
588
587
  _routeByVerb = async (options) => {
589
- const { file, appCtx, dir, pathPrefix, ns, alias, parent } = options
588
+ const { file, dir, pathPrefix, ns, alias, parent } = options
590
589
  const me = this
591
590
  const url = path.dirname(file).replace(`${dir}/extend/${pathPrefix}`, '')
592
591
  .replaceAll('@@', '*').replaceAll('@', ':')
@@ -620,7 +619,7 @@ async function factory (pkgName) {
620
619
  $ref: '5xxResp#'
621
620
  }
622
621
  if (action === 'find') defSchema.querystring = { $ref: 'qsFilter#' }
623
- if (isFunction(mod.schema)) mod.schema = await mod.schema.call(this, appCtx)
622
+ if (isFunction(mod.schema)) mod.schema = await mod.schema.call(this)
624
623
  mod.schema = merge({}, defSchema, mod.schema ?? {})
625
624
  const oldHandler = mod.handler
626
625
  mod.handler = async function (req, reply) {
package/lib/config.js CHANGED
@@ -2,7 +2,6 @@
2
2
  * @typedef {Object} TConfig
3
3
  * @property {Object} [waibu={}] - Waibu configuration
4
4
  * @property {string} [waibu.prefix='api'] - Prefix for the Waibu REST API routes
5
- * @property {boolean} [mountMainAsRoot=true] - Whether to mount the main route as root
6
5
  * @property {boolean} [exposeHead=true] - Whether to expose the HEAD method
7
6
  * @property {boolean} [enablePatch=false] - Whether to enable the PATCH method
8
7
  * @property {number} [bodyLimit=1048576] - Maximum size of the request body in bytes
@@ -38,7 +37,6 @@ const config = {
38
37
  waibu: {
39
38
  prefix: 'api'
40
39
  },
41
- mountMainAsRoot: true,
42
40
  exposeHead: true,
43
41
  enablePatch: false,
44
42
  bodyLimit: 1048576,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-rest-api",
3
- "version": "2.15.0",
3
+ "version": "2.16.1",
4
4
  "description": "REST API for Waibu",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/wiki/CHANGES.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-11-12
4
+
5
+ - [2.16.1] Bug fix in `routePath()`
6
+
7
+ ## 2026-11-10
8
+
9
+ - [2.16.0] Remove all module context and start using one `webAppCtx`
10
+
3
11
  ## 2026-10-21
4
12
 
5
13
  - [2.15.0] Add support for `_attachments`