waibu-rest-api 1.1.2 → 1.1.4

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/error.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import { handler } from './not-found.js'
2
2
 
3
3
  const extHandler = async function (err, req, reply, ctx) {
4
- const code = err.statusCode ?? 500
4
+ err.statusCode = err.statusCode ?? 500
5
5
  const msg = err.message
6
- if (msg === '_notFound') {
6
+ if (msg === '_notFound' || err.statusCode === 404) {
7
7
  return handler.call(this, req, reply, ctx)
8
8
  }
9
- const data = ctx.httpErrors.createError(code, msg)
10
- data.statusCode = code
9
+ const data = ctx.httpErrors.createError(err.statusCode, msg)
10
+ data.statusCode = err.statusCode
11
11
  data.details = err.details
12
12
  return this.transformResult({ data, req, reply })
13
13
  }
@@ -1,7 +1,7 @@
1
1
  async function xml (req, reply, payload) {
2
2
  const { importPkg } = this.app.bajo
3
3
  const { XMLBuilder } = await importPkg('bajoExtra:fast-xml-parser', { returnDefault: false })
4
- const { get, set, isPlainObject, isArray } = this.app.bajo.lib._
4
+ const { get, set, isPlainObject, isArray } = this.lib._
5
5
  const { wrapper, valueAsAttributes, declaration } = this.config.format.xml.response
6
6
 
7
7
  reply.type('application/xml; charset=utf-8')
@@ -7,7 +7,7 @@ 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.app.bajo.lib._
10
+ const { camelCase, omit, merge, keys } = this.lib._
11
11
  const builder = await readConfig(file, { ns, ignoreError: true })
12
12
  const { schema } = getInfo(builder.model)
13
13
  const url = path.dirname(file).replace(`${dir}/${pathPrefix}`, '').replaceAll('@', ':')
@@ -4,7 +4,7 @@ import serveRoute from './serve-route.js'
4
4
  async function routeByVerb ({ file, ctx, appCtx, dir, pathPrefix, ns, alias, parent }) {
5
5
  const { importModule, parseObject, isSet } = this.app.bajo
6
6
  const { methodMap, getParams } = this.app.waibuDb
7
- const { isFunction, merge } = this.app.bajo.lib._
7
+ const { isFunction, merge } = this.lib._
8
8
  const me = this
9
9
  const url = path.dirname(file).replace(`${dir}/${pathPrefix}`, '')
10
10
  .replaceAll('@@', '*').replaceAll('@', ':')
@@ -1,7 +1,7 @@
1
1
  async function serveRoute ({ mod, ns, parent }) {
2
2
  const { defaultsDeep, importModule } = this.app.bajo
3
3
  const { getPluginPrefix } = this.app.waibu
4
- const { pick } = this.app.bajo.lib._
4
+ const { pick } = this.lib._
5
5
  const mergeRouteHooks = await importModule('waibu:/lib/webapp-scope/merge-route-hooks.js')
6
6
  const cfgWdb = this.app.waibuDb.config
7
7
  mod.config = mod.config ?? {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-rest-api",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "description": "REST API for Waibu",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/plugin/factory.js CHANGED
@@ -60,22 +60,23 @@ async function factory (pkgName) {
60
60
  }
61
61
 
62
62
  init = async () => {
63
- const { uniq } = this.app.bajo.lib._
63
+ const { uniq } = this.lib._
64
64
  this.config.format.supported.push('json')
65
65
  this.config.format.supported = uniq(this.config.format.supported)
66
66
  }
67
67
 
68
- routePath = (name) => {
69
- const { get, trimStart } = this.app.bajo.lib._
68
+ routePath = (name, { defFormat = '.json' } = {}) => {
69
+ const { get, trimStart } = this.lib._
70
70
  const { fullPath, ns } = this.app.bajo.breakNsPath(name)
71
71
  const prefix = get(this.app[ns], 'config.waibu.prefix', this.app[ns].alias)
72
- const path = `/${this.config.waibu.prefix}/${prefix}${fullPath}${this.config.format.asExt ? '.json' : ''}`
72
+ const format = defFormat === false ? '' : (this.config.format.asExt ? defFormat : '')
73
+ const path = `/${this.config.waibu.prefix}/${prefix}${fullPath}${format}`
73
74
  return '/' + trimStart(path, '/')
74
75
  }
75
76
 
76
77
  transformResult = ({ data, req, reply, options = {} }) => {
77
78
  const reformat = ({ data, req, reply, options = {} }) => {
78
- const { forOwn, get } = this.app.bajo.lib._
79
+ const { forOwn, get } = this.lib._
79
80
  const newData = {}
80
81
  forOwn(data, (v, k) => {
81
82
  let key = get(this.config, `responseKey.${k}`, k)
@@ -87,7 +88,7 @@ async function factory (pkgName) {
87
88
 
88
89
  const returnError = ({ data, req, reply, options = {} }) => {
89
90
  const { pascalCase } = this.app.bajo
90
- const { last, map, kebabCase, upperFirst, keys, each, get, isEmpty } = this.app.bajo.lib._
91
+ const { last, map, kebabCase, upperFirst, keys, each, get, isEmpty } = this.lib._
91
92
  const cfg = this.config
92
93
  const cfgWdb = this.app.waibuDb.config
93
94
  const errNames = kebabCase(data.constructor.name).split('-')
@@ -109,7 +110,7 @@ async function factory (pkgName) {
109
110
 
110
111
  const returnSuccess = ({ data, req, reply, options = {} }) => {
111
112
  const { pascalCase } = this.app.bajo
112
- const { each, keys, omit, get } = this.app.bajo.lib._
113
+ const { each, keys, omit, get } = this.lib._
113
114
  const cfg = this.config
114
115
  const cfgWdb = this.app.waibuDb.config
115
116
  if (reply) {
@@ -1,5 +1,5 @@
1
1
  async function buildErrResp (ctx) {
2
- const { cloneDeep, merge, each, get } = this.app.bajo.lib._
2
+ const { cloneDeep, merge, each, get } = this.lib._
3
3
  const cfg = this.config
4
4
  const cfgWdb = this.app.waibuDb.config
5
5
  const def = {
@@ -1,5 +1,5 @@
1
1
  async function docSchemaLib (ctx, name, obj) {
2
- const { merge } = this.app.bajo.lib._
2
+ const { merge } = this.lib._
3
3
  if (ctx.getSchema(name)) return
4
4
  const value = merge({}, obj, { $id: name })
5
5
  ctx.addSchema(value)
@@ -45,7 +45,7 @@ async function buildPropsReqs ({ schema, method, options = {} }) {
45
45
  }
46
46
 
47
47
  async function buildResponse ({ ctx, schema, method, options }) {
48
- const { merge, cloneDeep } = this.app.bajo.lib._
48
+ const { merge, cloneDeep } = this.lib._
49
49
  const { transformResult } = this.app.waibuRestApi
50
50
  const cfgWdb = this.app.waibuDb.config
51
51
  const { properties } = await buildPropsReqs.call(this, { schema, method, options })
@@ -113,7 +113,7 @@ async function buildResponse ({ ctx, schema, method, options }) {
113
113
  async function docSchemaModel ({ model, method, ctx, options = {} }) {
114
114
  const { getInfo } = this.app.dobo
115
115
  const { schema } = getInfo(model)
116
- const { omit } = this.app.bajo.lib._
116
+ const { omit } = this.lib._
117
117
  const out = {
118
118
  description: options.description ?? this.docSchemaDescription(method),
119
119
  tags: [this.alias.toUpperCase(), ...(options.alias ?? [])]
@@ -1,5 +1,5 @@
1
1
  async function buildParams (ctx, paramName, ...args) {
2
- const { each, isEmpty, keys, last, isBoolean } = this.app.bajo.lib._
2
+ const { each, isEmpty, keys, last, isBoolean } = this.lib._
3
3
  const cfgWeb = this.app.waibu.config
4
4
  let transform = false
5
5
  if (isBoolean(last(args))) {
@@ -1,5 +1,5 @@
1
1
  async function buildParams (ctx, paramName, ...args) {
2
- const { each, isEmpty } = this.app.bajo.lib._
2
+ const { each, isEmpty } = this.lib._
3
3
  const item = {
4
4
  type: 'object',
5
5
  properties: {}
package/waibu/boot.js CHANGED
@@ -17,7 +17,7 @@ const boot = {
17
17
  level: 10,
18
18
  handler: async function (ctx, prefix) {
19
19
  const { importPkg, eachPlugins, importModule, runHook } = this.app.bajo
20
- const { fastGlob } = this.app.bajo.lib
20
+ const { fastGlob } = this.lib
21
21
  const { getPluginPrefix } = this.app.waibu
22
22
  const [bodyParser, accepts] = await importPkg('waibu:@fastify/formbody', 'waibu:@fastify/accepts')
23
23
  const routeHook = await importModule('waibu:/lib/webapp-scope/route-hook.js')