another route fix for rig ID
This commit is contained in:
parent
ae1486636e
commit
662a6e3278
1 changed files with 28 additions and 15 deletions
|
|
@ -2,47 +2,60 @@ import { NextRequest, NextResponse } from "next/server";
|
|||
import { cookies } from "next/headers";
|
||||
import { directusFetch } from "@/lib/directus";
|
||||
|
||||
const BASE_COLLECTION = "user_rigs";
|
||||
const BASE_COLLECTION = "rigs"; // change if your collection name differs
|
||||
|
||||
async function bearerFromCookies() {
|
||||
// In Next 15, types may represent `cookies()` as async—await it to satisfy TS.
|
||||
// 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, ctx: { params: { id: string } }) {
|
||||
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}/${ctx.params.id}`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: auth, // overrides submit token in helper
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
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 });
|
||||
return NextResponse.json(
|
||||
{ error: err?.message || "Update failed" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(_req: NextRequest, ctx: { params: { id: string } }) {
|
||||
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}/${ctx.params.id}`, {
|
||||
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 });
|
||||
return NextResponse.json(
|
||||
{ error: err?.message || "Delete failed" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue