Skip to content

Commit

Permalink
Extrapolate UseQueryConfig (breaking)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Sep 23, 2024
1 parent d25ae6d commit 2699690
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 30 deletions.
6 changes: 5 additions & 1 deletion packages/react/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { ClientWithEns } from '@ensdomains/ensjs/contracts'
import type { QueryClient } from '@tanstack/react-query'
import type { UseQueryParameters } from './hooks/useQuery.js'

export type ParamWithClients<T> = T & {
client: ClientWithEns
queryClient?: QueryClient
}

export type QueryConfig = {
queryClient?: QueryClient
} & UseQueryParameters
7 changes: 4 additions & 3 deletions packages/react/src/hooks/useDecodedName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type GetDecodedNameParameters,
type GetDecodedNameReturnType,
} from '@ensdomains/ensjs/subgraph'
import type { ParamWithClients } from '../client.js'
import type { ParamWithClients, QueryConfig } from '../client.js'
import { useQuery, type UseQueryReturnType } from './useQuery.js'

export type UseDecodedNameParams = ParamWithClients<GetDecodedNameParameters>
Expand All @@ -20,8 +20,9 @@ export type UseDecodedNameReturnType = GetDecodedNameReturnType
*/
export const useDecodedName = (
params: UseDecodedNameParams,
query?: QueryConfig,
): UseQueryReturnType<UseDecodedNameReturnType> => {
const { client, queryClient } = params
const { client } = params

return useQuery(
['ensjs', 'decoded-subgraph-name', params.name],
Expand All @@ -32,6 +33,6 @@ export const useDecodedName = (
return result
},
},
queryClient,
query,
)
}
7 changes: 4 additions & 3 deletions packages/react/src/hooks/useEnsAvailable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type GetAvailableParameters,
type GetAvailableReturnType,
} from '@ensdomains/ensjs/public'
import type { ParamWithClients } from '../client.js'
import type { ParamWithClients, QueryConfig } from '../client.js'
import { useQuery, type UseQueryReturnType } from './useQuery.js'

export type UseEnsAvailableParams = ParamWithClients<GetAvailableParameters>
Expand All @@ -20,8 +20,9 @@ export type UseEnsAvailableReturnType = GetAvailableReturnType
*/
export const useEnsAvailable = (
params: UseEnsAvailableParams,
query?: QueryConfig,
): UseQueryReturnType<UseEnsAvailableReturnType> => {
const { client, queryClient } = params
const { client } = params

return useQuery(
['ensjs', 'eth-name-available', params.name],
Expand All @@ -33,6 +34,6 @@ export const useEnsAvailable = (
return result
},
},
queryClient,
query,
)
}
7 changes: 4 additions & 3 deletions packages/react/src/hooks/useEnsCredentials.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getTextRecord } from '@ensdomains/ensjs/public'
import type { ParamWithClients } from '../client.js'
import type { ParamWithClients, QueryConfig } from '../client.js'
import { useQuery, type UseQueryReturnType } from './useQuery.js'

export type UseEnsCredentialsParams = ParamWithClients<{ name: string }>
Expand All @@ -18,8 +18,9 @@ export type UseEnsCredentialsReturnType = ExternalCredential[]
*/
export const useEnsCredentials = (
params: UseEnsCredentialsParams,
query?: QueryConfig,
): UseQueryReturnType<UseEnsCredentialsReturnType> => {
const { name, client, queryClient } = params
const { name, client } = params

return useQuery(
['ensjs', 'credentials', params.name],
Expand All @@ -41,6 +42,6 @@ export const useEnsCredentials = (
return credentials
},
},
queryClient,
query,
)
}
13 changes: 5 additions & 8 deletions packages/react/src/hooks/useEnsExpiry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type GetExpiryParameters,
type GetExpiryReturnType,
} from '@ensdomains/ensjs/public'
import type { ParamWithClients } from '../client.js'
import type { ParamWithClients, QueryConfig } from '../client.js'
import { useQuery, type UseQueryReturnType } from './useQuery.js'

