Skip to content

Commit

Permalink
Update Types
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Sep 4, 2024
1 parent 69a5ff2 commit d939d89
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 62 deletions.
28 changes: 10 additions & 18 deletions packages/react/src/hooks/useDecodedName.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
import { useQuery, type UseQueryResult } from '@tanstack/react-query'
import {
getDecodedName,
type GetDecodedNameParameters,
type GetDecodedNameReturnType,
} from '@ensdomains/ensjs/subgraph'
import type { ParamWithClients } from '../client.js'
import { fallbackQueryClient } from '../query.js'

export type UseDecodedNameParams = ParamWithClients<{
name: string
allowIncomplete?: boolean
}>
export type UseDecodedNameParams = ParamWithClients<GetDecodedNameParameters>

export type UseDecodedNameReturnType = GetDecodedNameReturnType

/**
* Decode names returned using the subgraph
*
* Performs network request only if the name needs to be decoded, otherwise transparent
*
* @param data - {@link UseDecodedNameParams}
* @param params - {@link UseDecodedNameParams}
* @returns - {@link GetDecodedNameReturnType}
*/
export const useDecodedName = (
data: UseDecodedNameParams,
): UseQueryResult<GetDecodedNameReturnType> => {
const {
name,
allowIncomplete,
client,
queryClient = fallbackQueryClient,
} = data
params: UseDecodedNameParams,
): UseQueryResult<UseDecodedNameReturnType> => {
const { client, queryClient = fallbackQueryClient } = params

return useQuery(
{
queryKey: ['ensjs', 'decoded-subgraph-name', name],
queryKey: ['ensjs', 'decoded-subgraph-name', params.name],
queryFn: async () => {
const result = await getDecodedName(client, {
name,
allowIncomplete,
})
const result = await getDecodedName(client, params)

return result
},
Expand Down
28 changes: 15 additions & 13 deletions packages/react/src/hooks/useEnsAvailable.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import { useQuery, type UseQueryResult } from '@tanstack/react-query'
import { getAvailable } from '@ensdomains/ensjs/public'
import {
getAvailable,
type GetAvailableParameters,
type GetAvailableReturnType,
} from '@ensdomains/ensjs/public'
import type { ParamWithClients } from '../client.js'
import { fallbackQueryClient } from '../query.js'

export type UseEnsAvailableParams = ParamWithClients<{
name: string
}>
export type UseEnsAvailableParams = ParamWithClients<GetAvailableParameters>

export type UseEnsAvailableReturnType = GetAvailableReturnType

/**
* Returns a list of names for an address
*
* Keep in mind that this function is limited to .eth names
*
* @param data - {@link UseEnsAvailableParams}
* @returns - {@link boolean}
* @param params - {@link UseEnsAvailableParams}
* @returns - {@link UseEnsAvailableReturnType}
*/
export const useEnsAvailable = (
data: UseEnsAvailableParams,
): UseQueryResult<boolean> => {
const { name, client, queryClient = fallbackQueryClient } = data
params: UseEnsAvailableParams,
): UseQueryResult<UseEnsAvailableReturnType> => {
const { client, queryClient = fallbackQueryClient } = params

return useQuery(
{
queryKey: ['ensjs', 'eth-name-available', name],
queryKey: ['ensjs', 'eth-name-available', params.name],
queryFn: async () => {
const result = await getAvailable(client, {
name,
})
const result = await getAvailable(client, params)

return result
},
Expand Down
37 changes: 12 additions & 25 deletions packages/react/src/hooks/useEnsResolverInterfaces.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,40 @@
import { useQuery, type UseQueryResult } from '@tanstack/react-query'
import type { Address, Hex } from 'viem'
import { getSupportedInterfaces } from '@ensdomains/ensjs/public'
import {
getSupportedInterfaces,
type GetSupportedInterfacesParameters,
type GetSupportedInterfacesReturnType,
} from '@ensdomains/ensjs/public'
import type { ParamWithClients } from '../client.js'
import { fallbackQueryClient } from '../query.js'
import { resolverInterfaces } from '../interfaces.js'

export type UseEnsResolverInterfacesParams<
Interfaces extends readonly Hex[] = [Hex, Hex],
> = ParamWithClients<{
resolver: Address
interfaces?: Interfaces
}>
> = ParamWithClients<GetSupportedInterfacesParameters<Interfaces>>

export type UseEnsResolverInterfacesReturnType<
Interfaces extends readonly Hex[],
> = {
[K in keyof Interfaces]: boolean
}
> = GetSupportedInterfacesReturnType<Interfaces>

/**
* Returns a wether or not the interfaces are supported by the resolver
* You can find a list of interfaces at https://docs.ens.domains/resolvers/interfaces
*
* You can use the {@link resolverInterfaces} shorthand, or manually specify a Hex value
*
* @param data - {@link UseEnsResolverInterfacesParams}
* @param params - {@link UseEnsResolverInterfacesParams}
* @returns - {@link boolean[]}
*/
export const useEnsResolverInterfaces = <Interfaces extends readonly Hex[]>(
data: UseEnsResolverInterfacesParams<Interfaces>,
params: UseEnsResolverInterfacesParams<Interfaces>,
): UseQueryResult<UseEnsResolverInterfacesReturnType<Interfaces>> => {
const {
resolver,
interfaces = [
resolverInterfaces.addrMulticoin,
resolverInterfaces.wildcard,
],
client,
queryClient = fallbackQueryClient,
} = data
const { client, queryClient = fallbackQueryClient } = params

return useQuery(
{
queryKey: ['ensjs', 'resolver-interfaces', resolver],
queryKey: ['ensjs', 'resolver-interfaces', params.address],
queryFn: async () => {
const result = await getSupportedInterfaces(client, {
address: resolver,
interfaces,
})
const result = await getSupportedInterfaces(client, params)

return result
},
Expand Down
14 changes: 8 additions & 6 deletions packages/react/src/hooks/useNamesForAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ import {
import { fallbackQueryClient } from '../query.js'
import type { ParamWithClients } from '../client.js'

export type UseNamesParams = ParamWithClients<{
export type UseNamesForAddressParams = ParamWithClients<{
address: Address
}>

export type UseNamesForAddressReturnType = GetNamesForAddressReturnType

/**
* Returns a list of names for an address
*
* Keep in mind that this list will be loaded from the subgraph, and only include watchable names.
* Read more about enumeration and watchability here: https://docs.ens.domains/web/enumerate
*
* @param data - {@link UseNamesParams}
* @returns - {@link GetNamesForAddressReturnType}
* @param params - {@link UseNamesForAddressParams}
* @returns - {@link UseNamesForAddressReturnType}
*/
export const useNamesForAddress = (
data: UseNamesParams,
): UseQueryResult<GetNamesForAddressReturnType> => {
const { address, client, queryClient = fallbackQueryClient } = data
params: UseNamesForAddressParams,
): UseQueryResult<UseNamesForAddressReturnType> => {
const { address, client, queryClient = fallbackQueryClient } = params

return useQuery(
{
Expand Down

0 comments on commit d939d89

Please sign in to comment.