Files
outline/server/routes/api/middlewares/apiWrapper.ts
2022-12-27 09:51:39 -08:00

22 lines
472 B
TypeScript

import stream from "stream";
import { Context, Next } from "koa";
export default function apiWrapper() {
return async function apiWrapperMiddleware(ctx: Context, next: Next) {
await next();
const ok = ctx.status < 400;
if (
typeof ctx.body === "object" &&
!(ctx.body instanceof stream.Readable) &&
!(ctx.body instanceof Buffer)
) {
ctx.body = {
...ctx.body,
status: ctx.status,
ok,
};
}
};
}