waibu-rest-api 1.2.2 → 2.0.1
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/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 +154 -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/{waibu → extend/waibu}/boot.js +18 -17
- package/index.js +25 -11
- package/lib/handle-response/xml.js +1 -1
- package/lib/not-found.js +1 -1
- package/lib/route-by-model-builder.js +3 -3
- package/lib/route-by-verb.js +5 -5
- package/lib/serve-route.js +2 -2
- package/lib/sub-app.js +2 -2
- package/{plugin-method → method}/doc-schema/description.js +1 -1
- package/{plugin-method → method}/doc-schema/general.js +1 -1
- package/{plugin-method → method}/doc-schema/lib.js +1 -1
- package/{plugin-method → method}/doc-schema/model.js +5 -5
- package/{plugin-method → method}/doc-schema/params.js +2 -2
- package/{plugin-method → method}/doc-schema/qs.js +2 -2
- package/package.json +40 -35
- package/wiki/CONFIG.md +3 -0
- package/wiki/CONTRIBUTING.md +5 -0
- /package/{bajo → extend/bajo}/hook/waibu-rest-api@pre-parsing.js +0 -0
- /package/{bajo → extend/bajo}/hook/waibu@after-init.js +0 -0
- /package/{bajo → extend/bajo}/intl/en-US.json +0 -0
- /package/{bajo → extend/bajo}/intl/id.json +0 -0
- /package/{plugin-method → method}/doc-schema/for-find.js +0 -0
package/index.js
CHANGED
|
@@ -1,11 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin factory
|
|
3
|
+
*
|
|
4
|
+
* @param {string} pkgName - NPM package name
|
|
5
|
+
* @returns {class}
|
|
6
|
+
*/
|
|
1
7
|
async function factory (pkgName) {
|
|
2
8
|
const me = this
|
|
3
9
|
|
|
4
|
-
|
|
10
|
+
/**
|
|
11
|
+
* WaibuRestApi class
|
|
12
|
+
*
|
|
13
|
+
* @class
|
|
14
|
+
*/
|
|
15
|
+
class WaibuRestApi extends this.app.pluginClass.base {
|
|
16
|
+
static alias = 'wra'
|
|
17
|
+
static dependencies = ['waibu', 'waibu-db', 'bajo-extra']
|
|
18
|
+
|
|
5
19
|
constructor () {
|
|
6
20
|
super(pkgName, me.app)
|
|
7
|
-
this.alias = 'wra'
|
|
8
|
-
this.dependencies = ['waibu', 'waibu-db', 'bajo-extra']
|
|
9
21
|
this.routePathHandlers = ['restapi']
|
|
10
22
|
this.config = {
|
|
11
23
|
waibu: {
|
|
@@ -60,13 +72,13 @@ async function factory (pkgName) {
|
|
|
60
72
|
}
|
|
61
73
|
|
|
62
74
|
init = async () => {
|
|
63
|
-
const { uniq } = this.lib._
|
|
75
|
+
const { uniq } = this.app.lib._
|
|
64
76
|
this.config.format.supported.push('json')
|
|
65
77
|
this.config.format.supported = uniq(this.config.format.supported)
|
|
66
78
|
}
|
|
67
79
|
|
|
68
80
|
routePath = (name, { defFormat = '.json', uriEncoded = true } = {}) => {
|
|
69
|
-
const { get, trimStart } = this.lib._
|
|
81
|
+
const { get, trimStart } = this.app.lib._
|
|
70
82
|
const { fullPath, ns } = this.app.bajo.breakNsPath(name)
|
|
71
83
|
const prefix = get(this.app[ns], 'config.waibu.prefix', this.app[ns].alias)
|
|
72
84
|
const format = defFormat === false ? '' : (this.config.format.asExt ? defFormat : '')
|
|
@@ -77,7 +89,7 @@ async function factory (pkgName) {
|
|
|
77
89
|
|
|
78
90
|
transformResult = ({ data, req, reply, options = {} }) => {
|
|
79
91
|
const reformat = ({ data, req, reply, options = {} }) => {
|
|
80
|
-
const { forOwn, get } = this.lib._
|
|
92
|
+
const { forOwn, get } = this.app.lib._
|
|
81
93
|
const newData = {}
|
|
82
94
|
forOwn(data, (v, k) => {
|
|
83
95
|
let key = get(this.config, `responseKey.${k}`, k)
|
|
@@ -88,13 +100,13 @@ async function factory (pkgName) {
|
|
|
88
100
|
}
|
|
89
101
|
|
|
90
102
|
const returnError = ({ data, req, reply, options = {} }) => {
|
|
91
|
-
const { pascalCase } = this.lib.aneka
|
|
92
|
-
const { last, map, kebabCase, upperFirst, keys, each, get, isEmpty } = this.lib._
|
|
103
|
+
const { pascalCase } = this.app.lib.aneka
|
|
104
|
+
const { last, map, kebabCase, upperFirst, keys, each, get, isEmpty } = this.app.lib._
|
|
93
105
|
const cfg = this.config
|
|
94
106
|
const cfgWdb = this.app.waibuDb.config
|
|
95
107
|
const errNames = kebabCase(data.constructor.name).split('-')
|
|
96
108
|
if (last(errNames) === 'error') errNames.pop()
|
|
97
|
-
data.error = this.
|
|
109
|
+
data.error = this.t(map(errNames, s => upperFirst(s)).join(' '))
|
|
98
110
|
data.success = false
|
|
99
111
|
data.statusCode = data.statusCode ?? 500
|
|
100
112
|
if (reply && cfgWdb.dbModel.dataOnly) {
|
|
@@ -110,8 +122,8 @@ async function factory (pkgName) {
|
|
|
110
122
|
}
|
|
111
123
|
|
|
112
124
|
const returnSuccess = ({ data, req, reply, options = {} }) => {
|
|
113
|
-
const { pascalCase } = this.lib.aneka
|
|
114
|
-
const { each, keys, omit, get } = this.lib._
|
|
125
|
+
const { pascalCase } = this.app.lib.aneka
|
|
126
|
+
const { each, keys, omit, get } = this.app.lib._
|
|
115
127
|
const cfg = this.config
|
|
116
128
|
const cfgWdb = this.app.waibuDb.config
|
|
117
129
|
if (reply) {
|
|
@@ -132,6 +144,8 @@ async function factory (pkgName) {
|
|
|
132
144
|
return returnSuccess({ data, req, reply, options })
|
|
133
145
|
}
|
|
134
146
|
}
|
|
147
|
+
|
|
148
|
+
return WaibuRestApi
|
|
135
149
|
}
|
|
136
150
|
|
|
137
151
|
export default factory
|
|
@@ -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/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
|
}
|
|
@@ -5,14 +5,14 @@ async function routeByModelBuilder ({ file, ctx, dir, pathPrefix, ns, parent })
|
|
|
5
5
|
const { getPlugin } = this.app.bajo
|
|
6
6
|
getPlugin('dobo') // ensure dobo is loaded
|
|
7
7
|
const { readConfig, parseObject } = this.app.bajo
|
|
8
|
-
const { isSet } = this.lib.aneka
|
|
8
|
+
const { isSet } = this.app.lib.aneka
|
|
9
9
|
const { methodMap, getParams } = this.app.waibuDb
|
|
10
10
|
const { getInfo } = this.app.dobo
|
|
11
|
-
const { isFunction, camelCase, omit, merge, keys, filter } = this.lib._
|
|
11
|
+
const { isFunction, camelCase, omit, merge, keys, filter } = this.app.lib._
|
|
12
12
|
const builder = await readConfig(file, { ns, ignoreError: true })
|
|
13
13
|
builder.disabled = builder.disabled ?? []
|
|
14
14
|
const { schema } = getInfo(builder.model)
|
|
15
|
-
let url = path.dirname(file).replace(`${dir}/${pathPrefix}`, '').replaceAll('@', ':')
|
|
15
|
+
let url = path.dirname(file).replace(`${dir}/extend/${pathPrefix}`, '').replaceAll('@', ':')
|
|
16
16
|
if (isFunction(builder.url)) url = await builder.url.call(this)
|
|
17
17
|
const methods = keys(methodMap)
|
|
18
18
|
if (this.config.enablePatch) methods.push('replace')
|
package/lib/route-by-verb.js
CHANGED
|
@@ -3,11 +3,11 @@ import serveRoute from './serve-route.js'
|
|
|
3
3
|
|
|
4
4
|
async function routeByVerb ({ file, ctx, appCtx, dir, pathPrefix, ns, alias, parent }) {
|
|
5
5
|
const { importModule, parseObject } = this.app.bajo
|
|
6
|
-
const { isSet } = this.lib.aneka
|
|
6
|
+
const { isSet } = this.app.lib.aneka
|
|
7
7
|
const { methodMap, getParams } = this.app.waibuDb
|
|
8
|
-
const { isFunction, merge } = this.lib._
|
|
8
|
+
const { isFunction, merge } = this.app.lib._
|
|
9
9
|
const me = this
|
|
10
|
-
const url = path.dirname(file).replace(`${dir}/${pathPrefix}`, '')
|
|
10
|
+
const url = path.dirname(file).replace(`${dir}/extend/${pathPrefix}`, '')
|
|
11
11
|
.replaceAll('@@', '*').replaceAll('@', ':')
|
|
12
12
|
const action = path.basename(file, '.js')
|
|
13
13
|
let method = methodMap[action]
|
|
@@ -30,12 +30,12 @@ async function routeByVerb ({ file, ctx, appCtx, dir, pathPrefix, ns, alias, par
|
|
|
30
30
|
}
|
|
31
31
|
if (['create', 'update', 'replace'].includes(action)) {
|
|
32
32
|
defSchema.response['4xx'] = {
|
|
33
|
-
description: this.
|
|
33
|
+
description: this.t('docErrorResponse'),
|
|
34
34
|
$ref: '4xxResp#'
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
defSchema.response['5xx'] = {
|
|
38
|
-
description: this.
|
|
38
|
+
description: this.t('generalErrorResponse'),
|
|
39
39
|
$ref: '5xxResp#'
|
|
40
40
|
}
|
|
41
41
|
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
|
}
|
|
@@ -48,7 +48,7 @@ async function buildPropsReqs ({ schema, method, options = {} }) {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
async function buildResponse ({ ctx, schema, method, options }) {
|
|
51
|
-
const { merge, cloneDeep } = this.lib._
|
|
51
|
+
const { merge, cloneDeep } = this.app.lib._
|
|
52
52
|
const { transformResult } = this.app.waibuRestApi
|
|
53
53
|
const cfgWdb = this.app.waibuDb.config
|
|
54
54
|
const { properties } = await buildPropsReqs.call(this, { schema, method, options })
|
|
@@ -69,18 +69,18 @@ async function buildResponse ({ ctx, schema, method, options }) {
|
|
|
69
69
|
|
|
70
70
|
const result = {
|
|
71
71
|
'2xx': {
|
|
72
|
-
description: this.
|
|
72
|
+
description: this.t('successfulResponse'),
|
|
73
73
|
type: 'object'
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
if (['create', 'update', 'replace'].includes(method)) {
|
|
77
77
|
result['4xx'] = {
|
|
78
|
-
description: this.
|
|
78
|
+
description: this.t('docErrorResponse'),
|
|
79
79
|
$ref: '4xxResp#'
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
result['5xx'] = {
|
|
83
|
-
description: this.
|
|
83
|
+
description: this.t('generalErrorResponse'),
|
|
84
84
|
$ref: '5xxResp#'
|
|
85
85
|
}
|
|
86
86
|
if (cfgWdb.dbModel.dataOnly) {
|
|
@@ -116,7 +116,7 @@ async function buildResponse ({ ctx, schema, method, options }) {
|
|
|
116
116
|
async function docSchemaModel ({ model, method, ctx, options = {} }) {
|
|
117
117
|
const { getInfo } = this.app.dobo
|
|
118
118
|
const { schema } = getInfo(model)
|
|
119
|
-
const { omit } = this.lib._
|
|
119
|
+
const { omit } = this.app.lib._
|
|
120
120
|
const out = {
|
|
121
121
|
description: options.description ?? this.docSchemaDescription(method),
|
|
122
122
|
tags: [this.alias.toUpperCase(), ...(options.alias ?? [])]
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
async function buildParams (ctx, paramName, ...args) {
|
|
2
|
-
const { each, isEmpty, keys, last, isBoolean } = this.lib._
|
|
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 => {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
async function buildParams (ctx, paramName, ...args) {
|
|
2
|
-
const { each, isEmpty } = this.lib._
|
|
2
|
+
const { each, isEmpty } = this.app.lib._
|
|
3
3
|
const item = {
|
|
4
4
|
type: 'object',
|
|
5
5
|
properties: {}
|
|
@@ -7,7 +7,7 @@ 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
|
}
|
package/package.json
CHANGED
|
@@ -1,35 +1,40 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "waibu-rest-api",
|
|
3
|
-
"version": "
|
|
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.0.1",
|
|
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
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/ardhi/waibu-rest-api.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"api",
|
|
20
|
+
"restful",
|
|
21
|
+
"restapi",
|
|
22
|
+
"waibu",
|
|
23
|
+
"web",
|
|
24
|
+
"webserver",
|
|
25
|
+
"bajo",
|
|
26
|
+
"framework",
|
|
27
|
+
"fastify",
|
|
28
|
+
"modular"
|
|
29
|
+
],
|
|
30
|
+
"author": "Ardhi Lukianto <ardhi@lukianto.com>",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/ardhi/waibu-rest-api/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/ardhi/waibu-rest-api#readme",
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"clean-jsdoc-theme": "^4.3.0",
|
|
38
|
+
"jsdoc-plugin-intersection": "^1.0.4"
|
|
39
|
+
}
|
|
40
|
+
}
|
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.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|