Add fetchManifest api

This commit is contained in:
Arnaud 2024-10-23 16:53:40 +02:00
parent 9decfba74d
commit b059830cec
No known key found for this signature in database
GPG Key ID: 69D6CE281FCAE663
4 changed files with 33 additions and 6 deletions

View File

@ -264,7 +264,7 @@ const space = await data.space();
Upload a file in a streaming manner
- file (File, require)
- file (File, required)
- onProgress (onProgress: (loaded: number, total: number) => void, optional)
- returns [UploadResponse](./src/data/types.ts#85)
@ -280,6 +280,20 @@ const upload = data.upload(file, (loaded: number, total: number) => {
await upload.result();
```
#### manifest
Download only the dataset manifest from the network to the local node if it's not available locally.
- cid (string, required)
- returns [CodexManifest](./src/data/types.ts#3)
Example:
```js
const cid = "QmYyQSo1c1Ym7orWxLYvCrM2EmxFTANf8wXmmE7DWjhx5N";
const manifest = await data.fetchManifest(cid);
```
### Debug
The following API assume that you have already a node module loaded, example:

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "@codex-storage/sdk-js",
"version": "0.0.8",
"version": "0.0.9",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@codex-storage/sdk-js",
"version": "0.0.8",
"version": "0.0.9",
"license": "MIT",
"dependencies": {
"valibot": "^0.32.0"

View File

@ -1,6 +1,6 @@
{
"name": "@codex-storage/sdk-js",
"version": "0.0.8",
"version": "0.0.9",
"description": "Codex SDK to interact with the Codex decentralized storage network.",
"repository": {
"type": "git",

View File

@ -4,6 +4,7 @@ import { Fetch } from "../fetch-safe/fetch-safe";
import type { SafeValue } from "../values/values";
import type {
CodexDataResponse,
CodexManifest,
CodexNodeSpace,
UploadResponse,
} from "./types";
@ -63,7 +64,7 @@ export class CodexData {
const xhr = new XMLHttpRequest();
const promise = new Promise<SafeValue<string>>(async (resolve) => {
const promise = new Promise<SafeValue<string>>((resolve) => {
xhr.upload.onprogress = (evt) => {
if (evt.lengthComputable) {
onProgress?.(evt.loaded, evt.total);
@ -71,7 +72,7 @@ export class CodexData {
};
xhr.open("POST", url, true);
// xhr.setRequestHeader("Content-Disposition", "attachment; filename=\"" + file.name + "\"")
xhr.send(file);
xhr.onload = function () {
@ -145,4 +146,16 @@ export class CodexData {
return res.data.body;
}
/**
* Download only the dataset manifest from the network to the local node
* if it's not available locally.
*/
async fetchManifest(cid: string) {
const url = this.url + Api.config.prefix + `/data/${cid}/network/manifest`;
return Fetch.safeJson<CodexManifest>(url, {
method: "GET",
});
}
}