Files
ai-taxonomy-literature-anal…/lib/api.ts
T
claude-bot 9ffe734275
Deploy / deploy (push) Successful in 7m40s
Write APIs for articles and tool taxonomy; taxonomy table replaces the tools table
- /api/articles[/slug]: GET/PUT/POST/DELETE, article JSON stored in PostgreSQL (schema analysis)
- /api/tool-taxonomy: categories (tree), per-paper tools, each tool in one or more categories
- /contents/tools-of-agents: category table with roll-up and links to the papers; removed
  "By kind of contribution" and "Recent papers"
- content moves out of the repo: removed content/articles, load-articles.js, agent-tools.ts

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FbMbJPadPzVwvt9rU9GBNo
2026-10-23 19:55:21 +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' | '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' } } };