From b059830cec606ad7f940f4f6be2e8b86e14e6082 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Wed, 23 Oct 2024 16:53:40 +0200 Subject: [PATCH] Add fetchManifest api --- README.md | 16 +++++++++++++++- package-lock.json | 4 ++-- package.json | 2 +- src/data/data.ts | 17 +++++++++++++++-- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ea5bc8d..f90540b 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/package-lock.json b/package-lock.json index d0f7622..eb3bb8a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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" diff --git a/package.json b/package.json index 62fefe8..222a9e6 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/data/data.ts b/src/data/data.ts index 113c11d..5b4a8f5 100644 --- a/src/data/data.ts +++ b/src/data/data.ts @@ -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>(async (resolve) => { + const promise = new Promise>((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(url, { + method: "GET", + }); + } }