ipfs-http-client
Version:
A client library for the IPFS HTTP API
25 lines • 820 B
JavaScript
import { Multiaddr } from 'multiaddr';
import { objectToCamel } from '../lib/object-to-camel.js';
import { configure } from '../lib/configure.js';
import { toUrlSearchParams } from '../lib/to-url-search-params.js';
export const createQuery = configure(api => {
async function* query(peerId, options = {}) {
const res = await api.post('dht/query', {
signal: options.signal,
searchParams: toUrlSearchParams({
arg: peerId.toString(),
...options
}),
headers: options.headers
});
for await (let message of res.ndjson()) {
message = objectToCamel(message);
message.responses = (message.responses || []).map(({ID, Addrs}) => ({
id: ID,
addrs: (Addrs || []).map(a => new Multiaddr(a))
}));
yield message;
}
}
return query;
});