export type UseEnsExpiryParams = ParamWithClients<GetExpiryParameters>
Expand All @@ -20,18 +20,15 @@ export type UseEnsExpiryReturnType = GetExpiryReturnType
*/
export const useEnsExpiry = (
params: UseEnsExpiryParams,
query?: QueryConfig,
): UseQueryReturnType<UseEnsExpiryReturnType> => {
const { client, queryClient } = params
const { client } = params

return useQuery(
['ensjs', 'ens-expiry', params.name],
{
queryFn: async () => {
const result = await getExpiry(client, params)

return result
},
queryFn: async () => getExpiry(client, params),
},
queryClient,
query,
)
}
8 changes: 4 additions & 4 deletions packages/react/src/hooks/useEnsResolverInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
type GetSupportedInterfacesParameters,
type GetSupportedInterfacesReturnType,
} from '@ensdomains/ensjs/public'
import type { ParamWithClients } from '../client.js'
import { fallbackQueryClient } from '../query.js'
import type { ParamWithClients, QueryConfig } from '../client.js'
import { useQuery, type UseQueryReturnType } from './useQuery.js'

export type UseEnsResolverInterfacesParams<
Expand All @@ -27,8 +26,9 @@ export type UseEnsResolverInterfacesReturnType<
*/
export const useEnsResolverInterfaces = <Interfaces extends readonly Hex[]>(
params: UseEnsResolverInterfacesParams<Interfaces>,
query?: QueryConfig,
): UseQueryReturnType<UseEnsResolverInterfacesReturnType<Interfaces>> => {
const { client, queryClient = fallbackQueryClient } = params
const { client } = params

return useQuery(
['ensjs', 'resolver-interfaces', params.address],
Expand All @@ -39,6 +39,6 @@ export const useEnsResolverInterfaces = <Interfaces extends readonly Hex[]>(
return result
},
},
queryClient,
query,
)
}
10 changes: 6 additions & 4 deletions packages/react/src/hooks/useNamesForAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {
getNamesForAddress,
type GetNamesForAddressReturnType,
} from '@ensdomains/ensjs/subgraph'
import { fallbackQueryClient } from '../query.js'
import type { ParamWithClients } from '../client.js'
import type { ParamWithClients, QueryConfig } from '../client.js'
import { useQuery, type UseQueryReturnType } from './useQuery.js'

export type UseNamesForAddressParams = ParamWithClients<{
Expand All @@ -24,8 +23,9 @@ export type UseNamesForAddressReturnType = GetNamesForAddressReturnType
*/
export const useNamesForAddress = (
params: UseNamesForAddressParams,
queryConfig?: QueryConfig,
): UseQueryReturnType<UseNamesForAddressReturnType> => {
const { address, client, queryClient = fallbackQueryClient } = params
const { address, client } = params

return useQuery(
['ensjs', 'names-for-address', address],
Expand All @@ -37,7 +37,9 @@ export const useNamesForAddress = (

return result
},
enabled: !!params.address,
initialData: [],
},
queryClient,
queryConfig,
)
}
13 changes: 9 additions & 4 deletions packages/react/src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
useQuery as useTanstackQuery,
type DefaultError,
type QueryClient,
type QueryKey,
type UseQueryOptions,
type UseQueryResult,
} from '@tanstack/react-query'
import type { ExactPartial } from 'viem'
import { fallbackQueryClient } from '../query.js'
import type { Compute } from '../utils/types.js'
import type { QueryConfig } from '../client.js'

export type UseQueryParameters<
QueryFnData = unknown,
Expand All @@ -31,20 +31,25 @@ export type UseQueryReturnType<
Error = DefaultError,
> = UseQueryResult<Data, Error>

export const useQuery = <Parameters, Data, Error>(
export const useQuery = <
Parameters extends UseQueryParameters,
Data = unknown,
Error = unknown,
>(
key: QueryKey,
queryParameters: Exclude<Parameters, 'queryKey'>,
queryClient?: QueryClient,
queryConfig?: QueryConfig,
): UseQueryReturnType<Data, Error> => {
const parameters = {
...queryParameters,
...queryConfig,
queryKey: key,
}

// TODO: figure out why this is necessary
// @ts-ignore
return useTanstackQuery(
{ ...parameters } as any,
queryClient ?? fallbackQueryClient,
queryConfig?.queryClient ?? fallbackQueryClient,
)
}

0 comments on commit 2699690

Please sign in to comment.