@techsio/storefront-data
Version:
Medusa-first storefront data layer built on TanStack Query.
115 lines • 4.91 kB
TypeScript
import { type CacheConfig, type CacheStrategy } from "./cache-config";
import type { QueryResult, SuspenseQueryResult } from "./hook-result-types";
import type { QueryFactoryOptions, ReadQueryOptions, SuspenseQueryOptions } from "./hook-types";
import { type PrefetchSkipMode } from "./prefetch";
import type { QueryKey } from "./query-keys";
type EnabledInput = {
enabled?: boolean;
};
type ListInputBase = EnabledInput & {
page?: number;
limit?: number;
offset?: number;
};
type DetailInputBase = EnabledInput & {
id?: string;
};
type SimpleQueryKeys<TListParams, TDetailParams> = {
list: (params: TListParams) => QueryKey;
detail: (params: TDetailParams) => QueryKey;
};
type SimpleReadOptions<TData> = {
queryOptions?: ReadQueryOptions<TData>;
};
type SimpleSuspenseOptions<TData> = {
queryOptions?: SuspenseQueryOptions<TData>;
};
export type SimpleListHookResult<TItem, TListResponse> = {
items: TItem[];
isLoading: boolean;
isFetching: boolean;
isSuccess: boolean;
error: string | null;
totalCount: number;
currentPage: number;
totalPages: number;
hasNextPage: boolean;
hasPrevPage: boolean;
query: QueryResult<TListResponse>;
};
export type SimpleSuspenseListHookResult<TItem, TListResponse> = {
items: TItem[];
isLoading: false;
isFetching: boolean;
isSuccess: true;
error: null;
totalCount: number;
currentPage: number;
totalPages: number;
hasNextPage: boolean;
hasPrevPage: boolean;
query: SuspenseQueryResult<TListResponse>;
};
export type SimpleDetailHookResult<TItem> = {
item: TItem | null;
isLoading: boolean;
isFetching: boolean;
isSuccess: boolean;
error: string | null;
query: QueryResult<TItem | null>;
};
export type SimpleSuspenseDetailHookResult<TItem> = {
item: TItem | null;
isLoading: false;
isFetching: boolean;
isSuccess: true;
error: null;
query: SuspenseQueryResult<TItem | null>;
};
type PrefetchHookOptions = {
cacheStrategy?: CacheStrategy;
defaultDelay?: number;
skipIfCached?: boolean;
skipMode?: PrefetchSkipMode;
};
export type CreateSimpleListDetailHooksConfig<TItem, TListResponse extends {
count?: number;
}, TListInput extends ListInputBase, TListParams, TDetailInput extends DetailInputBase, TDetailParams> = {
buildList: (input: TListInput) => TListParams;
buildDetail: (input: TDetailInput) => TDetailParams;
getListItems: (data: TListResponse | undefined) => TItem[];
getList: (params: TListParams, signal?: AbortSignal) => Promise<TListResponse>;
getDetail: (params: TDetailParams, signal?: AbortSignal) => Promise<TItem | null>;
getListQueryOptions: (input: TListInput, options?: {
queryOptions?: ReadQueryOptions<TListResponse>;
cacheStrategy?: CacheStrategy;
}) => QueryFactoryOptions<TListResponse>;
getDetailQueryOptions: (input: TDetailInput, options?: {
queryOptions?: ReadQueryOptions<TItem | null>;
cacheStrategy?: CacheStrategy;
}) => QueryFactoryOptions<TItem | null>;
resolvedCacheConfig: CacheConfig;
resolvedQueryKeys: SimpleQueryKeys<TListParams, TDetailParams>;
defaultPageSize: number;
defaultCacheStrategy: CacheStrategy;
};
export declare function createSimpleListDetailHooks<TItem, TListResponse extends {
count?: number;
}, TListInput extends ListInputBase, TListParams, TDetailInput extends DetailInputBase, TDetailParams>({ buildList, buildDetail, getListItems, getList, getDetail, getListQueryOptions, getDetailQueryOptions, resolvedCacheConfig, resolvedQueryKeys, defaultPageSize, defaultCacheStrategy, }: CreateSimpleListDetailHooksConfig<TItem, TListResponse, TListInput, TListParams, TDetailInput, TDetailParams>): {
useList: (input: TListInput, options?: SimpleReadOptions<TListResponse>) => SimpleListHookResult<TItem, TListResponse>;
useSuspenseList: (input: TListInput, options?: SimpleSuspenseOptions<TListResponse>) => SimpleSuspenseListHookResult<TItem, TListResponse>;
useDetail: (input: TDetailInput, options?: SimpleReadOptions<TItem | null>) => SimpleDetailHookResult<TItem>;
useSuspenseDetail: (input: TDetailInput, options?: SimpleSuspenseOptions<TItem | null>) => SimpleSuspenseDetailHookResult<TItem>;
usePrefetchList: (options?: PrefetchHookOptions) => {
prefetchList: (input: TListInput) => Promise<void>;
delayedPrefetch: (input: TListInput, delay?: number, prefetchId?: string) => string;
cancelPrefetch: (prefetchId: string) => void;
};
usePrefetchDetail: (options?: PrefetchHookOptions) => {
prefetchDetail: (input: TDetailInput) => Promise<void>;
delayedPrefetch: (input: TDetailInput, delay?: number, prefetchId?: string) => string;
cancelPrefetch: (prefetchId: string) => void;
};
};
export {};
//# sourceMappingURL=simple-list-detail-hooks.d.ts.map