@techsio/storefront-data
Version:
Medusa-first storefront data layer built on TanStack Query.
24 lines (23 loc) • 1.27 kB
JavaScript
import { createCacheConfig } from "../shared/cache-config.js";
import { createProductLocationAvailabilityQueryKeys } from "./query-keys.js";
function createProductLocationAvailabilityQueryOptionsFactory({ service, buildDetailParams, queryKeys, queryKeyNamespace = "storefront-data", cacheConfig }) {
const resolvedCacheConfig = cacheConfig ?? createCacheConfig();
const resolvedQueryKeys = queryKeys ?? createProductLocationAvailabilityQueryKeys(queryKeyNamespace);
const buildDetail = buildDetailParams ?? ((input)=>input);
return {
getDetailQueryOptions: (input, options)=>{
const detailParams = buildDetail(input);
const cacheStrategy = options?.cacheStrategy ?? "realtime";
return {
queryKey: resolvedQueryKeys.detail(detailParams),
queryFn: ({ signal })=>{
if (!input.productId) throw new Error("Product id is required for location availability.");
return service.getProductLocationAvailability(detailParams, signal);
},
...resolvedCacheConfig[cacheStrategy],
...options?.queryOptions ?? {}
};
}
};
}
export { createProductLocationAvailabilityQueryOptionsFactory };