Files
blog/write/src/app/api/rss/links/route.ts
T
2026-05-21 13:35:18 +08:00

77 lines
2.5 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { getLinks, addLink, updateLink, deleteLink, checkLinkViaProxy } from "@/lib/rssapi";
export async function GET(request: NextRequest) {
try {
const { searchParams } = request.nextUrl;
const all = searchParams.get("all") === "1";
const health = searchParams.get("health");
if (health) {
const result = await checkLinkViaProxy(health);
return NextResponse.json(result);
}
const links = await getLinks(all);
return NextResponse.json({ links });
} catch (error: unknown) {
return NextResponse.json(
{ error: error instanceof Error ? error.message : "Unknown error" },
{ status: 500 }
);
}
}
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { name, url, description, hidden } = body;
if (!url) {
return NextResponse.json({ error: "URL is required" }, { status: 400 });
}
await addLink({ name: name || "", url, description, hidden });
return NextResponse.json({ success: true });
} catch (error: unknown) {
return NextResponse.json(
{ error: error instanceof Error ? error.message : "Unknown error" },
{ status: 500 }
);
}
}
export async function PUT(request: NextRequest) {
try {
const body = await request.json();
const { oldUrl, url: newUrl, name, description, hidden } = body;
if (!oldUrl) {
return NextResponse.json({ error: "oldUrl is required" }, { status: 400 });
}
const data: Record<string, unknown> = {};
if (name !== undefined) data.name = name;
if (description !== undefined) data.description = description;
if (hidden !== undefined) data.hidden = hidden;
if (newUrl !== undefined) data.url = newUrl;
await updateLink(oldUrl, data);
return NextResponse.json({ success: true });
} catch (error: unknown) {
return NextResponse.json(
{ error: error instanceof Error ? error.message : "Unknown error" },
{ status: 500 }
);
}
}
export async function DELETE(request: NextRequest) {
try {
const url = request.nextUrl.searchParams.get("url");
if (!url) {
return NextResponse.json({ error: "URL is required" }, { status: 400 });
}
await deleteLink(url);
return NextResponse.json({ success: true });
} catch (error: unknown) {
return NextResponse.json(
{ error: error instanceof Error ? error.message : "Unknown error" },
{ status: 500 }
);
}
}