@helia/verified-fetch 0.0.0-6c88ee1 → 0.0.0-7a7c0c1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -124,6 +124,30 @@ const resp = await fetch('ipfs://bafy...')
124
124
  const json = await resp.json()
125
125
  ```
126
126
 
127
+ ### Custom content-type parsing
128
+
129
+ By default, `@helia/verified-fetch` sets the `Content-Type` header as `application/octet-stream` - this is because the `.json()`, `.text()`, `.blob()`, and `.arrayBuffer()` methods will usually work as expected without a detailed content type.
130
+
131
+ If you require an accurate content-type you can provide a `contentTypeParser` function as an option to `createVerifiedFetch` to handle parsing the content type.
132
+
133
+ The function you provide will be called with the first chunk of bytes from the file and should return a string or a promise of a string.
134
+
135
+ ## Example - Customizing content-type parsing
136
+
137
+ ```typescript
138
+ import { createVerifiedFetch } from '@helia/verified-fetch'
139
+ import { fileTypeFromBuffer } from '@sgtpooki/file-type'
140
+
141
+ const fetch = await createVerifiedFetch({
142
+ gateways: ['https://trustless-gateway.link'],
143
+ routers: ['http://delegated-ipfs.dev'],
144
+ contentTypeParser: async (bytes) => {
145
+ // call to some magic-byte recognition library like magic-bytes, file-type, or your own custom byte recognition
146
+ return fileTypeFromBuffer(bytes)?.mime
147
+ }
148
+ })
149
+ ```
150
+
127
151
  ## Comparison to fetch
128
152
 
129
153
  This module attempts to act as similarly to the `fetch()` API as possible.