42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { z } from "zod";
|
|
import { requireSession } from "@/lib/api-auth";
|
|
import { applyRateLimit } from "@/lib/rate-limit";
|
|
import { checkAndIncrementTierLimit } from "@/lib/tiers";
|
|
import { importFromUrl } from "@/lib/ai/features/import-url";
|
|
import { validateWebhookUrl } from "@/lib/validate-webhook-url";
|
|
|
|
const Schema = z.object({
|
|
url: z.string().url(),
|
|
provider: z.enum(["openai", "anthropic", "openrouter", "ollama"]).optional(),
|
|
model: z.string().optional(),
|
|
});
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const { session, response } = await requireSession();
|
|
if (response) return response;
|
|
|
|
const body = await req.json() as unknown;
|
|
const parsed = Schema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return NextResponse.json({ error: "Validation error", issues: parsed.error.issues }, { status: 400 });
|
|
}
|
|
|
|
const ssrfError = await validateWebhookUrl(parsed.data.url);
|
|
if (ssrfError) {
|
|
return NextResponse.json({ error: ssrfError }, { status: 400 });
|
|
}
|
|
|
|
const limited = await applyRateLimit(`rl:ai:${session!.user.id}`, 10, 60);
|
|
if (limited) return limited;
|
|
|
|
await checkAndIncrementTierLimit(session!.user.id, session!.user.tier as "free" | "pro", "aiCall");
|
|
|
|
const recipe = await importFromUrl(parsed.data.url, {
|
|
provider: parsed.data.provider,
|
|
model: parsed.data.model,
|
|
});
|
|
|
|
return NextResponse.json(recipe);
|
|
}
|