waibu-rest-api 2.14.0 → 2.14.2

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.
Files changed (50) hide show
  1. package/.github/FUNDING.yml +0 -0
  2. package/.github/workflows/repo-lockdown.yml +0 -0
  3. package/.jsdoc.conf.json +0 -0
  4. package/LICENSE +0 -0
  5. package/README.md +0 -0
  6. package/docs/WaibuRestApi.html +1 -1
  7. package/docs/data/search.json +1 -1
  8. package/docs/fonts/Inconsolata-Regular.ttf +0 -0
  9. package/docs/fonts/OpenSans-Regular.ttf +0 -0
  10. package/docs/fonts/WorkSans-Bold.ttf +0 -0
  11. package/docs/global.html +1 -1
  12. package/docs/index.html +1 -1
  13. package/docs/index.js.html +572 -82
  14. package/docs/lib_config.js.html +93 -0
  15. package/docs/scripts/core.js +0 -0
  16. package/docs/scripts/core.min.js +0 -0
  17. package/docs/scripts/resize.js +0 -0
  18. package/docs/scripts/search.js +0 -0
  19. package/docs/scripts/search.min.js +5 -5
  20. package/docs/scripts/third-party/Apache-License-2.0.txt +0 -0
  21. package/docs/scripts/third-party/fuse.js +0 -0
  22. package/docs/scripts/third-party/hljs-line-num-original.js +0 -0
  23. package/docs/scripts/third-party/hljs-line-num.js +0 -0
  24. package/docs/scripts/third-party/hljs-original.js +0 -0
  25. package/docs/scripts/third-party/hljs.js +0 -0
  26. package/docs/scripts/third-party/popper.js +0 -0
  27. package/docs/scripts/third-party/tippy.js +0 -0
  28. package/docs/scripts/third-party/tocbot.js +0 -0
  29. package/docs/scripts/third-party/tocbot.min.js +0 -0
  30. package/docs/static/bitcoin.jpeg +0 -0
  31. package/docs/static/home.md +0 -0
  32. package/docs/static/logo-ecosystem.png +0 -0
  33. package/docs/static/logo.png +0 -0
  34. package/docs/styles/clean-jsdoc-theme-base.css +0 -0
  35. package/docs/styles/clean-jsdoc-theme-dark.css +0 -0
  36. package/docs/styles/clean-jsdoc-theme-light.css +0 -0
  37. package/docs/styles/clean-jsdoc-theme-scrollbar.css +0 -0
  38. package/docs/styles/clean-jsdoc-theme-without-scrollbar.min.css +0 -0
  39. package/docs/styles/clean-jsdoc-theme.min.css +0 -0
  40. package/extend/bajo/hook.js +25 -25
  41. package/extend/bajo/intl/en-US.json +0 -0
  42. package/extend/bajo/intl/id.json +0 -0
  43. package/extend/waibu/{boot.js → webapp/boot.js} +0 -0
  44. package/index.js +2 -2
  45. package/lib/config.js +90 -90
  46. package/lib/helper.js +161 -161
  47. package/package.json +1 -1
  48. package/wiki/CHANGES.md +99 -91
  49. package/wiki/CONFIG.md +0 -0
  50. package/wiki/CONTRIBUTING.md +0 -0
package/lib/helper.js CHANGED
@@ -1,161 +1,161 @@
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
- }
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.14.0",
3
+ "version": "2.14.2",
4
4
  "description": "REST API for Waibu",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/wiki/CHANGES.md CHANGED
