From 9a6c8571afedd0b08ae3f04c62f561181cc0866a Mon Sep 17 00:00:00 2001 From: tate Date: Tue, 24 Sep 2024 12:21:11 +1000 Subject: [PATCH] added remaining public funcs + package.json updates --- packages/ensjs/package.json | 6 +- packages/react/README.md | 2 +- packages/react/package.json | 19 ++- packages/react/src/client.ts | 11 -- packages/react/src/hooks.ts | 7 - packages/react/src/hooks/useEnsAbi.ts | 57 ++++++++ packages/react/src/hooks/useEnsAddress.ts | 57 ++++++++ packages/react/src/hooks/useEnsContentHash.ts | 57 ++++++++ packages/react/src/hooks/useEnsOwner.ts | 57 ++++++++ packages/react/src/hooks/useEnsPrice.ts | 57 ++++++++ packages/react/src/hooks/useEnsRecords.ts | 77 ++++++++++ packages/react/src/index.ts | 68 ++++++++- packages/react/src/query.ts | 136 +++++++++++++++++- packages/react/src/query/getEnsAbi.ts | 62 ++++++++ packages/react/src/query/getEnsAddress.ts | 63 ++++++++ packages/react/src/query/getEnsContentHash.ts | 65 +++++++++ packages/react/src/query/getEnsOwner.ts | 63 ++++++++ packages/react/src/query/getEnsPrice.ts | 65 +++++++++ packages/react/src/query/getEnsRecords.ts | 113 +++++++++++++++ 19 files changed, 1013 insertions(+), 29 deletions(-) delete mode 100644 packages/react/src/client.ts delete mode 100644 packages/react/src/hooks.ts create mode 100644 packages/react/src/hooks/useEnsAbi.ts create mode 100644 packages/react/src/hooks/useEnsAddress.ts create mode 100644 packages/react/src/hooks/useEnsContentHash.ts create mode 100644 packages/react/src/hooks/useEnsOwner.ts create mode 100644 packages/react/src/hooks/useEnsPrice.ts create mode 100644 packages/react/src/hooks/useEnsRecords.ts create mode 100644 packages/react/src/query/getEnsAbi.ts create mode 100644 packages/react/src/query/getEnsAddress.ts create mode 100644 packages/react/src/query/getEnsContentHash.ts create mode 100644 packages/react/src/query/getEnsOwner.ts create mode 100644 packages/react/src/query/getEnsPrice.ts create mode 100644 packages/react/src/query/getEnsRecords.ts diff --git a/packages/ensjs/package.json b/packages/ensjs/package.json index 63e96b76..f3021225 100644 --- a/packages/ensjs/package.json +++ b/packages/ensjs/package.json @@ -74,7 +74,11 @@ "!src/**/*.test.ts", "!src/test" ], - "repository": "git@github.com:ensdomains/ensjs.git", + "repository": { + "type": "git", + "url": "git+https://github.com/ensdomains/ensjs.git", + "directory": "packages/ensjs" + }, "author": "TateB ", "license": "MIT", "scripts": { diff --git a/packages/react/README.md b/packages/react/README.md index 40aa413d..e547750e 100644 --- a/packages/react/README.md +++ b/packages/react/README.md @@ -7,7 +7,7 @@ React hooks & utilities to interact with the Ethereum Name Service using ENSjs After installing [wagmi](https://wagmi.sh), simply run ```sh -pnpm install @ensdomains/ensjs-react +pnpm install @ensdomains/ensjs-react @ensdomains/ensjs ``` ## Hooks diff --git a/packages/react/package.json b/packages/react/package.json index 5b41a313..fc3aff5f 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -14,6 +14,11 @@ "import": "./dist/esm/index.js", "default": "./dist/cjs/index.js" }, + "./query": { + "types": "./dist/types/query.d.ts", + "import": "./dist/esm/query.js", + "default": "./dist/cjs/query.js" + }, "./package.json": "./package.json" }, "files": [ @@ -22,8 +27,15 @@ "!src/**/*.test.ts", "!src/test" ], - "repository": "git@github.com:ensdomains/ensjs.git", - "author": "Lucemans ", + "repository": { + "type": "git", + "url": "git+https://github.com/ensdomains/ensjs.git", + "directory": "packages/react" + }, + "contributors": [ + "TateB ", + "Lucemans " + ], "license": "MIT", "scripts": { "clean": "rm -rf ./dist", @@ -49,7 +61,8 @@ "peerDependencies": { "@tanstack/react-query": "^5.54", "viem": "^2.9.2", - "wagmi": "^2" + "wagmi": "^2", + "@ensdomains/ensjs": "^4.0.1" }, "engines": { "node": ">=18" diff --git a/packages/react/src/client.ts b/packages/react/src/client.ts deleted file mode 100644 index 13e3690c..00000000 --- a/packages/react/src/client.ts +++ /dev/null @@ -1,11 +0,0 @@ -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 -} - -export type QueryConfig = { - queryClient?: QueryClient -} & UseQueryParameters diff --git a/packages/react/src/hooks.ts b/packages/react/src/hooks.ts deleted file mode 100644 index c09474f2..00000000 --- a/packages/react/src/hooks.ts +++ /dev/null @@ -1,7 +0,0 @@ -export { useEnsResolverInterfaces } from './hooks/useEnsResolverInterfaces.js' -export { useNamesForAddress } from './hooks/useNamesForAddress.js' -export { useEnsRecordsWrite } from './hooks/useEnsRecordsWrite.js' -export { useEnsCredentials } from './hooks/useEnsCredentials.js' -export { useEnsAvailable } from './hooks/useEnsAvailable.js' -export { useDecodedName } from './hooks/useDecodedName.js' -export { useEnsExpiry } from './hooks/useEnsExpiry.js' diff --git a/packages/react/src/hooks/useEnsAbi.ts b/packages/react/src/hooks/useEnsAbi.ts new file mode 100644 index 00000000..763cb2ea --- /dev/null +++ b/packages/react/src/hooks/useEnsAbi.ts @@ -0,0 +1,57 @@ +import { useChainId, useConfig } from 'wagmi' +import { useQuery, type UseQueryReturnType } from 'wagmi/query' +import { + getEnsAbiQueryOptions, + type GetEnsAbiData, + type GetEnsAbiErrorType, + type GetEnsAbiOptions, + type GetEnsAbiQueryFnData, + type GetEnsAbiQueryKey, +} from '../query/getEnsAbi.js' +import type { ConfigWithEns } from '../types/config.js' +import type { ConfigParameter, QueryParameter } from '../types/properties.js' +import type { ResolvedRegister } from '../types/register.js' +import type { Compute } from '../types/utils.js' + +export type UseEnsAbiParameters< + config extends ConfigWithEns = ConfigWithEns, + selectData = GetEnsAbiData, +> = Compute< + GetEnsAbiOptions & + ConfigParameter & + QueryParameter< + GetEnsAbiQueryFnData, + GetEnsAbiErrorType, + selectData, + GetEnsAbiQueryKey + > +> + +export type UseEnsAbiReturnType = + UseQueryReturnType + +/** + * Returns the ABI for a name + * + * @param parameters - {@link UseEnsAbiParameters} + * @returns - {@link UseEnsAbiReturnType} + */ +export const useEnsAbi = < + config extends ConfigWithEns = ResolvedRegister['config'], + selectData = GetEnsAbiData, +>( + parameters: UseEnsAbiParameters = {}, +): UseEnsAbiReturnType => { + const { name, query = {} } = parameters + + const config = useConfig() + const chainId = useChainId({ config }) + + const options = getEnsAbiQueryOptions(config, { + ...parameters, + chainId: parameters.chainId ?? chainId, + }) + const enabled = Boolean(name && (query.enabled ?? true)) + + return useQuery({ ...query, ...options, enabled }) +} diff --git a/packages/react/src/hooks/useEnsAddress.ts b/packages/react/src/hooks/useEnsAddress.ts new file mode 100644 index 00000000..06fbd903 --- /dev/null +++ b/packages/react/src/hooks/useEnsAddress.ts @@ -0,0 +1,57 @@ +import { useChainId, useConfig } from 'wagmi' +import { useQuery, type UseQueryReturnType } from 'wagmi/query' +import { + getEnsAddressQueryOptions, + type GetEnsAddressData, + type GetEnsAddressErrorType, + type GetEnsAddressOptions, + type GetEnsAddressQueryFnData, + type GetEnsAddressQueryKey, +} from '../query/getEnsAddress.js' +import type { ConfigWithEns } from '../types/config.js' +import type { ConfigParameter, QueryParameter } from '../types/properties.js' +import type { ResolvedRegister } from '../types/register.js' +import type { Compute } from '../types/utils.js' + +export type UseEnsAddressParameters< + config extends ConfigWithEns = ConfigWithEns, + selectData = GetEnsAddressData, +> = Compute< + GetEnsAddressOptions & + ConfigParameter & + QueryParameter< + GetEnsAddressQueryFnData, + GetEnsAddressErrorType, + selectData, + GetEnsAddressQueryKey + > +> + +export type UseEnsAddressReturnType = + UseQueryReturnType + +/** + * Returns the address for a name + * + * @param parameters - {@link UseEnsAddressParameters} + * @returns - {@link UseEnsAddressReturnType} + */ +export const useEnsAddress = < + config extends ConfigWithEns = ResolvedRegister['config'], + selectData = GetEnsAddressData, +>( + parameters: UseEnsAddressParameters = {}, +): UseEnsAddressReturnType => { + const { name, query = {} } = parameters + + const config = useConfig() + const chainId = useChainId({ config }) + + const options = getEnsAddressQueryOptions(config, { + ...parameters, + chainId: parameters.chainId ?? chainId, + }) + const enabled = Boolean(name && (query.enabled ?? true)) + + return useQuery({ ...query, ...options, enabled }) +} diff --git a/packages/react/src/hooks/useEnsContentHash.ts b/packages/react/src/hooks/useEnsContentHash.ts new file mode 100644 index 00000000..01753570 --- /dev/null +++ b/packages/react/src/hooks/useEnsContentHash.ts @@ -0,0 +1,57 @@ +import { useChainId, useConfig } from 'wagmi' +import { useQuery, type UseQueryReturnType } from 'wagmi/query' +import { + getEnsContentHashQueryOptions, + type GetEnsContentHashData, + type GetEnsContentHashErrorType, + type GetEnsContentHashOptions, + type GetEnsContentHashQueryFnData, + type GetEnsContentHashQueryKey, +} from '../query/getEnsContentHash.js' +import type { ConfigWithEns } from '../types/config.js' +import type { ConfigParameter, QueryParameter } from '../types/properties.js' +import type { ResolvedRegister } from '../types/register.js' +import type { Compute } from '../types/utils.js' + +export type UseEnsContentHashParameters< + config extends ConfigWithEns = ConfigWithEns, + selectData = GetEnsContentHashData, +> = Compute< + GetEnsContentHashOptions & + ConfigParameter & + QueryParameter< + GetEnsContentHashQueryFnData, + GetEnsContentHashErrorType, + selectData, + GetEnsContentHashQueryKey + > +> + +export type UseEnsContentHashReturnType = + UseQueryReturnType + +/** + * Returns the content hash for a name + * + * @param parameters - {@link UseEnsContentHashParameters} + * @returns - {@link UseEnsContentHashReturnType} + */ +export const useEnsContentHash = < + config extends ConfigWithEns = ResolvedRegister['config'], + selectData = GetEnsContentHashData, +>( + parameters: UseEnsContentHashParameters = {}, +): UseEnsContentHashReturnType => { + const { name, query = {} } = parameters + + const config = useConfig() + const chainId = useChainId({ config }) + + const options = getEnsContentHashQueryOptions(config, { + ...parameters, + chainId: parameters.chainId ?? chainId, + }) + const enabled = Boolean(name && (query.enabled ?? true)) + + return useQuery({ ...query, ...options, enabled }) +} diff --git a/packages/react/src/hooks/useEnsOwner.ts b/packages/react/src/hooks/useEnsOwner.ts new file mode 100644 index 00000000..b2e4f7d1 --- /dev/null +++ b/packages/react/src/hooks/useEnsOwner.ts @@ -0,0 +1,57 @@ +import { useChainId, useConfig } from 'wagmi' +import { useQuery, type UseQueryReturnType } from 'wagmi/query' +import { + getEnsOwnerQueryOptions, + type GetEnsOwnerData, + type GetEnsOwnerErrorType, + type GetEnsOwnerOptions, + type GetEnsOwnerQueryFnData, + type GetEnsOwnerQueryKey, +} from '../query/getEnsOwner.js' +import type { ConfigWithEns } from '../types/config.js' +import type { ConfigParameter, QueryParameter } from '../types/properties.js' +import type { ResolvedRegister } from '../types/register.js' +import type { Compute } from '../types/utils.js' + +export type UseEnsOwnerParameters< + config extends ConfigWithEns = ConfigWithEns, + selectData = GetEnsOwnerData, +> = Compute< + GetEnsOwnerOptions & + ConfigParameter & + QueryParameter< + GetEnsOwnerQueryFnData, + GetEnsOwnerErrorType, + selectData, + GetEnsOwnerQueryKey + > +> + +export type UseEnsOwnerReturnType = + UseQueryReturnType + +/** + * Returns the owner of a name + * + * @param parameters - {@link UseEnsOwnerParameters} + * @returns - {@link UseEnsOwnerReturnType} + */ +export const useEnsOwner = < + config extends ConfigWithEns = ResolvedRegister['config'], + selectData = GetEnsOwnerData, +>( + parameters: UseEnsOwnerParameters = {}, +): UseEnsOwnerReturnType => { + const { name, query = {} } = parameters + + const config = useConfig() + const chainId = useChainId({ config }) + + const options = getEnsOwnerQueryOptions(config, { + ...parameters, + chainId: parameters.chainId ?? chainId, + }) + const enabled = Boolean(name && (query.enabled ?? true)) + + return useQuery({ ...query, ...options, enabled }) +} diff --git a/packages/react/src/hooks/useEnsPrice.ts b/packages/react/src/hooks/useEnsPrice.ts new file mode 100644 index 00000000..5e6ce4b9 --- /dev/null +++ b/packages/react/src/hooks/useEnsPrice.ts @@ -0,0 +1,57 @@ +import { useChainId, useConfig } from 'wagmi' +import { useQuery, type UseQueryReturnType } from 'wagmi/query' +import { + getEnsPriceQueryOptions, + type GetEnsPriceData, + type GetEnsPriceErrorType, + type GetEnsPriceOptions, + type GetEnsPriceQueryFnData, + type GetEnsPriceQueryKey, +} from '../query/getEnsPrice.js' +import type { ConfigWithEns } from '../types/config.js' +import type { ConfigParameter, QueryParameter } from '../types/properties.js' +import type { ResolvedRegister } from '../types/register.js' +import type { Compute } from '../types/utils.js' + +export type UseEnsPriceParameters< + config extends ConfigWithEns = ConfigWithEns, + selectData = GetEnsPriceData, +> = Compute< + GetEnsPriceOptions & + ConfigParameter & + QueryParameter< + GetEnsPriceQueryFnData, + GetEnsPriceErrorType, + selectData, + GetEnsPriceQueryKey + > +> + +export type UseEnsPriceReturnType = + UseQueryReturnType + +/** + * Returns the price for registering or renewing a name + * + * @param parameters - {@link UseEnsPriceParameters} + * @returns - {@link UseEnsPriceReturnType} + */ +export const useEnsPrice = < + config extends ConfigWithEns = ResolvedRegister['config'], + selectData = GetEnsPriceData, +>( + parameters: UseEnsPriceParameters = {}, +): UseEnsPriceReturnType => { + const { nameOrNames, duration, query = {} } = parameters + + const config = useConfig() + const chainId = useChainId({ config }) + + const options = getEnsPriceQueryOptions(config, { + ...parameters, + chainId: parameters.chainId ?? chainId, + }) + const enabled = Boolean(nameOrNames && duration && (query.enabled ?? true)) + + return useQuery({ ...query, ...options, enabled }) +} diff --git a/packages/react/src/hooks/useEnsRecords.ts b/packages/react/src/hooks/useEnsRecords.ts new file mode 100644 index 00000000..6717b1b3 --- /dev/null +++ b/packages/react/src/hooks/useEnsRecords.ts @@ -0,0 +1,77 @@ +import { useChainId, useConfig } from 'wagmi' +import { useQuery, type UseQueryReturnType } from 'wagmi/query' +import { + getEnsRecordsQueryOptions, + type GetEnsRecordsData, + type GetEnsRecordsErrorType, + type GetEnsRecordsOptions, + type GetEnsRecordsQueryFnData, + type GetEnsRecordsQueryKey, +} from '../query/getEnsRecords.js' +import type { ConfigWithEns } from '../types/config.js' +import type { ConfigParameter, QueryParameter } from '../types/properties.js' +import type { ResolvedRegister } from '../types/register.js' +import type { Compute } from '../types/utils.js' + +export type UseEnsRecordsParameters< + config extends ConfigWithEns = ConfigWithEns, + texts extends readonly string[] | undefined = undefined, + coins extends readonly (string | number)[] | undefined = undefined, + contentHash extends boolean | undefined = undefined, + abi extends boolean | undefined = undefined, + selectData = GetEnsRecordsData, +> = Compute< + GetEnsRecordsOptions & + ConfigParameter & + QueryParameter< + GetEnsRecordsQueryFnData, + GetEnsRecordsErrorType, + selectData, + GetEnsRecordsQueryKey + > +> + +export type UseEnsRecordsReturnType< + texts extends readonly string[] | undefined = undefined, + coins extends readonly (string | number)[] | undefined = undefined, + contentHash extends boolean | undefined = undefined, + abi extends boolean | undefined = undefined, + selectData = GetEnsRecordsData, +> = UseQueryReturnType + +/** + * Returns arbitrary records for a name + * + * @param parameters - {@link UseEnsRecordsParameters} + * @returns - {@link UseEnsRecordsReturnType} + */ +export const useEnsRecords = < + config extends ConfigWithEns = ResolvedRegister['config'], + const texts extends readonly string[] | undefined = undefined, + const coins extends readonly (string | number)[] | undefined = undefined, + const contentHash extends boolean | undefined = undefined, + const abi extends boolean | undefined = undefined, + selectData = GetEnsRecordsData, +>( + parameters: UseEnsRecordsParameters< + config, + texts, + coins, + contentHash, + abi, + selectData + > = {}, +): UseEnsRecordsReturnType => { + const { name, query = {} } = parameters + + const config = useConfig() + const chainId = useChainId({ config }) + + const options = getEnsRecordsQueryOptions(config, { + ...parameters, + chainId: parameters.chainId ?? chainId, + }) + const enabled = Boolean(name && (query.enabled ?? true)) + + return useQuery({ ...query, ...options, enabled }) +} diff --git a/packages/react/src/index.ts b/packages/react/src/index.ts index 26593bd8..ebdd3096 100644 --- a/packages/react/src/index.ts +++ b/packages/react/src/index.ts @@ -1,3 +1,67 @@ -export * from './hooks.js' export { resolverInterfaces } from './interfaces.js' -export type { ParamWithClients } from './client.js' + +export { + useDecodedName, + type UseDecodedNameParameters, + type UseDecodedNameReturnType, +} from './hooks/useDecodedName.js' +export { + useEnsAbi, + type UseEnsAbiParameters, + type UseEnsAbiReturnType, +} from './hooks/useEnsAbi.js' +export { + useEnsAddress, + type UseEnsAddressParameters, + type UseEnsAddressReturnType, +} from './hooks/useEnsAddress.js' +export { + useEnsAvailable, + type UseEnsAvailableParameters, + type UseEnsAvailableReturnType, +} from './hooks/useEnsAvailable.js' +export { + useEnsContentHash, + type UseEnsContentHashParameters, + type UseEnsContentHashReturnType, +} from './hooks/useEnsContentHash.js' +export { + useEnsCredentials, + type UseEnsCredentialsParameters, + type UseEnsCredentialsReturnType, +} from './hooks/useEnsCredentials.js' +export { + useEnsExpiry, + type UseEnsExpiryParameters, + type UseEnsExpiryReturnType, +} from './hooks/useEnsExpiry.js' +export { + useEnsNamesForAddress, + type UseEnsNamesForAddressParameters, + type UseEnsNamesForAddressReturnType, +} from './hooks/useEnsNamesForAddress.js' +export { + useEnsOwner, + type UseEnsOwnerParameters, + type UseEnsOwnerReturnType, +} from './hooks/useEnsOwner.js' +export { + useEnsPrice, + type UseEnsPriceParameters, + type UseEnsPriceReturnType, +} from './hooks/useEnsPrice.js' +export { + useEnsRecords, + type UseEnsRecordsParameters, + type UseEnsRecordsReturnType, +} from './hooks/useEnsRecords.js' +export { + useEnsResolverInterfaces, + type UseEnsResolverInterfacesParameters, + type UseEnsResolverInterfacesReturnType, +} from './hooks/useEnsResolverInterfaces.js' +export { + useWriteEnsRecords, + type UseWriteEnsRecordsParameters, + type UseWriteEnsRecordsReturnType, +} from './hooks/useWriteEnsRecords.js' diff --git a/packages/react/src/query.ts b/packages/react/src/query.ts index abd7d6ba..ef109ea0 100644 --- a/packages/react/src/query.ts +++ b/packages/react/src/query.ts @@ -1,4 +1,132 @@ -import { QueryClient } from '@tanstack/react-query' - -// TODO: figure out why not taking from provider -export const fallbackQueryClient = new QueryClient() +export { + getDecodedNameQueryKey, + getDecodedNameQueryOptions, + type GetDecodedNameData, + type GetDecodedNameErrorType, + type GetDecodedNameOptions, + type GetDecodedNameParameters, + type GetDecodedNameQueryFnData, + type GetDecodedNameQueryKey, + type GetDecodedNameReturnType, +} from './query/getDecodedName.js' +export { + getEnsAbiQueryOptions, + type GetEnsAbiData, + type GetEnsAbiErrorType, + type GetEnsAbiOptions, + type GetEnsAbiParameters, + type GetEnsAbiQueryFnData, + type GetEnsAbiQueryKey, + type GetEnsAbiReturnType, +} from './query/getEnsAbi.js' +export { + getEnsAddressQueryOptions, + type GetEnsAddressData, + type GetEnsAddressErrorType, + type GetEnsAddressOptions, + type GetEnsAddressParameters, + type GetEnsAddressQueryFnData, + type GetEnsAddressQueryKey, + type GetEnsAddressReturnType, +} from './query/getEnsAddress.js' +export { + getEnsAvailableQueryOptions, + type GetEnsAvailableData, + type GetEnsAvailableErrorType, + type GetEnsAvailableOptions, + type GetEnsAvailableParameters, + type GetEnsAvailableQueryFnData, + type GetEnsAvailableQueryKey, + type GetEnsAvailableReturnType, +} from './query/getEnsAvailable.js' +export { + getEnsContentHashQueryOptions, + type GetEnsContentHashData, + type GetEnsContentHashErrorType, + type GetEnsContentHashOptions, + type GetEnsContentHashParameters, + type GetEnsContentHashQueryFnData, + type GetEnsContentHashQueryKey, + type GetEnsContentHashReturnType, +} from './query/getEnsContentHash.js' +export { + getEnsCredentialsQueryOptions, + type GetEnsCredentialsData, + type GetEnsCredentialsErrorType, + type GetEnsCredentialsOptions, + type GetEnsCredentialsParameters, + type GetEnsCredentialsQueryFnData, + type GetEnsCredentialsQueryKey, + type GetEnsCredentialsReturnType, +} from './query/getEnsCredentials.js' +export { + getEnsExpiryQueryOptions, + type GetEnsExpiryData, + type GetEnsExpiryErrorType, + type GetEnsExpiryOptions, + type GetEnsExpiryParameters, + type GetEnsExpiryQueryFnData, + type GetEnsExpiryQueryKey, + type GetEnsExpiryReturnType, +} from './query/getEnsExpiry.js' +export { + getEnsNamesForAddressQueryOptions, + type GetEnsNamesForAddressData, + type GetEnsNamesForAddressErrorType, + type GetEnsNamesForAddressOptions, + type GetEnsNamesForAddressParameters, + type GetEnsNamesForAddressQueryFnData, + type GetEnsNamesForAddressQueryKey, + type GetEnsNamesForAddressReturnType, +} from './query/getEnsNamesForAddress.js' +export { + getEnsOwnerQueryOptions, + type GetEnsOwnerData, + type GetEnsOwnerErrorType, + type GetEnsOwnerOptions, + type GetEnsOwnerParameters, + type GetEnsOwnerQueryFnData, + type GetEnsOwnerQueryKey, + type GetEnsOwnerReturnType, +} from './query/getEnsOwner.js' +export { + getEnsPriceQueryOptions, + type GetEnsPriceData, + type GetEnsPriceErrorType, + type GetEnsPriceOptions, + type GetEnsPriceParameters, + type GetEnsPriceQueryFnData, + type GetEnsPriceQueryKey, + type GetEnsPriceReturnType, +} from './query/getEnsPrice.js' +export { + getEnsRecordsQueryOptions, + type GetEnsRecordsData, + type GetEnsRecordsErrorType, + type GetEnsRecordsOptions, + type GetEnsRecordsParameters, + type GetEnsRecordsQueryFnData, + type GetEnsRecordsQueryKey, + type GetEnsRecordsReturnType, +} from './query/getEnsRecords.js' +export { + getEnsResolverInterfacesQueryOptions, + type GetEnsResolverInterfacesData, + type GetEnsResolverInterfacesErrorType, + type GetEnsResolverInterfacesOptions, + type GetEnsResolverInterfacesParameters, + type GetEnsResolverInterfacesQueryFnData, + type GetEnsResolverInterfacesQueryKey, + type GetEnsResolverInterfacesReturnType, +} from './query/getEnsResolverInterfaces.js' +export { + writeEnsRecords, + writeEnsRecordsMutationOptions, + type WriteEnsRecordsData, + type WriteEnsRecordsErrorType, + type WriteEnsRecordsMutate, + type WriteEnsRecordsMutateAsync, + type WriteEnsRecordsParameters, + type WriteEnsRecordsReturnType, + type WriteEnsRecordsVariables, +} from './query/writeEnsRecords.js' diff --git a/packages/react/src/query/getEnsAbi.ts b/packages/react/src/query/getEnsAbi.ts new file mode 100644 index 00000000..a934d5d3 --- /dev/null +++ b/packages/react/src/query/getEnsAbi.ts @@ -0,0 +1,62 @@ +import { + getAbiRecord, + type GetAbiRecordErrorType as ensjs_GetEnsAbiErrorType, + type GetAbiRecordParameters as ensjs_GetEnsAbiParameters, + type GetAbiRecordReturnType as ensjs_GetEnsAbiReturnType, +} from '@ensdomains/ensjs/public' +import type { QueryOptions } from '@tanstack/react-query' +import type { ConfigWithEns } from '../types/config.js' +import type { + ChainIdParameter, + ScopeKeyParameter, +} from '../types/properties.js' +import type { Compute, ExactPartial } from '../types/utils.js' +import { filterQueryOptions, getClientAndParameters } from './utils.js' + +export type GetEnsAbiParameters = + Compute> + +export type GetEnsAbiReturnType = ensjs_GetEnsAbiReturnType + +export type GetEnsAbiErrorType = ensjs_GetEnsAbiErrorType + +export type GetEnsAbiOptions = + Compute> & ScopeKeyParameter> + +export type GetEnsAbiQueryFnData = GetEnsAbiReturnType + +export type GetEnsAbiData = GetEnsAbiReturnType + +export const getEnsAbiQueryKey = ( + options: GetEnsAbiOptions, +) => { + return ['getEnsAbi', filterQueryOptions(options)] as const +} + +export type GetEnsAbiQueryKey = ReturnType< + typeof getEnsAbiQueryKey +> + +export const getEnsAbiQueryOptions = ( + config: config, + options: GetEnsAbiOptions = {}, +) => { + return { + async queryFn({ queryKey }) { + const { name, ...rest } = queryKey[1] + if (!name) throw new Error('name is required') + + const { client, parameters } = getClientAndParameters(config, { + ...rest, + name, + }) + return getAbiRecord(client, parameters) + }, + queryKey: getEnsAbiQueryKey(options), + } as const satisfies QueryOptions< + GetEnsAbiQueryFnData, + GetEnsAbiErrorType, + GetEnsAbiData, + GetEnsAbiQueryKey + > +} diff --git a/packages/react/src/query/getEnsAddress.ts b/packages/react/src/query/getEnsAddress.ts new file mode 100644 index 00000000..20bea9d7 --- /dev/null +++ b/packages/react/src/query/getEnsAddress.ts @@ -0,0 +1,63 @@ +import { + getAddressRecord, + type GetAddressRecordErrorType as ensjs_GetEnsAddressErrorType, + type GetAddressRecordParameters as ensjs_GetEnsAddressParameters, + type GetAddressRecordReturnType as ensjs_GetEnsAddressReturnType, +} from '@ensdomains/ensjs/public' +import type { QueryOptions } from '@tanstack/react-query' +import type { ConfigWithEns } from '../types/config.js' +import type { + ChainIdParameter, + ScopeKeyParameter, +} from '../types/properties.js' +import type { Compute, ExactPartial } from '../types/utils.js' +import { filterQueryOptions, getClientAndParameters } from './utils.js' + +export type GetEnsAddressParameters< + config extends ConfigWithEns = ConfigWithEns, +> = Compute> + +export type GetEnsAddressReturnType = ensjs_GetEnsAddressReturnType + +export type GetEnsAddressErrorType = ensjs_GetEnsAddressErrorType + +export type GetEnsAddressOptions = + Compute> & ScopeKeyParameter> + +export type GetEnsAddressQueryFnData = GetEnsAddressReturnType + +export type GetEnsAddressData = GetEnsAddressReturnType + +export const getEnsAddressQueryKey = ( + options: GetEnsAddressOptions, +) => { + return ['getEnsAddress', filterQueryOptions(options)] as const +} + +export type GetEnsAddressQueryKey = ReturnType< + typeof getEnsAddressQueryKey +> + +export const getEnsAddressQueryOptions = ( + config: config, + options: GetEnsAddressOptions = {}, +) => { + return { + async queryFn({ queryKey }) { + const { name, ...rest } = queryKey[1] + if (!name) throw new Error('name is required') + + const { client, parameters } = getClientAndParameters(config, { + ...rest, + name, + }) + return getAddressRecord(client, parameters) + }, + queryKey: getEnsAddressQueryKey(options), + } as const satisfies QueryOptions< + GetEnsAddressQueryFnData, + GetEnsAddressErrorType, + GetEnsAddressData, + GetEnsAddressQueryKey + > +} diff --git a/packages/react/src/query/getEnsContentHash.ts b/packages/react/src/query/getEnsContentHash.ts new file mode 100644 index 00000000..f5a643a8 --- /dev/null +++ b/packages/react/src/query/getEnsContentHash.ts @@ -0,0 +1,65 @@ +import { + getContentHashRecord, + type GetContentHashRecordErrorType as ensjs_GetEnsContentHashErrorType, + type GetContentHashRecordParameters as ensjs_GetEnsContentHashParameters, + type GetContentHashRecordReturnType as ensjs_GetEnsContentHashReturnType, +} from '@ensdomains/ensjs/public' +import type { QueryOptions } from '@tanstack/react-query' +import type { ConfigWithEns } from '../types/config.js' +import type { + ChainIdParameter, + ScopeKeyParameter, +} from '../types/properties.js' +import type { Compute, ExactPartial } from '../types/utils.js' +import { filterQueryOptions, getClientAndParameters } from './utils.js' + +export type GetEnsContentHashParameters< + config extends ConfigWithEns = ConfigWithEns, +> = Compute> + +export type GetEnsContentHashReturnType = ensjs_GetEnsContentHashReturnType + +export type GetEnsContentHashErrorType = ensjs_GetEnsContentHashErrorType + +export type GetEnsContentHashOptions< + config extends ConfigWithEns = ConfigWithEns, +> = Compute< + ExactPartial> & ScopeKeyParameter +> + +export type GetEnsContentHashQueryFnData = GetEnsContentHashReturnType + +export type GetEnsContentHashData = GetEnsContentHashReturnType + +export const getEnsContentHashQueryKey = ( + options: GetEnsContentHashOptions, +) => { + return ['getEnsContentHash', filterQueryOptions(options)] as const +} + +export type GetEnsContentHashQueryKey = + ReturnType> + +export const getEnsContentHashQueryOptions = ( + config: config, + options: GetEnsContentHashOptions = {}, +) => { + return { + async queryFn({ queryKey }) { + const { name, ...rest } = queryKey[1] + if (!name) throw new Error('name is required') + + const { client, parameters } = getClientAndParameters(config, { + ...rest, + name, + }) + return getContentHashRecord(client, parameters) + }, + queryKey: getEnsContentHashQueryKey(options), + } as const satisfies QueryOptions< + GetEnsContentHashQueryFnData, + GetEnsContentHashErrorType, + GetEnsContentHashData, + GetEnsContentHashQueryKey + > +} diff --git a/packages/react/src/query/getEnsOwner.ts b/packages/react/src/query/getEnsOwner.ts new file mode 100644 index 00000000..b7f1e5bf --- /dev/null +++ b/packages/react/src/query/getEnsOwner.ts @@ -0,0 +1,63 @@ +import { + getOwner, + type GetOwnerErrorType as ensjs_GetEnsOwnerErrorType, + type GetOwnerParameters as ensjs_GetEnsOwnerParameters, + type GetOwnerReturnType as ensjs_GetEnsOwnerReturnType, +} from '@ensdomains/ensjs/public' +import type { QueryOptions } from '@tanstack/react-query' +import type { ConfigWithEns } from '../types/config.js' +import type { + ChainIdParameter, + ScopeKeyParameter, +} from '../types/properties.js' +import type { Compute, ExactPartial } from '../types/utils.js' +import { filterQueryOptions, getClientAndParameters } from './utils.js' + +export type GetEnsOwnerParameters< + config extends ConfigWithEns = ConfigWithEns, +> = Compute> + +export type GetEnsOwnerReturnType = ensjs_GetEnsOwnerReturnType + +export type GetEnsOwnerErrorType = ensjs_GetEnsOwnerErrorType + +export type GetEnsOwnerOptions = + Compute> & ScopeKeyParameter> + +export type GetEnsOwnerQueryFnData = GetEnsOwnerReturnType + +export type GetEnsOwnerData = GetEnsOwnerReturnType + +export const getEnsOwnerQueryKey = ( + options: GetEnsOwnerOptions, +) => { + return ['getEnsOwner', filterQueryOptions(options)] as const +} + +export type GetEnsOwnerQueryKey = ReturnType< + typeof getEnsOwnerQueryKey +> + +export const getEnsOwnerQueryOptions = ( + config: config, + options: GetEnsOwnerOptions = {}, +) => { + return { + async queryFn({ queryKey }) { + const { name, ...rest } = queryKey[1] + if (!name) throw new Error('name is required') + + const { client, parameters } = getClientAndParameters(config, { + ...rest, + name, + }) + return getOwner(client, parameters) + }, + queryKey: getEnsOwnerQueryKey(options), + } as const satisfies QueryOptions< + GetEnsOwnerQueryFnData, + GetEnsOwnerErrorType, + GetEnsOwnerData, + GetEnsOwnerQueryKey + > +} diff --git a/packages/react/src/query/getEnsPrice.ts b/packages/react/src/query/getEnsPrice.ts new file mode 100644 index 00000000..5dbb13e8 --- /dev/null +++ b/packages/react/src/query/getEnsPrice.ts @@ -0,0 +1,65 @@ +import { + getPrice, + type GetPriceErrorType as ensjs_GetEnsPriceErrorType, + type GetPriceParameters as ensjs_GetEnsPriceParameters, + type GetPriceReturnType as ensjs_GetEnsPriceReturnType, +} from '@ensdomains/ensjs/public' +import type { QueryOptions } from '@tanstack/react-query' +import type { ConfigWithEns } from '../types/config.js' +import type { + ChainIdParameter, + ScopeKeyParameter, +} from '../types/properties.js' +import type { Compute, ExactPartial } from '../types/utils.js' +import { filterQueryOptions, getClientAndParameters } from './utils.js' + +export type GetEnsPriceParameters< + config extends ConfigWithEns = ConfigWithEns, +> = Compute> + +export type GetEnsPriceReturnType = ensjs_GetEnsPriceReturnType + +export type GetEnsPriceErrorType = ensjs_GetEnsPriceErrorType + +export type GetEnsPriceOptions = + Compute> & ScopeKeyParameter> + +export type GetEnsPriceQueryFnData = GetEnsPriceReturnType + +export type GetEnsPriceData = GetEnsPriceReturnType + +export const getEnsPriceQueryKey = ( + options: GetEnsPriceOptions, +) => { + return ['getEnsPrice', filterQueryOptions(options)] as const +} + +export type GetEnsPriceQueryKey = ReturnType< + typeof getEnsPriceQueryKey +> + +export const getEnsPriceQueryOptions = ( + config: config, + options: GetEnsPriceOptions = {}, +) => { + return { + async queryFn({ queryKey }) { + const { nameOrNames, duration, ...rest } = queryKey[1] + if (!nameOrNames) throw new Error('nameOrNames is required') + if (!duration) throw new Error('duration is required') + + const { client, parameters } = getClientAndParameters(config, { + ...rest, + nameOrNames, + duration, + }) + return getPrice(client, parameters) + }, + queryKey: getEnsPriceQueryKey(options), + } as const satisfies QueryOptions< + GetEnsPriceQueryFnData, + GetEnsPriceErrorType, + GetEnsPriceData, + GetEnsPriceQueryKey + > +} diff --git a/packages/react/src/query/getEnsRecords.ts b/packages/react/src/query/getEnsRecords.ts new file mode 100644 index 00000000..6adf8901 --- /dev/null +++ b/packages/react/src/query/getEnsRecords.ts @@ -0,0 +1,113 @@ +import { + getRecords, + type GetRecordsErrorType as ensjs_GetEnsRecordsErrorType, + type GetRecordsParameters as ensjs_GetEnsRecordsParameters, + type GetRecordsReturnType as ensjs_GetEnsRecordsReturnType, +} from '@ensdomains/ensjs/public' +import type { QueryOptions } from '@tanstack/react-query' +import type { ConfigWithEns } from '../types/config.js' +import type { + ChainIdParameter, + ScopeKeyParameter, +} from '../types/properties.js' +import type { Compute, ExactPartial } from '../types/utils.js' +import { filterQueryOptions, getClientAndParameters } from './utils.js' + +export type GetEnsRecordsParameters< + config extends ConfigWithEns = ConfigWithEns, + texts extends readonly string[] | undefined = undefined, + coins extends readonly (string | number)[] | undefined = undefined, + contentHash extends boolean | undefined = undefined, + abi extends boolean | undefined = undefined, +> = Compute< + ensjs_GetEnsRecordsParameters & + ChainIdParameter +> + +export type GetEnsRecordsReturnType< + texts extends readonly string[] | undefined = undefined, + coins extends readonly (string | number)[] | undefined = undefined, + contentHash extends boolean | undefined = undefined, + abi extends boolean | undefined = undefined, +> = ensjs_GetEnsRecordsReturnType + +export type GetEnsRecordsErrorType = ensjs_GetEnsRecordsErrorType + +export type GetEnsRecordsOptions< + config extends ConfigWithEns = ConfigWithEns, + texts extends readonly string[] | undefined = undefined, + coins extends readonly (string | number)[] | undefined = undefined, + contentHash extends boolean | undefined = undefined, + abi extends boolean | undefined = undefined, +> = Compute< + ExactPartial< + GetEnsRecordsParameters + > & + ScopeKeyParameter +> + +export type GetEnsRecordsQueryFnData< + texts extends readonly string[] | undefined = undefined, + coins extends readonly (string | number)[] | undefined = undefined, + contentHash extends boolean | undefined = undefined, + abi extends boolean | undefined = undefined, +> = GetEnsRecordsReturnType + +export type GetEnsRecordsData< + texts extends readonly string[] | undefined = undefined, + coins extends readonly (string | number)[] | undefined = undefined, + contentHash extends boolean | undefined = undefined, + abi extends boolean | undefined = undefined, +> = GetEnsRecordsReturnType + +export const getEnsRecordsQueryKey = < + config extends ConfigWithEns, + texts extends readonly string[] | undefined, + coins extends readonly (string | number)[] | undefined, + contentHash extends boolean | undefined, + abi extends boolean | undefined, +>( + options: GetEnsRecordsOptions, +) => { + return ['getEnsRecords', filterQueryOptions(options)] as const +} + +export type GetEnsRecordsQueryKey< + config extends ConfigWithEns, + texts extends readonly string[] | undefined, + coins extends readonly (string | number)[] | undefined, + contentHash extends boolean | undefined, + abi extends boolean | undefined, +> = ReturnType< + typeof getEnsRecordsQueryKey +> + +export const getEnsRecordsQueryOptions = < + config extends ConfigWithEns, + const texts extends readonly string[] | undefined = undefined, + const coins extends readonly (string | number)[] | undefined = undefined, + const contentHash extends boolean | undefined = undefined, + const abi extends boolean | undefined = undefined, +>( + config: config, + options: GetEnsRecordsOptions = {}, +) => { + return { + async queryFn({ queryKey }) { + const { name, ...rest } = queryKey[1] + if (!name) throw new Error('name is required') + + const { client, parameters } = getClientAndParameters(config, { + ...rest, + name, + }) + return getRecords(client, parameters) as any + }, + queryKey: getEnsRecordsQueryKey(options), + } as const satisfies QueryOptions< + GetEnsRecordsQueryFnData, + GetEnsRecordsErrorType, + GetEnsRecordsData, + GetEnsRecordsQueryKey + > +}