Download tasks/linkedin/tests/oracle-runtime/clone/lib/api.ts from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 2.09 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/tests/oracle-runtime/clone/lib/api.ts
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/linkedin/tests/oracle-runtime/clone/lib/api.ts
-
curl -L -o api.ts https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/tests/oracle-runtime/clone/lib/api.ts
2.09 kB
| import { NextRequest, NextResponse } from "next/server"; | |
| import { ZodError } from "zod"; | |
| import { requestOrigin } from "./request-origin"; | |
| import { getSessionUser } from "./session"; | |
| export class ApiError extends Error { | |
| constructor(public status: number, message: string, public code = "request_error") { | |
| super(message); | |
| } | |
| } | |
| export function requireSameOrigin(request: Request) { | |
| const origin = request.headers.get("origin"); | |
| if (origin && origin !== requestOrigin(request)) throw new ApiError(403, "Cross-origin mutation rejected.", "origin_rejected"); | |
| } | |
| export async function requireUser(request: NextRequest) { | |
| const session = await getSessionUser(request); | |
| if (!session) throw new ApiError(401, "Authentication required.", "authentication_required"); | |
| if (!session.user.emailVerifiedAt) throw new ApiError(403, "Verify your email before continuing.", "email_unverified"); | |
| return session.user; | |
| } | |
| export async function readJsonObject(request: Request) { | |
| let value: unknown; | |
| try { | |
| value = await request.json(); | |
| } catch { | |
| throw new ApiError(400, "Request body must be valid JSON.", "invalid_json"); | |
| } | |
| if (!value || typeof value !== "object" || Array.isArray(value)) { | |
| throw new ApiError(400, "Request body must be a JSON object.", "invalid_body"); | |
| } | |
| return value; | |
| } | |
| export function apiResponseError(error: unknown) { | |
| if (error instanceof ApiError) { | |
| return NextResponse.json({ error: error.message, code: error.code }, { status: error.status }); | |
| } | |
| if (error instanceof ZodError) { | |
| return NextResponse.json({ error: "Invalid request.", code: "validation_error", issues: error.flatten() }, { status: 400 }); | |
| } | |
| console.error(error); | |
| return NextResponse.json({ error: "Unexpected server error.", code: "internal_error" }, { status: 500 }); | |
| } | |
| export function parseLimit(value: string | null, fallback = 20, max = 50) { | |
| const limit = Number(value ?? fallback); | |
| if (!Number.isInteger(limit) || limit < 1 || limit > max) throw new ApiError(400, `limit must be between 1 and ${max}.`, "validation_error"); | |
| return limit; | |
| } | |