-
Notifications
You must be signed in to change notification settings - Fork 2
/
route.ts
35 lines (31 loc) · 933 Bytes
/
route.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { hookSecret } from "@/lib/sanity.api";
import { revalidateTag } from "next/cache";
import { type NextRequest, NextResponse } from "next/server";
import { parseBody } from "next-sanity/webhook";
export async function POST(req: NextRequest) {
try {
const { body, isValidSignature } = await parseBody<{
_type: string;
slug?: string | undefined;
}>(req, hookSecret);
// Check if signature is valid
if (!isValidSignature) {
return new Response("Invalid Signature", { status: 401 });
}
// Validate body
if (!body?._type) {
return new Response("Bad Request", { status: 400 });
}
// Revalidate Cache Tag
revalidateTag(body._type);
return NextResponse.json({
status: 200,
revalidated: true,
now: Date.now(),
body,
});
} catch (error: any) {
console.error(error);
return new Response(error.message, { status: 500 });
}
}