61 lines
2 KiB
TypeScript
61 lines
2 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { cookies } from "next/headers";
|
|
import { directusFetch } from "@/lib/directus";
|
|
|
|
const BASE_COLLECTION = "rigs"; // change if your collection name differs
|
|
|
|
async function bearerFromCookies() {
|
|
// Some Next 15 type defs model cookies() as async—await to satisfy TS in all envs.
|
|
const store = await cookies();
|
|
const at = store.get("ma_at")?.value;
|
|
if (!at) throw new Error("Not authenticated");
|
|
return `Bearer ${at}`;
|
|
}
|
|
|
|
export async function PATCH(req: NextRequest, { params }: any) {
|
|
try {
|
|
const auth = await bearerFromCookies();
|
|
const body = await req.json();
|
|
const id = params?.id;
|
|
if (!id) return NextResponse.json({ error: "Missing id" }, { status: 400 });
|
|
|
|
const data = await directusFetch<{ data: any }>(
|
|
`/items/${BASE_COLLECTION}/${id}`,
|
|
{
|
|
method: "PATCH",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: auth, // overrides helper's default token
|
|
},
|
|
body: JSON.stringify(body),
|
|
}
|
|
);
|
|
|
|
return NextResponse.json({ ok: true, data: data.data });
|
|
} catch (err: any) {
|
|
return NextResponse.json(
|
|
{ error: err?.message || "Update failed" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export async function DELETE(_req: NextRequest, { params }: any) {
|
|
try {
|
|
const auth = await bearerFromCookies();
|
|
const id = params?.id;
|
|
if (!id) return NextResponse.json({ error: "Missing id" }, { status: 400 });
|
|
|
|
await directusFetch(`/items/${BASE_COLLECTION}/${id}`, {
|
|
method: "DELETE",
|
|
headers: { Authorization: auth },
|
|
});
|
|
|
|
return NextResponse.json({ ok: true });
|
|
} catch (err: any) {
|
|
return NextResponse.json(
|
|
{ error: err?.message || "Delete failed" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
}
|