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.
- package/extend/bajo/hook.js +1 -1
- package/extend/waibu/boot.js +13 -31
- package/index.js +568 -79
- package/lib/config.js +90 -0
- package/lib/{doc-schema/model.js → helper.js} +161 -166
- package/package.json +1 -1
- package/wiki/CHANGES.md +8 -0
- package/lib/decorate.js +0 -4
- package/lib/doc-schema/description.js +0 -13
- package/lib/doc-schema/for-find.js +0 -17
- package/lib/doc-schema/general.js +0 -79
- package/lib/doc-schema/lib.js +0 -8
- package/lib/doc-schema/params.js +0 -28
- package/lib/doc-schema/qs.js +0 -15
- package/lib/error.js +0 -19
- package/lib/handle-response/xml.js +0 -36
- package/lib/handle-response.js +0 -21
- package/lib/not-found.js +0 -11
- package/lib/route-by-model-builder.js +0 -99
- package/lib/route-by-verb.js +0 -63
- package/lib/serve-route.js +0 -23
- package/lib/sub-app.js +0 -15
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
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
if (['
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (
|
|
46
|
-
return
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (['
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
properties,
|
|
157
|
-
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
|
|
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
package/wiki/CHANGES.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Changes
|
|
2
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
|
+
|
|
3
11
|
## 2026-05-28
|
|
4
12
|
|
|
5
13
|
- [2.12.0] Change hooks to be written in one ```hook.js``` file
|
package/lib/decorate.js
DELETED
|
@@ -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: <field>:<dir>[,<field>:<dir>,[...]]',
|
|
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
|
package/lib/doc-schema/lib.js
DELETED
package/lib/doc-schema/params.js
DELETED
|
@@ -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
|
package/lib/doc-schema/qs.js
DELETED
|
@@ -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
|