makearmy-app/app/api/my/rigs/[id]/route.ts

63 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-09-27 14:30:16 -04:00
// app/api/my/rigs/[id]/route.ts
2025-09-27 14:35:08 -04:00
import { NextResponse } from "next/server";
import { cookies } from "next/headers";
2025-09-26 14:48:58 -04:00
import { directusFetch } from "@/lib/directus";
2025-09-27 10:37:08 -04:00
const BASE_COLLECTION = "user_rigs";
2025-09-26 14:48:58 -04:00
async function bearerFromCookies() {
const store = await cookies();
const at = store.get("ma_at")?.value;
if (!at) throw new Error("Not authenticated");
return `Bearer ${at}`;
}
2025-09-27 14:35:08 -04:00
export async function PATCH(req: Request, ctx: any) {
try {
2025-09-26 14:48:58 -04:00
const auth = await bearerFromCookies();
2025-09-27 14:30:16 -04:00
const body = await req.json().catch(() => ({}));
2025-09-27 14:35:08 -04:00
const id = ctx?.params?.id as string | undefined;
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, // force user-token for this call
Accept: "application/json",
},
body: JSON.stringify(body),
});
2025-09-26 14:48:58 -04:00
return NextResponse.json({ ok: true, data: data.data });
} catch (err: any) {
2025-09-26 14:54:42 -04:00
return NextResponse.json(
{ error: err?.message || "Update failed" },
2025-09-27 14:35:08 -04:00
{ status: err?.message === "Not authenticated" ? 401 : 400 }
2025-09-26 14:54:42 -04:00
);
}
}
2025-09-27 14:35:08 -04:00
export async function DELETE(_req: Request, ctx: any) {
try {
2025-09-26 14:48:58 -04:00
const auth = await bearerFromCookies();
2025-09-27 14:35:08 -04:00
const id = ctx?.params?.id as string | undefined;
if (!id) return NextResponse.json({ error: "Missing id" }, { status: 400 });
2025-09-26 14:48:58 -04:00
2025-09-26 14:54:42 -04:00
await directusFetch(`/items/${BASE_COLLECTION}/${id}`, {
2025-09-26 14:48:58 -04:00
method: "DELETE",
2025-09-27 14:35:08 -04:00
headers: {
Authorization: auth, // force user-token
Accept: "application/json",
},
2025-09-26 14:48:58 -04:00
});
return NextResponse.json({ ok: true });
2025-09-26 14:48:58 -04:00
} catch (err: any) {
2025-09-26 14:54:42 -04:00
return NextResponse.json(
{ error: err?.message || "Delete failed" },
2025-09-27 14:35:08 -04:00
{ status: err?.message === "Not authenticated" ? 401 : 400 }
2025-09-26 14:54:42 -04:00
);
}
}