feat: add downloadData method
This commit is contained in:
parent
8b4d189c80
commit
50ed08ac6a
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue