fix(client): storage upload array (#66)

This commit is contained in:
Daniel Rochetti 2024-06-02 11:07:50 -07:00 committed by GitHub
parent 5f15da9d83
commit cd2089768d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 27 deletions

View File

@ -1,7 +1,7 @@
{ {
"name": "@fal-ai/serverless-client", "name": "@fal-ai/serverless-client",
"description": "The fal serverless JS/TS client", "description": "The fal serverless JS/TS client",
"version": "0.10.0", "version": "0.10.2",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -103,33 +103,29 @@ export const storageImpl: StorageSupport = {
}, },
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
transformInput: async (input: Record<string, any>) => { transformInput: async (input: any) => {
if (!isPlainObject(input)) { if (Array.isArray(input)) {
return input; return Promise.all(input.map((item) => storageImpl.transformInput(item)));
} } else if (
const promises = Object.entries(input).map(async ([key, value]) => { input instanceof Blob ||
if ( (typeof input === 'string' && isDataUri(input))
value instanceof Blob || ) {
(typeof value === 'string' && isDataUri(value)) let blob = input;
) { // If the string is a data URI, convert it to a blob
let blob = value; if (typeof input === 'string' && isDataUri(input)) {
// if string is a data uri, convert to blob const response = await fetch(input);
if (typeof value === 'string' && isDataUri(value)) { blob = await response.blob();
const response = await fetch(value);
blob = await response.blob();
}
const url = await storageImpl.upload(blob as Blob);
return [key, url];
} }
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)]; return [key, await storageImpl.transformInput(value)];
} });
if (Array.isArray(value)) { const results = await Promise.all(promises);
return [key, await Promise.all(value.map(storageImpl.transformInput))]; return Object.fromEntries(results);
} }
return [key, value] as KeyValuePair; // Return the input as is if it's neither an object nor a file/blob/data URI
}); return input;
const results = await Promise.all(promises);
return Object.fromEntries(results);
}, },
}; };