import { Cache, CacheKey } from '../../lib/cache'; import { MenuItemType } from '../../types'; import appFetch from '../../utils/app-fetch'; import { ServerVariables } from '../../utils/server-variables'; import { misc } from '../urls'; interface MenuResponse { menu: MenuItemType[]; } interface MenuHandlerParams { locale?: string; currency?: string; depth?: number; parent?: string; headers?: Record; } const DEFAULT_DEPTH = 3; const getMenuHandler = ({ locale = ServerVariables.locale, currency = ServerVariables.currency, depth, parent, headers }: MenuHandlerParams = {}) => async () => { const response = await appFetch({ url: misc.menus(depth ?? DEFAULT_DEPTH, parent), locale, currency, init: { headers } }); return response?.menu; }; /** * Returns menu data. * * Default depth is 3 */ export const getMenu = async (params?: MenuHandlerParams) => { return Cache.wrap( CacheKey.Menu(params?.depth ?? DEFAULT_DEPTH, params?.parent), params?.locale ?? ServerVariables.locale, getMenuHandler(params), { compressed: true } ); };