mtfosbot/route/line/index.js

35 lines
813 B
JavaScript

const Router = require('koa-router')
const r = new Router()
// const koaBody = require('koa-body')
const rawBody = require('raw-body')
const {
verifyLine
} = require('../../libs/middleware')
const getRaw = async (c, n) => {
let raw = await rawBody(c.req, {
length: c.request.length,
limit: '5mb',
encoding: c.request.charset
})
c.request.raw = raw
let txt = raw instanceof Buffer ? raw.toString() : raw
if (c.request.type === 'application/json') {
try {
c.request.body = JSON.parse(txt)
} catch (err) {
c.request.body = txt
}
}
return n()
}
r.post('/', getRaw, verifyLine, async (c, n) => {
console.log(c.request.body)
if (!('event' in c.request.body)) return c.throw(400, 'data struct error')
c.body = 'success'
c.status = 200
})
module.exports = r