import trim from 'lodash/trim'; const version = '1.0.0'; /** * Types of global (shared) query clients available. * - 'app': Client persists for the entire browser session (until refresh/close) * - 'page': Client uses reference counting. Disposes when the last page client instance is disposed (e.g., when all components using getQueryClient('page') unmount) */ type GlobalClientType = 'app' | 'page'; /** * Configuration for using a globally shared query client. * Can be specified as a simple string ('app' or 'page') or as an object with additional options. * * @template T - The default value type for the query * * @example * ```typescript * // Simple string syntax * queryOptions(): QueryApiOptions { * return { * queryKey: ['myapp', 'users'], * queryFn: async () => api.getUsers(), * globalClient: 'page', * }; * } * ``` * * @example * ```typescript * // Object syntax with dispose option * queryOptions(): QueryApiOptions { * return { * queryKey: ['myapp', 'users'], * queryFn: async () => api.getUsers(), * globalClient: { * type: 'page', * dispose: true, * }, * }; * } * ``` */ export type GlobalClientOptions = | GlobalClientType | { type: GlobalClientType; dispose?: boolean; defaultValue?: T; }; export function getTypeName(type: GlobalClientType) { return `${type}-${version}`; } export function getClientType(options?: GlobalClientOptions) { const clientOptions = getClientOptions(options); return trim(clientOptions?.type) as GlobalClientType | ''; } export function isSharedClient(options?: GlobalClientOptions) { return !!getClientType(options); } /** True when `globalClient` is set and the injected client does not isolate from `window.queryClients`. */ export function shouldShareWindowClient( globalClient?: GlobalClientOptions, client?: { isolateFromWindowClients?: boolean } ) { return isSharedClient(globalClient) && !client?.isolateFromWindowClients; } export function getClientOptions(options?: GlobalClientOptions) { if (!options) { return undefined; } if (typeof options === 'string') { return { type: options }; } return options; } export function isArrayofArrays(arr: any) { return Array.isArray(arr) && arr.every(Array.isArray); }