waibu-rest-api 1.1.0 → 1.1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "waibu-rest-api",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "REST API for Waibu",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,135 @@
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, trimStart } = 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
+ const path = `/${this.config.waibu.prefix}/${prefix}${fullPath}${this.config.format.asExt ? '.json' : ''}`
73
+ return '/' + trimStart(path, '/')
74
+ }
75
+
76
+ transformResult = ({ data, req, reply, options = {} }) => {
77
+ const reformat = ({ data, req, reply, options = {} }) => {
78
+ const { forOwn, get } = this.app.bajo.lib._
79
+ const newData = {}
80
+ forOwn(data, (v, k) => {
81
+ let key = get(this.config, `responseKey.${k}`, k)
82
+ if (options.forFind && k === 'data') key = get(this.config, 'responseKey.data')
83
+ newData[key] = v
84
+ })
85
+ return newData
86
+ }
87
+
88
+ const returnError = ({ data, req, reply, options = {} }) => {
89
+ const { pascalCase } = this.app.bajo
90
+ const { last, map, kebabCase, upperFirst, keys, each, get, isEmpty } = this.app.bajo.lib._
91
+ const cfg = this.config
92
+ const cfgWdb = this.app.waibuDb.config
93
+ const errNames = kebabCase(data.constructor.name).split('-')
94
+ if (last(errNames) === 'error') errNames.pop()
95
+ data.error = this.print.write(map(errNames, s => upperFirst(s)).join(' '))
96
+ data.success = false
97
+ data.statusCode = data.statusCode ?? 500
98
+ if (reply && cfgWdb.dbModel.dataOnly) {
99
+ each(keys(data), k => {
100
+ const key = get(cfg, `responseKey.${k}`, k)
101
+ if (k === 'details' && !isEmpty(data[k])) data[k] = JSON.stringify(data[k])
102
+ reply.header(`X-${pascalCase(this.alias)}-${pascalCase(key)}`, data[k])
103
+ })
104
+ }
105
+ reply.code(data.statusCode)
106
+ const result = cfgWdb.dbModel.dataOnly ? { error: data.message } : data
107
+ return reformat.call(this, { data: result, req, reply, options })
108
+ }
109
+
110
+ const returnSuccess = ({ data, req, reply, options = {} }) => {
111
+ const { pascalCase } = this.app.bajo
112
+ const { each, keys, omit, get } = this.app.bajo.lib._
113
+ const cfg = this.config
114
+ const cfgWdb = this.app.waibuDb.config
115
+ if (reply) {
116
+ reply.code(req.method.toUpperCase() === 'POST' ? 201 : 200)
117
+ if (cfgWdb.dbModel.dataOnly) {
118
+ each(keys(omit(data, ['data'])), k => {
119
+ const key = get(cfg, `responseKey.${k}`, k)
120
+ reply.header(`X-${pascalCase(this.alias)}-${pascalCase(key)}`, data[k])
121
+ })
122
+ return data.data
123
+ }
124
+ }
125
+ return reformat({ data, req, reply, options })
126
+ }
127
+
128
+ const isError = data instanceof Error
129
+ if (isError) return returnError({ data, req, reply, options })
130
+ return returnSuccess({ data, req, reply, options })
131
+ }
132
+ }
133
+ }
134
+
135
+ export default factory
package/plugin/.alias DELETED
@@ -1 +0,0 @@
1
- wra
@@ -1,3 +0,0 @@
1
- waibu-db
2
- bajo-extra
3
- waibu
@@ -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/plugin/init.js DELETED
@@ -1,7 +0,0 @@
1
- async function start () {
2
- const { uniq } = this.app.bajo.lib._
3
- this.config.format.supported.push('json')
4
- this.config.format.supported = uniq(this.config.format.supported)
5
- }
6
-
7
- export default start
@@ -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