Skip to content

Commit

Permalink
bug: fix generics
Browse files Browse the repository at this point in the history
  • Loading branch information
kalijonn committed Jan 31, 2024
1 parent d6e1af6 commit 339f5c7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 31 deletions.
8 changes: 4 additions & 4 deletions src/atomWithInfiniteQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
QueryKey,
QueryObserver,
} from '@tanstack/query-core'
import { Getter } from 'jotai'
import { Atom, Getter } from 'jotai'
import { baseAtomWithQuery } from './baseAtomWithQuery'
import { queryClientAtom } from './queryClientAtom'
import {
Expand Down Expand Up @@ -34,7 +34,7 @@ export function atomWithInfiniteQuery<
TPageParam
>,
getQueryClient?: (get: Getter) => QueryClient
): AtomWithInfiniteQueryResult<TData, TError>
): Atom<AtomWithInfiniteQueryResult<TData, TError>>
export function atomWithInfiniteQuery<
TQueryFnData,
TError = DefaultError,
Expand All @@ -52,7 +52,7 @@ export function atomWithInfiniteQuery<
TPageParam
>,
getQueryClient?: (get: Getter) => QueryClient
): DefinedAtomWithInfiniteQueryResult<TData, TError>
): Atom<DefinedAtomWithInfiniteQueryResult<TData, TError>>
export function atomWithInfiniteQuery<
TQueryFnData,
TError = DefaultError,
Expand All @@ -71,7 +71,7 @@ export function atomWithInfiniteQuery<
TPageParam
>,
getQueryClient?: (get: Getter) => QueryClient
): AtomWithInfiniteQueryResult<TData, TError>
): Atom<AtomWithInfiniteQueryResult<TData, TError>>
export function atomWithInfiniteQuery(
getOptions: (get: Getter) => AtomWithInfiniteQueryOptions,
getQueryClient: (get: Getter) => QueryClient = (get) => get(queryClientAtom)
Expand Down
4 changes: 2 additions & 2 deletions src/atomWithMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
MutationOptions,
QueryClient,
} from '@tanstack/query-core'
import { Getter, atom } from 'jotai'
import { Atom, Getter, atom } from 'jotai'
import { make, pipe, toObservable } from 'wonka'
import { queryClientAtom } from './queryClientAtom'
import { AtomWithMutationResult, MutateFunction } from './types'
Expand All @@ -20,7 +20,7 @@ export function atomWithMutation<
get: Getter
) => MutationOptions<TData, TError, TVariables, TContext>,
getQueryClient: (get: Getter) => QueryClient = (get) => get(queryClientAtom)
): AtomWithMutationResult<TData, TError, TVariables, TContext> {
): Atom<AtomWithMutationResult<TData, TError, TVariables, TContext>> {
const IN_RENDER = Symbol()

const optionsAtom = atom((get) => {
Expand Down
8 changes: 4 additions & 4 deletions src/atomWithQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
QueryKey,
QueryObserver,
} from '@tanstack/query-core'
import { Getter } from 'jotai'
import { Atom, Getter } from 'jotai'
import { baseAtomWithQuery } from './baseAtomWithQuery'
import { queryClientAtom } from './queryClientAtom'
import {
Expand All @@ -25,7 +25,7 @@ export function atomWithQuery<
get: Getter
) => UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
getQueryClient?: (get: Getter) => QueryClient
): AtomWithQueryResult<TData, TError>
): Atom<AtomWithQueryResult<TData, TError>>
export function atomWithQuery<
TQueryFnData = unknown,
TError = DefaultError,
Expand All @@ -36,7 +36,7 @@ export function atomWithQuery<
get: Getter
) => DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
getQueryClient?: (get: Getter) => QueryClient
): DefinedAtomWithQueryResult<TData, TError>
): Atom<DefinedAtomWithQueryResult<TData, TError>>
export function atomWithQuery<
TQueryFnData = unknown,
TError = DefaultError,
Expand All @@ -47,7 +47,7 @@ export function atomWithQuery<
get: Getter
) => AtomWithQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
getQueryClient?: (get: Getter) => QueryClient
): AtomWithQueryResult<TData, TError>
): Atom<AtomWithQueryResult<TData, TError>>
export function atomWithQuery(
getOptions: (get: Getter) => AtomWithQueryOptions,
getQueryClient: (get: Getter) => QueryClient = (get) => get(queryClientAtom)
Expand Down
6 changes: 3 additions & 3 deletions src/atomWithSuspenseInfiniteQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
QueryKey,
QueryObserver,
} from '@tanstack/query-core'
import { Getter, atom } from 'jotai'
import { Atom, Getter, atom } from 'jotai'
import { baseAtomWithQuery } from './baseAtomWithQuery'
import { queryClientAtom } from './queryClientAtom'
import {
Expand All @@ -33,7 +33,7 @@ export function atomWithSuspenseInfiniteQuery<
TPageParam
>,
getQueryClient: (get: Getter) => QueryClient = (get) => get(queryClientAtom)
): AtomWithSuspenseInfiniteQueryResult<TData, TError> {
): Atom<AtomWithSuspenseInfiniteQueryResult<TData, TError>> {
const suspenseOptionsAtom = atom((get) => {
const options = getOptions(get)
return {
Expand All @@ -48,5 +48,5 @@ export function atomWithSuspenseInfiniteQuery<
(get: Getter) => get(suspenseOptionsAtom),
InfiniteQueryObserver as typeof QueryObserver,
getQueryClient
) as unknown as AtomWithSuspenseInfiniteQueryResult<TData, TError>
) as unknown as Atom<AtomWithSuspenseInfiniteQueryResult<TData, TError>>
}
6 changes: 3 additions & 3 deletions src/atomWithSuspenseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
QueryKey,
QueryObserver,
} from '@tanstack/query-core'
import { Getter, atom } from 'jotai'
import { Atom, Getter, atom } from 'jotai'
import { baseAtomWithQuery } from './baseAtomWithQuery'
import { queryClientAtom } from './queryClientAtom'
import {
Expand All @@ -23,7 +23,7 @@ export function atomWithSuspenseQuery<
get: Getter
) => AtomWithSuspenseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
getQueryClient: (get: Getter) => QueryClient = (get) => get(queryClientAtom)
): AtomWithSuspenseQueryResult<TData, TError> {
): Atom<AtomWithSuspenseQueryResult<TData, TError>> {
const suspenseOptions = atom((get) => {
const options = getOptions(get)
return {
Expand All @@ -38,5 +38,5 @@ export function atomWithSuspenseQuery<
(get: Getter) => get(suspenseOptions),
QueryObserver,
getQueryClient
) as AtomWithSuspenseQueryResult<TData, TError>
) as Atom<AtomWithSuspenseQueryResult<TData, TError>>
}
26 changes: 11 additions & 15 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
QueryObserverResult,
WithRequired,
} from '@tanstack/query-core'
import { Atom } from 'jotai'

