Migrated to new webhook format

This commit is contained in:
DrMint 2024-06-15 17:37:59 +02:00
parent db6b331380
commit 73dfc452cc
4 changed files with 54 additions and 41 deletions

View File

@ -21,7 +21,7 @@ const redirect = (redirectURL: string, headers: Record<string, string> = {}): Re
const localeAgnosticPaths = ["/api/"];
const localeNegotiator = defineMiddleware(({ cookies, url, request }, next) => {
if (localeAgnosticPaths.some((prefix) => url.pathname.startsWith(prefix))) {
if (localeAgnosticPaths.some((prefix) => url.pathname.includes(prefix))) {
return next();
}

View File

@ -1,40 +0,0 @@
---
import { Collections } from "src/shared/payload/payload-sdk";
import {
refreshCurrencies,
refreshLocales,
refreshWordings,
refreshWebsiteConfig,
} from "src/utils/payload";
const auth = Astro.request.headers.get("Authorization");
if (auth !== `Bearer ${import.meta.env.WEB_HOOK_TOKEN}`) {
return new Response(null, { status: 403, statusText: "Forbidden" });
}
const collectionSlug = Astro.request.headers.get("Collection");
switch (collectionSlug) {
case Collections.Wordings:
await refreshWordings();
break;
case Collections.Currencies:
await refreshCurrencies();
break;
case Collections.Languages:
await refreshLocales();
break;
case Collections.WebsiteConfig:
await refreshWebsiteConfig();
break;
default:
return new Response(null, { status: 400, statusText: "Bad Request" });
}
return new Response(null, { status: 200, statusText: "Ok" });
---

View File

@ -0,0 +1,39 @@
import type { APIRoute } from "astro";
import { Collections, type WebHookMessage } from "src/shared/payload/payload-sdk";
import {
refreshCurrencies,
refreshLocales,
refreshWebsiteConfig,
refreshWordings,
} from "src/utils/payload";
export const POST: APIRoute = async ({ request }) => {
const auth = request.headers.get("Authorization");
if (auth !== `Bearer ${import.meta.env.WEB_HOOK_TOKEN}`) {
return new Response(null, { status: 403, statusText: "Forbidden" });
}
const message = (await request.json()) as WebHookMessage;
console.log("[Webhook] Received message from CMS:", message);
switch (message.collection) {
case Collections.Wordings:
await refreshWordings();
break;
case Collections.Currencies:
await refreshCurrencies();
break;
case Collections.Languages:
await refreshLocales();
break;
case Collections.WebsiteConfig:
await refreshWebsiteConfig();
break;
}
return new Response(null, { status: 200, statusText: "Ok" });
};

View File

@ -1276,6 +1276,20 @@ export enum AttributeTypes {
Tags = "Tags",
}
/* WEB HOOKS */
export interface WebHookMessage {
collection: Collections;
operation: WebHookOperationType;
id?: string;
}
export enum WebHookOperationType {
create = "create",
update = "update",
delete = "delete",
}
/* RICH TEXT */
export type RichTextContent = {