@@ -1,91 +1,99 @@
1
- # Changes
2
-
3
- ## 2026-07-04
4
-
5
- - [2.14.0] Reorganize projects to reduce boot time
6
-
7
- ## 2026-06-10
8
-
9
- - [2.13.0] Remove route redirects because now directly handled by ```waibu```
10
-
11
- ## 2026-05-28
12
-
13
- - [2.12.0] Change hooks to be written in one ```hook.js``` file
14
-
15
- ## 2026-05-22
16
-
17
- - [2.11.1] Bug fix in ```serve-route.js```
18
-
19
- ## 2026-04-25
20
-
21
- - [2.11.0] Change options to format value using the new key set by dobo
22
- - [2.11.0] Remove ```options.retainOriginalValue``` since it is not needed anymore
23
- - [2.11.0] Change related header to ```X-Fmt```
24
-
25
- ## 2026-04-11
26
-
27
- - [2.10.0] Add support to format output values using header ```X-Format-Value```
28
- - [2.10.0] Add support to retain original value using header ```X-Retain-Original``` if above key is ```true```
29
-
30
- ## 2026-04-02
31
-
32
- - [2.9.1] Add missing ```hardCapped``` key
33
-
34
- ## 2026-03-30
35
-
36
- - [2.8.0] Add inter site module support
37
- - [2.8.1] Bug fix in ```options.limit```, removed due to use ```findAllRecords()```
38
- - [2.9.0] Add support to show warnings through headers ```X-Warnings```
39
- - [2.9.0] Add support for ```dobo.hardCap``` when ```count``` is requested
40
- - [2.9.0] By default now ALWAYS request ```count``` which can be turned off with through headers ```X-NoCount```
41
-
42
-
43
- ## 2026-03-22
44
-
45
- - [2.7.1] bug fix in reference records
46
- - [2.7.2] Bug fix in applying routes to ```webCtx```
47
-
48
- ## 2026-03-14
49
-
50
- - [2.7.0] Accept model's name from ```req.params``` in ```route-by-model-builder.js```
51
-
52
- ## 2026-03-06
53
-
54
- - [2.6.1] Bug fix on schema enforcement. Only apply when it's stated explicitely in route handler
55
-
56
- ## 2026-03-05
57
-
58
- - [2.6.0] Add transaction support by setting property ```transaction: true``` on model's route handler definition
59
-
60
- ## 2026-02-23
61
-
62
- - [2.5.1] Bug fix on ```transformResult()```
63
-
64
- ## 2026-02-22
65
-
66
- - [2.5.0] Add warnings to the response objects
67
-
68
- ## 2026-02-09
69
-
70
- - [2.4.0] Add error & not found handlers
71
-
72
- ## 2026-02-08
73
-
74
- - [2.3.0] Simplify webApp context through ```plugin.webAppCtx```
75
-
76
- ## 2026-01-30
77
-
78
- - [2.2.1] Bug fix if output format is in xml
79
-
80
- ## 2026-01-24
81
-
82
- - [2.2.0] Add ```hooks``` property to model builder that will be considered as dynamic hoooks by models
83
-
84
- ## 2026-01-16
85
-
86
- - [2.1.1] Bug fix on model references
87
-
88
- ## 2025-12-28
89
-
90
- - [2.1.0] Ported to ```bajo@2.2.x``` specs
91
- - [2.1.0] Add waibuRestApi end point ```/api/db/:model/stat/:stat```
1
+ # Changes
2
+
3
+ ## 2026-09-05
4
+
5
+ - [2.14.2] Bug fix in `_routeByModelBuilder()` for aggregate/histogram endpoint
6
+
7
+ ## 2026-07-08
8
+
9
+ - [2.14.1] Bug fix in `boot.js`
10
+
11
+ ## 2026-07-04
12
+
13
+ - [2.14.0] Reorganize projects to reduce boot time
14
+
15
+ ## 2026-06-10
16
+
17
+ - [2.13.0] Remove route redirects because now directly handled by ```waibu```
18
+
19
+ ## 2026-05-28
20
+
21
+ - [2.12.0] Change hooks to be written in one ```hook.js``` file
22
+
23
+ ## 2026-05-22
24
+
25
+ - [2.11.1] Bug fix in ```serve-route.js```
26
+
27
+ ## 2026-04-25
28
+
29
+ - [2.11.0] Change options to format value using the new key set by dobo
30
+ - [2.11.0] Remove ```options.retainOriginalValue``` since it is not needed anymore
31
+ - [2.11.0] Change related header to ```X-Fmt```
32
+
33
+ ## 2026-04-11
34
+
35
+ - [2.10.0] Add support to format output values using header ```X-Format-Value```
36
+ - [2.10.0] Add support to retain original value using header ```X-Retain-Original``` if above key is ```true```
37
+
38
+ ## 2026-04-02
39
+
40
+ - [2.9.1] Add missing ```hardCapped``` key
41
+
42
+ ## 2026-03-30
43
+
44
+ - [2.8.0] Add inter site module support
45
+ - [2.8.1] Bug fix in ```options.limit```, removed due to use ```findAllRecords()```
46
+ - [2.9.0] Add support to show warnings through headers ```X-Warnings```
47
+ - [2.9.0] Add support for ```dobo.hardCap``` when ```count``` is requested
48
+ - [2.9.0] By default now ALWAYS request ```count``` which can be turned off with through headers ```X-NoCount```
49
+
50
+
51
+ ## 2026-03-22
52
+
53
+ - [2.7.1] bug fix in reference records
54
+ - [2.7.2] Bug fix in applying routes to ```webCtx```
55
+
56
+ ## 2026-03-14
57
+
58
+ - [2.7.0] Accept model's name from ```req.params``` in ```route-by-model-builder.js```
59
+
60
+ ## 2026-03-06
61
+
62
+ - [2.6.1] Bug fix on schema enforcement. Only apply when it's stated explicitely in route handler
63
+
64
+ ## 2026-03-05
65
+
66
+ - [2.6.0] Add transaction support by setting property ```transaction: true``` on model's route handler definition
67
+
68
+ ## 2026-02-23
69
+
70
+ - [2.5.1] Bug fix on ```transformResult()```
71
+
72
+ ## 2026-02-22
73
+
74
+ - [2.5.0] Add warnings to the response objects
75
+
76
+ ## 2026-02-09
77
+
78
+ - [2.4.0] Add error & not found handlers
79
+
80
+ ## 2026-02-08
81
+
82
+ - [2.3.0] Simplify webApp context through ```plugin.webAppCtx```
83
+
84
+ ## 2026-01-30
85
+
86
+ - [2.2.1] Bug fix if output format is in xml
87
+
88
+ ## 2026-01-24
89
+
90
+ - [2.2.0] Add ```hooks``` property to model builder that will be considered as dynamic hoooks by models
91
+
92
+ ## 2026-01-16
93
+
94
+ - [2.1.1] Bug fix on model references
95
+
96
+ ## 2025-12-28
97
+
98
+ - [2.1.0] Ported to ```bajo@2.2.x``` specs
99
+ - [2.1.0] Add waibuRestApi end point ```/api/db/:model/stat/:stat```
package/wiki/CONFIG.md CHANGED
File without changes
File without changes