Files
ai-taxonomy-literature-anal…/lib/api.ts
T
claude-bot 5407bf4dff
Deploy / deploy (push) Successful in 8m0s
Page blocks: papers and content pages as flat lists of story blocks in the database
A page is now an ordered list of independent blocks (analysis.page_block), no sections and no
headings; every block has an id shown on the page so it can be pointed at. Paper detail pages
use page "paper/<conf>/<ext_id>", analysis pages on /contents use "content/<node id>".

- /api/blocks (list, replace a page, add) and /api/blocks/:id (get, patch, move, delete)
- dynamic block types (stats, tool-table, paper-table, benchmark-table) are filled from the
  database at render time; the two content pages are rendered from their blocks (seeded with
  defaults on first view)
- one-time migration copies the legacy articles into blocks (section titles dropped, links
  become a links block); legacy tables are kept untouched
- old /api/articles, ArticleView and PaperGroup removed

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FbMbJPadPzVwvt9rU9GBNo
2026-10-23 22:51:54 +08:00

35 lines
1.3 KiB
TypeScript

import type { NextApiRequest, NextApiResponse } from 'next';
export class HttpError extends Error {
constructor(public status: number, message: string) { super(message); }
}
type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
type Handler = (req: NextApiRequest) => Promise<unknown>;
/** Dispatch by HTTP method; a handler returns the JSON to send, or throws HttpError. */
export function route(handlers: Partial<Record<Method, Handler>>) {
return async (req: NextApiRequest, res: NextApiResponse) => {
const h = handlers[req.method as Method];
if (!h) {
res.setHeader('Allow', Object.keys(handlers).join(', '));
return res.status(405).json({ error: `method ${req.method} not allowed` });
}
try {
res.status(200).json((await h(req)) ?? { ok: true });
} catch (e) {
if (e instanceof HttpError) return res.status(e.status).json({ error: e.message });
console.error(e);
res.status(500).json({ error: `internal error: ${(e as Error).message}` });
}
};
}
export function bodyObject(req: NextApiRequest): Record<string, any> {
const b = req.body;
if (!b || typeof b !== 'object' || Array.isArray(b)) throw new HttpError(400, 'request body must be a JSON object (send Content-Type: application/json)');
return b;
}
export const bigBody = { api: { bodyParser: { sizeLimit: '8mb' } } };