UNPKG

@techsio/storefront-data

Version:

Medusa-first storefront data layer built on TanStack Query.

46 lines (45 loc) • 1.99 kB
import { createCacheConfig } from "../shared/cache-config.js"; import { createProductDetailQueryDefinition, createProductListQueryDefinition } from "./query-definition.js"; import { createProductQueryKeys } from "./query-keys.js"; function createProductQueryOptionsFactory({ service, buildListParams, buildDetailParams, queryKeys, queryKeyNamespace = "storefront-data", cacheConfig }) { const resolvedCacheConfig = cacheConfig ?? createCacheConfig(); const resolvedQueryKeys = queryKeys ?? createProductQueryKeys(queryKeyNamespace); const buildList = buildListParams ?? ((input)=>input); const buildDetail = buildDetailParams ?? ((input)=>input); return { getListQueryOptions: (input, options)=>{ const { queryKey, queryFn } = createProductListQueryDefinition({ input, region: options?.region, service, buildListParams: buildList, queryKeys: resolvedQueryKeys, useGlobalFetcher: options?.useGlobalFetcher }); const cacheStrategy = options?.cacheStrategy ?? "semiStatic"; return { queryKey, queryFn, ...resolvedCacheConfig[cacheStrategy], ...options?.queryOptions ?? {} }; }, getDetailQueryOptions: (input, options)=>{ const { queryKey, queryFn } = createProductDetailQueryDefinition({ input, region: options?.region, service, buildDetailParams: buildDetail, queryKeys: resolvedQueryKeys }); const cacheStrategy = options?.cacheStrategy ?? "semiStatic"; return { queryKey, queryFn, ...resolvedCacheConfig[cacheStrategy], ...options?.queryOptions ?? {} }; } }; } export { createProductQueryOptionsFactory };