mirror of
https://github.com/outline/outline.git
synced 2026-11-25 15:15:15 +03:00
- crypto → node:crypto - fs → node:fs - fs/promises → node:fs/promises - path → node:path - http → node:http - https → node:https - stream → node:stream - buffer → node:buffer - url → node:url - os → node:os - net → node:net - dns → node:dns - events → node:events - readline → node:readline - querystring → node:querystring - util → node:util
27 lines
788 B
TypeScript
27 lines
788 B
TypeScript
import stream from "node:stream";
|
|
import type { Context, Next } from "koa";
|
|
import { Readable } from "readable-stream";
|
|
|
|
export default function apiResponse() {
|
|
return async function apiResponseMiddleware(ctx: Context, next: Next) {
|
|
await next();
|
|
const ok = ctx.status < 400;
|
|
|
|
if (
|
|
typeof ctx.body === "object" &&
|
|
!(ctx.body instanceof Readable) &&
|
|
!(ctx.body instanceof stream.Readable) &&
|
|
!(ctx.body instanceof Buffer) &&
|
|
// JSZip returns a wrapped stream instance that is not a true readable stream
|
|
// and not exported from the module either, so we must identify it like so.
|
|
!(ctx.body && "_readableState" in ctx.body)
|
|
) {
|
|
ctx.body = {
|
|
...ctx.body,
|
|
status: ctx.status,
|
|
ok,
|
|
};
|
|
}
|
|
};
|
|
}
|