waibu-rest-api 1.0.8 → 1.1.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/package.json +1 -1
- package/plugin/factory.js +134 -0
- package/bajo/.alias +0 -1
- package/bajo/.dependencies +0 -3
- package/bajo/config.json +0 -50
- package/bajo/init.js +0 -7
- package/bajo/method/route-path-handlers.json +0 -1
- package/bajo/method/route-path.js +0 -8
- package/bajo/method/transform-result.js +0 -58
- /package/{bajo → plugin}/method/doc-schema/description.js +0 -0
- /package/{bajo → plugin}/method/doc-schema/for-find.js +0 -0
- /package/{bajo → plugin}/method/doc-schema/general.js +0 -0
- /package/{bajo → plugin}/method/doc-schema/lib.js +0 -0
- /package/{bajo → plugin}/method/doc-schema/model.js +0 -0
- /package/{bajo → plugin}/method/doc-schema/params.js +0 -0
- /package/{bajo → plugin}/method/doc-schema/qs.js +0 -0
package/package.json
CHANGED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
async function factory (pkgName) {
|
|
2
|
+
const me = this
|
|
3
|
+
|
|
4
|
+
return class WaibuRestApi extends this.lib.BajoPlugin {
|
|
5
|
+
constructor () {
|
|
6
|
+
super(pkgName, me.app)
|
|
7
|
+
this.alias = 'wra'
|
|
8
|
+
this.dependencies = ['waibu', 'waibu-db', 'bajo-extra']
|
|
9
|
+
this.routePathHandlers = ['restapi']
|
|
10
|
+
this.config = {
|
|
11
|
+
waibu: {
|
|
12
|
+
prefix: 'api'
|
|
13
|
+
},
|
|
14
|
+
mountMainAsRoot: true,
|
|
15
|
+
exposeHead: true,
|
|
16
|
+
enablePatch: false,
|
|
17
|
+
bodyLimit: 1048576,
|
|
18
|
+
intl: {
|
|
19
|
+
detectors: ['qs', 'header']
|
|
20
|
+
},
|
|
21
|
+
format: {
|
|
22
|
+
supported: ['json', 'xml'],
|
|
23
|
+
asExt: false,
|
|
24
|
+
xml: {
|
|
25
|
+
bodyParser: {
|
|
26
|
+
contentTypes: ['text/xml', 'application/xml', 'application/rss+xml'],
|
|
27
|
+
validate: false
|
|
28
|
+
},
|
|
29
|
+
response: {
|
|
30
|
+
wrapper: 'doc',
|
|
31
|
+
declaration: true,
|
|
32
|
+
valueAsAttributes: true
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
responseKey: {
|
|
37
|
+
data: 'data',
|
|
38
|
+
oldData: 'oldData',
|
|
39
|
+
page: 'page',
|
|
40
|
+
count: 'count',
|
|
41
|
+
pages: 'pages',
|
|
42
|
+
success: 'success',
|
|
43
|
+
statusCode: 'code',
|
|
44
|
+
error: 'error',
|
|
45
|
+
cached: 'cached',
|
|
46
|
+
message: 'message',
|
|
47
|
+
details: 'details'
|
|
48
|
+
},
|
|
49
|
+
mapSlash: '~',
|
|
50
|
+
mapDot: ',',
|
|
51
|
+
multipart: {
|
|
52
|
+
},
|
|
53
|
+
cors: {},
|
|
54
|
+
helmet: {},
|
|
55
|
+
compress: false,
|
|
56
|
+
rateLimit: false,
|
|
57
|
+
disabled: [],
|
|
58
|
+
rerouted: {}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
init = async () => {
|
|
63
|
+
const { uniq } = this.app.bajo.lib._
|
|
64
|
+
this.config.format.supported.push('json')
|
|
65
|
+
this.config.format.supported = uniq(this.config.format.supported)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
routePath = (name) => {
|
|
69
|
+
const { get } = this.app.bajo.lib._
|
|
70
|
+
const { fullPath, ns } = this.app.bajo.breakNsPath(name)
|
|
71
|
+
const prefix = get(this.app[ns], 'config.waibu.prefix', this.app[ns].alias)
|
|
72
|
+
return `/${this.config.waibu.prefix}/${prefix}${fullPath}${this.config.format.asExt ? '.json' : ''}`
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
transformResult = ({ data, req, reply, options = {} }) => {
|
|
76
|
+
const reformat = ({ data, req, reply, options = {} }) => {
|
|
77
|
+
const { forOwn, get } = this.app.bajo.lib._
|
|
78
|
+
const newData = {}
|
|
79
|
+
forOwn(data, (v, k) => {
|
|
80
|
+
let key = get(this.config, `responseKey.${k}`, k)
|
|
81
|
+
if (options.forFind && k === 'data') key = get(this.config, 'responseKey.data')
|
|
82
|
+
newData[key] = v
|
|
83
|
+
})
|
|
84
|
+
return newData
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const returnError = ({ data, req, reply, options = {} }) => {
|
|
88
|
+
const { pascalCase } = this.app.bajo
|
|
89
|
+
const { last, map, kebabCase, upperFirst, keys, each, get, isEmpty } = this.app.bajo.lib._
|
|
90
|
+
const cfg = this.config
|
|
91
|
+
const cfgWdb = this.app.waibuDb.config
|
|
92
|
+
const errNames = kebabCase(data.constructor.name).split('-')
|
|
93
|
+
if (last(errNames) === 'error') errNames.pop()
|
|
94
|
+
data.error = this.print.write(map(errNames, s => upperFirst(s)).join(' '))
|
|
95
|
+
data.success = false
|
|
96
|
+
data.statusCode = data.statusCode ?? 500
|
|
97
|
+
if (reply && cfgWdb.dbModel.dataOnly) {
|
|
98
|
+
each(keys(data), k => {
|
|
99
|
+
const key = get(cfg, `responseKey.${k}`, k)
|
|
100
|
+
if (k === 'details' && !isEmpty(data[k])) data[k] = JSON.stringify(data[k])
|
|
101
|
+
reply.header(`X-${pascalCase(this.alias)}-${pascalCase(key)}`, data[k])
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
reply.code(data.statusCode)
|
|
105
|
+
const result = cfgWdb.dbModel.dataOnly ? { error: data.message } : data
|
|
106
|
+
return reformat.call(this, { data: result, req, reply, options })
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const returnSuccess = ({ data, req, reply, options = {} }) => {
|
|
110
|
+
const { pascalCase } = this.app.bajo
|
|
111
|
+
const { each, keys, omit, get } = this.app.bajo.lib._
|
|
112
|
+
const cfg = this.config
|
|
113
|
+
const cfgWdb = this.app.waibuDb.config
|
|
114
|
+
if (reply) {
|
|
115
|
+
reply.code(req.method.toUpperCase() === 'POST' ? 201 : 200)
|
|
116
|
+
if (cfgWdb.dbModel.dataOnly) {
|
|
117
|
+
each(keys(omit(data, ['data'])), k => {
|
|
118
|
+
const key = get(cfg, `responseKey.${k}`, k)
|
|
119
|
+
reply.header(`X-${pascalCase(this.alias)}-${pascalCase(key)}`, data[k])
|
|
120
|
+
})
|
|
121
|
+
return data.data
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return reformat({ data, req, reply, options })
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const isError = data instanceof Error
|
|
128
|
+
if (isError) return returnError({ data, req, reply, options })
|
|
129
|
+
return returnSuccess({ data, req, reply, options })
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export default factory
|
package/bajo/.alias
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
wra
|
package/bajo/.dependencies
DELETED
package/bajo/config.json
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"waibu": {
|
|
3
|
-
"prefix": "api"
|
|
4
|
-
},
|
|
5
|
-
"mountMainAsRoot": true,
|
|
6
|
-
"exposeHead": true,
|
|
7
|
-
"enablePatch": false,
|
|
8
|
-
"bodyLimit": 1048576,
|
|
9
|
-
"intl": {
|
|
10
|
-
"detectors": ["qs", "header"]
|
|
11
|
-
},
|
|
12
|
-
"format": {
|
|
13
|
-
"supported": ["json", "xml"],
|
|
14
|
-
"asExt": false,
|
|
15
|
-
"xml": {
|
|
16
|
-
"bodyParser": {
|
|
17
|
-
"contentTypes": ["text/xml", "application/xml", "application/rss+xml"],
|
|
18
|
-
"validate": false
|
|
19
|
-
},
|
|
20
|
-
"response": {
|
|
21
|
-
"wrapper": "doc",
|
|
22
|
-
"declaration": true,
|
|
23
|
-
"valueAsAttributes": true
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
"responseKey": {
|
|
28
|
-
"data": "data",
|
|
29
|
-
"oldData": "oldData",
|
|
30
|
-
"page": "page",
|
|
31
|
-
"count": "count",
|
|
32
|
-
"pages": "pages",
|
|
33
|
-
"success": "success",
|
|
34
|
-
"statusCode": "code",
|
|
35
|
-
"error": "error",
|
|
36
|
-
"cached": "cached",
|
|
37
|
-
"message": "message",
|
|
38
|
-
"details": "details"
|
|
39
|
-
},
|
|
40
|
-
"mapSlash": "~",
|
|
41
|
-
"mapDot": ",",
|
|
42
|
-
"multipart": {
|
|
43
|
-
},
|
|
44
|
-
"cors": {},
|
|
45
|
-
"helmet": {},
|
|
46
|
-
"compress": false,
|
|
47
|
-
"rateLimit": false,
|
|
48
|
-
"disabled": [],
|
|
49
|
-
"rerouted": {}
|
|
50
|
-
}
|
package/bajo/init.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
["restapi"]
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
function routePath (name) {
|
|
2
|
-
const { get } = this.app.bajo.lib._
|
|
3
|
-
const { fullPath, ns } = this.app.bajo.breakNsPath(name)
|
|
4
|
-
const prefix = get(this.app[ns], 'config.waibu.prefix', this.app[ns].alias)
|
|
5
|
-
return `/${this.config.waibu.prefix}/${prefix}${fullPath}${this.config.format.asExt ? '.json' : ''}`
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export default routePath
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
function reformat ({ data, req, reply, options = {} }) {
|
|
2
|
-
const { forOwn, get } = this.app.bajo.lib._
|
|
3
|
-
const newData = {}
|
|
4
|
-
forOwn(data, (v, k) => {
|
|
5
|
-
let key = get(this.config, `responseKey.${k}`, k)
|
|
6
|
-
if (options.forFind && k === 'data') key = get(this.config, 'responseKey.data')
|
|
7
|
-
newData[key] = v
|
|
8
|
-
})
|
|
9
|
-
return newData
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function returnError ({ data, req, reply, options = {} }) {
|
|
13
|
-
const { pascalCase } = this.app.bajo
|
|
14
|
-
const { last, map, kebabCase, upperFirst, keys, each, get, isEmpty } = this.app.bajo.lib._
|
|
15
|
-
const cfg = this.config
|
|
16
|
-
const cfgWdb = this.app.waibuDb.config
|
|
17
|
-
const errNames = kebabCase(data.constructor.name).split('-')
|
|
18
|
-
if (last(errNames) === 'error') errNames.pop()
|
|
19
|
-
data.error = this.print.write(map(errNames, s => upperFirst(s)).join(' '))
|
|
20
|
-
data.success = false
|
|
21
|
-
data.statusCode = data.statusCode ?? 500
|
|
22
|
-
if (reply && cfgWdb.dbModel.dataOnly) {
|
|
23
|
-
each(keys(data), k => {
|
|
24
|
-
const key = get(cfg, `responseKey.${k}`, k)
|
|
25
|
-
if (k === 'details' && !isEmpty(data[k])) data[k] = JSON.stringify(data[k])
|
|
26
|
-
reply.header(`X-${pascalCase(this.alias)}-${pascalCase(key)}`, data[k])
|
|
27
|
-
})
|
|
28
|
-
}
|
|
29
|
-
reply.code(data.statusCode)
|
|
30
|
-
const result = cfgWdb.dbModel.dataOnly ? { error: data.message } : data
|
|
31
|
-
return reformat.call(this, { data: result, req, reply, options })
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function returnSuccess ({ data, req, reply, options = {} }) {
|
|
35
|
-
const { pascalCase } = this.app.bajo
|
|
36
|
-
const { each, keys, omit, get } = this.app.bajo.lib._
|
|
37
|
-
const cfg = this.config
|
|
38
|
-
const cfgWdb = this.app.waibuDb.config
|
|
39
|
-
if (reply) {
|
|
40
|
-
reply.code(req.method.toUpperCase() === 'POST' ? 201 : 200)
|
|
41
|
-
if (cfgWdb.dbModel.dataOnly) {
|
|
42
|
-
each(keys(omit(data, ['data'])), k => {
|
|
43
|
-
const key = get(cfg, `responseKey.${k}`, k)
|
|
44
|
-
reply.header(`X-${pascalCase(this.alias)}-${pascalCase(key)}`, data[k])
|
|
45
|
-
})
|
|
46
|
-
return data.data
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return reformat.call(this, { data, req, reply, options })
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function transformResult ({ data, req, reply, options = {} }) {
|
|
53
|
-
const isError = data instanceof Error
|
|
54
|
-
if (isError) return returnError.call(this, { data, req, reply, options })
|
|
55
|
-
return returnSuccess.call(this, { data, req, reply, options })
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export default transformResult
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|