import { TRPCClient, TRPCClientError, TRPCRequestOptions } from '@trpc/client'; import type { AnyRouter, inferHandlerInput, inferProcedureInput, inferProcedureOutput, } from '@trpc/server'; import { createContext } from 'react'; import { CancelOptions, FetchInfiniteQueryOptions, FetchQueryOptions, InfiniteData, InvalidateOptions, InvalidateQueryFilters, QueryClient, RefetchOptions, RefetchQueryFilters, SetDataOptions, } from 'react-query'; import { Updater } from 'react-query/types/core/utils'; interface TRPCFetchQueryOptions extends FetchQueryOptions, TRPCRequestOptions {} interface TRPCFetchInfiniteQueryOptions extends FetchInfiniteQueryOptions, TRPCRequestOptions {} export type SSRState = false | 'prepass' | 'mounting' | 'mounted'; export interface TRPCContextState< TRouter extends AnyRouter, TSSRContext = undefined, > { queryClient: QueryClient; client: TRPCClient; /** * @deprecated use `ssrState === 'prepass'` */ isPrepass: boolean; ssrContext: TSSRContext | null; /** * State of SSR hydration. * - `false` if not using SSR. * - `prepass` when doing a prepass to fetch queries' data * - `mounting` before TRPCProvider has been rendered on the client * - `mounted` when the TRPCProvider has been rendered on the client */ ssrState: SSRState; /** * @link https://react-query.tanstack.com/guides/prefetching */ fetchQuery< TPath extends keyof TRouter['_def']['queries'] & string, TProcedure extends TRouter['_def']['queries'][TPath], TOutput extends inferProcedureOutput, TInput extends inferProcedureInput, >( pathAndInput: [path: TPath, ...args: inferHandlerInput], opts?: TRPCFetchQueryOptions, TOutput>, ): Promise; /** * @link https://react-query.tanstack.com/guides/prefetching */ fetchInfiniteQuery< TPath extends keyof TRouter['_def']['queries'] & string, TProcedure extends TRouter['_def']['queries'][TPath], TOutput extends inferProcedureOutput, TInput extends inferProcedureInput, >( pathAndInput: [path: TPath, ...args: inferHandlerInput], opts?: TRPCFetchInfiniteQueryOptions< TInput, TRPCClientError, TOutput >, ): Promise>; /** * @link https://react-query.tanstack.com/guides/prefetching */ prefetchQuery< TPath extends keyof TRouter['_def']['queries'] & string, TProcedure extends TRouter['_def']['queries'][TPath], TOutput extends inferProcedureOutput, TInput extends inferProcedureInput, >( pathAndInput: [path: TPath, ...args: inferHandlerInput], opts?: TRPCFetchQueryOptions, TOutput>, ): Promise; /** * @link https://react-query.tanstack.com/guides/prefetching */ prefetchInfiniteQuery< TPath extends keyof TRouter['_def']['queries'] & string, TProcedure extends TRouter['_def']['queries'][TPath], TOutput extends inferProcedureOutput, TInput extends inferProcedureInput, >( pathAndInput: [path: TPath, ...args: inferHandlerInput], opts?: TRPCFetchInfiniteQueryOptions< TInput, TRPCClientError, TOutput >, ): Promise; /** * @deprecated use `invalidateQueries` */ invalidateQuery< TPath extends keyof TRouter['_def']['queries'] & string, TInput extends inferProcedureInput, >( pathAndInput: [TPath, TInput?], options?: InvalidateOptions, ): Promise; /** * @link https://react-query.tanstack.com/guides/query-invalidation */ invalidateQueries< TPath extends keyof TRouter['_def']['queries'] & string, TInput extends inferProcedureInput, >( pathAndInput?: [TPath, TInput?] | TPath, filters?: InvalidateQueryFilters, options?: InvalidateOptions, ): Promise; /** * @link https://react-query.tanstack.com/guides/query-invalidation */ invalidateQueries( filters?: InvalidateQueryFilters, options?: InvalidateOptions, ): Promise; /** * @link https://react-query.tanstack.com/reference/QueryClient#queryclientrefetchqueries */ refetchQueries< TPath extends keyof TRouter['_def']['queries'] & string, TInput extends inferProcedureInput, >( pathAndInput: [TPath, TInput?], filters?: RefetchQueryFilters, options?: RefetchOptions, ): Promise; /** * @link https://react-query.tanstack.com/reference/QueryClient#queryclientrefetchqueries */ refetchQueries( filters?: RefetchQueryFilters, options?: RefetchOptions, ): Promise; /** * @link https://react-query.tanstack.com/guides/query-cancellation */ cancelQuery< TPath extends keyof TRouter['_def']['queries'] & string, TInput extends inferProcedureInput, >( pathAndInput: [TPath, TInput?], options?: CancelOptions, ): Promise; /** * @link https://react-query.tanstack.com/reference/QueryClient#queryclientsetquerydata */ setQueryData< TPath extends keyof TRouter['_def']['queries'] & string, TInput extends inferProcedureInput, TOutput extends inferProcedureOutput, >( pathAndInput: [TPath, TInput?], updater: Updater, options?: SetDataOptions, ): void; /** * @link https://react-query.tanstack.com/reference/QueryClient#queryclientgetquerydata */ getQueryData< TPath extends keyof TRouter['_def']['queries'] & string, TInput extends inferProcedureInput, TOutput extends inferProcedureOutput, >( pathAndInput: [TPath, TInput?], ): TOutput | undefined; /** * @link https://react-query.tanstack.com/reference/QueryClient#queryclientgetquerydata */ setInfiniteQueryData< TPath extends keyof TRouter['_def']['queries'] & string, TInput extends inferProcedureInput, TOutput extends inferProcedureOutput, >( pathAndInput: [TPath, TInput?], updater: Updater | undefined, InfiniteData>, options?: SetDataOptions, ): void; /** * @link https://react-query.tanstack.com/reference/QueryClient#queryclientgetquerydata */ getInfiniteQueryData< TPath extends keyof TRouter['_def']['queries'] & string, TInput extends inferProcedureInput, TOutput extends inferProcedureOutput, >( pathAndInput: [TPath, TInput?], ): InfiniteData | undefined; } export const TRPCContext = createContext(null as any);