UNPKG

@opendatalabs/vana-sdk

Version:

A TypeScript library for interacting with Vana Network smart contracts.

238 lines 7.42 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var dropbox_exports = {}; __export(dropbox_exports, { DropboxStorage: () => DropboxStorage }); module.exports = __toCommonJS(dropbox_exports); var import__ = require("../index"); class DropboxStorage { constructor(config) { this.config = config; if (!config.accessToken) { throw new import__.StorageError( "Dropbox access token is required", "MISSING_ACCESS_TOKEN", "dropbox" ); } this.rootPath = config.rootPath ?? "/Vana Data"; } config; apiUrl = "https://api.dropboxapi.com/2"; contentUrl = "https://content.dropboxapi.com/2"; rootPath; async upload(file, filename) { try { const fileName = filename ?? `vana-file-${Date.now()}.dat`; const path = `${this.rootPath}/${fileName}`; const response = await fetch(`${this.contentUrl}/files/upload`, { method: "POST", headers: { Authorization: `Bearer ${this.config.accessToken}`, "Content-Type": "application/octet-stream", "Dropbox-API-Arg": JSON.stringify({ path, mode: "add", autorename: true, mute: false }) }, body: file }); if (!response.ok) { const error = await response.text(); throw new import__.StorageError( `Failed to upload to Dropbox: ${error}`, "UPLOAD_FAILED", "dropbox" ); } const result = await response.json(); const sharedLinkUrl = await this.createSharedLink(result.path_lower); const directDownloadUrl = sharedLinkUrl.replace("www.dropbox.com", "dl.dropboxusercontent.com").replace("?dl=1", ""); return { url: directDownloadUrl, size: file.size, contentType: file.type || "application/octet-stream", metadata: { id: result.id, path: result.path_display } }; } catch (error) { if (error instanceof import__.StorageError) throw error; throw new import__.StorageError( `Dropbox upload error: ${error instanceof Error ? error.message : "Unknown error"}`, "UPLOAD_ERROR", "dropbox" ); } } async download(url) { const downloadUrl = url.replace( "www.dropbox.com", "dl.dropboxusercontent.com" ); try { const response = await fetch(downloadUrl); if (!response.ok) { throw new import__.StorageError( `Failed to download from Dropbox: ${response.statusText}`, "DOWNLOAD_FAILED", "dropbox" ); } return response.blob(); } catch (error) { if (error instanceof import__.StorageError) throw error; throw new import__.StorageError( `Dropbox download error: ${error instanceof Error ? error.message : "Unknown error"}`, "DOWNLOAD_ERROR", "dropbox" ); } } async list(options) { if (!this.config.accessToken) { throw new import__.StorageError( "Access token not provided", "AUTH_ERROR", "dropbox" ); } try { const response = await fetch(`${this.apiUrl}/files/list_folder`, { method: "POST", headers: { Authorization: `Bearer ${this.config.accessToken}`, "Content-Type": "application/json" }, body: JSON.stringify({ path: this.rootPath, limit: options?.limit ?? 100, include_deleted: false }) }); if (!response.ok) { throw new import__.StorageError( `Failed to list Dropbox files: ${await response.text()}`, "LIST_FAILED", "dropbox" ); } const result = await response.json(); return result.entries.filter((entry) => entry[".tag"] === "file").map((file) => ({ id: file.id, name: file.name, url: `dropbox://${file.path_lower}`, // Placeholder URL size: file.size, contentType: "application/octet-stream", // Dropbox API doesn't provide this in list createdAt: new Date(file.server_modified) })); } catch (error) { if (error instanceof import__.StorageError) throw error; throw new import__.StorageError( `Dropbox list error: ${error instanceof Error ? error.message : "Unknown error"}`, "LIST_ERROR", "dropbox" ); } } async delete(url) { try { const path = new URL(url).pathname; const response = await fetch(`${this.apiUrl}/files/delete_v2`, { method: "POST", headers: { Authorization: `Bearer ${this.config.accessToken}`, "Content-Type": "application/json" }, body: JSON.stringify({ path }) }); if (!response.ok && response.status !== 404) { const error = await response.text(); throw new import__.StorageError( `Failed to delete from Dropbox: ${error}`, "DELETE_FAILED", "dropbox" ); } return true; } catch (error) { if (error instanceof import__.StorageError) throw error; throw new import__.StorageError( `Dropbox delete error: ${error instanceof Error ? error.message : "Unknown error"}`, "DELETE_ERROR", "dropbox" ); } } getConfig() { return { name: "Dropbox", type: "dropbox", requiresAuth: true, features: { upload: true, download: true, list: true, delete: true } }; } async createSharedLink(path) { const response = await fetch( `${this.apiUrl}/sharing/create_shared_link_with_settings`, { method: "POST", headers: { Authorization: `Bearer ${this.config.accessToken}`, "Content-Type": "application/json" }, body: JSON.stringify({ path, settings: { requested_visibility: "public" } }) } ); if (!response.ok) { const errorData = await response.json(); if (response.status === 409 && errorData.error?.shared_link_already_exists) { return errorData.error.shared_link_already_exists.metadata.url; } throw new import__.StorageError( `Failed to create shared link: ${JSON.stringify(errorData)}`, "LINK_CREATION_FAILED", "dropbox" ); } const result = await response.json(); return result.url.replace("?dl=0", "?dl=1"); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { DropboxStorage }); //# sourceMappingURL=dropbox.cjs.map