waibu-rest-api 1.1.6 → 1.1.7

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/lib/not-found.js CHANGED
@@ -1,4 +1,4 @@
1
- export function handler (ctx, req, reply) {
1
+ export function handler (req, reply, ctx) {
2
2
  const msg = this.print.write('routeNotFound%s%s', req.url, req.method)
3
3
  const data = ctx.httpErrors.createError(404, msg)
4
4
  return this.transformResult({ data, req, reply })
@@ -7,7 +7,7 @@ export function handler (ctx, req, reply) {
7
7
  async function notFound (ctx) {
8
8
  const me = this
9
9
  await ctx.setNotFoundHandler(async function (req, reply) {
10
- return handler.call(me, ctx, req, reply)
10
+ return handler.call(me, req, reply, ctx)
11
11
  })
12
12
  }
13
13
 
@@ -7,8 +7,9 @@ async function routeByModelBuilder ({ file, ctx, dir, pathPrefix, ns, parent })
7
7
  const { readConfig, parseObject, isSet } = this.app.bajo
8
8
  const { methodMap, getParams } = this.app.waibuDb
9
9
  const { getInfo } = this.app.dobo
10
- const { camelCase, omit, merge, keys } = this.lib._
10
+ const { camelCase, omit, merge, keys, filter } = this.lib._
11
11
  const builder = await readConfig(file, { ns, ignoreError: true })
12
+ builder.disabled = builder.disabled ?? []
12
13
  const { schema } = getInfo(builder.model)
13
14
  const url = path.dirname(file).replace(`${dir}/${pathPrefix}`, '').replaceAll('@', ':')
14
15
  const methods = keys(methodMap)
@@ -16,7 +17,7 @@ async function routeByModelBuilder ({ file, ctx, dir, pathPrefix, ns, parent })
16
17
  const me = this
17
18
  const mods = []
18
19
  for (const method of methods) {
19
- let disabled = schema.disabled.includes(method) || (builder.disabled ?? []).includes(method)
20
+ let disabled = schema.disabled.includes(method) || builder.disabled.includes(method)
20
21
  if (method === 'replace' && (schema.disabled.includes('update') || (builder.disabled ?? []).includes('update'))) disabled = true
21
22
  if (disabled) continue
22
23
  const mod = omit(builder, ['model', 'url', 'method', 'handler', 'hidden', ...methods])
@@ -54,6 +55,30 @@ async function routeByModelBuilder ({ file, ctx, dir, pathPrefix, ns, parent })
54
55
  mod.config.pathSrc = url
55
56
  mods.push(await serveRoute.call(me, { mod, ns, parent }))
56
57
  }
58
+ // stats
59
+ const supported = ['aggregate', 'histogram']
60
+ builder.stat = filter(builder.stat ?? supported, item => supported.includes(item))
61
+ const method = 'GET'
62
+ const disabled = schema.disabled.includes(method) || builder.disabled.includes(method)
63
+ if (disabled) return mods
64
+ const mod = {
65
+ schema: false,
66
+ method,
67
+ url: `${url}/stat/:stat`,
68
+ handler: async function (req, reply) {
69
+ if (!supported.includes(req.params.stat)) throw me.error('_notFound')
70
+ const helper = me.app.waibuDb[camelCase(`stat ${req.params.stat}`)]
71
+ const options = { queryHandler: builder.queryHandler }
72
+ const data = await helper({ model: builder.model, req, reply, ctx: this, options })
73
+ data.success = true
74
+ data.statusCode = method === 'create' ? 201 : 200
75
+ options.forFind = true
76
+ return me.transformResult({ data, req, reply, options })
77
+ }
78
+ }
79
+ mod.config = builder.config ?? {}
80
+ mod.config.pathSrc = url
81
+ mods.push(await serveRoute.call(me, { mod, ns, parent }))
57
82
  return mods
58
83
  }
59
84
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-rest-api",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "REST API for Waibu",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,6 +1,6 @@
1
1
  async function buildParams (ctx, paramName, ...args) {
2
2
  const { each, isEmpty, keys, last, isBoolean } = this.lib._
3
- const cfgWeb = this.app.waibu.config
3
+ const cfgWeb = this.app.waibu.getConfig()
4
4
  let transform = false
5
5
  if (isBoolean(last(args))) {
6
6
  transform = args.pop()