feat: add downloadData method

This commit is contained in:
Derrick Hammer 2023-12-10 23:33:55 -05:00
parent 8b4d189c80
commit 50ed08ac6a
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 38 additions and 3 deletions

View File

@ -14,7 +14,12 @@ import {
uploadSmallFileRequest, uploadSmallFileRequest,
uploadLargeFileRequest, uploadLargeFileRequest,
} from "./methods/upload.js"; } 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"; import { defaultPortalUrl, ensureUrl } from "./utils/url.js";
@ -123,6 +128,7 @@ export class S5Client {
// Download // Download
downloadFile = downloadFile; downloadFile = downloadFile;
downloadData = downloadData;
getCidUrl = getCidUrl; getCidUrl = getCidUrl;
getMetadata = getMetadata; getMetadata = getMetadata;

View File

@ -20,7 +20,7 @@ export type CustomDownloadOptions = BaseCustomOptions & {
path?: string; path?: string;
range?: string; range?: string;
responseType?: ResponseType; responseType?: ResponseType;
subdomain?: boolean; subdomain?: string;
}; };
export type CustomGetMetadataOptions = BaseCustomOptions & { export type CustomGetMetadataOptions = BaseCustomOptions & {
@ -45,7 +45,7 @@ export const DEFAULT_DOWNLOAD_OPTIONS = {
path: undefined, path: undefined,
range: undefined, range: undefined,
responseType: undefined, responseType: undefined,
subdomain: false, subdomain: "",
}; };
const DEFAULT_GET_METADATA_OPTIONS = { const DEFAULT_GET_METADATA_OPTIONS = {
@ -139,3 +139,32 @@ export async function getMetadata(
return response.data; 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<ArrayBuffer> {
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;
}