Skip to content

Commit 2a030bc

Browse files
committed
refactor: move filter url logic to private method
1 parent 21253d0 commit 2a030bc

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

packages/client/src/client.ts

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,7 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
9393

9494
// https://specs.ipfs.tech/routing/http-routing-v1/
9595
const url = new URL(`${this.clientUrl}routing/v1/providers/${cid.toString()}`)
96-
// IPIP-484 filtering. options takes precedence over global filter
97-
if (options.filterAddrs != null || this.filterAddrs != null) {
98-
const filterAddrs = options.filterAddrs?.join(',') ?? this.filterAddrs?.join(',') ?? ''
99-
if (filterAddrs !== '') {
100-
url.searchParams.set('filter-addrs', filterAddrs)
101-
}
102-
}
103-
if (options.filterProtocols != null || this.filterProtocols != null) {
104-
const filterProtocols = options.filterProtocols?.join(',') ?? this.filterProtocols?.join(',') ?? ''
105-
if (filterProtocols !== '') {
106-
url.searchParams.set('filter-protocols', filterProtocols)
107-
}
108-
}
96+
this.#addFilterParams(url, options.filterAddrs, options.filterProtocols)
10997
const getOptions = { headers: { Accept: 'application/x-ndjson' }, signal }
11098
const res = await fetch(url, getOptions)
11199

@@ -171,20 +159,8 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
171159

172160
// https://specs.ipfs.tech/routing/http-routing-v1/
173161
const url = new URL(`${this.clientUrl}routing/v1/peers/${peerId.toCID().toString()}`)
162+
this.#addFilterParams(url, options.filterAddrs, options.filterProtocols)
174163

175-
// IPIP-484 filtering. local options filter precedence over global filter
176-
if (options.filterAddrs != null || this.filterAddrs != null) {
177-
const filterAddrs = options.filterAddrs?.join(',') ?? this.filterAddrs?.join(',') ?? ''
178-
if (filterAddrs !== '') {
179-
url.searchParams.set('filter-addrs', filterAddrs)
180-
}
181-
}
182-
if (options.filterProtocols != null || this.filterProtocols != null) {
183-
const filterProtocols = options.filterProtocols?.join(',') ?? this.filterProtocols?.join(',') ?? ''
184-
if (filterProtocols !== '') {
185-
url.searchParams.set('filter-protocols', filterProtocols)
186-
}
187-
}
188164
const getOptions = { headers: { Accept: 'application/x-ndjson' }, signal }
189165
const res = await fetch(url, getOptions)
190166

@@ -357,4 +333,20 @@ export class DefaultDelegatedRoutingV1HttpApiClient implements DelegatedRoutingV
357333
log.error('could not conform record to peer schema', err)
358334
}
359335
}
336+
337+
#addFilterParams (url: URL, filterAddrs?: string[], filterProtocols?: string[]): void {
338+
// IPIP-484 filtering. local options filter precedence over global filter
339+
if (filterAddrs != null || this.filterAddrs != null) {
340+
const adressFilter = filterAddrs?.join(',') ?? this.filterAddrs?.join(',') ?? ''
341+
if (adressFilter !== '') {
342+
url.searchParams.set('filter-addrs', adressFilter)
343+
}
344+
}
345+
if (filterProtocols != null || this.filterProtocols != null) {
346+
const protocolFilter = filterProtocols?.join(',') ?? this.filterProtocols?.join(',') ?? ''
347+
if (protocolFilter !== '') {
348+
url.searchParams.set('filter-protocols', protocolFilter)
349+
}
350+
}
351+
}
360352
}

0 commit comments

Comments
 (0)