import { GetCategoryResponse, SearchParams } from '../../types'; import { generateCommerceSearchParams } from '../../utils'; import appFetch, { FetchResponseType } from '../../utils/app-fetch'; import { category, product } from '../urls'; import { Cache, CacheKey } from '../../lib/cache'; import { parse } from 'lossless-json'; import logger from '../../utils/log'; import { headers as nHeaders } from 'next/headers'; import { ServerVariables } from '../../utils/server-variables'; function getCategoryDataHandler( pk: number, locale: string, currency: string, searchParams?: SearchParams, headers?: Record ) { return async function () { const params = generateCommerceSearchParams(searchParams); const rawData = await appFetch({ url: `${category.getCategoryByPk(pk)}${params ? params : ''}`, locale, currency, init: { headers: { Accept: 'application/json', 'Content-Type': 'application/json', ...(headers ?? {}) } }, responseType: FetchResponseType.TEXT }); let data: GetCategoryResponse; try { const numberValueParser = (value) => { return String(value); }; data = parse( rawData, undefined, numberValueParser ) as GetCategoryResponse; } catch (error) { logger.fatal('Error while parsing category data', { handler: 'getCategoryDataHandler', error, rawData: rawData?.startsWith?.('') ? `${rawData?.substring(0, 50)}...` : rawData }); } const menuItemModel = data?.category?.menuitemmodel; if (!menuItemModel) { logger.warn('menuItemModel is undefined, skipping breadcrumbData fetch', { handler: 'getCategoryDataHandler', pk }); return { data, breadcrumbData: undefined }; } const breadcrumbData = await appFetch({ url: product.breadcrumbUrl(menuItemModel), locale, currency, init: { headers: { Accept: 'application/json', 'Content-Type': 'application/json' } } }); return { data, breadcrumbData: breadcrumbData?.menu }; }; } export const getCategoryData = ({ pk, searchParams, headers, locale = ServerVariables.locale, currency = ServerVariables.currency }: { pk: number; locale?: string; currency?: string; searchParams?: SearchParams; headers?: Record; }) => { return Cache.wrap( CacheKey.Category(pk, searchParams, headers), locale, getCategoryDataHandler(pk, locale, currency, searchParams, headers), { expire: 300, compressed: true } ); }; function getCategoryBySlugDataHandler( slug: string, locale: string, currency: string ) { return async function () { const rawData = await appFetch({ url: category.getCategoryBySlug(slug), locale, currency, init: { headers: { Accept: 'application/json', 'Content-Type': 'application/json' } }, responseType: FetchResponseType.TEXT }); let data: GetCategoryResponse; try { const numberValueParser = (value) => { return String(value); }; data = parse( rawData, undefined, numberValueParser ) as GetCategoryResponse; } catch (error) { logger.fatal('Error while parsing category data', { handler: 'getCategoryBySlugDataHandler', error, rawData: rawData?.startsWith?.('') ? `${rawData?.substring(0, 50)}...` : rawData }); } return data; }; } export const getCategoryBySlugData = async ({ slug, locale = ServerVariables.locale, currency = ServerVariables.currency }) => { return Cache.wrap( CacheKey.CategorySlug(slug), locale, getCategoryBySlugDataHandler(slug, locale, currency), { expire: 300, compressed: true } ); };