rig route fix
This commit is contained in:
parent
58ef1b62b4
commit
7b897e3672
1 changed files with 22 additions and 19 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
// app/api/my/rigs/[id]/route.ts
|
// app/api/my/rigs/[id]/route.ts
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { cookies } from "next/headers";
|
import { cookies } from "next/headers";
|
||||||
import { directusFetch } from "@/lib/directus";
|
import { directusFetch } from "@/lib/directus";
|
||||||
|
|
||||||
|
|
@ -12,48 +12,51 @@ async function bearerFromCookies() {
|
||||||
return `Bearer ${at}`;
|
return `Bearer ${at}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function PATCH(req: NextRequest, { params }: { params: { id: string } }) {
|
export async function PATCH(req: Request, ctx: any) {
|
||||||
try {
|
try {
|
||||||
const auth = await bearerFromCookies();
|
const auth = await bearerFromCookies();
|
||||||
const body = await req.json().catch(() => ({}));
|
const body = await req.json().catch(() => ({}));
|
||||||
const id = params.id;
|
const id = ctx?.params?.id as string | undefined;
|
||||||
|
if (!id) return NextResponse.json({ error: "Missing id" }, { status: 400 });
|
||||||
|
|
||||||
const data = await directusFetch<{ data: any }>(
|
const data = await directusFetch<{ data: any }>(`/items/${BASE_COLLECTION}/${id}`, {
|
||||||
`/items/${BASE_COLLECTION}/${id}`,
|
method: "PATCH",
|
||||||
{
|
headers: {
|
||||||
method: "PATCH",
|
"Content-Type": "application/json",
|
||||||
headers: {
|
Authorization: auth, // force user-token for this call
|
||||||
"Content-Type": "application/json",
|
Accept: "application/json",
|
||||||
Authorization: auth, // force user-token for this call
|
},
|
||||||
},
|
body: JSON.stringify(body),
|
||||||
body: JSON.stringify(body),
|
});
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return NextResponse.json({ ok: true, data: data.data });
|
return NextResponse.json({ ok: true, data: data.data });
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: err?.message || "Update failed" },
|
{ error: err?.message || "Update failed" },
|
||||||
{ status: 400 }
|
{ status: err?.message === "Not authenticated" ? 401 : 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function DELETE(_req: NextRequest, { params }: { params: { id: string } }) {
|
export async function DELETE(_req: Request, ctx: any) {
|
||||||
try {
|
try {
|
||||||
const auth = await bearerFromCookies();
|
const auth = await bearerFromCookies();
|
||||||
const id = params.id;
|
const id = ctx?.params?.id as string | undefined;
|
||||||
|
if (!id) return NextResponse.json({ error: "Missing id" }, { status: 400 });
|
||||||
|
|
||||||
await directusFetch(`/items/${BASE_COLLECTION}/${id}`, {
|
await directusFetch(`/items/${BASE_COLLECTION}/${id}`, {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: { Authorization: auth }, // force user-token
|
headers: {
|
||||||
|
Authorization: auth, // force user-token
|
||||||
|
Accept: "application/json",
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return NextResponse.json({ ok: true });
|
return NextResponse.json({ ok: true });
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
{ error: err?.message || "Delete failed" },
|
{ error: err?.message || "Delete failed" },
|
||||||
{ status: 400 }
|
{ status: err?.message === "Not authenticated" ? 401 : 400 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue