diff --git a/src/middleware.ts b/src/middleware.ts index e5bbab9..2ba454f 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -21,7 +21,7 @@ const redirect = (redirectURL: string, headers: Record = {}): 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(); } diff --git a/src/pages/[locale]/api/hooks/collection-operation.astro b/src/pages/[locale]/api/hooks/collection-operation.astro deleted file mode 100644 index 4e1b901..0000000 --- a/src/pages/[locale]/api/hooks/collection-operation.astro +++ /dev/null @@ -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" }); ---- diff --git a/src/pages/[locale]/api/hooks/collection-operation.ts b/src/pages/[locale]/api/hooks/collection-operation.ts new file mode 100644 index 0000000..a2b256e --- /dev/null +++ b/src/pages/[locale]/api/hooks/collection-operation.ts @@ -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" }); +}; diff --git a/src/shared/payload/payload-sdk.ts b/src/shared/payload/payload-sdk.ts index 6bd6bc5..1489d68 100644 --- a/src/shared/payload/payload-sdk.ts +++ b/src/shared/payload/payload-sdk.ts @@ -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 = {