From d4b4df06fd1e08db1e48255579ada0311cb7a46b Mon Sep 17 00:00:00 2001 From: jelveh Date: Thu, 6 Feb 2025 19:45:10 -0800 Subject: [PATCH] Use the `fetch` API in Puter.js's `Convert` module --- src/puter-js/src/modules/Convert.js | 43 +++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/puter-js/src/modules/Convert.js b/src/puter-js/src/modules/Convert.js index 9a5aeae20..8ceb0c7f5 100644 --- a/src/puter-js/src/modules/Convert.js +++ b/src/puter-js/src/modules/Convert.js @@ -1,10 +1,8 @@ -import * as utils from '../lib/utils.js'; - class Convert { constructor(context) { this.authToken = context.authToken; this.APIOrigin = context.APIOrigin; - this.appID = context.appID; // Might not be strictly necessary for this module, but good for consistency + this.appID = context.appID; } setAuthToken(authToken) { @@ -15,11 +13,10 @@ class Convert { this.APIOrigin = APIOrigin; } - convert = async (...args) => { let options = {}; - // if args is a single object, assume it is the options object + // If args is a single object, assume it is the options object if (typeof args[0] === 'object' && args[0] !== null) { options = args[0]; } else { @@ -32,7 +29,41 @@ class Convert { }; } - return utils.make_driver_method(['source', 'to'], 'convert-files', undefined, 'convert').call(this, options); + try { + const response = await fetch(`${this.APIOrigin}/drivers/call`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${this.authToken}` + }, + body: JSON.stringify({ + interface: 'convert-files', + method: 'convert', + args: { + source: options.source, + to: options.to + } + }) + }); + + const data = await response.json(); + + if (!response.ok) { + throw new Error(data.message || 'Conversion failed'); + } + + if (options.success && typeof options.success === 'function') { + options.success(data); + } + + return data; + + } catch (error) { + if (options.error && typeof options.error === 'function') { + options.error(error); + } + throw error; + } } }