-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: content-type response header hints how to process response (#426)
## JSON codec ```TypeScript import { verifiedFetch } from '@helia/verified-fetch' import * as json from 'multiformats/codecs/json' const res = await verifiedFetch('ipfs://bafyJSONCodec') // get object representation of JSON body console.info(await res.json()) // { ... } // alternative way to do the same thing const obj = json.decode(new Uint8Array(await res.arrayBuffer())) console.info(obj) // { ... } ``` ## DAG-JSON codec ```TypeScript import { verifiedFetch } from '@helia/verified-fetch' import * as dagJson from '@ipld/dag-json' const res = await verifiedFetch('ipfs://bafyDAGJSONCodec') // get spec-compliant plain-old-JSON object that happens to contain a CID console.info(await res.json()) // { cid: { '/': 'Qmfoo' } } // ...or use @ipld/dag-json to get rich-object version const obj = dagJson.decode(new Uint8Array(await res.arrayBuffer())) console.info(obj) // { cid: CID('Qmfoo') } ``` ## DAG-CBOR codec ```TypeScript import { verifiedFetch } from '@helia/verified-fetch' import * as dagCbor from '@ipld/dag-cbor' const res = await verifiedFetch('ipfs://bafyDAGCBORCodec') if (res.headers.get('content-type') === 'application/json') { // CBOR was JSON-friendly console.info(await res.json()) // { ... } } else { // CBOR was not JSON-friendly, must use @ipld/dag-cbor to decode const obj = dagCbor.decode(new Uint8Array(await res.arrayBuffer())) console.info(obj) // { ... } } ``` If the `DAG-CBOR` block contains anything that cannot round-trip to JSON (e.g. `CID`s, `Uint8Array`s, `BigInt`s, etc), the content type will be `application/octet-stream` - `.json()` will throw and so `@ipld/dag-cbor` must be used to decode the resolved value of `.arrayBuffer()`. --------- Co-authored-by: Daniel Norman <[email protected]> Co-authored-by: Daniel N <[email protected]> Co-authored-by: Russell Dempsey <[email protected]>
- Loading branch information
1 parent
ea39b48
commit 8b78e79
Showing
6 changed files
with
730 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.