Files
blog/write/src/app/api/recycle/route.ts
T

66 lines
1.7 KiB
TypeScript
Raw Normal View History

2026-05-21 00:27:35 +08:00
import { NextRequest, NextResponse } from "next/server";
import {
listRecycle,
restoreFromRecycle,
permanentDelete,
} from "@/lib/recycle";
export async function GET() {
try {
const items = listRecycle();
return NextResponse.json({ items });
} 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 { slug } = body;
if (!slug) {
return NextResponse.json({ error: "Slug is required" }, { status: 400 });
}
const success = restoreFromRecycle(slug);
if (!success) {
return NextResponse.json(
{ error: "Item not found in recycle bin" },
{ status: 404 }
);
}
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 { searchParams } = request.nextUrl;
const slug = searchParams.get("slug");
if (!slug) {
return NextResponse.json({ error: "Slug is required" }, { status: 400 });
}
const success = permanentDelete(slug);
if (!success) {
return NextResponse.json(
{ error: "Item not found in recycle bin" },
{ status: 404 }
);
}
return NextResponse.json({ success: true });
} catch (error: unknown) {
return NextResponse.json(
{ error: error instanceof Error ? error.message : "Unknown error" },
{ status: 500 }
);
}
}