Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow use as a singleton #413

Merged
merged 6 commits into from
Feb 6, 2024
Merged

Conversation

achingbrain
Copy link
Member

@achingbrain achingbrain commented Feb 2, 2024

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

To allow no-option, all-defaults construction of verified fetch

```TypeScript
import { createVerifiedFetch } from '@helia/verified-fetch'

const fetch = await createVerifiedFetch()

const resp = await fetch('ipfs://bafy...')
// ...
```
This might make things even easier to get started?

```TypeScript
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:

```TypeScript
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.

```TypeScript
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
```
@achingbrain achingbrain requested a review from a team as a code owner February 2, 2024 15:59
Co-authored-by: Daniel Norman <1992255+2color@users.noreply.github.com>
@SgtPooki
Copy link
Member

SgtPooki commented Feb 2, 2024

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.

Base automatically changed from fix/make-verified-fetch-init-optional to main February 2, 2024 18:06
Copy link
Member

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love the updates to the readme.

@achingbrain achingbrain merged commit 6c88ee1 into main Feb 6, 2024
18 checks passed
@achingbrain achingbrain deleted the feat/add-verified-fetch-singleton branch February 6, 2024 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants