Download src/proxy.ts from AnesKAM/gen: direct link, hf CLI and curl.
- Browser
- Download file 720 Bytes
-
https://huggingface.co/spaces/AnesKAM/gen/resolve/main/src/proxy.ts
- Command line
-
hf download hf://spaces/AnesKAM/gen/src/proxy.ts
-
curl -L -o proxy.ts https://huggingface.co/spaces/AnesKAM/gen/resolve/main/src/proxy.ts
720 Bytes
| import { NextResponse, type NextRequest } from "next/server"; | |
| // Next.js 16 "proxy" convention. | |
| // Keeps CSRF protection on state-changing API requests while allowing open access without login. | |
| export default function proxy(req: NextRequest) { | |
| const { pathname } = req.nextUrl; | |
| if (pathname.startsWith("/api/")) { | |
| const method = req.method.toUpperCase(); | |
| const isWrite = method !== "GET" && method !== "HEAD" && method !== "OPTIONS"; | |
| if (isWrite && req.headers.get("sec-fetch-site") === "cross-site") { | |
| return NextResponse.json({ error: "Cross-site requests are not allowed." }, { status: 403 }); | |
| } | |
| } | |
| return NextResponse.next(); | |
| } | |
| export const config = { | |
| matcher: ["/api/:path*"], | |
| }; | |