@@ -5,11 +5,14 @@ const {
55 withAutomaticRetries,
66 validateWebhook,
77 parseProgressFromLogs,
8+ transformFileInputsToReplicateFileURLs,
9+ transformFileInputsToBase64EncodedDataURIs,
810} = require ( "./lib/util" ) ;
911
1012const accounts = require ( "./lib/accounts" ) ;
1113const collections = require ( "./lib/collections" ) ;
1214const deployments = require ( "./lib/deployments" ) ;
15+ const files = require ( "./lib/files" ) ;
1316const hardware = require ( "./lib/hardware" ) ;
1417const models = require ( "./lib/models" ) ;
1518const predictions = require ( "./lib/predictions" ) ;
@@ -45,13 +48,23 @@ class Replicate {
4548 * @param {string } options.userAgent - Identifier of your app
4649 * @param {string } [options.baseUrl] - Defaults to https://api.replicate.com/v1
4750 * @param {Function } [options.fetch] - Fetch function to use. Defaults to `globalThis.fetch`
51+ * @param {Function } [options.prepareInput] - Function to prepare input data before sending it to the API.
4852 */
4953 constructor ( options = { } ) {
5054 this . auth = options . auth || process . env . REPLICATE_API_TOKEN ;
5155 this . userAgent =
5256 options . userAgent || `replicate-javascript/${ packageJSON . version } ` ;
5357 this . baseUrl = options . baseUrl || "https://api.replicate.com/v1" ;
5458 this . fetch = options . fetch || globalThis . fetch ;
59+ this . prepareInputs =
60+ options . prepareInputs ||
61+ ( async ( inputs ) => {
62+ try {
63+ return await transformFileInputsToReplicateFileURLs ( this , inputs ) ;
64+ } catch ( error ) {
65+ return await transformFileInputsToBase64EncodedDataURIs ( inputs ) ;
66+ }
67+ } ) ;
5568
5669 this . accounts = {
5770 current : accounts . current . bind ( this ) ,
@@ -69,6 +82,13 @@ class Replicate {
6982 } ,
7083 } ;
7184
85+ this . files = {
86+ create : files . create . bind ( this ) ,
87+ list : files . list . bind ( this ) ,
88+ get : files . get . bind ( this ) ,
89+ delete : files . delete . bind ( this ) ,
90+ } ;
91+
7292 this . hardware = {
7393 list : hardware . list . bind ( this ) ,
7494 } ;
@@ -222,10 +242,17 @@ class Replicate {
222242 }
223243 }
224244
245+ let body = undefined ;
246+ if ( data instanceof FormData ) {
247+ body = data ;
248+ } else if ( data ) {
249+ body = JSON . stringify ( data ) ;
250+ }
251+
225252 const init = {
226253 method,
227254 headers,
228- body : data ? JSON . stringify ( data ) : undefined ,
255+ body,
229256 } ;
230257
231258 const shouldRetry =
0 commit comments