From 7c116aef061f1bce96c0c0df816d575a07d7ea35 Mon Sep 17 00:00:00 2001 From: soniaklimas <57659625+soniaklimas@users.noreply.github.com> Date: Tue, 17 Dec 2024 13:01:00 +0100 Subject: [PATCH] fix: Make NEXT_PUBLIC_BUTTER_CMS_API_KEY optional (#125) --- apps/storefront/src/envs/client.ts | 2 +- apps/storefront/src/services/cms.ts | 12 ++++++++++++ .../cms-menu/infrastructure/cms-menu-get-infra.ts | 5 +++++ .../cms-page/infrastructure/cms-page-get-infra.ts | 5 +++++ 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/apps/storefront/src/envs/client.ts b/apps/storefront/src/envs/client.ts index 79428e6..e28bcc3 100644 --- a/apps/storefront/src/envs/client.ts +++ b/apps/storefront/src/envs/client.ts @@ -18,7 +18,7 @@ const schema = z.object({ PAYMENT_APP_ID: z.string().default("dev.marina-stripe-saleor-dev"), - NEXT_PUBLIC_BUTTER_CMS_API_KEY: z.string(), + NEXT_PUBLIC_BUTTER_CMS_API_KEY: z.string().optional(), CMS_SERVICE: z.enum(["saleor", "butter"]).default("saleor"), }); diff --git a/apps/storefront/src/services/cms.ts b/apps/storefront/src/services/cms.ts index af1682d..5d686da 100644 --- a/apps/storefront/src/services/cms.ts +++ b/apps/storefront/src/services/cms.ts @@ -1,3 +1,5 @@ +import { invariant } from "ts-invariant"; + import type { CMSMenuService } from "@nimara/infrastructure/use-cases/cms-menu/types"; import type { CMSPageService } from "@nimara/infrastructure/use-cases/cms-page/types"; @@ -15,6 +17,11 @@ const getCMSPageService = async (): Promise => { apiURL: clientEnvs.NEXT_PUBLIC_SALEOR_API_URL, }); } else { + invariant( + clientEnvs.NEXT_PUBLIC_BUTTER_CMS_API_KEY, + "ButterCMS API key is required but not provided. Please set NEXT_PUBLIC_BUTTER_CMS_API_KEY in the environment variables.", + ); + const { butterCMSPageService } = await import( "@nimara/infrastructure/public/butter-cms/cms-page/providers" ); @@ -35,6 +42,11 @@ const getCMSMenuService = async (): Promise => { apiURL: clientEnvs.NEXT_PUBLIC_SALEOR_API_URL, }); } else { + invariant( + clientEnvs.NEXT_PUBLIC_BUTTER_CMS_API_KEY, + "ButterCMS API key is required but not provided. Please set NEXT_PUBLIC_BUTTER_CMS_API_KEY in the environment variables.", + ); + const { butterCMSMenuService } = await import( "@nimara/infrastructure/public/butter-cms/cms-menu/providers" ); diff --git a/packages/infrastructure/src/public/butter-cms/cms-menu/infrastructure/cms-menu-get-infra.ts b/packages/infrastructure/src/public/butter-cms/cms-menu/infrastructure/cms-menu-get-infra.ts index e337a39..4ad8247 100644 --- a/packages/infrastructure/src/public/butter-cms/cms-menu/infrastructure/cms-menu-get-infra.ts +++ b/packages/infrastructure/src/public/butter-cms/cms-menu/infrastructure/cms-menu-get-infra.ts @@ -1,4 +1,5 @@ import Butter from "buttercms/lib/butter"; +import { invariant } from "ts-invariant"; import type { ButterCMSMenuItem } from "@nimara/domain/objects/Menu"; @@ -11,6 +12,10 @@ import type { ButterCMSMenuServiceConfig } from "../types"; export const butterCMSMenuGetInfra = ({ token }: ButterCMSMenuServiceConfig): CMSMenuGetInfra => async ({ languageCode, slug }) => { + invariant( + token, + "ButterCMS token is required but was not provided. Set NEXT_PUBLIC_BUTTER_CMS_API_KEY in the environment variables.", + ); const locale = convertLanguageCode(languageCode); let menu; diff --git a/packages/infrastructure/src/public/butter-cms/cms-page/infrastructure/cms-page-get-infra.ts b/packages/infrastructure/src/public/butter-cms/cms-page/infrastructure/cms-page-get-infra.ts index 4865812..71e1973 100644 --- a/packages/infrastructure/src/public/butter-cms/cms-page/infrastructure/cms-page-get-infra.ts +++ b/packages/infrastructure/src/public/butter-cms/cms-page/infrastructure/cms-page-get-infra.ts @@ -1,5 +1,6 @@ import type PageRetrieveParams from "buttercms"; import Butter from "buttercms/lib/butter"; +import { invariant } from "ts-invariant"; import { type ButterCMSPageFields, @@ -25,6 +26,10 @@ interface PageRetrieveParams { export const butterCMSPageGetInfra = ({ token }: ButterCMSPageServiceConfig): CMSPageGetInfra => async ({ pageType, slug, languageCode }) => { + invariant( + token, + "ButterCMS token is required but was not provided. Set NEXT_PUBLIC_BUTTER_CMS_API_KEY in the environment variables.", + ); const resolvedPageType = pageType ?? PageType.STATIC_PAGE; const locale = convertLanguageCode(languageCode); let page;