Skip to content

Commit

Permalink
🚑 非get/post/all打印出错
Browse files Browse the repository at this point in the history
  • Loading branch information
sky authored and sky committed Aug 31, 2022
1 parent 6209893 commit 1573187
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## [0.1.91](https://github.com/kongnet/skybase/compare/v0.1.90...v0.1.91) (2022-05-16)


### :memo:

* 增加RTS例子 ([ed4d52c](https://github.com/kongnet/skybase/commit/ed4d52ceeb04bd8eaba839117588ff90e4c7aac3))



## [0.1.90](https://github.com/kongnet/skybase/compare/v0.1.89...v0.1.90) (2022-05-01)


Expand Down
54 changes: 42 additions & 12 deletions lib/middleware/sky-check-param.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,38 @@
* */

const $ = require('meeko')
const $G = global.$G = global.$G || {}
const api = $G.api = $G.api || {}
const $G = (global.$G = global.$G || {})
const api = ($G.api = $G.api || {})

const config = require('../../skyconfig')

const apiCheckLog = function (url, obj, method, oldParam, ctx) {
if (config.logger) {
let checkColor = 'c'
obj = obj || {}
if (obj.code >= 400) { checkColor = 'y' }
if (obj.code >= 400) {
checkColor = 'y'
}
let methodStr = method.toUpperCase()
$.log($.c[{ 'GET': 'dimg', 'POST': 'dimy' }[methodStr]](`[ ${methodStr} ]`) + $.c.dimy(url.join ? url.join('/') : url) + $.c[checkColor](JSON.stringify(obj || '{}')), '实际参数:', oldParam ? $.c.r(JSON.stringify(oldParam || '{}')) : '', 'header参数:', $.c.c(JSON.stringify(ctx.header || '{}')))
$.log(
$.c[{ GET: 'dimg', POST: 'dimy' }[methodStr]]?.(`[ ${methodStr} ]`) ??
$.c.r(`[ ${methodStr} ]`) +
$.c.dimy(url.join ? url.join('/') : url) +
$.c[checkColor](JSON.stringify(obj || '{}')),
'实际参数:',
oldParam ? $.c.r(JSON.stringify(oldParam || '{}')) : '',
'header参数:',
$.c.c(JSON.stringify(ctx.header || '{}'))
)
}
}

// 重定向
const checkRedirect = (ctx, apiPath) => {
if (config.redirect) {
let url = config.redirect[apiPath] || null
if (url) { // 跳转
if (url) {
// 跳转
if (ctx.request.header) {
url = `${ctx.request.header['x-forwarded-prefix'] || ''}${url}`
}
Expand All @@ -48,7 +60,7 @@ const checkCache = (ctx, method, url) => {
// 快速检查是否可以不执行具体方法
const preCheck = (ctx, method, url, apiPath) => {
if (!['get', 'post'].includes(method)) {
ctx.throwCode(ctx.status, '只支持get/post方式')
ctx.throwCode(ctx.status, '只支持get/post/all方式')
return { succ: false, continue: false }
}
if (checkRedirect(ctx, apiPath)) {
Expand All @@ -64,16 +76,25 @@ const preCheck = (ctx, method, url, apiPath) => {
let apiSetting = api[apiPath]
//NOTICE: 支持动态路由,但找不到的时候性能有点损失
if (!apiSetting) {
apiSetting = api[Object.keys(api).find(x => x.includes(':')
&& new RegExp(x.replace(/(:[0-9a-z]+)/g, '[0-9a-z]+'), 'g').test(apiPath))]
apiSetting =
api[
Object.keys(api).find(
x =>
x.includes(':') &&
new RegExp(x.replace(/(:[0-9a-z]+)/g, '[0-9a-z]+'), 'g').test(
apiPath
)
)
]
}
if (!apiSetting) {
ctx.throwCode(404, '接口不存在')
return { succ: false, continue: false }
}

const needMethod = apiSetting.method
if (method !== needMethod && needMethod !== 'all') { // 允许get和post都接入
if (method !== needMethod && needMethod !== 'all') {
// 允许get和post都接入
$.err(`方法类型不对,应为${needMethod},不能为${method}`)
ctx.throwCode(405, '方法类型不对')
return { succ: false, continue: false }
Expand All @@ -86,15 +107,21 @@ module.exports = async (ctx, next) => {
const method = ctx.method.toLowerCase()
const url = ctx.request.url
const apiPath = url.split('?')[0]
const { continue: canContinue, succ, needMethod, apiSetting } = preCheck(ctx, method, url, apiPath)
const { continue: canContinue, succ, needMethod, apiSetting } = preCheck(
ctx,
method,
url,
apiPath
)
if (!canContinue) {
apiCheckLog(apiPath, ctx.body, method, null, ctx)
return succ ? 1 : 0
}

ctx.apiSetting = apiSetting

const params = method === 'get' ? ctx.query || {} : ctx.request.fields || ctx.body || {}
const params =
method === 'get' ? ctx.query || {} : ctx.request.fields || ctx.body || {}

// 下面是原来有的逻辑,感觉不安全,所以去掉了
// NOTICE 如果是上传文件不过滤参数 参数里有files的即为上传
Expand All @@ -106,7 +133,10 @@ module.exports = async (ctx, next) => {
// }
// }

ctx.checkedData = $.tools.checkParam(params, needMethod === 'all' && apiSetting.param[method] || apiSetting.param)
ctx.checkedData = $.tools.checkParam(
params,
(needMethod === 'all' && apiSetting.param[method]) || apiSetting.param
)
apiCheckLog(apiPath, ctx.checkedData, method, params, ctx)
if (ctx.checkedData.code >= 400) {
return ctx.throwCode(400, ctx.checkedData.msg)
Expand Down

0 comments on commit 1573187

Please sign in to comment.