From 50ed08ac6a23f9fa7e43450c0f8942124fa175cd Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 10 Dec 2023 23:33:55 -0500 Subject: [PATCH] feat: add downloadData method --- src/client.ts | 8 +++++++- src/methods/download.ts | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/client.ts b/src/client.ts index 2aa3f1d..1d8bcc7 100644 --- a/src/client.ts +++ b/src/client.ts @@ -14,7 +14,12 @@ import { uploadSmallFileRequest, uploadLargeFileRequest, } from "./methods/upload.js"; -import { downloadFile, getCidUrl, getMetadata } from "./methods/download.js"; +import { + downloadData, + downloadFile, + getCidUrl, + getMetadata, +} from "./methods/download.js"; import { defaultPortalUrl, ensureUrl } from "./utils/url.js"; @@ -123,6 +128,7 @@ export class S5Client { // Download downloadFile = downloadFile; + downloadData = downloadData; getCidUrl = getCidUrl; getMetadata = getMetadata; diff --git a/src/methods/download.ts b/src/methods/download.ts index df49bd3..c244ea5 100644 --- a/src/methods/download.ts +++ b/src/methods/download.ts @@ -20,7 +20,7 @@ export type CustomDownloadOptions = BaseCustomOptions & { path?: string; range?: string; responseType?: ResponseType; - subdomain?: boolean; + subdomain?: string; }; export type CustomGetMetadataOptions = BaseCustomOptions & { @@ -45,7 +45,7 @@ export const DEFAULT_DOWNLOAD_OPTIONS = { path: undefined, range: undefined, responseType: undefined, - subdomain: false, + subdomain: "", }; const DEFAULT_GET_METADATA_OPTIONS = { @@ -139,3 +139,32 @@ export async function getMetadata( return response.data; } + +/** + * Downloads in-memory data from a S5 cid. + * @param this - S5Client + * @param cid - 46-character cid, or a valid cid URL. Can be followed by a path. Note that the cid will not be encoded, so if your path might contain special characters, consider using `customOptions.path`. + * @param [customOptions] - Additional settings that can optionally be set. + * @returns - The data + */ +export async function downloadData( + this: S5Client, + cid: string, + customOptions?: CustomDownloadOptions, +): Promise { + const opts = { + ...DEFAULT_DOWNLOAD_OPTIONS, + ...this.customOptions, + ...customOptions, + download: true, + }; + + const response = await this.executeRequest({ + ...opts, + method: "get", + extraPath: cid, + responseType: "arraybuffer", + }); + + return response.data; +}