Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): storage upload array #66

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/client/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
48 changes: 22 additions & 26 deletions libs/client/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
* @returns the file extension or `bin` if the content type is not recognized.
*/
function getExtensionFromContentType(contentType: string): string {
const [_, fileType] = contentType.split('/');

Check warning on line 62 in libs/client/src/storage.ts

View workflow job for this annotation

GitHub Actions / build

'_' is assigned a value but never used
return fileType.split(/[-;]/)[0] ?? 'bin';
}

Expand All @@ -85,7 +85,7 @@
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type KeyValuePair = [string, any];

Check warning on line 88 in libs/client/src/storage.ts

View workflow job for this annotation

GitHub Actions / build

'KeyValuePair' is defined but never used

export const storageImpl: StorageSupport = {
upload: async (file: Blob) => {
Expand All @@ -103,33 +103,29 @@
},

// eslint-disable-next-line @typescript-eslint/no-explicit-any
transformInput: async (input: Record<string, any>) => {
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;
},
};
Loading