Files
outline/server/utils/koa.ts
T
Tom MoorandClaude Opus 5.5 00b124d03c chore: Rename APIContext to Context and flatten its fields (#14684)
* chore: Rename APIContext to Context and flatten its fields

Auth and transaction now live directly on the context instead of in
ctx.state and a nested ctx.context, and model helpers read them directly.
APIContext remains as a deprecated alias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove deprecated APIContext alias

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* fix: Read auth from context in OAuth provider callbacks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Add withUser and withTransaction context helpers

Replaces manual createContext rebuilds that copy fields from an existing context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

* chore: Remove arbitrary property access from Koa context types

AppContext and Context no longer inherit the any index signature from
Koa's DefaultContext, custom fields are declared explicitly, and importing
Koa's loose Context type is disallowed by lint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016MJXFeUxvU46KyvKnP8sXf

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-23 23:17:06 -05:00

70 lines
1.8 KiB
TypeScript

import contentDisposition from "content-disposition";
import type formidable from "formidable";
import type { Request } from "koa";
import { isArray } from "es-toolkit/compat";
import { ZipFile } from "yazl";
import type { AppContext } from "@server/types";
/**
* Get the first file from an incoming koa request
*
* @param request The incoming request
* @returns The first file or undefined
*/
export const getFileFromRequest = (
request: Request
): formidable.File | undefined => {
const { files } = request;
if (!files) {
return undefined;
}
const file = Object.values(files)[0];
if (!file) {
return undefined;
}
return isArray(file) ? file[0] : file;
};
/**
* Stream a freshly-built zip archive as the response body. The supplied
* `build` callback receives a yazl ZipFile to populate with entries; the
* helper handles response headers, error forwarding, and finalizing the
* archive once `build` resolves.
*
* @param ctx The koa context to write the response to.
* @param fileName The filename to advertise in the Content-Disposition header.
* @param build Callback that adds entries to the provided ZipFile.
*/
export const streamZipResponse = (
ctx: AppContext,
fileName: string,
build: (zip: ZipFile) => void | Promise<void>
): void => {
const zip = new ZipFile();
ctx.set("Content-Type", "application/zip");
ctx.set(
"Content-Disposition",
contentDisposition(fileName, { type: "attachment" })
);
const handleError = (err: Error) => {
ctx.app.emit("error", err, ctx);
ctx.res.destroy(err);
};
zip.outputStream.on("error", handleError);
ctx.body = zip.outputStream;
void (async () => {
try {
await build(zip);
zip.end();
} catch (err) {
handleError(err instanceof Error ? err : new Error(String(err)));
}
})();
};