From 9e63625a12c9f422cf60638f42bd1ebf18b97eb7 Mon Sep 17 00:00:00 2001 From: tate Date: Wed, 13 Mar 2024 18:42:58 +1100 Subject: [PATCH] add searchType to getNamesForAddress --- .../subgraph/getNamesForAddress.test.ts | 20 +++++++++++++++++++ .../functions/subgraph/getNamesForAddress.ts | 12 ++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/ensjs/src/functions/subgraph/getNamesForAddress.test.ts b/packages/ensjs/src/functions/subgraph/getNamesForAddress.test.ts index ba29049e..4d03ea42 100644 --- a/packages/ensjs/src/functions/subgraph/getNamesForAddress.test.ts +++ b/packages/ensjs/src/functions/subgraph/getNamesForAddress.test.ts @@ -181,6 +181,26 @@ describe('filter', () => { expect(name.labelName).toContain('test123') } }) + it.only('filters by search string - name', async () => { + const result = await getNamesForAddress(publicClient, { + address: accounts[2], + pageSize: 1000, + filter: { + owner: true, + registrant: true, + resolvedAddress: true, + wrappedOwner: true, + searchString: 'wrapped-with-subnames', + searchType: 'name', + }, + }) + + if (!result.length) throw new Error('No names found') + const subnames = result.filter( + (x) => x.parentName === 'wrapped-with-subnames.eth', + ) + expect(subnames.length).toBeGreaterThan(0) + }) }) describe.each([ diff --git a/packages/ensjs/src/functions/subgraph/getNamesForAddress.ts b/packages/ensjs/src/functions/subgraph/getNamesForAddress.ts index 23fd4645..3054e2a3 100644 --- a/packages/ensjs/src/functions/subgraph/getNamesForAddress.ts +++ b/packages/ensjs/src/functions/subgraph/getNamesForAddress.ts @@ -43,8 +43,10 @@ type GetNamesForAddressRelation = { } type GetNamesForAddressFilter = GetNamesForAddressRelation & { - /** Search string filter for subname label */ + /** Search string filter for name */ searchString?: string + /** Search string filter type (default: `labelName`) */ + searchType?: 'labelName' | 'name' /** Allows expired names to be included (default: false) */ allowExpired?: boolean /** Allows reverse record nodes to be included (default: false) */ @@ -179,8 +181,9 @@ const getNamesForAddress = async ( allowExpired: false, allowDeleted: false, allowReverseRecord: false, + searchType: 'labelName', ..._filter, - } + } as const const subgraphClient = createSubgraphClient({ client }) @@ -189,6 +192,7 @@ const getNamesForAddress = async ( allowDeleted, allowReverseRecord, searchString, + searchType, ...filters } = filter const ownerWhereFilters: DomainFilter[] = Object.entries(filters).reduce( @@ -281,10 +285,8 @@ const getNamesForAddress = async ( } if (searchString) { - // using labelName_contains instead of name_contains because name_contains - // includes the parent name whereFilters.push({ - labelName_contains: searchString, + [`${searchType}_contains`]: searchString, }) }