From d2199764120932f6ce308238e366606764ec50cd Mon Sep 17 00:00:00 2001 From: Arnaud Date: Fri, 25 Oct 2024 11:36:51 +0200 Subject: [PATCH] Return the Response object for download --- README.md | 2 +- src/data/data.ts | 30 ++++++------------------------ 2 files changed, 7 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 6bce562..4c54812 100644 --- a/README.md +++ b/README.md @@ -300,7 +300,7 @@ Download a file from the network in a streaming manner. If the file is not available locally, it will be retrieved from other nodes in the network if able. - cid (string, required) -- returns ReadableStream | null +- returns Response Example: diff --git a/src/data/data.ts b/src/data/data.ts index 0c19523..7fadac2 100644 --- a/src/data/data.ts +++ b/src/data/data.ts @@ -58,7 +58,7 @@ export class CodexData { * A callback onProgress can be passed to receive upload progress data information. */ upload( - file: File, + file: Document | XMLHttpRequestBodyInit, onProgress?: (loaded: number, total: number) => void ): UploadResponse { const url = this.url + Api.config.prefix + "/data"; @@ -109,21 +109,12 @@ export class CodexData { * Download a file from the local node in a streaming manner. * If the file is not available locally, a 404 is returned. */ - async localDownload(cid: string): Promise | null>> { + async localDownload(cid: string): Promise> { const url = this.url + Api.config.prefix + "/data/" + cid; - const res = await Fetch.safe(url, { + return Fetch.safe(url, { method: "GET", - headers: { - "content-type": "application/octet-stream", - }, }); - - if (res.error) { - return res; - } - - return { error: false, data: res.data.body }; } /** @@ -134,10 +125,7 @@ export class CodexData { const url = this.url + Api.config.prefix + `/data/${cid}/network`; return Fetch.safeJson(url, { - method: "POST", - headers: { - "content-type": "application/octet-stream", - }, + method: "POST" }); } @@ -145,18 +133,12 @@ export class CodexData { * Download a file from the network in a streaming manner. * If the file is not available locally, it will be retrieved from other nodes in the network if able. */ - async networkDownloadStream(cid: string): Promise | null>> { + async networkDownloadStream(cid: string): Promise> { const url = this.url + Api.config.prefix + `/data/${cid}/network/stream`; - const res = await Fetch.safe(url, { + return Fetch.safe(url, { method: "GET" }); - - if (res.error) { - return res; - } - - return { error: false, data: res.data.body }; } /**