@ensdomains/ensjs 3.0.0-alpha.62 → 3.0.0-alpha.64

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.
Files changed (42) hide show
  1. package/README.md +0 -16
  2. package/dist/cjs/GqlManager.js +4 -0
  3. package/dist/cjs/functions/batch.js +23 -1
  4. package/dist/cjs/functions/getHistory.js +11 -9
  5. package/dist/cjs/functions/getNames.js +10 -2
  6. package/dist/cjs/functions/getOwner.js +64 -17
  7. package/dist/cjs/functions/getProfile.js +66 -26
  8. package/dist/cjs/functions/getSubnames.js +11 -2
  9. package/dist/cjs/functions/supportsTLD.js +1 -1
  10. package/dist/cjs/utils/errors.js +67 -0
  11. package/dist/esm/GqlManager.mjs +4 -0
  12. package/dist/esm/functions/batch.mjs +23 -1
  13. package/dist/esm/functions/getHistory.mjs +14 -9
  14. package/dist/esm/functions/getNames.mjs +14 -2
  15. package/dist/esm/functions/getOwner.mjs +68 -17
  16. package/dist/esm/functions/getProfile.mjs +70 -26
  17. package/dist/esm/functions/getSubnames.mjs +15 -2
  18. package/dist/esm/functions/supportsTLD.mjs +1 -1
  19. package/dist/esm/utils/errors.mjs +49 -0
  20. package/dist/types/functions/getHistory.d.ts +19 -0
  21. package/dist/types/functions/getOwner.d.ts +7 -3
  22. package/dist/types/functions/getProfile.d.ts +1 -0
  23. package/dist/types/functions/getSubnames.d.ts +5 -4
  24. package/dist/types/index.d.ts +9 -7
  25. package/dist/types/utils/errors.d.ts +14 -0
  26. package/package.json +1 -1
  27. package/src/GqlManager.test.ts +17 -0
  28. package/src/GqlManager.ts +7 -0
  29. package/src/functions/batch.test.ts +41 -0
  30. package/src/functions/batch.ts +31 -1
  31. package/src/functions/getHistory.test.ts +25 -0
  32. package/src/functions/getHistory.ts +28 -11
  33. package/src/functions/getNames.test.ts +28 -0
  34. package/src/functions/getNames.ts +19 -2
  35. package/src/functions/getOwner.test.ts +161 -2
  36. package/src/functions/getOwner.ts +90 -18
  37. package/src/functions/getProfile.test.ts +129 -2
  38. package/src/functions/getProfile.ts +89 -32
  39. package/src/functions/getSubnames.test.ts +35 -0
  40. package/src/functions/getSubnames.ts +25 -3
  41. package/src/functions/supportsTLD.ts +1 -1
  42. package/src/utils/errors.ts +68 -0
@@ -1,4 +1,9 @@
1
1
  import { ENSArgs } from '..'
2
+ import {
3
+ ENSJSError,
4
+ getClientErrors,
5
+ debugSubgraphLatency,
6
+ } from '../utils/errors'
2
7
  import { truncateFormat } from '../utils/format'
3
8
  import { AllCurrentFuses, checkPCCBurned, decodeFuses } from '../utils/fuses'
4
9
  import { decryptName } from '../utils/labels'
