waibu-rest-api 2.12.0 → 2.14.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 @@ async function hook () {
4
4
  name: 'waibuRestApi:preParsing',
5
5
  handler: async function (req, reply) {
6
6
  const { importModule } = this.app.bajo
7
- const attachIntl = await importModule('waibu:/lib/webapp-scope/attach-intl.js')
7
+ const { attachIntl } = await importModule('waibu:/lib/webapp.js', { asDefaultImport: false })
8
8
  await attachIntl.call(this, this.config.intl.detectors, req, reply)
9
9
  reply.header('Content-Language', req.lang)
10
10
  if (this.config.format.asExt && req.params.format) {
@@ -1,13 +1,4 @@
1
1
  import path from 'path'
2
- import decorate from '../../lib/decorate.js'
3
- import routeByModelBuilder from '../../lib/route-by-model-builder.js'
4
- import routeByVerb from '../../lib/route-by-verb.js'
5
- import notFoundHandler from '../../lib/not-found.js'
6
- import errorHandler from '../../lib/error.js'
7
- import subApp from '../../lib/sub-app.js'
8
- import handleResponse from '../../lib/handle-response.js'
9
-
10
- const routeActions = { routeByModelBuilder, routeByVerb }
11
2
 
12
3
  function formatExt (item) {
13
4
  return item + '.:format'
@@ -15,26 +6,19 @@ function formatExt (item) {
15
6
 
16
7
  const boot = {
17
8
  level: 10,
18
- notFoundHandler,
19
- errorHandler,
20
9
  handler: async function (prefix) {
21
10
  const { importPkg, eachPlugins, importModule, runHook } = this.app.bajo
22
11
  const { fastGlob } = this.app.lib
23
- const { getPluginPrefix } = this.app.waibu
12
+ const { getPluginPrefix, isRouteDisabled } = this.app.waibu
24
13
  const [bodyParser, accepts] = await importPkg('waibu:@fastify/formbody', 'waibu:@fastify/accepts')
25
- const routeHook = await importModule('waibu:/lib/webapp-scope/route-hook.js')
26
- const handleMultipart = await importModule('waibu:/lib/webapp-scope/handle-multipart-body.js')
27
- const handleXmlBody = await importModule('waibu:/lib/handle-xml-body.js')
28
- const handleCors = await importModule('waibu:/lib/webapp-scope/handle-cors.js')
29
- const handleHelmet = await importModule('waibu:/lib/webapp-scope/handle-helmet.js')
30
- const handleCompress = await importModule('waibu:/lib/webapp-scope/handle-compress.js')
31
- const handleRateLimit = await importModule('waibu:/lib/webapp-scope/handle-rate-limit.js')
32
- const reroutedPath = await importModule('waibu:/lib/webapp-scope/rerouted-path.js')
14
+ const {
15
+ routeHook, handleMultipartBody, handleXmlBody, handleCors, handleHelmet, handleCompress,
16
+ handleRateLimit, reroutedPath
17
+ } = await importModule('waibu:/lib/webapp.js', { asDefaultImport: false })
33
18
 
34
19
  const pathPrefix = `${this.ns}/route`
35
20
  await this.docSchemaGeneral()
36
21
  await routeHook.call(this, this.ns)
37
- await decorate.call(this)
38
22
  if (this.config.format.supported.includes('xml')) {
39
23
  await handleXmlBody.call(this, this.config.format.xml.bodyParser)
40
24
  }
@@ -43,9 +27,9 @@ const boot = {
43
27
  await handleRateLimit.call(this, this.config.rateLimit)
44
28
  await handleCors.call(this, this.config.cors)
45
29
  await handleHelmet.call(this, this.config.helmet)
46
- await handleMultipart.call(this, this.config.multipart)
30
+ await handleMultipartBody.call(this, this.config.multipart)
47
31
  await handleCompress.call(this, this.config.compress)
48
- await handleResponse.call(this)
32
+ await this._handleResponse()
49
33
  await runHook(`${this.ns}:beforeCreateRoutes`, this.webAppCtx)
50
34
  const actions = ['find', 'get', 'create', 'update', 'remove']
51
35
  if (this.config.enablePatch) actions.push('replace')
@@ -63,16 +47,14 @@ const boot = {
63
47
  await me.webAppCtx.register(async (appCtx) => {
64
48
  for (const file of files) {
65
49
  const base = path.basename(file, path.extname(file))
66
- const action = base === 'model-builder' ? 'routeByModelBuilder' : 'routeByVerb'
67
- let mods = await routeActions[action].call(me, { file, appCtx, dir, pathPrefix, ns, alias, parent: me.ns })
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 })
53
+
68
54
  if (!Array.isArray(mods)) mods = [mods]
69
55
  for (const mod of mods) {
70
56
  const fullPath = appPrefix === '/' ? mod.url : (appPrefix + mod.url)
71
- const isRouteDisabled = await importModule('waibu:/lib/webapp-scope/is-route-disabled.js')
72
- if (await isRouteDisabled.call(this, fullPath, mod.method, me.config.disabled)) {
73
- this.log.warn('routeDisabled%s%s', `${prefix}${fullPath}`, mod.method)
74
- continue
75
- }
57
+ if (isRouteDisabled(`${prefix === '' ? '' : `/${prefix}`}${fullPath}`)) continue
76
58
  const rpath = await reroutedPath.call(this, fullPath, me.config.rerouted)
77
59
  if (me.config.format.asExt) mod.url = formatExt(mod.url)
78
60
  if (rpath) {
@@ -86,7 +68,7 @@ const boot = {
86
68
  }, { prefix: appPrefix })
87
69
  })
88
70
  await runHook(`${this.ns}:afterCreateRoutes`, this.webAppCtx)
89
- await subApp.call(this)
71
+ await this._handleSubApp()
90
72
  }
91
73
  }
92
74