waibu-rest-api 2.13.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.
package/lib/config.js ADDED
@@ -0,0 +1,90 @@
1
+ /**
2
+ * @typedef {Object} TConfig
3
+ * @property {Object} [waibu={}] - Waibu configuration
4
+ * @property {string} [waibu.prefix='api'] - Prefix for the Waibu REST API routes
5
+ * @property {boolean} [mountMainAsRoot=true] - Whether to mount the main route as root
6
+ * @property {boolean} [exposeHead=true] - Whether to expose the HEAD method
7
+ * @property {boolean} [enablePatch=false] - Whether to enable the PATCH method
8
+ * @property {number} [bodyLimit=1048576] - Maximum size of the request body in bytes
9
+ * @property {Object} [intl={}] - Internationalization configuration
10
+ * @property {Array<string>} [intl.detectors=['qs', 'header']] - List of detectors for internationalization
11
+ * @property {Object} [format={}] - Format configuration
12
+ * @property {Array<string>} [format.supported=['json', 'xml']] - List of supported formats (e.g., 'json', 'xml')
13
+ * @property {boolean} [format.asExt=false] - Whether to use file extensions for format detection
14
+ * @property {Object} [format.xml={}] - XML format configuration
15
+ * @property {Object} [format.xml.bodyParser={}] - XML body parser configuration
16
+ * @property {Array<string>} [format.xml.bodyParser.contentTypes=['text/xml', 'application/xml', 'application/rss+xml']] - List of content types for XML body parsing
17
+ * @property {boolean} [format.xml.bodyParser.validate=false] - Whether to validate XML body
18
+ * @property {Object} [format.xml.response={}] - XML response configuration
19
+ * @property {string} [format.xml.response.wrapper='doc'] - Wrapper element for XML response
20
+ * @property {boolean} [format.xml.response.declaration=true] - Whether to include XML declaration in response
21
+ * @property {boolean} [format.xml.response.valueAsAttributes=true] - Whether to treat values as attributes in XML response
22
+ * @property {Object} [responseKey={}] - Response key mapping configuration
23
+ * @property {string} [responseKey.data='data'] - Key for data in the response
24
+ * @property {string} [responseKey.oldData='oldData'] - Key for old data in the response
25
+ * @property {string} [responseKey.page='page'] - Key for page number in the response
26
+ * @property {string} [responseKey.count='count'] - Key for count
27
+ * @property {string} [responseKey.pages='pages'] - Key for total pages
28
+ * @property {string} [responseKey.success='success'] - Key for success status
29
+ * @property {string} [responseKey.statusCode='code'] - Key for status code
30
+ * @property {string} [responseKey.error='error'] - Key for error message
31
+ * @property {string} [responseKey.cached='cached'] - Key for cached status
32
+ * @property {string} [responseKey.hardCapped='hardCapped'] - Key for hard capped status
33
+ * @property {string} [responseKey.message='message'] - Key for message
34
+ * @property {string} [responseKey.warnings='warnings'] - Key for warnings
35
+ * @property {string} [responseKey.details='details'] - Key for details
36
+ */
37
+ const config = {
38
+ waibu: {
39
+ prefix: 'api'
40
+ },
41
+ mountMainAsRoot: true,
42
+ exposeHead: true,
43
+ enablePatch: false,
44
+ bodyLimit: 1048576,
45
+ intl: {
46
+ detectors: ['qs', 'header']
47
+ },
48
+ format: {
49
+ supported: ['json', 'xml'],
50
+ asExt: false,
51
+ xml: {
52
+ bodyParser: {
53
+ contentTypes: ['text/xml', 'application/xml', 'application/rss+xml'],
54
+ validate: false
55
+ },
56
+ response: {
57
+ wrapper: 'doc',
58
+ declaration: true,
59
+ valueAsAttributes: true
60
+ }
61
+ }
62
+ },
63
+ responseKey: {
64
+ data: 'data',
65
+ oldData: 'oldData',
66
+ page: 'page',
67
+ count: 'count',
68
+ pages: 'pages',
69
+ success: 'success',
70
+ statusCode: 'code',
71
+ error: 'error',
72
+ cached: 'cached',
73
+ hardCapped: 'hardCapped',
74
+ message: 'message',
75
+ warnings: 'warnings',
76
+ details: 'details'
77
+ },
78
+ mapSlash: '~',
79
+ mapDot: ',',
80
+ multipart: {
81
+ },
82
+ cors: {},
83
+ helmet: {},
84
+ compress: false,
85
+ rateLimit: false,
86
+ disabled: [],
87
+ rerouted: {}
88
+ }
89
+
90
+ export default config
@@ -1,166 +1,161 @@
1
- function getType (input) {
2
- let type = 'string'
3
- if (['float', 'double'].includes(input)) type = 'number'
4
- if (['boolean'].includes(input)) type = 'boolean'
5
- if (['integer', 'smallint'].includes(input)) type = 'integer'
6
- if (['object'].includes(input)) type = 'object'
7
- if (['array'].includes(input)) type = 'array'
8
- return type
9
- }
10
-
11
- async function buildPropsReqs ({ model, method, options = {} }) {
12
- const properties = {}
13
- const required = []
14
- const refs = {}
15
- const hidden = [...model.hidden, ...(options.hidden ?? [])] ?? []
16
- for (const p of model.properties) {
17
- if (hidden.includes(p.name)) continue
18
- properties[p.name] = { type: getType(p.type) }
19
- if (properties[p.name].type === 'object') {
20
- properties[p.name].additionalProperties = true
21
- }
22
- if (!p.required) properties[p.name].nullable = true
23
- else if (method === 'create' && p.name !== 'id') required.push(p.name)
24
- if (['datetime'].includes(p.type)) properties[p.name].format = 'date-time'
25
- if (p.ref) {
26
- for (const key in p.ref) {
27
- const val = p.ref[key]
28
- if (val.fields.length === 0) continue
29
- const props = { type: 'object', properties: {} }
30
- const rModel = this.app.dobo.getModel(val.model)
31
- for (const f of val.fields) {
32
- const item = rModel.properties.find(s => s.name === f)
33
- props.properties[item.name] = { type: getType(item.type) }
34
- }
35
- if (Object.keys(props.properties).length > 0) {
36
- if (val.type === '1:n') {
37
- refs[key] = { type: 'array', items: props }
38
- } else if (val.type === '1:1') {
39
- refs[key] = props
40
- }
41
- }
42
- }
43
- }
44
- }
45
- if (Object.keys(refs).length > 0) properties._ref = { type: 'object', properties: refs }
46
- return { properties, required }
47
- }
48
-
49
- async function buildResponse ({ model, method, options }) {
50
- const { merge, cloneDeep } = this.app.lib._
51
- const { transformResult } = this.app.waibuRestApi
52
- const cfgWdb = this.app.waibuDb.config
53
- const { properties } = await buildPropsReqs.call(this, { model, method, options })
54
-
55
- async function buildData (keys) {
56
- const data = {}
57
- for (const k of keys) {
58
- const name = 'model' + model.name
59
- const props = cloneDeep(properties)
60
- await this.docSchemaLib(name, {
61
- type: 'object',
62
- properties: props
63
- })
64
- data[k] = { $ref: name + '#' }
65
- }
66
- return data
67
- }
68
-
69
- const result = {
70
- '2xx': {
71
- description: this.t('successfulResponse'),
72
- type: 'object'
73
- }
74
- }
75
- if (['create', 'update', 'replace'].includes(method)) {
76
- result['4xx'] = {
77
- description: this.t('docErrorResponse'),
78
- $ref: '4xxResp#'
79
- }
80
- }
81
- result['5xx'] = {
82
- description: this.t('generalErrorResponse'),
83
- $ref: '5xxResp#'
84
- }
85
- if (cfgWdb.dbModel.dataOnly) {
86
- if (method === 'find') {
87
- result['2xx'] = {
88
- type: 'array',
89
- items: (await buildData.call(this, ['data'])).data
90
- }
91
- } else if (method === 'get') result['2xx'] = (await buildData.call(this, ['data'])).data
92
- else result['2xx'] = (await buildData.call(this, ['data'], true)).data
93
- return result
94
- }
95
- const success = { type: 'boolean', default: true }
96
- const statusCode = { type: 'integer', default: 200 }
97
- if (method === 'get') {
98
- result['2xx'].properties = transformResult({ data: merge({}, await buildData.call(this, ['data']), { success, statusCode }) })
99
- } else if (method === 'create') {
100
- statusCode.default = 201
101
- result['2xx'].properties = transformResult({ data: merge({}, await buildData.call(this, ['data']), { success, statusCode }) })
102
- } else if (['update', 'replace'].includes(method)) {
103
- result['2xx'].properties = transformResult({ data: merge({}, await buildData.call(this, ['data', 'oldData']), { success, statusCode }) })
104
- } else if (method === 'remove') {
105
- result['2xx'].properties = transformResult({ data: merge({}, await buildData.call(this, ['oldData']), { success, statusCode }) })
106
- } else if (method === 'find') {
107
- result['2xx'].properties = transformResult({
108
- data: await this.docSchemaForFind({ type: 'object', properties }),
109
- options: { forFind: true }
110
- })
111
- }
112
- return result
113
- }
114
-
115
- async function docSchemaModel ({ model, method, options = {} }) {
116
- const mdl = this.app.dobo.getModel(model)
117
- const { omit } = this.app.lib._
118
- const out = {
119
- description: options.description ?? this.docSchemaDescription(method),
120
- tags: [this.alias.toUpperCase(), ...(options.alias ?? [])]
121
- }
122
- if (['find'].includes(method)) {
123
- out.querystring = { $ref: 'qsFilter#' }
124
- }
125
- if (['get', 'update', 'replace', 'remove'].includes(method)) {
126
- out.querystring = { $ref: 'qsFields#' }
127
- if (!options.noId) out.params = { $ref: 'paramsId#' }
128
- }
129
- if (['update'].includes(method)) {
130
- const { properties } = await buildPropsReqs.call(this, { model: mdl, method, options })
131
- const name = 'model' + mdl.name + 'Update'
132
- delete properties._ref
133
- await this.docSchemaLib(name, {
134
- type: 'object',
135
- properties: omit(properties, ['id'])
136
- })
137
- out.body = { $ref: name + '#' }
138
- }
139
- if (['replace'].includes(method)) {
140
- const { properties, required } = await buildPropsReqs.call(this, { model: mdl, method, options })
141
- const name = 'model' + model.name + 'Replace'
142
- delete properties._ref
143
- await this.docSchemaLib(name, {
144
- type: 'object',
145
- properties: omit(properties, ['id']),
146
- required
147
- })
148
- out.body = { $ref: name + '#' }
149
- }
150
- if (['create'].includes(method)) {
151
- const { properties, required } = await buildPropsReqs.call(this, { model: mdl, method, options })
152
- const name = 'model' + model.name + 'Create'
153
- delete properties._ref
154
- await this.docSchemaLib(name, {
155
- type: 'object',
156
- properties,
157
- required
158
- })
159
- out.body = { $ref: name + '#' }
160
- out.querystring = { $ref: 'qsFields#' }
161
- }
162
- out.response = await buildResponse.call(this, { model: mdl, method, options })
163
- return out
164
- }
165
-
166
- export default docSchemaModel
1
+ export function extractHeaders (req, options) {
2
+ const { parseObject } = this.app.lib
3
+ const { isSet } = this.app.lib.aneka
4
+ options.count = true
5
+ options.refs = []
6
+ const headers = parseObject(req.headers, { parseValue: true })
7
+ if (headers['x-no-count']) options.count = false
8
+ if (isSet(headers['x-refs'])) options.refs = headers['x-refs']
9
+ if (typeof options.refs === 'string' && !['*', 'all'].includes(options.refs)) options.refs = [options.refs]
10
+ if (headers['x-warnings']) options.showWarnings = true
11
+ if (headers['x-formatted'] || headers['x-fmt']) options.fmt = true
12
+ }
13
+
14
+ export async function handleResponseXml (req, reply, payload) {
15
+ const { importPkg } = this.app.bajo
16
+ const { XMLBuilder } = await importPkg('bajoExtra:fast-xml-parser')
17
+ const { get, set, isPlainObject, isArray } = this.app.lib._
18
+ const { wrapper, valueAsAttributes, declaration } = this.config.format.xml.response
19
+
20
+ reply.type('application/xml; charset=utf-8')
21
+ const wrapped = set({}, wrapper, JSON.parse(payload))
22
+ const opts = {
23
+ attributeValueProcessor: function (k, v) {
24
+ return v === 'null' ? '' : v
25
+ }
26
+ }
27
+ const dataKey = this.config.responseKey.data
28
+ if (valueAsAttributes) {
29
+ const dataValue = get(wrapped, `${wrapper}.${dataKey}`)
30
+ opts.attributesGroupName = '_attrs'
31
+ if (isPlainObject(dataValue)) {
32
+ delete wrapped[wrapper][dataKey]
33
+ set(wrapped[wrapper], `${dataKey}._attrs`, dataValue)
34
+ } else if (isArray(dataValue)) {
35
+ delete wrapped[wrapper][dataKey]
36
+ const recs = []
37
+ for (const d of dataValue) {
38
+ recs.push(set({}, '_attrs', d))
39
+ }
40
+ set(wrapped[wrapper], dataKey, recs)
41
+ }
42
+ }
43
+ const builder = new XMLBuilder(opts)
44
+ let content = builder.build(wrapped)
45
+ if (declaration) content = '<?xml version="1.0" encoding="UTF-8" ?>' + content
46
+ return content
47
+ }
48
+
49
+ function getType (input) {
50
+ let type = 'string'
51
+ if (['float', 'double'].includes(input)) type = 'number'
52
+ if (['boolean'].includes(input)) type = 'boolean'
53
+ if (['integer', 'smallint'].includes(input)) type = 'integer'
54
+ if (['object'].includes(input)) type = 'object'
55
+ if (['array'].includes(input)) type = 'array'
56
+ return type
57
+ }
58
+
59
+ export async function buildPropsReqsForSchema ({ model, method, options = {} }) {
60
+ const properties = {}
61
+ const required = []
62
+ const refs = {}
63
+ const hidden = [...model.hidden, ...(options.hidden ?? [])] ?? []
64
+ for (const p of model.properties) {
65
+ if (hidden.includes(p.name)) continue
66
+ properties[p.name] = { type: getType(p.type) }
67
+ if (properties[p.name].type === 'object') {
68
+ properties[p.name].additionalProperties = true
69
+ }
70
+ if (!p.required) properties[p.name].nullable = true
71
+ else if (method === 'create' && p.name !== 'id') required.push(p.name)
72
+ if (['datetime'].includes(p.type)) properties[p.name].format = 'date-time'
73
+ if (p.ref) {
74
+ for (const key in p.ref) {
75
+ const val = p.ref[key]
76
+ if (val.fields.length === 0) continue
77
+ const props = { type: 'object', properties: {} }
78
+ const rModel = this.app.dobo.getModel(val.model)
79
+ for (const f of val.fields) {
80
+ const item = rModel.properties.find(s => s.name === f)
81
+ props.properties[item.name] = { type: getType(item.type) }
82
+ }
83
+ if (Object.keys(props.properties).length > 0) {
84
+ if (val.type === '1:n') {
85
+ refs[key] = { type: 'array', items: props }
86
+ } else if (val.type === '1:1') {
87
+ refs[key] = props
88
+ }
89
+ }
90
+ }
91
+ }
92
+ }
93
+ if (Object.keys(refs).length > 0) properties._ref = { type: 'object', properties: refs }
94
+ return { properties, required }
95
+ }
96
+
97
+ export async function buildResponseForSchema ({ model, method, options }) {
98
+ const { merge, cloneDeep } = this.app.lib._
99
+ const { transformResult } = this.app.waibuRestApi
100
+ const cfgWdb = this.app.waibuDb.config
101
+ const { properties } = await buildPropsReqsForSchema.call(this, { model, method, options })
102
+
103
+ async function buildData (keys) {
104
+ const data = {}
105
+ for (const k of keys) {
106
+ const name = 'model' + model.name
107
+ const props = cloneDeep(properties)
108
+ await this.docSchemaLib(name, {
109
+ type: 'object',
110
+ properties: props
111
+ })
112
+ data[k] = { $ref: name + '#' }
113
+ }
114
+ return data
115
+ }
116
+
117
+ const result = {
118
+ '2xx': {
119
+ description: this.t('successfulResponse'),
120
+ type: 'object'
121
+ }
122
+ }
123
+ if (['create', 'update', 'replace'].includes(method)) {
124
+ result['4xx'] = {
125
+ description: this.t('docErrorResponse'),
126
+ $ref: '4xxResp#'
127
+ }
128
+ }
129
+ result['5xx'] = {
130
+ description: this.t('generalErrorResponse'),
131
+ $ref: '5xxResp#'
132
+ }
133
+ if (cfgWdb.dbModel.dataOnly) {
134
+ if (method === 'find') {
135
+ result['2xx'] = {
136
+ type: 'array',
137
+ items: (await buildData.call(this, ['data'])).data
138
+ }
139
+ } else if (method === 'get') result['2xx'] = (await buildData.call(this, ['data'])).data
140
+ else result['2xx'] = (await buildData.call(this, ['data'], true)).data
141
+ return result
142
+ }
143
+ const success = { type: 'boolean', default: true }
144
+ const statusCode = { type: 'integer', default: 200 }
145
+ if (method === 'get') {
146
+ result['2xx'].properties = transformResult({ data: merge({}, await buildData.call(this, ['data']), { success, statusCode }) })
147
+ } else if (method === 'create') {
148
+ statusCode.default = 201
149
+ result['2xx'].properties = transformResult({ data: merge({}, await buildData.call(this, ['data']), { success, statusCode }) })
150
+ } else if (['update', 'replace'].includes(method)) {
151
+ result['2xx'].properties = transformResult({ data: merge({}, await buildData.call(this, ['data', 'oldData']), { success, statusCode }) })
152
+ } else if (method === 'remove') {
153
+ result['2xx'].properties = transformResult({ data: merge({}, await buildData.call(this, ['oldData']), { success, statusCode }) })
154
+ } else if (method === 'find') {
155
+ result['2xx'].properties = transformResult({
156
+ data: await this.docSchemaForFind({ type: 'object', properties }),
157
+ options: { forFind: true }
158
+ })
159
+ }
160
+ return result
161
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-rest-api",
3
- "version": "2.13.0",
3
+ "version": "2.14.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-07-04
4
+
5
+ - [2.14.0] Reorganize projects to reduce boot time
6
+
3
7
  ## 2026-06-10
4
8
 
5
9
  - [2.13.0] Remove route redirects because now directly handled by ```waibu```
package/lib/decorate.js DELETED
@@ -1,4 +0,0 @@
1
- async function decorate () {
2
- }
3
-
4
- export default decorate
@@ -1,13 +0,0 @@
1
- function docSchemaDescription (method) {
2
- const desc = {
3
- create: 'postRecordWithBody',
4
- find: 'findRecordsWithFilter',
5
- get: 'getRecordById',
6
- update: 'updateRecordByIdWithBody',
7
- remove: 'removeRecordById'
8
- }
9
-
10
- return this.t(desc[method])
11
- }
12
-
13
- export default docSchemaDescription
@@ -1,17 +0,0 @@
1
- async function docSchemaForFind (items) {
2
- return {
3
- data: {
4
- type: 'array',
5
- items
6
- },
7
- limit: { type: 'integer' },
8
- page: { type: 'integer' },
9
- pages: { type: 'integer' },
10
- count: { type: 'integer' },
11
- success: { type: 'boolean', default: true },
12
- statusCode: { type: 'integer', default: 200 },
13
- warnings: { type: 'array' }
14
- }
15
- }
16
-
17
- export default docSchemaForFind
@@ -1,79 +0,0 @@
1
- async function buildErrResp () {
2
- const { cloneDeep, merge, each, get } = this.app.lib._
3
- const cfg = this.config
4
- const cfgWdb = this.app.waibuDb.config
5
- const def = {
6
- type: 'object',
7
- properties: {
8
- error: { type: 'string' },
9
- message: { type: 'string' }
10
- }
11
- }
12
- for (const type of ['4xx', '5xx']) {
13
- const item = cloneDeep(def)
14
- if (type === '4xx') {
15
- merge(item, {
16
- properties: {
17
- details: {
18
- type: 'array',
19
- items: {
20
- type: 'object',
21
- properties: {
22
- field: { type: 'string' },
23
- error: { type: 'string' }
24
- }
25
- }
26
- },
27
- success: { type: 'boolean', default: false },
28
- statusCode: { type: 'integer', default: 400 }
29
- }
30
- })
31
- } else {
32
- merge(item, {
33
- properties: {
34
- success: { type: 'boolean', default: false },
35
- statusCode: { type: 'integer', default: 500 }
36
- }
37
- })
38
- }
39
- if (cfgWdb.dbModel.dataOnly) item.properties = { error: item.properties.message }
40
- const props = {}
41
- each(item.properties, (v, k) => {
42
- const key = get(cfg, `responseKey.${k}`, k)
43
- props[key] = item.properties[k]
44
- })
45
- item.properties = props
46
- await this.docSchemaLib(type + 'Resp', item)
47
- }
48
- }
49
-
50
- async function buildFilter () {
51
- await this.docSchemaParams('qsFilter',
52
- 'query||NQL/Mongo Query. Leave empty to disable query',
53
- 'limit|integer|Number of records per page. Must be >= 1|' + this.app.dobo.config.default.filter.limit,
54
- 'page|integer|Desired page number. Must be >= 1|1',
55
- 'sort||Order of records, format: &lt;field&gt;:&lt;dir&gt;[,&lt;field&gt;:&lt;dir&gt;,[...]]',
56
- 'fields||Comma delimited fields to show. Leave empty to show all fields',
57
- true
58
- )
59
- }
60
-
61
- async function buildFields () {
62
- await this.docSchemaParams('qsFields',
63
- 'fields||Comma delimited fields to show. Leave empty to show all fields',
64
- true
65
- )
66
- }
67
-
68
- async function buildParamsId () {
69
- await this.docSchemaParams('paramsId', 'id||Record ID')
70
- }
71
-
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
- }
78
-
79
- export default docSchemaGeneral
@@ -1,8 +0,0 @@
1
- async function docSchemaLib (name, obj) {
2
- const { merge } = this.app.lib._
3
- if (this.webAppCtx.getSchema(name)) return
4
- const value = merge({}, obj, { $id: name })
5
- this.webAppCtx.addSchema(value)
6
- }
7
-
8
- export default docSchemaLib
@@ -1,28 +0,0 @@
1
- async function docSchemaParams (paramName, ...args) {
2
- const { each, isEmpty, keys, last, isBoolean } = this.app.lib._
3
- const cfgWeb = this.app.waibu.getConfig()
4
- let transform = false
5
- if (isBoolean(last(args))) {
6
- transform = args.pop()
7
- }
8
- const item = {
9
- type: 'object',
10
- properties: {}
11
- }
12
- each(args, a => {
13
- let [name, type, description, def] = a.split('|')
14
- if (isEmpty(type)) type = 'string'
15
- item.properties[name] = { type, description: this.t(description), default: def }
16
- })
17
- if (transform) {
18
- each(keys(cfgWeb.qsKey), k => {
19
- const v = cfgWeb.qsKey[k]
20
- if (k === v || !item.properties[k]) return undefined
21
- item.properties[v] = item.properties[k]
22
- delete item.properties[k]
23
- })
24
- }
25
- if (!isEmpty(args)) await this.docSchemaLib(paramName, item)
26
- }
27
-
28
- export default docSchemaParams
@@ -1,15 +0,0 @@
1
- async function docSchemaQs (paramName, ...args) {
2
- const { each, isEmpty } = this.app.lib._
3
- const item = {
4
- type: 'object',
5
- properties: {}
6
- }
7
- each(args, a => {
8
- let [name, type, description] = a.split(':')
9
- if (isEmpty(type)) type = 'string'
10
- item.properties[name] = { type, description: this.t(description) }
11
- })
12
- if (!isEmpty(args)) await this.docSchemaLib(paramName, item)
13
- }
14
-
15
- export default docSchemaQs
package/lib/error.js DELETED
@@ -1,19 +0,0 @@
1
- import { handler } from './not-found.js'
2
-
3
- const extHandler = async function (err, req, reply) {
4
- err.statusCode = err.statusCode ?? 500
5
- const msg = err.message
6
- if (msg === '_notFound' || err.statusCode === 404) {
7
- return handler.call(this, req, reply)
8
- }
9
- const data = this.webAppCtx.httpErrors.createError(err.statusCode, msg)
10
- data.statusCode = err.statusCode
11
- data.details = err.details
12
- return this.transformResult({ data, req, reply })
13
- }
14
-
15
- async function error (err, req, reply) {
16
- return await extHandler.call(this, err, req, reply)
17
- }
18
-
19
- export default error