type Override<A, B> = { [K in keyof A]: K extends keyof B ? B[K] : A[K] }

Expand All @@ -33,14 +32,13 @@ export type MutateAsyncFunction<
TContext = unknown,
> = QueryMutateFunction<TData, TError, TVariables, TContext>

export type AtomWithMutationResult<TData, TError, TVariables, TContext> = Atom<
export type AtomWithMutationResult<TData, TError, TVariables, TContext> =
Override<
MutationObserverResult<TData, TError, TVariables, TContext>,
{ mutate: MutateFunction<TData, TError, TVariables, TContext> }
> & {
mutateAsync: MutateAsyncFunction<TData, TError, TVariables, TContext>
}
>

export interface MutationOptions<
TData = unknown,
Expand Down Expand Up @@ -133,42 +131,40 @@ export interface AtomWithSuspenseInfiniteQueryOptions<
'enabled' | 'throwOnError' | 'placeholderData'
> {}

export type AtomWithQueryResult<TData = unknown, TError = DefaultError> = Atom<
QueryObserverResult<TData, TError>
>
export type AtomWithQueryResult<
TData = unknown,
TError = DefaultError,
> = QueryObserverResult<TData, TError>

export type DefinedAtomWithQueryResult<
TData = unknown,
TError = DefaultError,
> = Atom<DefinedQueryObserverResult<TData, TError>>
> = DefinedQueryObserverResult<TData, TError>

export type AtomWithSuspenseQueryResult<
TData = unknown,
TError = DefaultError,
> = Atom<
> =
| Omit<DefinedQueryObserverResult<TData, TError>, 'isPlaceholderData'>
| Promise<
Omit<DefinedQueryObserverResult<TData, TError>, 'isPlaceholderData'>
>
>

export type AtomWithInfiniteQueryResult<
TData = unknown,
TError = DefaultError,
> = Atom<InfiniteQueryObserverResult<TData, TError>>
> = InfiniteQueryObserverResult<TData, TError>

export type DefinedAtomWithInfiniteQueryResult<
TData = unknown,
TError = DefaultError,
> = Atom<DefinedInfiniteQueryObserverResult<TData, TError>>
> = DefinedInfiniteQueryObserverResult<TData, TError>

export type AtomWithSuspenseInfiniteQueryResult<
TData = unknown,
TError = DefaultError,
> = Atom<
Promise<
Omit<DefinedInfiniteQueryObserverResult<TData, TError>, 'isPlaceholderData'>
>
> = Promise<
Omit<DefinedInfiniteQueryObserverResult<TData, TError>, 'isPlaceholderData'>
>

export type UndefinedInitialDataOptions<
Expand Down

0 comments on commit 339f5c7

Please sign in to comment.