@@ -31,6 +36,11 @@ type WrappedSubname = BaseSubname & {
31
36
 
32
37
  type Subname = WrappedSubname | UnwrappedSubname
33
38
 
39
+ type ReturnData = {
40
+ subnames: Subname[]
41
+ subnameCount: number
42
+ }
43
+
34
44
  type Params = {
35
45
  name: string
36
46
  page?: number
@@ -127,8 +137,20 @@ const largeQuery = async (
127
137
  orderDirection,
128
138
  search: search?.toLowerCase(),
129
139
  }
130
- const response = await client.request(finalQuery, queryVars)
131
- const domain = response?.domain
140
+ const response = await client
141
+ .request(finalQuery, queryVars)
142
+ .catch((e) => {
143
+ throw new ENSJSError({
144
+ data: {
145
+ subnames: [],
146
+ subnameCount: 0,
147
+ },
148
+ errors: getClientErrors(e),
149
+ })
150
+ })
151
+ .finally(debugSubgraphLatency)
152
+
153
+ const domain = response?.domain || { subdomains: [], subdomainCount: 0 }
132
154
  const subdomains = domain.subdomains.map(
133
155
  ({ wrappedDomain, ...subname }: Domain) => {
134
156
  const decrypted = decryptName(subname.name!)
@@ -174,7 +196,7 @@ const largeQuery = async (
174
196
  const getSubnames = (
175
197
  injected: ENSArgs<'gqlInstance'>,
176
198
  functionArgs: Params,
177
- ): Promise<{ subnames: Subname[]; subnameCount: number }> => {
199
+ ): Promise<ReturnData> => {
178
200
  return largeQuery(injected, functionArgs)
179
201
  }
180
202
 
@@ -17,7 +17,7 @@ export default async function (
17
17
 
18
18
  if (tld === 'eth') return true
19
19
 
20
- const tldOwner = await getOwner(tld, 'registry')
20
+ const tldOwner = await getOwner(tld, { contract: 'registry' })
21
21
  if (!tldOwner?.owner) return false
22
22
 
23
23
  const dnsRegistrar = DNSRegistrar__factory.connect(
@@ -0,0 +1,68 @@
1
+ import { ClientError } from 'graphql-request'
2
+ import { GraphQLError } from 'graphql'
3
+
4
+ export type ENSJSErrorName = 'ENSJSSubgraphError'
5
+
6
+ export class ENSJSError<T> extends Error {
7
+ name = 'ENSJSSubgraphError'
8
+
9
+ errors?: GraphQLError[]
10
+
11
+ data?: T
12
+
13
+ constructor({ data, errors }: { data?: T; errors?: GraphQLError[] }) {
14
+ super()
15
+ this.data = data
16
+ this.errors = errors
17
+ }
18
+ }
19
+
20
+ export const getClientErrors = (e: unknown) => {
21
+ const error = e instanceof ClientError ? (e as ClientError) : undefined
22
+ return error?.response?.errors || [new GraphQLError('unknown_error')]
23
+ }
24
+
25
+ const isDebugEnvironmentActive = () => {
26
+ return (
27
+ process.env.NODE_ENV === 'development' ||
28
+ process.env.NEXT_PUBLIC_ENSJS_DEBUG === 'on'
29
+ )
30
+ }
31
+
32
+ export const debugSubgraphError = (request: any) => {
33
+ if (
34
+ isDebugEnvironmentActive() &&
35
+ typeof localStorage !== 'undefined' &&
36
+ localStorage.getItem('ensjs-debug') === 'ENSJSSubgraphError'
37
+ ) {
38
+ // If we are testing indexing errors, we will allow _meta queries
39
+ // to pass through.
40
+ if (
41
+ localStorage.getItem('subgraph-debug') === 'ENSJSSubgraphIndexingError' &&
42
+ /_meta {/.test(request.body)
43
+ )
44
+ return
45
+ throw new ClientError(
46
+ {
47
+ data: undefined,
48
+ errors: [new GraphQLError('ensjs-debug')],
49
+ status: 200,
50
+ },
51
+ request,
52
+ )
53
+ }
54
+ }
55
+
56
+ export const debugSubgraphLatency = (): Promise<void> | undefined => {
57
+ if (
58
+ isDebugEnvironmentActive() &&
59
+ typeof localStorage !== 'undefined' &&
60
+ localStorage.getItem('ensjs-debug') === 'ENSJSSubgraphLatency'
61
+ ) {
62
+ return new Promise((resolve) => {
63
+ setTimeout(() => {
64
+ resolve()
65
+ }, 10000)
66
+ })
67
+ }
68
+ }