skybase 0.1.91 → 0.1.92
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/CHANGELOG.md +9 -0
- package/lib/middleware/sky-check-param.js +42 -12
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
## [0.1.91](https://github.com/kongnet/skybase/compare/v0.1.90...v0.1.91) (2022-05-16)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### :memo:
|
|
5
|
+
|
|
6
|
+
* 增加RTS例子 ([ed4d52c](https://github.com/kongnet/skybase/commit/ed4d52ceeb04bd8eaba839117588ff90e4c7aac3))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
1
10
|
## [0.1.90](https://github.com/kongnet/skybase/compare/v0.1.89...v0.1.90) (2022-05-01)
|
|
2
11
|
|
|
3
12
|
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* */
|
|
4
4
|
|
|
5
5
|
const $ = require('meeko')
|
|
6
|
-
const $G = global.$G = global.$G || {}
|
|
7
|
-
const api = $G.api = $G.api || {}
|
|
6
|
+
const $G = (global.$G = global.$G || {})
|
|
7
|
+
const api = ($G.api = $G.api || {})
|
|
8
8
|
|
|
9
9
|
const config = require('../../skyconfig')
|
|
10
10
|
|
|
@@ -12,9 +12,20 @@ const apiCheckLog = function (url, obj, method, oldParam, ctx) {
|
|
|
12
12
|
if (config.logger) {
|
|
13
13
|
let checkColor = 'c'
|
|
14
14
|
obj = obj || {}
|
|
15
|
-
if (obj.code >= 400) {
|
|
15
|
+
if (obj.code >= 400) {
|
|
16
|
+
checkColor = 'y'
|
|
17
|
+
}
|
|
16
18
|
let methodStr = method.toUpperCase()
|
|
17
|
-
$.log(
|
|
19
|
+
$.log(
|
|
20
|
+
$.c[{ GET: 'dimg', POST: 'dimy' }[methodStr]]?.(`[ ${methodStr} ]`) ??
|
|
21
|
+
$.c.r(`[ ${methodStr} ]`) +
|
|
22
|
+
$.c.dimy(url.join ? url.join('/') : url) +
|
|
23
|
+
$.c[checkColor](JSON.stringify(obj || '{}')),
|
|
24
|
+
'实际参数:',
|
|
25
|
+
oldParam ? $.c.r(JSON.stringify(oldParam || '{}')) : '',
|
|
26
|
+
'header参数:',
|
|
27
|
+
$.c.c(JSON.stringify(ctx.header || '{}'))
|
|
28
|
+
)
|
|
18
29
|
}
|
|
19
30
|
}
|
|
20
31
|
|
|
@@ -22,7 +33,8 @@ const apiCheckLog = function (url, obj, method, oldParam, ctx) {
|
|
|
22
33
|
const checkRedirect = (ctx, apiPath) => {
|
|
23
34
|
if (config.redirect) {
|
|
24
35
|
let url = config.redirect[apiPath] || null
|
|
25
|
-
if (url) {
|
|
36
|
+
if (url) {
|
|
37
|
+
// 跳转
|
|
26
38
|
if (ctx.request.header) {
|
|
27
39
|
url = `${ctx.request.header['x-forwarded-prefix'] || ''}${url}`
|
|
28
40
|
}
|
|
@@ -48,7 +60,7 @@ const checkCache = (ctx, method, url) => {
|
|
|
48
60
|
// 快速检查是否可以不执行具体方法
|
|
49
61
|
const preCheck = (ctx, method, url, apiPath) => {
|
|
50
62
|
if (!['get', 'post'].includes(method)) {
|
|
51
|
-
ctx.throwCode(ctx.status, '只支持get/post方式')
|
|
63
|
+
ctx.throwCode(ctx.status, '只支持get/post/all方式')
|
|
52
64
|
return { succ: false, continue: false }
|
|
53
65
|
}
|
|
54
66
|
if (checkRedirect(ctx, apiPath)) {
|
|
@@ -64,8 +76,16 @@ const preCheck = (ctx, method, url, apiPath) => {
|
|
|
64
76
|
let apiSetting = api[apiPath]
|
|
65
77
|
//NOTICE: 支持动态路由,但找不到的时候性能有点损失
|
|
66
78
|
if (!apiSetting) {
|
|
67
|
-
apiSetting =
|
|
68
|
-
|
|
79
|
+
apiSetting =
|
|
80
|
+
api[
|
|
81
|
+
Object.keys(api).find(
|
|
82
|
+
x =>
|
|
83
|
+
x.includes(':') &&
|
|
84
|
+
new RegExp(x.replace(/(:[0-9a-z]+)/g, '[0-9a-z]+'), 'g').test(
|
|
85
|
+
apiPath
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
]
|
|
69
89
|
}
|
|
70
90
|
if (!apiSetting) {
|
|
71
91
|
ctx.throwCode(404, '接口不存在')
|
|
@@ -73,7 +93,8 @@ const preCheck = (ctx, method, url, apiPath) => {
|
|
|
73
93
|
}
|
|
74
94
|
|
|
75
95
|
const needMethod = apiSetting.method
|
|
76
|
-
if (method !== needMethod && needMethod !== 'all') {
|
|
96
|
+
if (method !== needMethod && needMethod !== 'all') {
|
|
97
|
+
// 允许get和post都接入
|
|
77
98
|
$.err(`方法类型不对,应为${needMethod},不能为${method}`)
|
|
78
99
|
ctx.throwCode(405, '方法类型不对')
|
|
79
100
|
return { succ: false, continue: false }
|
|
@@ -86,7 +107,12 @@ module.exports = async (ctx, next) => {
|
|
|
86
107
|
const method = ctx.method.toLowerCase()
|
|
87
108
|
const url = ctx.request.url
|
|
88
109
|
const apiPath = url.split('?')[0]
|
|
89
|
-
const { continue: canContinue, succ, needMethod, apiSetting } = preCheck(
|
|
110
|
+
const { continue: canContinue, succ, needMethod, apiSetting } = preCheck(
|
|
111
|
+
ctx,
|
|
112
|
+
method,
|
|
113
|
+
url,
|
|
114
|
+
apiPath
|
|
115
|
+
)
|
|
90
116
|
if (!canContinue) {
|
|
91
117
|
apiCheckLog(apiPath, ctx.body, method, null, ctx)
|
|
92
118
|
return succ ? 1 : 0
|
|
@@ -94,7 +120,8 @@ module.exports = async (ctx, next) => {
|
|
|
94
120
|
|
|
95
121
|
ctx.apiSetting = apiSetting
|
|
96
122
|
|
|
97
|
-
const params =
|
|
123
|
+
const params =
|
|
124
|
+
method === 'get' ? ctx.query || {} : ctx.request.fields || ctx.body || {}
|
|
98
125
|
|
|
99
126
|
// 下面是原来有的逻辑,感觉不安全,所以去掉了
|
|
100
127
|
// NOTICE 如果是上传文件不过滤参数 参数里有files的即为上传
|
|
@@ -106,7 +133,10 @@ module.exports = async (ctx, next) => {
|
|
|
106
133
|
// }
|
|
107
134
|
// }
|
|
108
135
|
|
|
109
|
-
ctx.checkedData = $.tools.checkParam(
|
|
136
|
+
ctx.checkedData = $.tools.checkParam(
|
|
137
|
+
params,
|
|
138
|
+
(needMethod === 'all' && apiSetting.param[method]) || apiSetting.param
|
|
139
|
+
)
|
|
110
140
|
apiCheckLog(apiPath, ctx.checkedData, method, params, ctx)
|
|
111
141
|
if (ctx.checkedData.code >= 400) {
|
|
112
142
|
return ctx.throwCode(400, ctx.checkedData.msg)
|