UNPKG

jotai-query-toolkit

Version:

A toolkit for opinionated ways to use Jotai, react-query, and next.js

196 lines (174 loc) 11.1 kB
import * as jotai from 'jotai'; import { Atom, PrimitiveAtom, Getter, WritableAtom } from 'jotai'; import { AtomWithQueryAction, AtomWithInfiniteQueryAction } from 'jotai/query'; import * as react_query from 'react-query'; import { QueryObserverOptions, InfiniteQueryObserverOptions, InfiniteData, QueryKey, QueryFunctionContext, SetDataOptions, MutateOptions, QueryObserver, InfiniteQueryObserver, QueryClient } from 'react-query'; import { NextPageContext, GetServerSidePropsContext, GetStaticPropsContext } from 'next'; interface BaseQueryAtomCustomOptions<Data> { equalityFn?: (a: Data, b: Data) => boolean; queryKeyAtom?: Atom<QueryKey> | PrimitiveAtom<QueryKey>; getShouldRefetch?: (initialData: Data) => boolean; } declare type AtomWithQueryOptions<Data> = QueryObserverOptions<Data, void, Data> & BaseQueryAtomCustomOptions<Data>; interface AtomWithInfiniteQueryOptions<Data> extends InfiniteQueryObserverOptions<Data, unknown, Data, Data> { equalityFn?: (a: InfiniteData<Data>, b: InfiniteData<Data>) => boolean; getShouldRefetch?: (initialData: InfiniteData<Data>) => boolean; queryKeyAtom?: Atom<QueryKey> | PrimitiveAtom<QueryKey>; } declare type AtomWithQueryFn<Data> = (get: Getter, context: QueryFunctionContext) => Data | Promise<Data>; declare type AtomWithInfiniteQueryFn<Data> = (get: Getter, context: QueryFunctionContext) => Data | Promise<Data>; declare type InfiniteQueryDispatch = { type: 'mount'; } | { type: 'next'; } | { type: 'prev'; } | { type: 'refresh'; }; interface ListParams { limit: number; offset: number; } declare type ParamWithListParams<T> = [param: T, options: ListParams]; declare type AtomFamilyWithQueryFn<Param, Data> = (get: Getter, param: Param, context: QueryFunctionContext) => Data | Promise<Data>; declare type AtomFamilyWithInfiniteQueryFn<Param, Data> = (get: Getter, param: Param, context: QueryFunctionContext) => Data | Promise<Data>; declare type ShouldRemove<Param> = (createdAt: number, param: Param) => boolean; declare type AtomFamily<Param, AtomType> = { (param: Param): AtomType; remove(param: Param): void; setShouldRemove(shouldRemove: ShouldRemove<Param> | null): void; }; declare type GetQueryKey$1<Param> = (get: Getter, param: Param) => QueryKey; declare type QueryKeyOrGetQueryKey$2<Param> = QueryKey | GetQueryKey$1<Param>; declare type QueryKeyOrGetQueryKey$1 = QueryKey | ((get: Getter) => QueryKey); declare type QueryOptionsOrGetQueryOptions$1<Data> = ((get: Getter) => AtomWithQueryOptions<Data>) | AtomWithQueryOptions<Data>; declare type JQTAtomWithQueryActions<Data> = AtomWithQueryAction | { type: 'setQueryData'; payload: { data: Data; options?: SetDataOptions; }; } | { type: 'mutate'; payload: MutateOptions<Data>; }; declare const atomWithQuery: <Data>(key: QueryKeyOrGetQueryKey$1, queryFn: AtomWithQueryFn<Data>, queryOptions?: QueryOptionsOrGetQueryOptions$1<Data>) => jotai.WritableAtom<Data, JQTAtomWithQueryActions<Data>, void>; declare const atomFamilyWithQuery: <Param, Data, Error_1 = void, TQueryData = Data>(key: QueryKeyOrGetQueryKey$2<Param>, queryFn: AtomFamilyWithQueryFn<Param, Data>, options?: AtomWithQueryOptions<Data> | ((param: Param, get: Getter) => AtomWithQueryOptions<Data>)) => { (param: Param): jotai.WritableAtom<Data, JQTAtomWithQueryActions<Data>, void>; remove(param: Param): void; setShouldRemove(shouldRemove: ((createdAt: number, param: Param) => boolean) | null): void; }; declare const atomFamilyWithInfiniteQuery: <Param, Data>(key: QueryKeyOrGetQueryKey$2<Param>, queryFn: AtomFamilyWithInfiniteQueryFn<Param, Data>, options?: AtomWithInfiniteQueryOptions<Data> | ((param: Param, get: Getter) => AtomWithInfiniteQueryOptions<Data>)) => AtomFamily<Param, WritableAtom<InfiniteData<Data> | undefined, AtomWithInfiniteQueryAction<Data>, void>>; declare type QueryKeyOrGetQueryKey = QueryKey | ((get: Getter) => QueryKey); declare type QueryOptionsOrGetQueryOptions<Data> = ((get: Getter) => AtomWithInfiniteQueryOptions<Data>) | AtomWithInfiniteQueryOptions<Data>; declare const atomWithInfiniteQuery: <Data>(key: QueryKeyOrGetQueryKey, queryFn: AtomWithInfiniteQueryFn<Data>, queryOptions?: QueryOptionsOrGetQueryOptions<Data>) => jotai.WritableAtom<InfiniteData<Data> | undefined, AtomWithInfiniteQueryAction<Data>, void>; declare const queryClientAtom: jotai.Atom<react_query.QueryClient> & { write: (get: { <Value>(atom: jotai.Atom<Value | Promise<Value>>): Value; <Value_1>(atom: jotai.Atom<Promise<Value_1>>): Value_1; <Value_2>(atom: jotai.Atom<Value_2>): Value_2 extends Promise<infer V> ? V : Value_2; } & { <Value_3>(atom: jotai.Atom<Value_3 | Promise<Value_3>>, options: { unstable_promise: true; }): Value_3 | Promise<Value_3>; <Value_4>(atom: jotai.Atom<Promise<Value_4>>, options: { unstable_promise: true; }): Value_4 | Promise<Value_4>; <Value_5>(atom: jotai.Atom<Value_5>, options: { unstable_promise: true; }): (Value_5 extends Promise<infer V> ? V : Value_5) | Promise<Value_5 extends Promise<infer V> ? V : Value_5>; }, set: { <Value_6, Result extends void | Promise<void>>(atom: jotai.WritableAtom<Value_6, undefined, Result>): Result; <Value_7, Update, Result_1 extends void | Promise<void>>(atom: jotai.WritableAtom<Value_7, Update, Result_1>, update: Update): Result_1; }, update: react_query.QueryClient | ((prev: react_query.QueryClient) => react_query.QueryClient)) => void; onMount?: (<S extends (update: react_query.QueryClient | ((prev: react_query.QueryClient) => react_query.QueryClient)) => void>(setAtom: S) => void | (() => void)) | undefined; } & { init: react_query.QueryClient; }; declare const getQueryClientAtom: (get: Getter) => react_query.QueryClient; declare const initialDataAtom: { (param: string): jotai.Atom<unknown>; remove(param: string): void; setShouldRemove(shouldRemove: ((createdAt: number, param: string) => boolean) | null): void; }; interface InfiniteQueryStatus { isFetchingNextPage: boolean; isFetchingPreviousPage: boolean; hasPreviousPage?: boolean; hasNextPage?: boolean; } declare const infiniteQueryKeyStatusAtom: { (param: QueryKey): jotai.Atom<InfiniteQueryStatus>; remove(param: QueryKey): void; setShouldRemove(shouldRemove: ((createdAt: number, param: QueryKey) => boolean) | null): void; }; interface QueryStatus { isLoading: boolean; isFetching: boolean; isIdle: boolean; isSuccess: boolean; isStale: boolean; } declare const queryKeyStatusAtom: { (param: QueryKey): jotai.Atom<QueryStatus>; remove(param: QueryKey): void; setShouldRemove(shouldRemove: ((createdAt: number, param: QueryKey) => boolean) | null): void; }; declare const queryKeyObserver: { (param: QueryKey | undefined): jotai.Atom<QueryObserver<unknown, unknown, unknown, unknown, QueryKey> | undefined>; remove(param: QueryKey | undefined): void; setShouldRemove(shouldRemove: ((createdAt: number, param: QueryKey | undefined) => boolean) | null): void; }; declare const infiniteQueryKeyObserver: { (param: QueryKey): jotai.Atom<InfiniteQueryObserver<unknown, unknown, unknown, unknown>>; remove(param: QueryKey): void; setShouldRemove(shouldRemove: ((createdAt: number, param: QueryKey) => boolean) | null): void; }; declare const devtoolAtom: { (param: PrimitiveAtom<any>): jotai.Atom<void>; remove(param: PrimitiveAtom<any>): void; setShouldRemove(shouldRemove: ((createdAt: number, param: PrimitiveAtom<any>) => boolean) | null): void; }; declare type QueryPropsDefault = unknown | undefined; declare type Fetcher<Data = unknown, QueryProps = QueryPropsDefault> = (ctx: NextPageContext | GetServerSidePropsContext | GetStaticPropsContext, queryProps?: QueryProps, queryClient?: QueryClient) => Promise<Data> | Data; declare type GetQueryKey<QueryProps = QueryPropsDefault> = (ctx: NextPageContext | GetServerSidePropsContext | GetStaticPropsContext, queryProps?: QueryProps, queryClient?: QueryClient) => QueryKey | Promise<QueryKey | undefined> | undefined; declare type Query<Data = unknown, QueryProps = QueryPropsDefault> = [ queryKey: GetQueryKey<QueryProps> | QueryKey | undefined, fetcher: Fetcher<Data, QueryProps> ]; declare type Queries<QueryProps = QueryPropsDefault> = Readonly<Query<QueryProps>>[]; declare function makeInitialDataAtom(queryKey: QueryKey): Atom<unknown>; declare function atomFamilyWithStaticQuery<ParamType, ReturnType>(getQueryKey: QueryKey | ((param: ParamType) => QueryKey), queryFn: (param: ParamType, get?: Getter) => ReturnType | Promise<ReturnType>): { (param: ParamType): Atom<ReturnType | Promise<ReturnType>>; remove(param: ParamType): void; setShouldRemove(shouldRemove: ((createdAt: number, param: ParamType) => boolean) | null): void; }; declare function queryFamilyFactory<ParamType, ReturnType>(getQueryKey: QueryKey | ((param: ParamType) => QueryKey), queryFn: (param: ParamType) => ReturnType | Promise<ReturnType>): (param: ParamType) => Queries[number]; interface UseInfiniteQueryAtomBaseExtras { fetchNextPage: () => void; fetchPreviousPage: () => void; refetch: () => void; } interface OptionalStatus extends UseInfiniteQueryAtomBaseExtras { isFetchingPreviousPage: boolean; isFetchingNextPage: boolean; hasNextPage: boolean; hasPreviousPage: boolean; } declare function useInfiniteQueryAtom<T>(anAtom: WritableAtom<InfiniteData<T> | undefined, AtomWithInfiniteQueryAction<T>>): [InfiniteData<T> | undefined, OptionalStatus]; interface UseQueryAtomBaseExtras<T> extends QueryStatus { refetch: () => void; setQueryData: ({ data, options }: { data: T; options?: SetDataOptions; }) => void; } declare function useQueryAtom<T>(anAtom: WritableAtom<T, JQTAtomWithQueryActions<T>>): [T extends Promise<infer V> ? V : T, UseQueryAtomBaseExtras<T>]; declare const queryClient: QueryClient; declare type WeakCache<T> = WeakMap<object, [WeakCache<T>] | [WeakCache<T>, T]>; declare function makeQueryKey<P>(key: QueryKey, param?: P): [QueryKey, P] | QueryKey; declare const queryKeyCache: WeakCache<QueryKey>; declare const IS_SSR: boolean; declare const QueryRefreshRates: Record<'Default' | 'Fast' | 'RealTime' | 'None', number | false>; export { AtomFamilyWithQueryFn, AtomWithInfiniteQueryFn, AtomWithInfiniteQueryOptions, AtomWithQueryFn, AtomWithQueryOptions, IS_SSR, InfiniteQueryDispatch, InfiniteQueryStatus, ListParams, OptionalStatus, ParamWithListParams, QueryRefreshRates, QueryStatus, UseInfiniteQueryAtomBaseExtras, UseQueryAtomBaseExtras, atomFamilyWithInfiniteQuery, atomFamilyWithQuery, atomFamilyWithStaticQuery, atomWithInfiniteQuery, atomWithQuery, devtoolAtom, getQueryClientAtom, infiniteQueryKeyObserver, infiniteQueryKeyStatusAtom, initialDataAtom, makeInitialDataAtom, makeQueryKey, queryClient, queryClientAtom, queryFamilyFactory, queryKeyCache, queryKeyObserver, queryKeyStatusAtom, useInfiniteQueryAtom, useQueryAtom };