ipfs / helia

An implementation of IPFS in JavaScript
https://helia.io
Other
814 stars 81 forks source link

feat: allow use as a singleton #413

Closed achingbrain closed 5 months ago

achingbrain commented 5 months ago

This might make things even easier to get started? It exports a singleton version of verified fetch that creates the backing Helia node on the first fetch access, transparent to the user.

import { verifiedFetch } from '@helia/verified-fetch'

const response = await verifiedFetch('ipfs://Qmfoo')

console.info(await response.json())

It's not in this PR but maybe we'd want to allow some config:

import { verifiedFetch } from '@helia/verified-fetch'

const response = await verifiedFetch('ipfs://Qmfoo', {
  gateways: ['https://...']
})

console.info(await response.json())

Calling this twice with different gateways might result in an error.

import { verifiedFetch } from '@helia/verified-fetch'

const response1 = await verifiedFetch('ipfs://Qmfoo', {
  gateways: ['https://foo...']
})

const response2 = await verifiedFetch('ipfs://Qmfoo', {
  gateways: ['https://bar...']
})
// Error: Only one set of gateways/routers may be specified, please use `createVerifiedFetch` for more flexibility
SgtPooki commented 5 months ago

It's not in this PR but maybe we'd want to allow some config:

I think we should leave the singleton to the most basic, unconfigurable, usecase and have users use createVerifiedFetch if they want to customize gateways, so that we don't have to muck around with error and edge-cases or confusion on the second options argument.