waibu-rest-api 2.0.0 → 2.1.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/.github/FUNDING.yml +13 -0
- package/.github/workflows/repo-lockdown.yml +24 -0
- package/.jsdoc.conf.json +45 -0
- package/LICENSE +0 -0
- package/README.md +33 -7
- package/docs/WaibuRestApi.html +3 -0
- package/docs/data/search.json +1 -0
- package/docs/fonts/Inconsolata-Regular.ttf +0 -0
- package/docs/fonts/OpenSans-Regular.ttf +0 -0
- package/docs/fonts/WorkSans-Bold.ttf +0 -0
- package/docs/global.html +3 -0
- package/docs/index.html +3 -0
- package/docs/index.js.html +180 -0
- package/docs/scripts/core.js +726 -0
- package/docs/scripts/core.min.js +23 -0
- package/docs/scripts/resize.js +90 -0
- package/docs/scripts/search.js +265 -0
- package/docs/scripts/search.min.js +6 -0
- package/docs/scripts/third-party/Apache-License-2.0.txt +202 -0
- package/docs/scripts/third-party/fuse.js +9 -0
- package/docs/scripts/third-party/hljs-line-num-original.js +369 -0
- package/docs/scripts/third-party/hljs-line-num.js +1 -0
- package/docs/scripts/third-party/hljs-original.js +5171 -0
- package/docs/scripts/third-party/hljs.js +1 -0
- package/docs/scripts/third-party/popper.js +5 -0
- package/docs/scripts/third-party/tippy.js +1 -0
- package/docs/scripts/third-party/tocbot.js +672 -0
- package/docs/scripts/third-party/tocbot.min.js +1 -0
- package/docs/static/bitcoin.jpeg +0 -0
- package/docs/static/home.md +23 -0
- package/docs/static/logo-ecosystem.png +0 -0
- package/docs/static/logo.png +0 -0
- package/docs/styles/clean-jsdoc-theme-base.css +1159 -0
- package/docs/styles/clean-jsdoc-theme-dark.css +412 -0
- package/docs/styles/clean-jsdoc-theme-light.css +482 -0
- package/docs/styles/clean-jsdoc-theme-scrollbar.css +30 -0
- package/docs/styles/clean-jsdoc-theme-without-scrollbar.min.css +1 -0
- package/docs/styles/clean-jsdoc-theme.min.css +1 -0
- package/extend/bajo/hook/waibu-rest-api@pre-parsing.js +0 -0
- package/extend/bajo/hook/waibu@after-init.js +0 -0
- package/extend/bajo/intl/en-US.json +6 -1
- package/extend/bajo/intl/id.json +6 -1
- package/extend/waibu/boot.js +8 -8
- package/index.js +48 -11
- package/lib/decorate.js +0 -0
- package/lib/doc-schema/description.js +13 -0
- package/{method → lib}/doc-schema/for-find.js +2 -2
- package/{method → lib}/doc-schema/general.js +1 -1
- package/{method → lib}/doc-schema/lib.js +1 -1
- package/{method → lib}/doc-schema/model.js +30 -32
- package/{method → lib}/doc-schema/params.js +4 -4
- package/{method → lib}/doc-schema/qs.js +4 -4
- package/lib/error.js +0 -0
- package/lib/handle-response/xml.js +1 -1
- package/lib/handle-response.js +0 -0
- package/lib/not-found.js +1 -1
- package/lib/route-by-model-builder.js +11 -12
- package/lib/route-by-verb.js +6 -5
- package/lib/serve-route.js +2 -2
- package/lib/sub-app.js +2 -2
- package/package.json +46 -35
- package/wiki/CHANGES.md +6 -0
- package/wiki/CONFIG.md +3 -0
- package/wiki/CONTRIBUTING.md +5 -0
- package/method/doc-schema/description.js +0 -13
package/index.js
CHANGED
|
@@ -1,11 +1,28 @@
|
|
|
1
|
+
import docSchemaDescription from './lib/doc-schema/description.js'
|
|
2
|
+
import docSchemaForFind from './lib/doc-schema/for-find.js'
|
|
3
|
+
import docSchemaGeneral from './lib/doc-schema/general.js'
|
|
4
|
+
import docSchemaLib from './lib/doc-schema/lib.js'
|
|
5
|
+
import docSchemaModel from './lib/doc-schema/model.js'
|
|
6
|
+
import docSchemaParams from './lib/doc-schema/params.js'
|
|
7
|
+
import docSchemaQs from './lib/doc-schema/qs.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Plugin factory
|
|
11
|
+
*
|
|
12
|
+
* @param {string} pkgName - NPM package name
|
|
13
|
+
* @returns {class}
|
|
14
|
+
*/
|
|
1
15
|
async function factory (pkgName) {
|
|
2
16
|
const me = this
|
|
3
17
|
|
|
4
|
-
|
|
18
|
+
/**
|
|
19
|
+
* WaibuRestApi class
|
|
20
|
+
*
|
|
21
|
+
* @class
|
|
22
|
+
*/
|
|
23
|
+
class WaibuRestApi extends this.app.baseClass.Base {
|
|
5
24
|
constructor () {
|
|
6
25
|
super(pkgName, me.app)
|
|
7
|
-
this.alias = 'wra'
|
|
8
|
-
this.dependencies = ['waibu', 'waibu-db', 'bajo-extra']
|
|
9
26
|
this.routePathHandlers = ['restapi']
|
|
10
27
|
this.config = {
|
|
11
28
|
waibu: {
|
|
@@ -57,16 +74,26 @@ async function factory (pkgName) {
|
|
|
57
74
|
disabled: [],
|
|
58
75
|
rerouted: {}
|
|
59
76
|
}
|
|
77
|
+
|
|
78
|
+
this.selfBind([
|
|
79
|
+
'docSchemaDescription',
|
|
80
|
+
'docSchemaForFind',
|
|
81
|
+
'docSchemaGeneral',
|
|
82
|
+
'docSchemaLib',
|
|
83
|
+
'docSchemaModel',
|
|
84
|
+
'docSchemaParams',
|
|
85
|
+
'docSchemaQs'
|
|
86
|
+
])
|
|
60
87
|
}
|
|
61
88
|
|
|
62
89
|
init = async () => {
|
|
63
|
-
const { uniq } = this.lib._
|
|
90
|
+
const { uniq } = this.app.lib._
|
|
64
91
|
this.config.format.supported.push('json')
|
|
65
92
|
this.config.format.supported = uniq(this.config.format.supported)
|
|
66
93
|
}
|
|
67
94
|
|
|
68
95
|
routePath = (name, { defFormat = '.json', uriEncoded = true } = {}) => {
|
|
69
|
-
const { get, trimStart } = this.lib._
|
|
96
|
+
const { get, trimStart } = this.app.lib._
|
|
70
97
|
const { fullPath, ns } = this.app.bajo.breakNsPath(name)
|
|
71
98
|
const prefix = get(this.app[ns], 'config.waibu.prefix', this.app[ns].alias)
|
|
72
99
|
const format = defFormat === false ? '' : (this.config.format.asExt ? defFormat : '')
|
|
@@ -77,7 +104,7 @@ async function factory (pkgName) {
|
|
|
77
104
|
|
|
78
105
|
transformResult = ({ data, req, reply, options = {} }) => {
|
|
79
106
|
const reformat = ({ data, req, reply, options = {} }) => {
|
|
80
|
-
const { forOwn, get } = this.lib._
|
|
107
|
+
const { forOwn, get } = this.app.lib._
|
|
81
108
|
const newData = {}
|
|
82
109
|
forOwn(data, (v, k) => {
|
|
83
110
|
let key = get(this.config, `responseKey.${k}`, k)
|
|
@@ -88,13 +115,13 @@ async function factory (pkgName) {
|
|
|
88
115
|
}
|
|
89
116
|
|
|
90
117
|
const returnError = ({ data, req, reply, options = {} }) => {
|
|
91
|
-
const { pascalCase } = this.lib.aneka
|
|
92
|
-
const { last, map, kebabCase, upperFirst, keys, each, get, isEmpty } = this.lib._
|
|
118
|
+
const { pascalCase } = this.app.lib.aneka
|
|
119
|
+
const { last, map, kebabCase, upperFirst, keys, each, get, isEmpty } = this.app.lib._
|
|
93
120
|
const cfg = this.config
|
|
94
121
|
const cfgWdb = this.app.waibuDb.config
|
|
95
122
|
const errNames = kebabCase(data.constructor.name).split('-')
|
|
96
123
|
if (last(errNames) === 'error') errNames.pop()
|
|
97
|
-
data.error = this.
|
|
124
|
+
data.error = this.t(map(errNames, s => upperFirst(s)).join(' '))
|
|
98
125
|
data.success = false
|
|
99
126
|
data.statusCode = data.statusCode ?? 500
|
|
100
127
|
if (reply && cfgWdb.dbModel.dataOnly) {
|
|
@@ -110,8 +137,8 @@ async function factory (pkgName) {
|
|
|
110
137
|
}
|
|
111
138
|
|
|
112
139
|
const returnSuccess = ({ data, req, reply, options = {} }) => {
|
|
113
|
-
const { pascalCase } = this.lib.aneka
|
|
114
|
-
const { each, keys, omit, get } = this.lib._
|
|
140
|
+
const { pascalCase } = this.app.lib.aneka
|
|
141
|
+
const { each, keys, omit, get } = this.app.lib._
|
|
115
142
|
const cfg = this.config
|
|
116
143
|
const cfgWdb = this.app.waibuDb.config
|
|
117
144
|
if (reply) {
|
|
@@ -131,7 +158,17 @@ async function factory (pkgName) {
|
|
|
131
158
|
if (isError) return returnError({ data, req, reply, options })
|
|
132
159
|
return returnSuccess({ data, req, reply, options })
|
|
133
160
|
}
|
|
161
|
+
|
|
162
|
+
docSchemaDescription = docSchemaDescription
|
|
163
|
+
docSchemaForFind = docSchemaForFind
|
|
164
|
+
docSchemaGeneral = docSchemaGeneral
|
|
165
|
+
docSchemaLib = docSchemaLib
|
|
166
|
+
docSchemaModel = docSchemaModel
|
|
167
|
+
docSchemaParams = docSchemaParams
|
|
168
|
+
docSchemaQs = docSchemaQs
|
|
134
169
|
}
|
|
170
|
+
|
|
171
|
+
return WaibuRestApi
|
|
135
172
|
}
|
|
136
173
|
|
|
137
174
|
export default factory
|
package/lib/decorate.js
CHANGED
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
|
@@ -8,13 +8,12 @@ function getType (input) {
|
|
|
8
8
|
return type
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
async function buildPropsReqs ({
|
|
12
|
-
const { getSchema } = this.app.dobo
|
|
11
|
+
async function buildPropsReqs ({ model, method, options = {} }) {
|
|
13
12
|
const properties = {}
|
|
14
13
|
const required = []
|
|
15
|
-
const
|
|
16
|
-
const hidden = [...
|
|
17
|
-
for (const p of
|
|
14
|
+
const refs = {}
|
|
15
|
+
const hidden = [...model.hidden, ...(options.hidden ?? [])] ?? []
|
|
16
|
+
for (const p of model.properties) {
|
|
18
17
|
if (hidden.includes(p.name)) continue
|
|
19
18
|
properties[p.name] = { type: getType(p.type) }
|
|
20
19
|
if (properties[p.name].type === 'object') {
|
|
@@ -23,40 +22,40 @@ async function buildPropsReqs ({ schema, method, options = {} }) {
|
|
|
23
22
|
if (!p.required) properties[p.name].nullable = true
|
|
24
23
|
else if (method === 'create' && p.name !== 'id') required.push(p.name)
|
|
25
24
|
if (['datetime'].includes(p.type)) properties[p.name].format = 'date-time'
|
|
26
|
-
if (p.
|
|
27
|
-
for (const key in p.
|
|
28
|
-
const val = p.
|
|
25
|
+
if (p.ref) {
|
|
26
|
+
for (const key in p.ref) {
|
|
27
|
+
const val = p.ref[key]
|
|
29
28
|
if (val.fields.length === 0) continue
|
|
30
29
|
const props = { type: 'object', properties: {} }
|
|
31
|
-
const
|
|
30
|
+
const rModel = this.app.dobo.getModel(val.model)
|
|
32
31
|
for (const f of val.fields) {
|
|
33
|
-
const item =
|
|
32
|
+
const item = rModel.properties.find(s => s.name === f)
|
|
34
33
|
props.properties[item.name] = { type: getType(item.type) }
|
|
35
34
|
}
|
|
36
35
|
if (Object.keys(props.properties).length > 0) {
|
|
37
|
-
if (val.type === '
|
|
38
|
-
|
|
39
|
-
} else if (val.type === '
|
|
40
|
-
|
|
36
|
+
if (val.type === '1:n') {
|
|
37
|
+
refs[key] = { type: 'array', items: props }
|
|
38
|
+
} else if (val.type === '1:1') {
|
|
39
|
+
refs[key] = props
|
|
41
40
|
}
|
|
42
41
|
}
|
|
43
42
|
}
|
|
44
43
|
}
|
|
45
44
|
}
|
|
46
|
-
if (Object.keys(
|
|
45
|
+
if (Object.keys(refs).length > 0) properties._ref = { type: 'object', properties: refs }
|
|
47
46
|
return { properties, required }
|
|
48
47
|
}
|
|
49
48
|
|
|
50
|
-
async function buildResponse ({ ctx,
|
|
51
|
-
const { merge, cloneDeep } = this.lib._
|
|
49
|
+
async function buildResponse ({ ctx, model, method, options }) {
|
|
50
|
+
const { merge, cloneDeep } = this.app.lib._
|
|
52
51
|
const { transformResult } = this.app.waibuRestApi
|
|
53
52
|
const cfgWdb = this.app.waibuDb.config
|
|
54
|
-
const { properties } = await buildPropsReqs.call(this, {
|
|
53
|
+
const { properties } = await buildPropsReqs.call(this, { model, method, options })
|
|
55
54
|
|
|
56
55
|
async function buildData (keys) {
|
|
57
56
|
const data = {}
|
|
58
57
|
for (const k of keys) {
|
|
59
|
-
const name = 'model' +
|
|
58
|
+
const name = 'model' + model.name
|
|
60
59
|
const props = cloneDeep(properties)
|
|
61
60
|
await this.docSchemaLib(ctx, name, {
|
|
62
61
|
type: 'object',
|
|
@@ -69,18 +68,18 @@ async function buildResponse ({ ctx, schema, method, options }) {
|
|
|
69
68
|
|
|
70
69
|
const result = {
|
|
71
70
|
'2xx': {
|
|
72
|
-
description: this.
|
|
71
|
+
description: this.t('successfulResponse'),
|
|
73
72
|
type: 'object'
|
|
74
73
|
}
|
|
75
74
|
}
|
|
76
75
|
if (['create', 'update', 'replace'].includes(method)) {
|
|
77
76
|
result['4xx'] = {
|
|
78
|
-
description: this.
|
|
77
|
+
description: this.t('docErrorResponse'),
|
|
79
78
|
$ref: '4xxResp#'
|
|
80
79
|
}
|
|
81
80
|
}
|
|
82
81
|
result['5xx'] = {
|
|
83
|
-
description: this.
|
|
82
|
+
description: this.t('generalErrorResponse'),
|
|
84
83
|
$ref: '5xxResp#'
|
|
85
84
|
}
|
|
86
85
|
if (cfgWdb.dbModel.dataOnly) {
|
|
@@ -114,9 +113,8 @@ async function buildResponse ({ ctx, schema, method, options }) {
|
|
|
114
113
|
}
|
|
115
114
|
|
|
116
115
|
async function docSchemaModel ({ model, method, ctx, options = {} }) {
|
|
117
|
-
const
|
|
118
|
-
const {
|
|
119
|
-
const { omit } = this.lib._
|
|
116
|
+
const mdl = this.app.dobo.getModel(model)
|
|
117
|
+
const { omit } = this.app.lib._
|
|
120
118
|
const out = {
|
|
121
119
|
description: options.description ?? this.docSchemaDescription(method),
|
|
122
120
|
tags: [this.alias.toUpperCase(), ...(options.alias ?? [])]
|
|
@@ -129,8 +127,8 @@ async function docSchemaModel ({ model, method, ctx, options = {} }) {
|
|
|
129
127
|
if (!options.noId) out.params = { $ref: 'paramsId#' }
|
|
130
128
|
}
|
|
131
129
|
if (['update'].includes(method)) {
|
|
132
|
-
const { properties } = await buildPropsReqs.call(this, {
|
|
133
|
-
const name = 'model' +
|
|
130
|
+
const { properties } = await buildPropsReqs.call(this, { model: mdl, method, options })
|
|
131
|
+
const name = 'model' + mdl.name + 'Update'
|
|
134
132
|
delete properties._rel
|
|
135
133
|
await this.docSchemaLib(ctx, name, {
|
|
136
134
|
type: 'object',
|
|
@@ -139,8 +137,8 @@ async function docSchemaModel ({ model, method, ctx, options = {} }) {
|
|
|
139
137
|
out.body = { $ref: name + '#' }
|
|
140
138
|
}
|
|
141
139
|
if (['replace'].includes(method)) {
|
|
142
|
-
const { properties, required } = await buildPropsReqs.call(this, {
|
|
143
|
-
const name = 'model' +
|
|
140
|
+
const { properties, required } = await buildPropsReqs.call(this, { model: mdl, method, options })
|
|
141
|
+
const name = 'model' + model.name + 'Replace'
|
|
144
142
|
delete properties._rel
|
|
145
143
|
await this.docSchemaLib(ctx, name, {
|
|
146
144
|
type: 'object',
|
|
@@ -150,8 +148,8 @@ async function docSchemaModel ({ model, method, ctx, options = {} }) {
|
|
|
150
148
|
out.body = { $ref: name + '#' }
|
|
151
149
|
}
|
|
152
150
|
if (['create'].includes(method)) {
|
|
153
|
-
const { properties, required } = await buildPropsReqs.call(this, {
|
|
154
|
-
const name = 'model' +
|
|
151
|
+
const { properties, required } = await buildPropsReqs.call(this, { model: mdl, method, options })
|
|
152
|
+
const name = 'model' + model.name + 'Create'
|
|
155
153
|
delete properties._rel
|
|
156
154
|
await this.docSchemaLib(ctx, name, {
|
|
157
155
|
type: 'object',
|
|
@@ -161,7 +159,7 @@ async function docSchemaModel ({ model, method, ctx, options = {} }) {
|
|
|
161
159
|
out.body = { $ref: name + '#' }
|
|
162
160
|
out.querystring = { $ref: 'qsFields#' }
|
|
163
161
|
}
|
|
164
|
-
out.response = await buildResponse.call(this, { ctx,
|
|
162
|
+
out.response = await buildResponse.call(this, { ctx, model: mdl, method, options })
|
|
165
163
|
return out
|
|
166
164
|
}
|
|
167
165
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
async function
|
|
2
|
-
const { each, isEmpty, keys, last, isBoolean } = this.lib._
|
|
1
|
+
async function docSchemaParams (ctx, paramName, ...args) {
|
|
2
|
+
const { each, isEmpty, keys, last, isBoolean } = this.app.lib._
|
|
3
3
|
const cfgWeb = this.app.waibu.getConfig()
|
|
4
4
|
let transform = false
|
|
5
5
|
if (isBoolean(last(args))) {
|
|
@@ -12,7 +12,7 @@ async function buildParams (ctx, paramName, ...args) {
|
|
|
12
12
|
each(args, a => {
|
|
13
13
|
let [name, type, description, def] = a.split('|')
|
|
14
14
|
if (isEmpty(type)) type = 'string'
|
|
15
|
-
item.properties[name] = { type, description: this.
|
|
15
|
+
item.properties[name] = { type, description: this.t(description), default: def }
|
|
16
16
|
})
|
|
17
17
|
if (transform) {
|
|
18
18
|
each(keys(cfgWeb.qsKey), k => {
|
|
@@ -25,4 +25,4 @@ async function buildParams (ctx, paramName, ...args) {
|
|
|
25
25
|
if (!isEmpty(args)) await this.docSchemaLib(ctx, paramName, item)
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
export default
|
|
28
|
+
export default docSchemaParams
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
async function
|
|
2
|
-
const { each, isEmpty } = this.lib._
|
|
1
|
+
async function docSchemaQs (ctx, paramName, ...args) {
|
|
2
|
+
const { each, isEmpty } = this.app.lib._
|
|
3
3
|
const item = {
|
|
4
4
|
type: 'object',
|
|
5
5
|
properties: {}
|
|
@@ -7,9 +7,9 @@ async function buildParams (ctx, paramName, ...args) {
|
|
|
7
7
|
each(args, a => {
|
|
8
8
|
let [name, type, description] = a.split(':')
|
|
9
9
|
if (isEmpty(type)) type = 'string'
|
|
10
|
-
item.properties[name] = { type, description: this.
|
|
10
|
+
item.properties[name] = { type, description: this.t(description) }
|
|
11
11
|
})
|
|
12
12
|
if (!isEmpty(args)) await this.docSchemaLib(ctx, paramName, item)
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export default
|
|
15
|
+
export default docSchemaQs
|
package/lib/error.js
CHANGED
|
File without changes
|
|
@@ -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.lib._
|
|
4
|
+
const { get, set, isPlainObject, isArray } = this.app.lib._
|
|
5
5
|
const { wrapper, valueAsAttributes, declaration } = this.config.format.xml.response
|
|
6
6
|
|
|
7
7
|
reply.type('application/xml; charset=utf-8')
|
package/lib/handle-response.js
CHANGED
|
File without changes
|
package/lib/not-found.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export function handler (req, reply, ctx) {
|
|
2
|
-
const msg = this.
|
|
2
|
+
const msg = this.t('routeNotFound%s%s', req.url, req.method)
|
|
3
3
|
const data = ctx.httpErrors.createError(404, msg)
|
|
4
4
|
return this.transformResult({ data, req, reply })
|
|
5
5
|
}
|
|
@@ -2,25 +2,23 @@ import path from 'path'
|
|
|
2
2
|
import serveRoute from './serve-route.js'
|
|
3
3
|
|
|
4
4
|
async function routeByModelBuilder ({ file, ctx, dir, pathPrefix, ns, parent }) {
|
|
5
|
-
const {
|
|
6
|
-
|
|
7
|
-
const {
|
|
8
|
-
const { isSet } = this.lib.aneka
|
|
5
|
+
const { readConfig } = this.app.bajo
|
|
6
|
+
const { parseObject } = this.app.lib
|
|
7
|
+
const { isSet } = this.app.lib.aneka
|
|
9
8
|
const { methodMap, getParams } = this.app.waibuDb
|
|
10
|
-
const {
|
|
11
|
-
const { isFunction, camelCase, omit, merge, keys, filter } = this.lib._
|
|
9
|
+
const { isFunction, camelCase, omit, merge, keys, filter } = this.app.lib._
|
|
12
10
|
const builder = await readConfig(file, { ns, ignoreError: true })
|
|
13
11
|
builder.disabled = builder.disabled ?? []
|
|
14
|
-
const { schema } = getInfo(builder.model)
|
|
15
12
|
let url = path.dirname(file).replace(`${dir}/extend/${pathPrefix}`, '').replaceAll('@', ':')
|
|
16
13
|
if (isFunction(builder.url)) url = await builder.url.call(this)
|
|
17
14
|
const methods = keys(methodMap)
|
|
18
15
|
if (this.config.enablePatch) methods.push('replace')
|
|
19
16
|
const me = this
|
|
20
17
|
const mods = []
|
|
18
|
+
const model = this.app.dobo.getModel(builder.model)
|
|
21
19
|
for (const method of methods) {
|
|
22
|
-
let disabled =
|
|
23
|
-
if (method === 'replace' && (
|
|
20
|
+
let disabled = model.disabled.includes(method) || builder.disabled.includes(method)
|
|
21
|
+
if (method === 'replace' && (model.disabled.includes('update') || (builder.disabled ?? []).includes('update'))) disabled = true
|
|
24
22
|
if (disabled) continue
|
|
25
23
|
const mod = omit(builder, ['model', 'url', 'method', 'handler', 'hidden', ...methods])
|
|
26
24
|
const customMod = builder[method] ?? {}
|
|
@@ -34,7 +32,7 @@ async function routeByModelBuilder ({ file, ctx, dir, pathPrefix, ns, parent })
|
|
|
34
32
|
url: ['get', 'update', 'replace', 'remove'].includes(method) ? `${url}/:id` : url,
|
|
35
33
|
method: mapmethod,
|
|
36
34
|
handler: async function (req, reply) {
|
|
37
|
-
const helper = me.app.waibuDb[
|
|
35
|
+
const helper = me.app.waibuDb[`${method === 'replace' ? 'update' : method}Record`]
|
|
38
36
|
let { fields, count } = getParams(req)
|
|
39
37
|
let rels = []
|
|
40
38
|
const headers = parseObject(req.headers, { parseValue: true })
|
|
@@ -61,7 +59,7 @@ async function routeByModelBuilder ({ file, ctx, dir, pathPrefix, ns, parent })
|
|
|
61
59
|
const supported = ['aggregate', 'histogram']
|
|
62
60
|
builder.stat = filter(builder.stat ?? supported, item => supported.includes(item))
|
|
63
61
|
const method = 'GET'
|
|
64
|
-
const disabled =
|
|
62
|
+
const disabled = model.disabled.includes(method) || builder.disabled.includes(method)
|
|
65
63
|
if (disabled) return mods
|
|
66
64
|
const mod = {
|
|
67
65
|
schema: false,
|
|
@@ -69,7 +67,8 @@ async function routeByModelBuilder ({ file, ctx, dir, pathPrefix, ns, parent })
|
|
|
69
67
|
url: `${url}/stat/:stat`,
|
|
70
68
|
handler: async function (req, reply) {
|
|
71
69
|
if (!supported.includes(req.params.stat)) throw me.error('_notFound')
|
|
72
|
-
const helper = me.app.waibuDb[camelCase(`
|
|
70
|
+
const helper = me.app.waibuDb[camelCase(`create ${req.params.stat}`)]
|
|
71
|
+
if (!helper) throw this.error('_notFound')
|
|
73
72
|
const options = { queryHandler: builder.queryHandler }
|
|
74
73
|
const data = await helper({ model: builder.model, req, reply, ctx: this, options })
|
|
75
74
|
data.success = true
|
package/lib/route-by-verb.js
CHANGED
|
@@ -2,10 +2,11 @@ import path from 'path'
|
|
|
2
2
|
import serveRoute from './serve-route.js'
|
|
3
3
|
|
|
4
4
|
async function routeByVerb ({ file, ctx, appCtx, dir, pathPrefix, ns, alias, parent }) {
|
|
5
|
-
const { importModule
|
|
6
|
-
const {
|
|
5
|
+
const { importModule } = this.app.bajo
|
|
6
|
+
const { parseObject } = this.app.lib
|
|
7
|
+
const { isSet } = this.app.lib.aneka
|
|
7
8
|
const { methodMap, getParams } = this.app.waibuDb
|
|
8
|
-
const { isFunction, merge } = this.lib._
|
|
9
|
+
const { isFunction, merge } = this.app.lib._
|
|
9
10
|
const me = this
|
|
10
11
|
const url = path.dirname(file).replace(`${dir}/extend/${pathPrefix}`, '')
|
|
11
12
|
.replaceAll('@@', '*').replaceAll('@', ':')
|
|
@@ -30,12 +31,12 @@ async function routeByVerb ({ file, ctx, appCtx, dir, pathPrefix, ns, alias, par
|
|
|
30
31
|
}
|
|
31
32
|
if (['create', 'update', 'replace'].includes(action)) {
|
|
32
33
|
defSchema.response['4xx'] = {
|
|
33
|
-
description: this.
|
|
34
|
+
description: this.t('docErrorResponse'),
|
|
34
35
|
$ref: '4xxResp#'
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
38
|
defSchema.response['5xx'] = {
|
|
38
|
-
description: this.
|
|
39
|
+
description: this.t('generalErrorResponse'),
|
|
39
40
|
$ref: '5xxResp#'
|
|
40
41
|
}
|
|
41
42
|
if (action === 'find') defSchema.querystring = { $ref: 'qsFilter#' }
|
package/lib/serve-route.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
async function serveRoute ({ mod, ns, parent }) {
|
|
2
|
-
const { defaultsDeep } = this.lib.aneka
|
|
2
|
+
const { defaultsDeep } = this.app.lib.aneka
|
|
3
3
|
const { importModule } = this.app.bajo
|
|
4
4
|
const { getPluginPrefix } = this.app.waibu
|
|
5
|
-
const { pick } = this.lib._
|
|
5
|
+
const { pick } = this.app.lib._
|
|
6
6
|
const mergeRouteHooks = await importModule('waibu:/lib/webapp-scope/merge-route-hooks.js')
|
|
7
7
|
const cfgWdb = this.app.waibuDb.config
|
|
8
8
|
mod.config = mod.config ?? {}
|
package/lib/sub-app.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
async function subApp (ctx) {
|
|
2
2
|
const { importModule, runHook } = this.app.bajo
|
|
3
3
|
const { collect } = await importModule('waibu:/lib/app.js', { asDefaultImport: false })
|
|
4
|
-
const mods = await collect.call(this.app[this.
|
|
4
|
+
const mods = await collect.call(this.app[this.ns], 'boot.js', 'waibuRestApi')
|
|
5
5
|
for (const m of mods) {
|
|
6
6
|
this.log.debug('bootSubApp%s', m.ns)
|
|
7
7
|
await ctx.register(async (subCtx) => {
|
|
8
8
|
this.app[m.ns].instance = subCtx
|
|
9
|
-
await runHook(`${this.
|
|
9
|
+
await runHook(`${this.ns}.${m.alias}:afterCreateContext`, subCtx)
|
|
10
10
|
await m.handler.call(this.app[m.ns], subCtx, m.prefix)
|
|
11
11
|
}, { prefix: m.prefix })
|
|
12
12
|
}
|
package/package.json
CHANGED
|
@@ -1,35 +1,46 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "waibu-rest-api",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"description": "REST API for Waibu",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "waibu-rest-api",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "REST API for Waibu",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build-doc": "jsdoc -c .jsdoc.conf.json",
|
|
8
|
+
"test": "mocha"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"bajo": {
|
|
12
|
+
"type": "plugin",
|
|
13
|
+
"alias": "wra",
|
|
14
|
+
"dependencies": [
|
|
15
|
+
"waibu",
|
|
16
|
+
"waibu-db",
|
|
17
|
+
"bajo-extra"
|
|
18
|
+
]
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/ardhi/waibu-rest-api.git"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"api",
|
|
26
|
+
"restful",
|
|
27
|
+
"restapi",
|
|
28
|
+
"waibu",
|
|
29
|
+
"web",
|
|
30
|
+
"webserver",
|
|
31
|
+
"bajo",
|
|
32
|
+
"framework",
|
|
33
|
+
"fastify",
|
|
34
|
+
"modular"
|
|
35
|
+
],
|
|
36
|
+
"author": "Ardhi Lukianto <ardhi@lukianto.com>",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/ardhi/waibu-rest-api/issues"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/ardhi/waibu-rest-api#readme",
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"clean-jsdoc-theme": "^4.3.0",
|
|
44
|
+
"jsdoc-plugin-intersection": "^1.0.4"
|
|
45
|
+
}
|
|
46
|
+
}
|
package/wiki/CHANGES.md
ADDED
package/wiki/CONFIG.md
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
Thanks very much to everyone who wants to join as a contributor, but I have committed to devoting 100% of my time to this project. Therefore, I'm currently unable to accept pull requests from anyone until the project is large enough for me to manage it independently.
|
|
4
|
+
|
|
5
|
+
However, I am available if you encounter any bugs that require immediate fixes. Please report your issues in a GitHub issue, and I'll try to address them promptly. I'm also open to criticism and suggestions for improvements or requests for new features.
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
const desc = {
|
|
2
|
-
create: 'Post a new record',
|
|
3
|
-
find: 'Find records by query, page size and number.',
|
|
4
|
-
get: 'Get record by ID',
|
|
5
|
-
update: 'Update record by ID',
|
|
6
|
-
remove: 'Remove record by ID'
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function docDescription (method) {
|
|
10
|
-
return this.print.write(desc[method])
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export default docDescription
|