@ensnode/ensnode-react 0.0.0-next-20260102143513

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 NameHash
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,339 @@
1
+ # @ensnode/ensnode-react
2
+
3
+ React hooks and providers for the ENSNode API. This package provides a React-friendly interface to the ENSNode SDK with automatic caching, loading states, and error handling. **TanStack Query is handled automatically** - no setup required unless you want custom configuration.
4
+
5
+ Learn more about [ENSNode](https://ensnode.io/) from [the ENSNode docs](https://ensnode.io/docs/).
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @ensnode/ensnode-react @ensnode/ensnode-sdk
11
+ ```
12
+
13
+ Note: `@tanstack/react-query` is a peer dependency but you don't need to interact with it directly unless you want advanced query customization.
14
+
15
+ ## Quick Start
16
+
17
+ ### 1. Setup the Provider
18
+
19
+ Wrap your app with the `ENSNodeProvider`:
20
+
21
+ ```tsx
22
+ import { ENSNodeProvider, createConfig } from "@ensnode/ensnode-react";
23
+
24
+ const config = createConfig({ url: "https://api.alpha.ensnode.io" });
25
+
26
+ function App() {
27
+ return (
28
+ <ENSNodeProvider config={config}>
29
+ <YourApp />
30
+ </ENSNodeProvider>
31
+ );
32
+ }
33
+ ```
34
+
35
+ That's it! No need to wrap with `QueryClientProvider` or create a `QueryClient` - it's all handled automatically. Each ENSNode endpoint gets its own isolated cache for proper data separation.
36
+
37
+ ### 2. Use the Hooks
38
+
39
+ #### Records Resolution — `useRecords`
40
+
41
+ ```tsx
42
+ import { useRecords } from "@ensnode/ensnode-react";
43
+
44
+ function DisplayNameRecords() {
45
+ const { data, isLoading, error } = useRecords({
46
+ name: "vitalik.eth",
47
+ selection: {
48
+ addresses: [60], // ETH CoinType
49
+ texts: ["avatar", "com.twitter"],
50
+ },
51
+ });
52
+
53
+ if (isLoading) return <div>Loading...</div>;
54
+ if (error) return <div>Error: {error.message}</div>;
55
+
56
+ return (
57
+ <div>
58
+ <h3>Resolved Records for vitalik.eth</h3>
59
+ {data.records.addresses && (
60
+ <p>ETH Address: {data.records.addresses["60"]}</p>
61
+ )}
62
+ {data.records.texts && (
63
+ <div>
64
+ <p>Avatar: {data.records.texts.avatar}</p>
65
+ <p>Twitter: {data.records.texts["com.twitter"]}</p>
66
+ </div>
67
+ )}
68
+ </div>
69
+ );
70
+ }
71
+ ```
72
+
73
+ #### Primary Name Resolution — `usePrimaryName`
74
+
75
+ ```tsx
76
+ import { mainnet } from 'viem/chains';
77
+ import { usePrimaryName } from "@ensnode/ensnode-react";
78
+
79
+ function DisplayPrimaryName() {
80
+ const { data, isLoading, error } = usePrimaryName({
81
+ address: "0x179A862703a4adfb29896552DF9e307980D19285",
82
+ chainId: mainnet.id,
83
+ accelerate: true, // Attempt Protocol Acceleration
84
+ });
85
+
86
+ if (isLoading) return <div>Loading...</div>;
87
+ if (error) return <div>Error: {error.message}</div>;
88
+
89
+ return (
90
+ <div>
91
+ <h3>Primary Name (for Mainnet)</h3>
92
+ <p>{data.name ?? 'No Primary Name'}</p>
93
+ </div>
94
+ );
95
+ }
96
+ ```
97
+
98
+ #### Primary Names Resolution — `usePrimaryNames`
99
+
100
+ ```tsx
101
+ import { mainnet } from 'viem/chains';
102
+ import { usePrimaryNames } from "@ensnode/ensnode-react";
103
+
104
+ function DisplayPrimaryNames() {
105
+ const { data, isLoading, error } = usePrimaryNames({
106
+ address: "0x179A862703a4adfb29896552DF9e307980D19285",
107
+ });
108
+
109
+ if (isLoading) return <div>Loading...</div>;
110
+ if (error) return <div>Error: {error.message}</div>;
111
+
112
+ return (
113
+ <div>
114
+ {Object.entries(data.names).map(([chainId, name]) => (
115
+ <div key={chainId}>
116
+ <h3>Primary Name (Chain Id: {chainId})</h3>
117
+ <p>{name}</p>
118
+ </div>
119
+ ))}
120
+ </div>
121
+ );
122
+ }
123
+ ```
124
+
125
+ ## API Reference
126
+
127
+ ### ENSNodeProvider
128
+
129
+ The provider component that supplies ENSNode configuration to all child components.
130
+
131
+ ```tsx
132
+ interface ENSNodeProviderProps {
133
+ config: ENSNodeConfig;
134
+ queryClient?: QueryClient;
135
+ queryClientOptions?: QueryClientOptions;
136
+ }
137
+ ```
138
+
139
+ #### Props
140
+
141
+ - `config`: ENSNode configuration object
142
+ - `queryClient`: Optional TanStack Query client instance (requires manual QueryClientProvider setup)
143
+ - `queryClientOptions`: Optional Custom options for auto-created QueryClient (only used when queryClient is not provided)
144
+
145
+ ### createConfig
146
+
147
+ Helper function to create ENSNode configuration with defaults.
148
+
149
+ ```tsx
150
+ const config = createConfig({
151
+ url: "https://api.alpha.ensnode.io",
152
+ });
153
+ ```
154
+
155
+ ### `useRecords`
156
+
157
+ Hook that resolves records for an ENS name (Forward Resolution), via ENSNode, which implements Protocol Acceleration for indexed names.
158
+
159
+ The returned `name` field, if set, is guaranteed to be a normalized name. If the name record returned by the resolver is not normalized, `null` is returned as if no name record was set.
160
+
161
+ #### Parameters
162
+
163
+ - `name`: The ENS Name whose records to resolve
164
+ - `selection`: Selection of Resolver records to resolve
165
+ - `addresses`: Array of coin types to resolve addresses for
166
+ - `texts`: Array of text record keys to resolve
167
+ - `trace`: (optional) Whether to include a trace in the response (default: false)
168
+ - `accelerate`: (optional) Whether to attempt Protocol Acceleration (default: false)
169
+ - `query`: (optional) TanStack Query options for customization
170
+
171
+ #### Example
172
+
173
+ ```tsx
174
+ const { data, isLoading, error, refetch } = useRecords({
175
+ name: "example.eth",
176
+ selection: {
177
+ addresses: [60], // ETH
178
+ texts: ["avatar", "description", "url"],
179
+ },
180
+ });
181
+ ```
182
+
183
+ ### `usePrimaryName`
184
+
185
+ Hook that resolves the primary name of the provided `address` on the specified `chainId`, via ENSNode, which implements Protocol Acceleration for indexed names. If the chainId-specific Primary Name is not defined, but the `address` specifies a valid [ENSIP-19 Default Name](https://docs.ens.domains/ensip/19/#default-primary-name), the Default Name will be returned. You _may_ query the Default EVM Chain Id (`0`) in order to determine the `address`'s Default Name directly.
186
+
187
+ The returned Primary Name, if set, is guaranteed to be a [Normalized Name](https://ensnode.io/docs/reference/terminology#normalized-name). If the primary name set for the address is not normalized, `null` is returned as if no primary name was set.
188
+
189
+ #### Parameters
190
+
191
+ - `address`: The Address whose Primary Name to resolve
192
+ - `chainId`: The chain id within which to query the address' ENSIP-19 Multichain Primary Name
193
+ - `trace`: (optional) Whether to include a trace in the response (default: false)
194
+ - `accelerate`: (optional) Whether to attempt Protocol Acceleration (default: false)
195
+ - `query`: (optional) TanStack Query options for customization
196
+
197
+ #### Example
198
+
199
+ ```tsx
200
+ const { data, isLoading, error, refetch } = usePrimaryName({
201
+ address: "0x179A862703a4adfb29896552DF9e307980D19285",
202
+ chainId: 10, // Optimism
203
+ accelerate: true, // Attempt Protocol Acceleration
204
+ });
205
+ ```
206
+
207
+ ### `usePrimaryNames`
208
+
209
+ Hook that resolves the primary names of the provided `address` on the specified chainIds, via ENSNode, which implements Protocol Acceleration for indexed names. For each Primary Name, if the chainId-specific Primary Name is not defined, but the `address` specifies a valid [ENSIP-19 Default Name](https://docs.ens.domains/ensip/19/#default-primary-name), the Default Name will be returned. You _may not_ query the Default EVM Chain Id (`0`) directly, and should rely on the aforementioned per-chain defaulting behavior.
210
+
211
+ Each returned Primary Name, if set, is guaranteed to be a [Normalized Name](https://ensnode.io/docs/reference/terminology#normalized-name). If the primary name set for the address on any chain is not normalized, `null` is returned for that chain as if no primary name was set.
212
+
213
+ #### Parameters
214
+
215
+ - `address`: The Address whose Primary Names to resolve
216
+ - `chainIds`: (optional) Array of chain ids to query the address' ENSIP-19 Multichain Primary Names (default: all ENSIP-19 supported chains)
217
+ - `trace`: (optional) Whether to include a trace in the response (default: false)
218
+ - `accelerate`: (optional) Whether to attempt Protocol Acceleration (default: false)
219
+ - `query`: (optional) TanStack Query options for customization
220
+
221
+ #### Example
222
+
223
+ ```tsx
224
+ const { data, isLoading, error, refetch } = usePrimaryNames({
225
+ address: "0x179A862703a4adfb29896552DF9e307980D19285",
226
+ });
227
+ ```
228
+
229
+ ## Advanced Usage
230
+
231
+ ### Custom Query Configuration
232
+
233
+ The `ENSNodeProvider` automatically creates and manages a QueryClient for you. Cache keys include the ENSNode endpoint URL, so different endpoints (mainnet vs testnet) maintain separate caches. You can customize the QueryClient without importing TanStack Query:
234
+
235
+ ```tsx
236
+ // Simple setup - no TanStack Query knowledge needed
237
+ <ENSNodeProvider
238
+ config={config}
239
+ queryClientOptions={{
240
+ defaultOptions: {
241
+ queries: {
242
+ staleTime: 1000 * 60 * 10, // 10 minutes
243
+ gcTime: 1000 * 60 * 60, // 1 hour
244
+ retry: 5,
245
+ },
246
+ },
247
+ }}
248
+ >
249
+ <App />
250
+ </ENSNodeProvider>
251
+ ```
252
+
253
+ ### Advanced: Bring Your Own QueryClient
254
+
255
+ If you need full control over TanStack Query, you can provide your own `QueryClient`:
256
+
257
+ ```tsx
258
+ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
259
+
260
+ const queryClient = new QueryClient({
261
+ defaultOptions: {
262
+ queries: {
263
+ staleTime: 1000 * 60 * 10, // 10 minutes
264
+ gcTime: 1000 * 60 * 60, // 1 hour
265
+ retry: 5,
266
+ },
267
+ },
268
+ });
269
+
270
+ <QueryClientProvider client={queryClient}>
271
+ <ENSNodeProvider config={config} queryClient={queryClient}>
272
+ <App />
273
+ </ENSNodeProvider>
274
+ </QueryClientProvider>;
275
+ ```
276
+
277
+ TanStack Query v5+ is used internally. Hook return types are TanStack Query's `UseQueryResult` for full compatibility, but you don't need to interact with TanStack Query directly unless you want advanced customization.
278
+
279
+ ### Conditional Queries
280
+
281
+ Queries only execute if all required variables are provided:
282
+
283
+ ```tsx
284
+ const [address, setAddress] = useState("");
285
+
286
+ // only executes when address is not null
287
+ const { data } = usePrimaryName({
288
+ address: address || null,
289
+ chainId: 1,
290
+ accelerate: true, // Attempt Protocol Acceleration
291
+ });
292
+ ```
293
+
294
+ You can also conditionally enable/disable queries based on your own logic:
295
+
296
+ ```tsx
297
+ const [showPrimaryName, setShowPrimaryName] = useState(false);
298
+
299
+ // will not execute until `showPrimaryName` is true
300
+ const { data } = usePrimaryName({
301
+ address: "0x179A862703a4adfb29896552DF9e307980D19285",
302
+ chainId: 1,
303
+ accelerate: true, // Attempt Protocol Acceleration
304
+ query: { enabled: showPrimaryName },
305
+ });
306
+ ```
307
+
308
+ ### Multichain Reverse Resolution
309
+
310
+ ENS supports [Multichain Primary Names](https://docs.ens.domains/ensip/19/), and ENSNode supports
311
+ resolving the Primary Name of an address within the context of a specific `chainId`.
312
+
313
+ ```tsx
314
+ function ShowMultichainPrimaryNames({ address }: { address: Address }) {
315
+ const mainnet = usePrimaryName({ address, chainId: 1 });
316
+ const optimism = usePrimaryName({ address, chainId: 10 });
317
+ const polygon = usePrimaryName({ address, chainId: 137 });
318
+
319
+ return (
320
+ <div>
321
+ <div>Mainnet: {mainnet.data?.records.name || "None"}</div>
322
+ <div>Optimism: {optimism.data?.records.name || "None"}</div>
323
+ <div>Polygon: {polygon.data?.records.name || "None"}</div>
324
+ </div>
325
+ );
326
+ }
327
+ ```
328
+
329
+ ### Error Handling
330
+
331
+ Use the `error` and `isError` result parameters to handle error states in your application.
332
+
333
+ ```tsx
334
+ const { data, error, isError } = useRecords({ name: "vitalik.eth" });
335
+
336
+ if (isError) {
337
+ return <div>Failed to resolve: {error.message}</div>;
338
+ }
339
+ ```