Files
Khaled Ahmed 2088f387bc refactor: shared session/error route helpers and markdown code renderer
- lib/api-utils: resolveSessionPathOr404 collapses the resolveSessionPath +
  'Session not found' guard repeated across 10 session routes; apiErrorResponse
  replaces the ~20 { error: String(error) }, { status: 500 } one-liners.
- components/MarkdownCode: the  renderer duplicated verbatim between
  MarkdownBody and FileViewer (mermaid/CodeBlock dispatch + inline code) is now
  one factory; the defaultPreview/isStreaming/inline-class deltas are options.
- upload loop error handling deduped into a local recordError.
2026-08-13 14:12:57 +03:00

20 lines
854 B
TypeScript

import { NextResponse } from "next/server";
import { resolveSessionPath } from "./session-reader";
const SESSION_NOT_FOUND = { error: "Session not found", code: "session_not_found" } as const;
/** Resolve a session id to its file path, or a 404 JSON response. Replaces the
* repeated `resolveSessionPath(id)` + "Session not found" guard across routes. */
export async function resolveSessionPathOr404(
id: string,
): Promise<{ filePath: string } | { response: NextResponse }> {
const filePath = await resolveSessionPath(id);
if (!filePath) return { response: NextResponse.json(SESSION_NOT_FOUND, { status: 404 }) };
return { filePath };
}
/** Uniform JSON error body used by most API routes. */
export function apiErrorResponse(error: unknown, status = 500): NextResponse {
return NextResponse.json({ error: String(error) }, { status });
}