2018-08-14 16:15:28 +00:00
|
|
|
const _ = require('lodash')
|
2018-08-15 09:58:59 +00:00
|
|
|
const resMessage = require('./res-message')
|
2018-08-14 06:17:48 +00:00
|
|
|
/**
|
|
|
|
* check Object
|
|
|
|
* @param {string} key key name
|
|
|
|
* @param {string} type type name
|
|
|
|
* @param {boolean} empty can empty
|
|
|
|
*/
|
|
|
|
const chkObject = function (key = '', type = '', empty = false) {
|
|
|
|
const uuidChk = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
|
|
|
if (!(key in this.body)) return false
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'string':
|
|
|
|
if (typeof this.body[key] !== 'string' || (!empty && !this.body[key])) return false
|
|
|
|
break
|
|
|
|
case 'number':
|
|
|
|
if (!isFinite(this.body[key])) return false
|
|
|
|
break
|
|
|
|
case 'boolean':
|
|
|
|
if (typeof this.body[key] !== 'boolean') return false
|
|
|
|
break
|
|
|
|
case 'array':
|
|
|
|
if (!Array.isArray(this.body[key]) || (!empty && this.body[key].length === 0)) return false
|
|
|
|
break
|
|
|
|
case 'uuid':
|
|
|
|
if (typeof this.body[key] !== 'string') return false
|
|
|
|
if (!empty && this.body[key] === '') return false
|
|
|
|
if (!empty && !uuidChk.test(this.body[key])) return false
|
|
|
|
break
|
|
|
|
case 'object':
|
|
|
|
if (typeof this.body[key] !== 'object') return false
|
|
|
|
try {
|
|
|
|
let str = JSON.stringify(this.body[key])
|
|
|
|
JSON.parse(str)
|
|
|
|
} catch (err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-08-15 13:19:06 +00:00
|
|
|
/**
|
|
|
|
* parse number
|
|
|
|
* @param {any} v input number
|
|
|
|
* @param {number} def default number
|
|
|
|
* @param {number} min min number
|
|
|
|
* @param {number} max max number
|
|
|
|
* @return {number}
|
|
|
|
*/
|
|
|
|
const toInt = (v, def = 0, min = null, max = null) => {
|
|
|
|
if (!isFinite(def)) def = 0
|
|
|
|
if (typeof def === 'string') def = parseInt(def)
|
|
|
|
min = isFinite(min) ? (typeof min === 'string' ? parseInt(min) : min) : null
|
|
|
|
max = isFinite(max) ? (typeof max === 'string' ? parseInt(max) : max) : null
|
|
|
|
if (!isFinite(v)) return def
|
|
|
|
v = parseInt(v)
|
|
|
|
if (min !== null && v < min) v = min
|
|
|
|
if (max !== null && v > max) v = max
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* response object
|
|
|
|
* @param {string} key response key
|
|
|
|
* @param {string|object} message response object or message
|
|
|
|
* @param {number} msgCode message code
|
|
|
|
*/
|
2018-08-15 09:58:59 +00:00
|
|
|
const resObject = (key = null, message = null, msgCode = null) => {
|
2018-08-15 13:19:06 +00:00
|
|
|
if (key === null || typeof key !== 'string' || key.trim().length === 0) key = 'InternalError'
|
|
|
|
key = key.trim()
|
|
|
|
let resObj = _.cloneDeep(resMessage[key] || {})
|
|
|
|
|
|
|
|
if (msgCode !== null && isFinite(msgCode)) {
|
|
|
|
resObj.obj.msgCode = toInt(msgCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof message === 'string' && message.trim().length > 0) {
|
|
|
|
resObj.obj.message = message.trim()
|
2018-08-17 15:15:51 +00:00
|
|
|
} else if (message !== null && typeof message === 'object') {
|
2018-08-15 13:19:06 +00:00
|
|
|
resObj.obj = message
|
|
|
|
}
|
|
|
|
return resObj
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* API Error cls
|
|
|
|
*/
|
|
|
|
class APIError extends Error {
|
|
|
|
set apiMsg (txt = '') {
|
|
|
|
if (typeof txt !== 'string') txt = null
|
|
|
|
this._apiMsg = txt
|
|
|
|
}
|
|
|
|
get apiMsg () {
|
|
|
|
return this._apiMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
set msgCode (code = 0) {
|
|
|
|
this._code = toInt(code, 0)
|
|
|
|
}
|
|
|
|
get msgCode () {
|
|
|
|
return this._code
|
|
|
|
}
|
|
|
|
|
|
|
|
set resKey (txt = '') {
|
|
|
|
if (typeof txt !== 'string' || txt.trim().length === 0) txt = 'InternalError'
|
|
|
|
this._resKey = txt
|
|
|
|
}
|
|
|
|
get resKey () {
|
|
|
|
return this._resKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const genError = (key = null, message = null, msgCode = null) => {
|
|
|
|
let cls = new APIError()
|
|
|
|
cls.apiMsg = message
|
|
|
|
cls.msgCode = msgCode
|
|
|
|
cls.resKey = key
|
|
|
|
return cls
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkSession = async (c, n) => {
|
|
|
|
if (!('session' in c) || !('user' in c.session) || !('loginType' in c.session)) throw genError('NotLogin')
|
2018-08-14 16:15:28 +00:00
|
|
|
|
2018-08-19 12:28:52 +00:00
|
|
|
c.state.loginUser = {
|
2018-08-19 12:49:34 +00:00
|
|
|
name: c.session.loginType === 'twitch' ? c.session.user.login : c.session.user.name,
|
2018-08-19 12:28:52 +00:00
|
|
|
type: c.session.loginType
|
|
|
|
}
|
|
|
|
|
2018-08-15 13:19:06 +00:00
|
|
|
return n()
|
2018-08-14 16:15:28 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 06:17:48 +00:00
|
|
|
module.exports = {
|
2018-08-14 16:15:28 +00:00
|
|
|
chkObject,
|
2018-08-15 13:19:06 +00:00
|
|
|
resObject,
|
|
|
|
toInt,
|
|
|
|
APIError,
|
|
|
|
genError,
|
|
|
|
checkSession
|
2018-08-14 06:17:48 +00:00
|
|
|
}
|