Skip to content

Commit

Permalink
Add fetchManifest api
Browse files Browse the repository at this point in the history
  • Loading branch information
2-towns committed Oct 23, 2024
1 parent 9decfba commit b059830
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
17 changes: 15 additions & 2 deletions src/data/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -63,15 +64,15 @@ 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);
}
};

xhr.open("POST", url, true);

// xhr.setRequestHeader("Content-Disposition", "attachment; filename=\"" + file.name + "\"")
xhr.send(file);

xhr.onload = function () {
Expand Down Expand Up @@ -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",
});
}
}

0 comments on commit b059830

Please sign in to comment.