waibu-rest-api 2.6.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.
@@ -4,7 +4,7 @@ import serveRoute from './serve-route.js'
4
4
  async function routeByModelBuilder ({ file, dir, pathPrefix, ns, parent }) {
5
5
  const { readConfig } = this.app.bajo
6
6
  const { parseObject } = this.app.lib
7
- const { isSet } = this.app.lib.aneka
7
+ const { isSet, pascalCase } = this.app.lib.aneka
8
8
  const { methodMap, getParams } = this.app.waibuDb
9
9
  const { isFunction, camelCase, omit, merge, keys, filter } = this.app.lib._
10
10
  const builder = await readConfig(file, { ns, ignoreError: true })
@@ -15,14 +15,16 @@ async function routeByModelBuilder ({ file, dir, pathPrefix, ns, parent }) {
15
15
  if (this.config.enablePatch) methods.push('replace')
16
16
  const me = this
17
17
  const mods = []
18
- const model = this.app.dobo.getModel(builder.model)
18
+ let model = {}
19
+ if (!builder.model.startsWith(':')) model = this.app.dobo.getModel(builder.model)
19
20
  for (const method of methods) {
20
- let disabled = model.disabled.includes(method) || builder.disabled.includes(method)
21
- if (method === 'replace' && (model.disabled.includes('update') || (builder.disabled ?? []).includes('update'))) disabled = true
21
+ let disabled = (model.disabled ?? []).includes(method) || builder.disabled.includes(method)
22
+ if (method === 'replace' && ((model.disabled ?? []).includes('update') || (builder.disabled ?? []).includes('update'))) disabled = true
22
23
  if (disabled) continue
23
24
  const mod = omit(builder, ['model', 'url', 'method', 'handler', 'hidden', ...methods])
24
25
  const customMod = builder[method] ?? {}
25
- if (!customMod.schema) customMod.schema = await this.docSchemaModel({ model: builder.model, method, options: { hidden: builder.hidden } })
26
+ customMod.schema = customMod.schema ?? false
27
+ if (customMod.schema === true && !builder.model.startsWith(':')) customMod.schema = await this.docSchemaModel({ model: builder.model, method, options: { hidden: builder.hidden } })
26
28
  let mapmethod = methodMap[method]
27
29
  if (this.config.enablePatch) {
28
30
  if (method === 'replace') mapmethod = 'PUT'
@@ -44,14 +46,15 @@ async function routeByModelBuilder ({ file, dir, pathPrefix, ns, parent }) {
44
46
  options.queryHandler = builder.queryHandler
45
47
  if (method === 'replace') options.partial = false
46
48
  if (['get', 'remove', 'update'].includes(method)) options.throwNotFound = true
47
- const data = await helper({ model: builder.model, req, reply, options, transaction: builder.transaction })
49
+ const model = builder.model.startsWith(':') ? pascalCase(req.params[builder.model.slice(1)]) : builder.model
50
+ const data = await helper({ model, req, reply, options, transaction: builder.transaction })
48
51
  data.success = true
49
52
  data.statusCode = method === 'create' ? 201 : 200
50
53
  if (method === 'find') options.forFind = true
51
54
  return me.transformResult({ data, req, reply, options })
52
55
  }
53
56
  })
54
- if (builder.schema === false) delete mod.schema
57
+ if (mod.schema === false) delete mod.schema
55
58
  mod.config = mod.config ?? {}
56
59
  mod.config.pathSrc = url
57
60
  mods.push(await serveRoute.call(me, { mod, ns, parent }))
@@ -60,7 +63,7 @@ async function routeByModelBuilder ({ file, dir, pathPrefix, ns, parent }) {
60
63
  const supported = ['aggregate', 'histogram']
61
64
  builder.stat = filter(builder.stat ?? supported, item => supported.includes(item))
62
65
  const method = 'GET'
63
- const disabled = model.disabled.includes(method) || builder.disabled.includes(method)
66
+ const disabled = (model.disabled ?? []).includes(method) || builder.disabled.includes(method)
64
67
  if (disabled) return mods
65
68
  const mod = {
66
69
  schema: false,
@@ -71,7 +74,8 @@ async function routeByModelBuilder ({ file, dir, pathPrefix, ns, parent }) {
71
74
  const helper = me.app.waibuDb[camelCase(`create ${req.params.stat}`)]
72
75
  if (!helper) throw this.error('_notFound')
73
76
  const options = { queryHandler: builder.queryHandler }
74
- const data = await helper({ model: builder.model, req, reply, options })
77
+ const model = builder.model.startsWith(':') ? pascalCase(req.params[builder.model.slice(1)]) : builder.model
78
+ const data = await helper({ model, req, reply, options })
75
79
  data.success = true
76
80
  data.statusCode = method === 'create' ? 201 : 200
77
81
  options.forFind = true
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-rest-api",
3
- "version": "2.6.0",
3
+ "version": "2.7.0",
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-03-14
4
+
5
+ - [2.7.0] Accept model's name from ```req.params``` in ```route-by-model-builder.js```
6
+
7
+ ## 2026-03-06
8
+
9
+ - [2.6.1] Bug fix on schema enforcement. Only apply when it's stated explicitely in route handler
10
+
3
11
  ## 2026-03-05
4
12
 
5
13
  - [2.6.0] Add transaction support by setting property ```transaction: true``` on model's route handler definition