import { Cache, CacheKey } from '../../lib/cache'; import 'server-only'; import { CacheOptions, WidgetResultType, WidgetSchemaType } from '../../types'; import appFetch from '../../utils/app-fetch'; import { widgets } from '../urls'; import { ServerVariables } from '../../utils/server-variables'; const getWidgetDataHandler = ( slug: string, locale: string, currency: string, headers?: Record ) => async () => { if (!slug) { return null; } return await appFetch({ url: widgets.getWidget(slug), locale, currency, init: { headers } }); }; const getWidgetSchemaDataHandler = (widgetSlug: string, locale: string, currency: string) => async () => { if (!widgetSlug) { return null; } return await appFetch({ url: widgets.getWidgetSchema(widgetSlug), locale, currency }); }; export const getWidgetData = async ({ slug, locale = ServerVariables.locale, currency = ServerVariables.currency, cacheOptions, headers }: { slug: string; locale?: string; currency?: string; cacheOptions?: CacheOptions; headers?: Record; }): Promise> => { return Cache.wrap( CacheKey.Widget(slug), locale, getWidgetDataHandler(slug, locale, currency, headers), { compressed: true, ...cacheOptions } ); }; const getCollectionWidgetDataHandler = (slug: string, locale: string, currency: string) => async () => { if (!slug) { return null; } return await appFetch({ url: widgets.getCollectionWidget(slug), locale, currency }); }; export const getCollectionWidgetData = async ({ slug, locale = ServerVariables.locale, currency = ServerVariables.currency, cacheOptions }: { slug: string; locale?: string; currency?: string; cacheOptions?: CacheOptions; }): Promise> => { return Cache.wrap( CacheKey.Widget(`collection:${slug}`), locale, getCollectionWidgetDataHandler(slug, locale, currency), { compressed: true, ...cacheOptions } ); }; export const getWidgetSchemaData = async ({ widgetSlug, locale = ServerVariables.locale, currency = ServerVariables.currency, cacheOptions }: { widgetSlug: string; locale?: string; currency?: string; cacheOptions?: CacheOptions; }): Promise> => { return Cache.wrap( CacheKey.WidgetSchema(widgetSlug), locale, getWidgetSchemaDataHandler(widgetSlug, locale, currency), cacheOptions ); };