waibu-rest-api 2.2.1 → 2.3.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.
@@ -2,8 +2,8 @@ import path from 'path'
2
2
  import decorate from '../../lib/decorate.js'
3
3
  import routeByModelBuilder from '../../lib/route-by-model-builder.js'
4
4
  import routeByVerb from '../../lib/route-by-verb.js'
5
- import notFound from '../../lib/not-found.js'
6
- import error from '../../lib/error.js'
5
+ // import notFound from '../../lib/not-found.js'
6
+ // import error from '../../lib/error.js'
7
7
  import subApp from '../../lib/sub-app.js'
8
8
  import handleResponse from '../../lib/handle-response.js'
9
9
 
@@ -15,7 +15,7 @@ function formatExt (item) {
15
15
 
16
16
  const boot = {
17
17
  level: 10,
18
- handler: async function (ctx, prefix) {
18
+ handler: async function (prefix) {
19
19
  const { importPkg, eachPlugins, importModule, runHook } = this.app.bajo
20
20
  const { fastGlob } = this.app.lib
21
21
  const { getPluginPrefix } = this.app.waibu
@@ -30,22 +30,22 @@ const boot = {
30
30
  const reroutedPath = await importModule('waibu:/lib/webapp-scope/rerouted-path.js')
31
31
 
32
32
  const pathPrefix = `${this.ns}/route`
33
- await this.docSchemaGeneral(ctx)
33
+ await this.docSchemaGeneral()
34
34
  await routeHook.call(this, this.ns)
35
- await decorate.call(this, ctx)
35
+ await decorate.call(this)
36
36
  if (this.config.format.supported.includes('xml')) {
37
- await handleXmlBody.call(this, ctx, this.config.format.xml.bodyParser)
37
+ await handleXmlBody.call(this, this.config.format.xml.bodyParser)
38
38
  }
39
- await ctx.register(accepts)
40
- await ctx.register(bodyParser)
41
- await handleRateLimit.call(this, ctx, this.config.rateLimit)
42
- await handleCors.call(this, ctx, this.config.cors)
43
- await handleHelmet.call(this, ctx, this.config.helmet)
44
- await handleMultipart.call(this, ctx, this.config.multipart)
45
- await handleCompress.call(this, ctx, this.config.compress)
46
- await handleResponse.call(this, ctx)
47
- await error.call(this, ctx)
48
- await runHook(`${this.ns}:beforeCreateRoutes`, ctx)
39
+ await this.webAppCtx.register(accepts)
40
+ await this.webAppCtx.register(bodyParser)
41
+ await handleRateLimit.call(this, this.config.rateLimit)
42
+ await handleCors.call(this, this.config.cors)
43
+ await handleHelmet.call(this, this.config.helmet)
44
+ await handleMultipart.call(this, this.config.multipart)
45
+ await handleCompress.call(this, this.config.compress)
46
+ await handleResponse.call(this)
47
+ // await error.call(this)
48
+ await runHook(`${this.ns}:beforeCreateRoutes`, this.webAppCtx)
49
49
  const actions = ['find', 'get', 'create', 'update', 'remove']
50
50
  if (this.config.enablePatch) actions.push('replace')
51
51
  const me = this
@@ -59,11 +59,11 @@ const boot = {
59
59
  ]
60
60
  const files = await fastGlob(pattern)
61
61
  if (files.length === 0) return undefined
62
- await ctx.register(async (appCtx) => {
62
+ await me.webAppCtx.register(async (appCtx) => {
63
63
  for (const file of files) {
64
64
  const base = path.basename(file, path.extname(file))
65
65
  const action = base === 'model-builder' ? 'routeByModelBuilder' : 'routeByVerb'
66
- let mods = await routeActions[action].call(me, { file, appCtx, ctx, dir, pathPrefix, ns, alias, parent: me.ns })
66
+ let mods = await routeActions[action].call(me, { file, appCtx, dir, pathPrefix, ns, alias, parent: me.ns })
67
67
  if (!Array.isArray(mods)) mods = [mods]
68
68
  for (const mod of mods) {
69
69
  const fullPath = appPrefix === '/' ? mod.url : (appPrefix + mod.url)
@@ -78,15 +78,15 @@ const boot = {
78
78
  mod.config.pathReroutedTo = rpath
79
79
  this.log.warn('rerouted%s%s', `${prefix}${fullPath}`, `${prefix}${rpath}`)
80
80
  mod.url = me.config.format.asExt ? formatExt(rpath) : rpath
81
- await ctx.route(mod)
81
+ await me.webAppCtx.route(mod)
82
82
  } else await appCtx.route(mod)
83
83
  }
84
84
  }
85
85
  }, { prefix: appPrefix })
86
86
  })
87
- await runHook(`${this.ns}:afterCreateRoutes`, ctx)
88
- await subApp.call(this, ctx)
89
- await notFound.call(this, ctx)
87
+ await runHook(`${this.ns}:afterCreateRoutes`, this.webAppCtx)
88
+ await subApp.call(this)
89
+ // await notFound.call(this)
90
90
  }
91
91
  }
92
92
 
package/lib/decorate.js CHANGED
@@ -1,4 +1,4 @@
1
- async function decorate (ctx) {
1
+ async function decorate () {
2
2
  }
3
3
 
4
4
  export default decorate
@@ -1,4 +1,4 @@
1
- async function docSchemaForFind (ctx, items) {
1
+ async function docSchemaForFind (items) {
2
2
  return {
3
3
  data: {
4
4
  type: 'array',
@@ -1,4 +1,4 @@
1
- async function buildErrResp (ctx) {
1
+ async function buildErrResp () {
2
2
  const { cloneDeep, merge, each, get } = this.app.lib._
3
3
  const cfg = this.config
4
4
  const cfgWdb = this.app.waibuDb.config
@@ -43,12 +43,12 @@ async function buildErrResp (ctx) {
43
43
  props[key] = item.properties[k]
44
44
  })
45
45
  item.properties = props
46
- await this.docSchemaLib(ctx, type + 'Resp', item)
46
+ await this.docSchemaLib(type + 'Resp', item)
47
47
  }
48
48
  }
49
49
 
50
- async function buildFilter (ctx) {
51
- await this.docSchemaParams(ctx, 'qsFilter',
50
+ async function buildFilter () {
51
+ await this.docSchemaParams('qsFilter',
52
52
  'query||NQL/Mongo Query. Leave empty to disable query',
53
53
  'limit|integer|Number of records per page. Must be >= 1|' + this.app.dobo.config.default.filter.limit,
54
54
  'page|integer|Desired page number. Must be >= 1|1',
@@ -58,22 +58,22 @@ async function buildFilter (ctx) {
58
58
  )
59
59
  }
60
60
 
61
- async function buildFields (ctx) {
62
- await this.docSchemaParams(ctx, 'qsFields',
61
+ async function buildFields () {
62
+ await this.docSchemaParams('qsFields',
63
63
  'fields||Comma delimited fields to show. Leave empty to show all fields',
64
64
  true
65
65
  )
66
66
  }
67
67
 
68
- async function buildParamsId (ctx) {
69
- await this.docSchemaParams(ctx, 'paramsId', 'id||Record ID')
68
+ async function buildParamsId () {
69
+ await this.docSchemaParams('paramsId', 'id||Record ID')
70
70
  }
71
71
 
72
- async function docSchemaGeneral (ctx) {
73
- await buildErrResp.call(this, ctx)
74
- await buildFilter.call(this, ctx)
75
- await buildParamsId.call(this, ctx)
76
- await buildFields.call(this, ctx)
72
+ async function docSchemaGeneral () {
73
+ await buildErrResp.call(this)
74
+ await buildFilter.call(this)
75
+ await buildParamsId.call(this)
76
+ await buildFields.call(this)
77
77
  }
78
78
 
79
79
  export default docSchemaGeneral
@@ -1,8 +1,8 @@
1
- async function docSchemaLib (ctx, name, obj) {
1
+ async function docSchemaLib (name, obj) {
2
2
  const { merge } = this.app.lib._
3
- if (ctx.getSchema(name)) return
3
+ if (this.webAppCtx.getSchema(name)) return
4
4
  const value = merge({}, obj, { $id: name })
5
- ctx.addSchema(value)
5
+ this.webAppCtx.addSchema(value)
6
6
  }
7
7
 
8
8
  export default docSchemaLib
@@ -46,7 +46,7 @@ async function buildPropsReqs ({ model, method, options = {} }) {
46
46
  return { properties, required }
47
47
  }
48
48
 
49
- async function buildResponse ({ ctx, model, method, options }) {
49
+ async function buildResponse ({ model, method, options }) {
50
50
  const { merge, cloneDeep } = this.app.lib._
51
51
  const { transformResult } = this.app.waibuRestApi
52
52
  const cfgWdb = this.app.waibuDb.config
@@ -57,7 +57,7 @@ async function buildResponse ({ ctx, model, method, options }) {
57
57
  for (const k of keys) {
58
58
  const name = 'model' + model.name
59
59
  const props = cloneDeep(properties)
60
- await this.docSchemaLib(ctx, name, {
60
+ await this.docSchemaLib(name, {
61
61
  type: 'object',
62
62
  properties: props
63
63
  })
@@ -105,14 +105,14 @@ async function buildResponse ({ ctx, model, method, options }) {
105
105
  result['2xx'].properties = transformResult({ data: merge({}, await buildData.call(this, ['oldData']), { success, statusCode }) })
106
106
  } else if (method === 'find') {
107
107
  result['2xx'].properties = transformResult({
108
- data: await this.docSchemaForFind(ctx, { type: 'object', properties }),
108
+ data: await this.docSchemaForFind({ type: 'object', properties }),
109
109
  options: { forFind: true }
110
110
  })
111
111
  }
112
112
  return result
113
113
  }
114
114
 
115
- async function docSchemaModel ({ model, method, ctx, options = {} }) {
115
+ async function docSchemaModel ({ model, method, options = {} }) {
116
116
  const mdl = this.app.dobo.getModel(model)
117
117
  const { omit } = this.app.lib._
118
118
  const out = {
@@ -130,7 +130,7 @@ async function docSchemaModel ({ model, method, ctx, options = {} }) {
130
130
  const { properties } = await buildPropsReqs.call(this, { model: mdl, method, options })
131
131
  const name = 'model' + mdl.name + 'Update'
132
132
  delete properties._rel
133
- await this.docSchemaLib(ctx, name, {
133
+ await this.docSchemaLib(name, {
134
134
  type: 'object',
135
135
  properties: omit(properties, ['id'])
136
136
  })
@@ -140,7 +140,7 @@ async function docSchemaModel ({ model, method, ctx, options = {} }) {
140
140
  const { properties, required } = await buildPropsReqs.call(this, { model: mdl, method, options })
141
141
  const name = 'model' + model.name + 'Replace'
142
142
  delete properties._rel
143
- await this.docSchemaLib(ctx, name, {
143
+ await this.docSchemaLib(name, {
144
144
  type: 'object',
145
145
  properties: omit(properties, ['id']),
146
146
  required
@@ -151,7 +151,7 @@ async function docSchemaModel ({ model, method, ctx, options = {} }) {
151
151
  const { properties, required } = await buildPropsReqs.call(this, { model: mdl, method, options })
152
152
  const name = 'model' + model.name + 'Create'
153
153
  delete properties._rel
154
- await this.docSchemaLib(ctx, name, {
154
+ await this.docSchemaLib(name, {
155
155
  type: 'object',
156
156
  properties,
157
157
  required
@@ -159,7 +159,7 @@ async function docSchemaModel ({ model, method, ctx, options = {} }) {
159
159
  out.body = { $ref: name + '#' }
160
160
  out.querystring = { $ref: 'qsFields#' }
161
161
  }
162
- out.response = await buildResponse.call(this, { ctx, model: mdl, method, options })
162
+ out.response = await buildResponse.call(this, { model: mdl, method, options })
163
163
  return out
164
164
  }
165
165
 
@@ -1,4 +1,4 @@
1
- async function docSchemaParams (ctx, paramName, ...args) {
1
+ async function docSchemaParams (paramName, ...args) {
2
2
  const { each, isEmpty, keys, last, isBoolean } = this.app.lib._
3
3
  const cfgWeb = this.app.waibu.getConfig()
4
4
  let transform = false
@@ -22,7 +22,7 @@ async function docSchemaParams (ctx, paramName, ...args) {
22
22
  delete item.properties[k]
23
23
  })
24
24
  }
25
- if (!isEmpty(args)) await this.docSchemaLib(ctx, paramName, item)
25
+ if (!isEmpty(args)) await this.docSchemaLib(paramName, item)
26
26
  }
27
27
 
28
28
  export default docSchemaParams
@@ -1,4 +1,4 @@
1
- async function docSchemaQs (ctx, paramName, ...args) {
1
+ async function docSchemaQs (paramName, ...args) {
2
2
  const { each, isEmpty } = this.app.lib._
3
3
  const item = {
4
4
  type: 'object',
@@ -9,7 +9,7 @@ async function docSchemaQs (ctx, paramName, ...args) {
9
9
  if (isEmpty(type)) type = 'string'
10
10
  item.properties[name] = { type, description: this.t(description) }
11
11
  })
12
- if (!isEmpty(args)) await this.docSchemaLib(ctx, paramName, item)
12
+ if (!isEmpty(args)) await this.docSchemaLib(paramName, item)
13
13
  }
14
14
 
15
15
  export default docSchemaQs
package/lib/error.js CHANGED
@@ -1,21 +1,21 @@
1
1
  import { handler } from './not-found.js'
2
2
 
3
- const extHandler = async function (err, req, reply, ctx) {
3
+ const extHandler = async function (err, req, reply) {
4
4
  err.statusCode = err.statusCode ?? 500
5
5
  const msg = err.message
6
6
  if (msg === '_notFound' || err.statusCode === 404) {
7
- return handler.call(this, req, reply, ctx)
7
+ return handler.call(this, req, reply)
8
8
  }
9
- const data = ctx.httpErrors.createError(err.statusCode, msg)
9
+ const data = this.webAppCtx.httpErrors.createError(err.statusCode, msg)
10
10
  data.statusCode = err.statusCode
11
11
  data.details = err.details
12
12
  return this.transformResult({ data, req, reply })
13
13
  }
14
14
 
15
- async function error (ctx) {
15
+ async function error () {
16
16
  const { importModule } = this.app.bajo
17
17
  const errorHandler = await importModule('waibu:/lib/webapp-scope/error-handler.js')
18
- await errorHandler.call(this, ctx, extHandler)
18
+ await errorHandler.call(this, extHandler)
19
19
  }
20
20
 
21
21
  export default error
@@ -1,9 +1,9 @@
1
1
  import xml from './handle-response/xml.js'
2
2
 
3
- async function handleResponse (ctx) {
3
+ async function handleResponse () {
4
4
  const me = this
5
5
 
6
- ctx.addHook('onSend', async function (req, reply, payload) {
6
+ this.webAppCtx.addHook('onSend', async function (req, reply, payload) {
7
7
  let type = req.params.format
8
8
  if (!type) {
9
9
  const types = req.accepts().types()
package/lib/not-found.js CHANGED
@@ -1,13 +1,13 @@
1
- export function handler (req, reply, ctx) {
1
+ export function handler (req, reply) {
2
2
  const msg = this.t('routeNotFound%s%s', req.url, req.method)
3
- const data = ctx.httpErrors.createError(404, msg)
3
+ const data = this.webAppCtx.httpErrors.createError(404, msg)
4
4
  return this.transformResult({ data, req, reply })
5
5
  }
6
6
 
7
- async function notFound (ctx) {
7
+ async function notFound () {
8
8
  const me = this
9
- await ctx.setNotFoundHandler(async function (req, reply) {
10
- return handler.call(me, req, reply, ctx)
9
+ await this.webAppCtx.setNotFoundHandler(async function (req, reply) {
10
+ return handler.call(me, req, reply)
11
11
  })
12
12
  }
13
13
 
@@ -1,7 +1,7 @@
1
1
  import path from 'path'
2
2
  import serveRoute from './serve-route.js'
3
3
 
4
- async function routeByModelBuilder ({ file, ctx, dir, pathPrefix, ns, parent }) {
4
+ async function routeByModelBuilder ({ file, dir, pathPrefix, ns, parent }) {
5
5
  const { readConfig } = this.app.bajo
6
6
  const { parseObject } = this.app.lib
7
7
  const { isSet } = this.app.lib.aneka
@@ -22,7 +22,7 @@ async function routeByModelBuilder ({ file, ctx, dir, pathPrefix, ns, parent })
22
22
  if (disabled) continue
23
23
  const mod = omit(builder, ['model', 'url', 'method', 'handler', 'hidden', ...methods])
24
24
  const customMod = builder[method] ?? {}
25
- if (!customMod.schema) customMod.schema = await this.docSchemaModel({ model: builder.model, method, ctx, options: { hidden: builder.hidden } })
25
+ if (!customMod.schema) customMod.schema = await this.docSchemaModel({ model: builder.model, method, options: { hidden: builder.hidden } })
26
26
  let mapmethod = methodMap[method]
27
27
  if (this.config.enablePatch) {
28
28
  if (method === 'replace') mapmethod = 'PUT'
@@ -44,7 +44,7 @@ async function routeByModelBuilder ({ file, ctx, dir, pathPrefix, ns, parent })
44
44
  options.queryHandler = builder.queryHandler
45
45
  if (method === 'replace') options.partial = false
46
46
  if (['get', 'remove', 'update'].includes(method)) options.throwNotFound = true
47
- const data = await helper({ model: builder.model, req, reply, ctx: this, options })
47
+ const data = await helper({ model: builder.model, req, reply, options })
48
48
  data.success = true
49
49
  data.statusCode = method === 'create' ? 201 : 200
50
50
  if (method === 'find') options.forFind = true
@@ -71,7 +71,7 @@ async function routeByModelBuilder ({ file, ctx, dir, pathPrefix, ns, parent })
71
71
  const helper = me.app.waibuDb[camelCase(`create ${req.params.stat}`)]
72
72
  if (!helper) throw this.error('_notFound')
73
73
  const options = { queryHandler: builder.queryHandler }
74
- const data = await helper({ model: builder.model, req, reply, ctx: this, options })
74
+ const data = await helper({ model: builder.model, req, reply, options })
75
75
  data.success = true
76
76
  data.statusCode = method === 'create' ? 201 : 200
77
77
  options.forFind = true
@@ -1,7 +1,7 @@
1
1
  import path from 'path'
2
2
  import serveRoute from './serve-route.js'
3
3
 
4
- async function routeByVerb ({ file, ctx, appCtx, dir, pathPrefix, ns, alias, parent }) {
4
+ async function routeByVerb ({ file, appCtx, dir, pathPrefix, ns, alias, parent }) {
5
5
  const { importModule } = this.app.bajo
6
6
  const { parseObject } = this.app.lib
7
7
  const { isSet } = this.app.lib.aneka
@@ -18,7 +18,7 @@ async function routeByVerb ({ file, ctx, appCtx, dir, pathPrefix, ns, alias, par
18
18
  }
19
19
  let mod = await importModule(file)
20
20
  if (isFunction(mod)) {
21
- if (mod.length <= 1) mod = await mod.call(this, { ctx })
21
+ if (mod.length <= 1) mod = await mod.call(this)
22
22
  else mod = { handler: mod }
23
23
  }
24
24
  mod.url = mod.url ?? url
@@ -40,7 +40,7 @@ async function routeByVerb ({ file, ctx, appCtx, dir, pathPrefix, ns, alias, par
40
40
  $ref: '5xxResp#'
41
41
  }
42
42
  if (action === 'find') defSchema.querystring = { $ref: 'qsFilter#' }
43
- if (isFunction(mod.schema)) mod.schema = await mod.schema.call(this, appCtx, ctx)
43
+ if (isFunction(mod.schema)) mod.schema = await mod.schema.call(this, appCtx)
44
44
  mod.schema = merge({}, defSchema, mod.schema ?? {})
45
45
  const oldHandler = mod.handler
46
46
  mod.handler = async function (req, reply) {
package/lib/sub-app.js CHANGED
@@ -1,10 +1,10 @@
1
- async function subApp (ctx) {
1
+ async function subApp () {
2
2
  const { importModule, runHook } = this.app.bajo
3
3
  const { collect } = await importModule('waibu:/lib/app.js', { asDefaultImport: false })
4
4
  const mods = await collect.call(this.app[this.ns], 'boot.js', 'waibuRestApi')
5
5
  for (const m of mods) {
6
6
  this.log.debug('bootSubApp%s', m.ns)
7
- await ctx.register(async (subCtx) => {
7
+ await this.webAppCtx.register(async (subCtx) => {
8
8
  this.app[m.ns].instance = subCtx
9
9
  await runHook(`${this.ns}.${m.alias}:afterCreateContext`, subCtx)
10
10
  await m.handler.call(this.app[m.ns], subCtx, m.prefix)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-rest-api",
3
- "version": "2.2.1",
3
+ "version": "2.3.0",
4
4
  "description": "REST API for Waibu",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/wiki/CHANGES.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-02-08
4
+
5
+ - [2.3.0] Simplify webApp context through ```plugin.webAppCtx```
6
+
3
7
  ## 2026-01-30
4
8
 
5
9
  - [2.2.1] Bug fix if output format is in xml