Skip to content

Commit

Permalink
chore: 区分 json 语法报错
Browse files Browse the repository at this point in the history
  • Loading branch information
vv314 committed Dec 26, 2021
1 parent 9d608c3 commit dcef227
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions lib/parse-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ function tokenFormat(token, index = 0) {
tgUid: '',
qywxUid: '',
barkKey: '',
larkWebhook: ''
larkWebhook: '',
qq: ''
}

if (typeof token == 'string') {
Expand All @@ -17,21 +18,19 @@ function tokenFormat(token, index = 0) {
}

function parseToken(token) {
try {
token = JSON.parse(token)
} catch (e) {}
if (!token) throw '请配置 TOKEN'

const likeArray = token.constructor == Array
const likeObject = token.constructor == Object
let tokenList = []
const likeJson = ['{', '['].includes(token.trim()[0])

if (!likeArray && !likeObject) {
return [tokenFormat(token)]
}
if (!likeJson) return [tokenFormat(token)]

tokenList = tokenList.concat(token)
try {
token = JSON.parse(token)
} catch (e) {
throw `TOKEN 解析错误: ${e.message}`
}

return tokenList.map(tokenFormat)
return [].concat(token).map(tokenFormat)
}

module.exports = parseToken

0 comments on commit dcef227

Please sign in to comment.