@techsio/storefront-data
Version:
Medusa-first storefront data layer built on TanStack Query.
98 lines • 5.22 kB
TypeScript
import type { DefaultError } from "@tanstack/react-query";
import { type CacheConfig, type CacheStrategy } from "../shared/cache-config";
import type { InfiniteQueryOptions, QueryFactoryOptions, ReadQueryOptions, SuspenseQueryOptions } from "../shared/hook-types";
import { type PrefetchSkipMode } from "../shared/prefetch";
import { type QueryNamespace } from "../shared/query-keys";
import type { ProductDetailInputBase, ProductInfiniteData, ProductInfiniteInputBase, ProductListInputBase, ProductListResponse, ProductQueryKeys, ProductService, RegionInfo, UseInfiniteProductsResult, UseProductResult, UseProductsResult, UseSuspenseProductResult, UseSuspenseProductsResult } from "./types";
type SuspenseInput<TInput> = Omit<TInput, "enabled">;
export type PrefetchListOptions = {
cacheStrategy?: CacheStrategy;
prefetchedBy?: string;
useGlobalFetcher?: boolean;
skipIfCached?: boolean;
skipMode?: PrefetchSkipMode;
};
export type PrefetchProductOptions = {
cacheStrategy?: CacheStrategy;
prefetchedBy?: string;
skipIfCached?: boolean;
skipMode?: PrefetchSkipMode;
};
export type UsePrefetchPagesParams<TListInput> = {
enabled?: boolean;
shouldPrefetch?: boolean;
baseInput: TListInput;
currentPage: number;
hasNextPage: boolean;
hasPrevPage: boolean;
totalPages: number;
pageSize: number;
mode?: "priority" | "simple";
cacheStrategy?: CacheStrategy;
delays?: {
medium?: number;
low?: number;
};
};
export type CreateProductHooksConfig<TProduct, TListInput extends ProductListInputBase, TListParams, TDetailInput extends ProductDetailInputBase, TDetailParams> = {
service: ProductService<TProduct, TListParams, TDetailParams>;
buildListParams?: (input: TListInput) => TListParams;
buildPrefetchParams?: (input: TListInput) => TListParams;
buildDetailParams?: (input: TDetailInput) => TDetailParams;
queryKeys?: ProductQueryKeys<TListParams, TDetailParams>;
queryKeyNamespace?: QueryNamespace;
cacheConfig?: CacheConfig;
defaultPageSize?: number;
requireRegion?: boolean;
};
export type ProductHooks<TProduct, TListInput extends ProductListInputBase, TDetailInput extends ProductDetailInputBase> = {
getListQueryOptions: (input: TListInput, options?: {
queryOptions?: ReadQueryOptions<ProductListResponse<TProduct>>;
region?: RegionInfo | null;
useGlobalFetcher?: boolean;
}) => QueryFactoryOptions<ProductListResponse<TProduct>>;
getDetailQueryOptions: (input: TDetailInput, options?: {
queryOptions?: ReadQueryOptions<TProduct | null>;
region?: RegionInfo | null;
}) => QueryFactoryOptions<TProduct | null>;
useProducts: (input: TListInput, options?: {
queryOptions?: ReadQueryOptions<ProductListResponse<TProduct>>;
}) => UseProductsResult<TProduct>;
useInfiniteProducts: (input: TListInput & ProductInfiniteInputBase, options?: {
queryOptions?: InfiniteQueryOptions<ProductListResponse<TProduct>, DefaultError, ProductInfiniteData<TProduct>>;
}) => UseInfiniteProductsResult<TProduct>;
useSuspenseProducts: (input: SuspenseInput<TListInput>, options?: {
queryOptions?: SuspenseQueryOptions<ProductListResponse<TProduct>>;
}) => UseSuspenseProductsResult<TProduct>;
useProduct: (input: TDetailInput, options?: {
queryOptions?: ReadQueryOptions<TProduct | null>;
}) => UseProductResult<TProduct>;
useSuspenseProduct: (input: SuspenseInput<TDetailInput>, options?: {
queryOptions?: SuspenseQueryOptions<TProduct | null>;
}) => UseSuspenseProductResult<TProduct>;
usePrefetchProducts: (options?: {
cacheStrategy?: CacheStrategy;
defaultDelay?: number;
skipIfCached?: boolean;
skipMode?: PrefetchSkipMode;
}) => {
prefetchProducts: (input: TListInput, prefetchOptions?: PrefetchListOptions) => Promise<void>;
prefetchFirstPage: (input: TListInput, prefetchOptions?: PrefetchListOptions) => Promise<void>;
delayedPrefetch: (input: TListInput, delay?: number, prefetchId?: string) => string;
cancelPrefetch: (prefetchId: string) => void;
};
usePrefetchProduct: (options?: {
cacheStrategy?: CacheStrategy;
defaultDelay?: number;
skipIfCached?: boolean;
skipMode?: PrefetchSkipMode;
}) => {
prefetchProduct: (input: TDetailInput, prefetchOptions?: PrefetchProductOptions) => Promise<void>;
delayedPrefetch: (input: TDetailInput, delay?: number, prefetchId?: string) => string;
cancelPrefetch: (prefetchId: string) => void;
};
usePrefetchPages: (params: UsePrefetchPagesParams<TListInput>) => void;
};
export declare function createProductHooks<TProduct, TListInput extends ProductListInputBase, TListParams, TDetailInput extends ProductDetailInputBase, TDetailParams>({ service, buildListParams, buildPrefetchParams, buildDetailParams, queryKeys, queryKeyNamespace, cacheConfig, defaultPageSize, requireRegion, }: CreateProductHooksConfig<TProduct, TListInput, TListParams, TDetailInput, TDetailParams>): ProductHooks<TProduct, TListInput, TDetailInput>;
export {};
//# sourceMappingURL=hooks.d.ts.map