diff --git a/src/client/node.ts b/src/client/node.ts index 3b2305e9..1fd43c0c 100644 --- a/src/client/node.ts +++ b/src/client/node.ts @@ -1,4 +1,4 @@ -import { downloadFileHnsToPath, downloadFileToPath } from "../download/node"; +import { downloadFileHnsToPath, downloadFileToPath, downloadFileToPathRequest } from "../download/node"; import { uploadDirectoryFromPath, uploadDirectoryFromPathRequest, @@ -13,6 +13,7 @@ export class SkynetClient extends Client { // Download downloadFileToPath = downloadFileToPath; downloadFileHnsToPath = downloadFileHnsToPath; + protected downloadFileToPathRequest = downloadFileToPathRequest; // Upload uploadDirectoryFromPath = uploadDirectoryFromPath; diff --git a/src/download/node.ts b/src/download/node.ts index dc0ca959..51f3190f 100644 --- a/src/download/node.ts +++ b/src/download/node.ts @@ -1,6 +1,6 @@ import fs from "fs"; -import { SkynetClient } from "../client"; +import { SkynetClient } from "../client/node"; import { CustomDownloadOptions, defaultDownloadOptions, @@ -77,6 +77,27 @@ export async function downloadFileHnsToPath( const url = this.getHnsUrl(domain, opts); + return this.downloadFileToPathRequest(url, path, opts); +} + +/** + * Makes a request to download a file to a path from Skynet. + * + * @param this - SkynetClient + * @param url - URL. + * @param path - Path to create the local file at. + * @param [customOptions] - Additional settings that can optionally be set. + * @returns - The metadata in JSON format. Each field will be empty if no metadata was found. + * @throws - Will throw if the request does not succeed or the response is missing data. + */ +export async function downloadFileToPathRequest( + this: SkynetClient, + url: string, + path: string, + customOptions?: CustomDownloadOptions +): Promise { + const opts = { ...defaultDownloadHnsOptions, ...this.customOptions, ...customOptions }; + const writer = fs.createWriteStream(path); const response = await this.executeRequest({ diff --git a/src/download/web.ts b/src/download/web.ts index bc22dfbf..64211991 100644 --- a/src/download/web.ts +++ b/src/download/web.ts @@ -1,4 +1,4 @@ -import { SkynetClient } from "../client"; +import { SkynetClient } from "../client/web"; import { CustomDownloadOptions, defaultDownloadOptions, diff --git a/src/index.node.ts b/src/index.node.ts index 38b080e8..e52820fc 100644 --- a/src/index.node.ts +++ b/src/index.node.ts @@ -1,16 +1,9 @@ export { SkynetClient } from "./client/node"; export { deriveChildSeed, genKeyPairAndSeed, genKeyPairFromSeed } from "./crypto"; -export { - MAX_REVISION, - defaultPortalUrl, - defaultSkynetPortalUrl, - getRelativeFilePath, - getRootDirectory, - parseSkylink, - uriHandshakePrefix, - uriHandshakeResolverPrefix, - uriSkynetPrefix, -} from "./utils"; +export { parseSkylink, uriHandshakePrefix, uriHandshakeResolverPrefix, uriSkynetPrefix } from "./utils/skylink"; +export { MAX_REVISION } from "./utils/number"; +export { defaultPortalUrl, defaultSkynetPortalUrl } from "./utils/url"; +export { getRelativeFilePath, getRootDirectory } from "./utils/file"; // Export types. @@ -20,4 +13,4 @@ export type { CustomDownloadOptions, ResolveHnsResponse } from "./download"; export type { CustomGetEntryOptions, CustomSetEntryOptions, SignedRegistryEntry, RegistryEntry } from "./registry"; export type { CustomGetJSONOptions, CustomSetJSONOptions, VersionedEntryData } from "./skydb"; export type { CustomUploadOptions, UploadRequestResponse } from "./upload/index"; -export type { ParseSkylinkOptions } from "./utils"; +export type { ParseSkylinkOptions } from "./utils/skylink"; diff --git a/src/upload/index.ts b/src/upload/index.ts index 149690b4..e25c69a2 100644 --- a/src/upload/index.ts +++ b/src/upload/index.ts @@ -1,4 +1,4 @@ -import { defaultOptions, BaseCustomOptions } from "../utils"; +import { defaultOptions, BaseCustomOptions } from "../utils/skylink"; /** * Custom upload options. @@ -6,7 +6,6 @@ import { defaultOptions, BaseCustomOptions } from "../utils"; * @property [portalFileFieldname="file"] - The file fieldname for uploading files on this portal. * @property [portalDirectoryfilefieldname="files[]"] - The file fieldname for uploading directories on this portal. * @property [customFilename] - The custom filename to use when uploading files. - * @property [query] - Query parameters. */ export type CustomUploadOptions = BaseCustomOptions & { portalFileFieldname?: string; diff --git a/src/upload/node.ts b/src/upload/node.ts index 6c87d062..af5befcb 100644 --- a/src/upload/node.ts +++ b/src/upload/node.ts @@ -9,7 +9,7 @@ import p from "path"; import { SkynetClient } from "../client/node"; import { CustomUploadOptions, defaultUploadOptions, UploadRequestResponse } from "./index"; -import { formatSkylink } from "../utils"; +import { formatSkylink } from "../utils/skylink"; /** * Uploads a file from the given local path to Skynet. diff --git a/src/utils/skylink.ts b/src/utils/skylink.ts index dc37d68a..8a9d34d1 100644 --- a/src/utils/skylink.ts +++ b/src/utils/skylink.ts @@ -8,9 +8,11 @@ import { trimForwardSlash, trimSuffix, trimUriPrefix } from "./string"; * Base custom options for methods hitting the API. * * @property [endpointPath] - The relative URL path of the portal endpoint to contact. + * @property [query] - Query parameters. */ export type BaseCustomOptions = CustomClientOptions & { endpointPath?: string; + query?: Record; }; /**