From ba24ba20cfda9a8bcf5c35aeb3aba1f2e6925bb5 Mon Sep 17 00:00:00 2001 From: Daniel Rochetti Date: Sat, 1 Jun 2024 18:01:43 -0700 Subject: [PATCH] fix(client): storage upload array --- libs/client/package.json | 2 +- libs/client/src/storage.ts | 48 +++++++++++++++++--------------------- 2 files changed, 23 insertions(+), 27 deletions(-) diff --git a/libs/client/package.json b/libs/client/package.json index de72bdf..4e0e814 100644 --- a/libs/client/package.json +++ b/libs/client/package.json @@ -1,7 +1,7 @@ { "name": "@fal-ai/serverless-client", "description": "The fal serverless JS/TS client", - "version": "0.10.0", + "version": "0.10.2", "license": "MIT", "repository": { "type": "git", diff --git a/libs/client/src/storage.ts b/libs/client/src/storage.ts index 439a5f8..3871974 100644 --- a/libs/client/src/storage.ts +++ b/libs/client/src/storage.ts @@ -103,33 +103,29 @@ export const storageImpl: StorageSupport = { }, // eslint-disable-next-line @typescript-eslint/no-explicit-any - transformInput: async (input: Record) => { - if (!isPlainObject(input)) { - return input; - } - const promises = Object.entries(input).map(async ([key, value]) => { - if ( - value instanceof Blob || - (typeof value === 'string' && isDataUri(value)) - ) { - let blob = value; - // if string is a data uri, convert to blob - if (typeof value === 'string' && isDataUri(value)) { - const response = await fetch(value); - blob = await response.blob(); - } - const url = await storageImpl.upload(blob as Blob); - return [key, url]; + transformInput: async (input: any) => { + if (Array.isArray(input)) { + return Promise.all(input.map((item) => storageImpl.transformInput(item))); + } else if ( + input instanceof Blob || + (typeof input === 'string' && isDataUri(input)) + ) { + let blob = input; + // If the string is a data URI, convert it to a blob + if (typeof input === 'string' && isDataUri(input)) { + const response = await fetch(input); + blob = await response.blob(); } - if (isPlainObject(value)) { + const url = await storageImpl.upload(blob as Blob); + return url; + } else if (isPlainObject(input)) { + const promises = Object.entries(input).map(async ([key, value]) => { return [key, await storageImpl.transformInput(value)]; - } - if (Array.isArray(value)) { - return [key, await Promise.all(value.map(storageImpl.transformInput))]; - } - return [key, value] as KeyValuePair; - }); - const results = await Promise.all(promises); - return Object.fromEntries(results); + }); + const results = await Promise.all(promises); + return Object.fromEntries(results); + } + // Return the input as is if it's neither an object nor a file/blob/data URI + return input; }, };