Koa系列-Response Transformation:koa-res
2019-04-01 字数统计:2.6k 字 阅读时长 ≈ 2 分钟

代码比较简单,干净。主要是控制responseBody的数据。

首先判断 options.custom 是不是一个函数,若不是则提示类型错误。

然后如果存在 ctx._returnRawtrue,则直接返回 ctx.body

否则,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const status = ctx.status
const data = ctx.body
// 忽略 method = option and status = 404
if (ctx.method.toLowerCase !== 'option' && status !== 404) {
ctx.body = {
ok: true,
data: data,
version: options.version || version || '1.0.0',
now: new Date()
}
if (custom) {
Object.assign(ctx.body, custom(ctx))
}
ctx.status = status
}

如果应用运行错误,则在 ctx.body 中加入 statck: e.stack

配置了 options.debug = true ,则不显示 ctx.body.stack