diff --git a/apps/storefront/src/envs/client.ts b/apps/storefront/src/envs/client.ts index 9ed7f7d..79428e6 100644 --- a/apps/storefront/src/envs/client.ts +++ b/apps/storefront/src/envs/client.ts @@ -19,6 +19,8 @@ const schema = z.object({ PAYMENT_APP_ID: z.string().default("dev.marina-stripe-saleor-dev"), NEXT_PUBLIC_BUTTER_CMS_API_KEY: z.string(), + + CMS_SERVICE: z.enum(["saleor", "butter"]).default("saleor"), }); export const clientEnvs = schema.parse({ @@ -34,4 +36,6 @@ export const clientEnvs = schema.parse({ PAYMENT_APP_ID: process.env.NEXT_PUBLIC_PAYMENT_APP_ID, NEXT_PUBLIC_BUTTER_CMS_API_KEY: process.env.NEXT_PUBLIC_BUTTER_CMS_API_KEY, + + CMS_SERVICE: process.env.CMS_SERVICE, }); diff --git a/apps/storefront/src/services/cms.ts b/apps/storefront/src/services/cms.ts index 5b6fca9..af1682d 100644 --- a/apps/storefront/src/services/cms.ts +++ b/apps/storefront/src/services/cms.ts @@ -1,28 +1,49 @@ -import { butterCMSMenuService } from "@nimara/infrastructure/public/butter-cms/cms-menu/providers"; -import { butterCMSPageService } from "@nimara/infrastructure/public/butter-cms/cms-page/providers"; -import { saleorCMSMenuService } from "@nimara/infrastructure/public/saleor/cms-menu/providers"; -import { saleorCMSPageService } from "@nimara/infrastructure/public/saleor/cms-page/providers"; import type { CMSMenuService } from "@nimara/infrastructure/use-cases/cms-menu/types"; import type { CMSPageService } from "@nimara/infrastructure/use-cases/cms-page/types"; import { clientEnvs } from "@/envs/client"; -export const cmsPageServiceSaleor = saleorCMSPageService({ - apiURL: clientEnvs.NEXT_PUBLIC_SALEOR_API_URL, -}); +const isSaleorCMS = clientEnvs.CMS_SERVICE === "saleor"; -export const cmsPageServiceButterCMS = butterCMSPageService({ - token: clientEnvs.NEXT_PUBLIC_BUTTER_CMS_API_KEY, -}); +const getCMSPageService = async (): Promise => { + if (isSaleorCMS) { + const { saleorCMSPageService } = await import( + "@nimara/infrastructure/public/saleor/cms-page/providers" + ); -export const cmsMenuServiceSaleor = saleorCMSMenuService({ - apiURL: clientEnvs.NEXT_PUBLIC_SALEOR_API_URL, -}); + return saleorCMSPageService({ + apiURL: clientEnvs.NEXT_PUBLIC_SALEOR_API_URL, + }); + } else { + const { butterCMSPageService } = await import( + "@nimara/infrastructure/public/butter-cms/cms-page/providers" + ); -export const cmsMenuServiceButterCMS = butterCMSMenuService({ - token: clientEnvs.NEXT_PUBLIC_BUTTER_CMS_API_KEY, -}); + return butterCMSPageService({ + token: clientEnvs.NEXT_PUBLIC_BUTTER_CMS_API_KEY, + }); + } +}; -export const cmsPageService: CMSPageService = cmsPageServiceButterCMS; +const getCMSMenuService = async (): Promise => { + if (isSaleorCMS) { + const { saleorCMSMenuService } = await import( + "@nimara/infrastructure/public/saleor/cms-menu/providers" + ); -export const cmsMenuService: CMSMenuService = cmsMenuServiceButterCMS; + return saleorCMSMenuService({ + apiURL: clientEnvs.NEXT_PUBLIC_SALEOR_API_URL, + }); + } else { + const { butterCMSMenuService } = await import( + "@nimara/infrastructure/public/butter-cms/cms-menu/providers" + ); + + return butterCMSMenuService({ + token: clientEnvs.NEXT_PUBLIC_BUTTER_CMS_API_KEY, + }); + } +}; + +export const cmsPageService: CMSPageService = await getCMSPageService(); +export const cmsMenuService: CMSMenuService = await getCMSMenuService();