Skip to content

Commit 1d1075c

Browse files
authored
refactor(client): move .peers() method from TelemetryAPI to MainAPI (#259)
1 parent 168b7f6 commit 1d1075c

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

packages/client/api.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ export class MainAPI {
207207
await ResponseError.assertStatus(response, 200)
208208
return response.json()
209209
}
210+
211+
public peers(): Promise<PeerJson[]> {
212+
return getPeers(this.http)
213+
}
210214
}
211215

212216
async function handleQueryResponse(resp: Response): Promise<dm.QueryResponse> {
@@ -248,21 +252,11 @@ export class TelemetryAPI {
248252
return response.arrayBuffer().then((buffer) => getCodec(dm.Status).decode(new Uint8Array(buffer)))
249253
}
250254

251-
// TODO: move once metrics are updated
252-
public async peers(): Promise<PeerJson[]> {
253-
const response = await this.http.getFetch()(urlJoinPath(this.http.toriiBaseURL, ENDPOINT_PEERS))
254-
await ResponseError.assertStatus(response, 200)
255-
return response.json().then(
256-
// array of strings in format `<pub key multihash>@<socket addr>`
257-
(ids: string[]) => {
258-
assert(Array.isArray(ids))
259-
return ids.map((id) => {
260-
assert(typeof id === 'string')
261-
const [pubkey, address] = id.split('@')
262-
return { id: dm.PublicKey.fromMultihash(pubkey), address }
263-
})
264-
},
265-
)
255+
/**
256+
* @deprecated use {@linkcode MainAPI#peers}
257+
*/
258+
public peers(): Promise<PeerJson[]> {
259+
return getPeers(this.http)
266260
}
267261

268262
public async metrics(): Promise<string> {
@@ -271,3 +265,19 @@ export class TelemetryAPI {
271265
return response.text()
272266
}
273267
}
268+
269+
async function getPeers(http: HttpTransport) {
270+
const response = await http.getFetch()(urlJoinPath(http.toriiBaseURL, ENDPOINT_PEERS))
271+
await ResponseError.assertStatus(response, 200)
272+
return response.json().then(
273+
// array of strings in format `<pub key multihash>@<socket addr>`
274+
(ids: string[]) => {
275+
assert(Array.isArray(ids))
276+
return ids.map((id) => {
277+
assert(typeof id === 'string')
278+
const [pubkey, address] = id.split('@')
279+
return { id: dm.PublicKey.fromMultihash(pubkey), address }
280+
})
281+
},
282+
)
283+
}

tests/node/tests/client-apis.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe('Telemetry API methods', () => {
111111
test('peers (network of 4)', async () => {
112112
const { peers } = await useNetwork({ peers: 4, seed: new Uint8Array(Buffer.from('deadbeef', 'hex')) })
113113

114-
const peersData = await peers[0].client.api.telemetry.peers()
114+
const peersData = await peers[0].client.api.peers()
115115

116116
expect(peersData.map((x) => x.id.multihash())).contain.all.members(
117117
peers.slice(1).map((x) => x.keypair.publicKey().multihash()),

0 commit comments

Comments
 (0)