Files
outline/server/api/middlewares/apiWrapper.ts
T
2020-12-29 13:07:18 -08:00

27 lines
502 B
TypeScript

import stream from "stream";
import { Context } from "koa";
export default function apiWrapper() {
return async function apiWrapperMiddleware(
ctx: Context,
next: (() => Promise<unknown>)
) {
await next();
const ok = ctx.status < 400;
if (
typeof ctx.body !== "string" &&
!(ctx.body instanceof stream.Readable)
) {
// $FlowFixMe
ctx.body = {
// $FlowFixMe
...ctx.body,
status: ctx.status,
ok,
};
}
};
}