From a250d041725359cec9797766ad27b4cfc626a196 Mon Sep 17 00:00:00 2001 From: Luc Date: Thu, 5 Sep 2024 20:59:43 +0000 Subject: [PATCH] Extrapolate UseQueryConfig (breaking) --- packages/react/src/client.ts | 6 +++++- packages/react/src/hooks/useDecodedName.ts | 7 ++++--- packages/react/src/hooks/useEnsAvailable.ts | 7 ++++--- packages/react/src/hooks/useEnsCredentials.ts | 7 ++++--- packages/react/src/hooks/useEnsExpiry.ts | 13 +++++-------- .../react/src/hooks/useEnsResolverInterfaces.ts | 8 ++++---- packages/react/src/hooks/useNamesForAddress.ts | 10 ++++++---- packages/react/src/hooks/useQuery.ts | 13 +++++++++---- 8 files changed, 41 insertions(+), 30 deletions(-) diff --git a/packages/react/src/client.ts b/packages/react/src/client.ts index 62e1c534..13e3690c 100644 --- a/packages/react/src/client.ts +++ b/packages/react/src/client.ts @@ -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 & { client: ClientWithEns - queryClient?: QueryClient } + +export type QueryConfig = { + queryClient?: QueryClient +} & UseQueryParameters diff --git a/packages/react/src/hooks/useDecodedName.ts b/packages/react/src/hooks/useDecodedName.ts index 0704d3ae..99486433 100644 --- a/packages/react/src/hooks/useDecodedName.ts +++ b/packages/react/src/hooks/useDecodedName.ts @@ -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 @@ -20,8 +20,9 @@ export type UseDecodedNameReturnType = GetDecodedNameReturnType */ export const useDecodedName = ( params: UseDecodedNameParams, + query?: QueryConfig, ): UseQueryReturnType => { - const { client, queryClient } = params + const { client } = params return useQuery( ['ensjs', 'decoded-subgraph-name', params.name], @@ -32,6 +33,6 @@ export const useDecodedName = ( return result }, }, - queryClient, + query, ) } diff --git a/packages/react/src/hooks/useEnsAvailable.ts b/packages/react/src/hooks/useEnsAvailable.ts index da234556..cecfc51d 100644 --- a/packages/react/src/hooks/useEnsAvailable.ts +++ b/packages/react/src/hooks/useEnsAvailable.ts @@ -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 @@ -20,8 +20,9 @@ export type UseEnsAvailableReturnType = GetAvailableReturnType */ export const useEnsAvailable = ( params: UseEnsAvailableParams, + query?: QueryConfig, ): UseQueryReturnType => { - const { client, queryClient } = params + const { client } = params return useQuery( ['ensjs', 'eth-name-available', params.name], @@ -33,6 +34,6 @@ export const useEnsAvailable = ( return result }, }, - queryClient, + query, ) } diff --git a/packages/react/src/hooks/useEnsCredentials.ts b/packages/react/src/hooks/useEnsCredentials.ts index 6345f5b8..ad78ac1b 100644 --- a/packages/react/src/hooks/useEnsCredentials.ts +++ b/packages/react/src/hooks/useEnsCredentials.ts @@ -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 }> @@ -18,8 +18,9 @@ export type UseEnsCredentialsReturnType = ExternalCredential[] */ export const useEnsCredentials = ( params: UseEnsCredentialsParams, + query?: QueryConfig, ): UseQueryReturnType => { - const { name, client, queryClient } = params + const { name, client } = params return useQuery( ['ensjs', 'credentials', params.name], @@ -41,6 +42,6 @@ export const useEnsCredentials = ( return credentials }, }, - queryClient, + query, ) } diff --git a/packages/react/src/hooks/useEnsExpiry.ts b/packages/react/src/hooks/useEnsExpiry.ts index 24cdbd9e..eb6ee2a3 100644 --- a/packages/react/src/hooks/useEnsExpiry.ts +++ b/packages/react/src/hooks/useEnsExpiry.ts @@ -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 @@ -20,18 +20,15 @@ export type UseEnsExpiryReturnType = GetExpiryReturnType */ export const useEnsExpiry = ( params: UseEnsExpiryParams, + query?: QueryConfig, ): UseQueryReturnType => { - 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, ) } diff --git a/packages/react/src/hooks/useEnsResolverInterfaces.ts b/packages/react/src/hooks/useEnsResolverInterfaces.ts index 41f44003..7665ba94 100644 --- a/packages/react/src/hooks/useEnsResolverInterfaces.ts +++ b/packages/react/src/hooks/useEnsResolverInterfaces.ts @@ -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< @@ -27,8 +26,9 @@ export type UseEnsResolverInterfacesReturnType< */ export const useEnsResolverInterfaces = ( params: UseEnsResolverInterfacesParams, + query?: QueryConfig, ): UseQueryReturnType> => { - const { client, queryClient = fallbackQueryClient } = params + const { client } = params return useQuery( ['ensjs', 'resolver-interfaces', params.address], @@ -39,6 +39,6 @@ export const useEnsResolverInterfaces = ( return result }, }, - queryClient, + query, ) } diff --git a/packages/react/src/hooks/useNamesForAddress.ts b/packages/react/src/hooks/useNamesForAddress.ts index e3884a64..3e613902 100644 --- a/packages/react/src/hooks/useNamesForAddress.ts +++ b/packages/react/src/hooks/useNamesForAddress.ts @@ -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<{ @@ -24,8 +23,9 @@ export type UseNamesForAddressReturnType = GetNamesForAddressReturnType */ export const useNamesForAddress = ( params: UseNamesForAddressParams, + queryConfig?: QueryConfig, ): UseQueryReturnType => { - const { address, client, queryClient = fallbackQueryClient } = params + const { address, client } = params return useQuery( ['ensjs', 'names-for-address', address], @@ -37,7 +37,9 @@ export const useNamesForAddress = ( return result }, + enabled: !!params.address, + initialData: [], }, - queryClient, + queryConfig, ) } diff --git a/packages/react/src/hooks/useQuery.ts b/packages/react/src/hooks/useQuery.ts index af439986..c8232457 100644 --- a/packages/react/src/hooks/useQuery.ts +++ b/packages/react/src/hooks/useQuery.ts @@ -1,7 +1,6 @@ import { useQuery as useTanstackQuery, type DefaultError, - type QueryClient, type QueryKey, type UseQueryOptions, type UseQueryResult, @@ -9,6 +8,7 @@ import { 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, @@ -31,13 +31,18 @@ export type UseQueryReturnType< Error = DefaultError, > = UseQueryResult -export const useQuery = ( +export const useQuery = < + Parameters extends UseQueryParameters, + Data = unknown, + Error = unknown, +>( key: QueryKey, queryParameters: Exclude, - queryClient?: QueryClient, + queryConfig?: QueryConfig, ): UseQueryReturnType => { const parameters = { ...queryParameters, + ...queryConfig, queryKey: key, } @@ -45,6 +50,6 @@ export const useQuery = ( // @ts-ignore return useTanstackQuery( { ...parameters } as any, - queryClient ?? fallbackQueryClient, + queryConfig?.queryClient ?? fallbackQueryClient, ) }