The top level of the module exports a function to create instances of ipfs-fetch.
It takes an initialized ipfs instance which you can initialize somewhere in your code.
It will then return a fetch() function which conforms to The Web API, but with the twist that it supports ipns:// and ipfs:// URLs.
Fetch API
await fetch('ipfs://CID/example.txt')
If you specify a URL for a file (no trailing slashes), it will be loaded from IPFS and the content will be sent as the response body.
The response headers will contain a Content-Length header set to the size of the file.
await fetch('ipfs://CID/example/')
If you specify a URL for a folder (has a trailing slash), the folder will be enumerated from IPFS and an HTML page listing its various files will be rendered.
Hyperlinks to files/folders will be automatically generated as relative URLs.
Links will have a trailing slash for folders.
If the folder contains an index.html it will be served as a file instead of performing a directory listing.
If you specify the X-Resolve: none header in your request, the resolution of index.html will be ignored and a directory listing will always be performed.
If you specify a URL for a folder, and set the Accept header to only contain application/json, the directory will be enumerated and the list of files/folders will be returned as a JSON array.
You can get the file/folder list out of the response using await response.json().
You can upload files to IPFS by using PUT messages.
The response will contain a Location header with the created URL.
e.g. const url = response.headers.get('Location')
Note that ipfs://bafyaabakaieac/ is a IPFS URL representing an empty directory (using an inline block definition).
await fetch('ipfs://bafyaabakaieac/', {method: 'put', body: new FormData()})
You can upload several files to IPFS by using PUT messages with a FormData body.
You can append to a FormData with formData.append(fieldname, content, 'filename.txt') where fieldname gets ignored (use something like file?), the content can either be a String, Blob, or some sort of stream.
The filename will be the filename inside the IPFS directory that gets created.
The response will contain a Location header with the created URL.
e.g. const url = response.headers.get('Location')
Note that ipfs://bafyaabakaieac/ is a IPFS URL representing an empty directory (using an inline definition).
await fetch('ipns://CID/example.txt')
You can specify an IPNS URL to have it resolve to whatever resource you wanted using the Inter-Planetary Naming System
请发表评论