first
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
const Router = require('koa-router')
|
||||
const r = new Router()
|
||||
const {
|
||||
resObject,
|
||||
MError
|
||||
} = require('../utils')
|
||||
|
||||
r.use(async(c, n) => {
|
||||
c.obj = c.obj || {}
|
||||
c.chkBody = 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 c.request.body)) return false
|
||||
|
||||
switch (type) {
|
||||
case 'string':
|
||||
if (typeof c.request.body[key] !== 'string' || (!empty && !c.request.body[key])) return false
|
||||
break
|
||||
case 'number':
|
||||
if (!isFinite(c.request.body[key])) return false
|
||||
break
|
||||
case 'boolean':
|
||||
if (typeof c.request.body[key] !== 'boolean') return false
|
||||
break
|
||||
case 'array':
|
||||
if (!Array.isArray(c.request.body[key]) || (!empty && c.request.body[key].length === 0)) return false
|
||||
break
|
||||
case 'uuid':
|
||||
if (typeof c.request.body[key] !== 'string') return false
|
||||
if (!empty && c.request.body[key] === '') return false
|
||||
if (!empty && !uuidChk.test(c.request.body[key])) return false
|
||||
break
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
let obj = null
|
||||
try {
|
||||
await n()
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
obj = resObject(e instanceof MError ? e.mtype : 'InternalError', e.msg || '')
|
||||
}
|
||||
|
||||
if (Object.keys(c.obj).length > 0 && 'status' in c.obj && 'obj' in c.obj) {
|
||||
c.status = c.obj.status
|
||||
c.body = c.obj.obj
|
||||
}
|
||||
|
||||
if (obj !== null) {
|
||||
c.status = obj.status
|
||||
c.body = obj.obj
|
||||
}
|
||||
})
|
||||
|
||||
r.get('/', async (c, n) => {
|
||||
c.obj = resObject('Success')
|
||||
})
|
||||
|
||||
module.exports = r
|
||||
@@ -0,0 +1,11 @@
|
||||
const Router = require('koa-router')
|
||||
const r = new Router()
|
||||
|
||||
r.use('/api', require('./api').routes())
|
||||
|
||||
r.get('/', async (c, n) => {
|
||||
c.status = 200
|
||||
c.body = ""
|
||||
})
|
||||
|
||||
module.exports = r
|
||||
@@ -0,0 +1,65 @@
|
||||
const message = require('./message')
|
||||
/**
|
||||
* api response message util
|
||||
* @param {String} key
|
||||
* @param {String} msg
|
||||
*/
|
||||
const resObject = function (key = '', msg = null) {
|
||||
if (!key || !(key in message)) {
|
||||
// default message
|
||||
return
|
||||
}
|
||||
|
||||
let obj = Object.assign({}, message[key])
|
||||
|
||||
if (typeof msg === 'string' && msg.length > 0) obj.obj.message = msg
|
||||
if (msg !== null && typeof msg === 'object') obj.obj = msg
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
const genError = function (type, msg = '') {
|
||||
let err = new MError()
|
||||
err.mtype = type
|
||||
err.msg = msg
|
||||
return err
|
||||
}
|
||||
|
||||
class MError extends Error {
|
||||
/**
|
||||
* @param {String} str
|
||||
*/
|
||||
constructor (str) {
|
||||
super(str)
|
||||
this._mtype = ''
|
||||
this._msg = ''
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} type
|
||||
*/
|
||||
set mtype (type = '') {
|
||||
this._mtype = type
|
||||
}
|
||||
|
||||
get mtype () {
|
||||
return this._mtype
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} s
|
||||
*/
|
||||
set msg (s = '') {
|
||||
this._msg = s
|
||||
}
|
||||
|
||||
get msg () {
|
||||
return this._msg
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
resObject,
|
||||
MError,
|
||||
genError
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
module.exports = {
|
||||
Success: {
|
||||
status: 200,
|
||||
obj: {
|
||||
resCode: 0,
|
||||
message: 'success'
|
||||
}
|
||||
},
|
||||
InternalError: {
|
||||
status: 500,
|
||||
obj: {
|
||||
resCode: 500,
|
||||
message: 'Internal Error'
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user