From d9aaf2a2ba5650d11b8086db8b2eafcfb4c1189c Mon Sep 17 00:00:00 2001 From: Daniel Rochetti Date: Fri, 19 Jul 2024 00:09:21 -0700 Subject: [PATCH 1/4] feat(client): allow custom fetch impl --- apps/demo-nextjs-app-router/app/queue/page.tsx | 14 ++++++++++++++ libs/client/package.json | 2 +- libs/client/src/config.ts | 1 + libs/client/src/request.ts | 1 + libs/client/src/storage.ts | 1 + libs/client/src/streaming.ts | 2 ++ 6 files changed, 20 insertions(+), 1 deletion(-) diff --git a/apps/demo-nextjs-app-router/app/queue/page.tsx b/apps/demo-nextjs-app-router/app/queue/page.tsx index e663a3b..8bad3e7 100644 --- a/apps/demo-nextjs-app-router/app/queue/page.tsx +++ b/apps/demo-nextjs-app-router/app/queue/page.tsx @@ -49,6 +49,20 @@ export default function Home() { setLoading(true); const start = Date.now(); try { + const requestId = await fal.queue.submit( + 'fal-ai/fast-animatediff/turbo/text-to-video', + { + input: { + prompt: prompt, + }, + webhookUrl: + 'https://webhook.site/6679a19d-9dbc-4496-8c2d-53679e417861?userId=1234', + } + ); + console.log(requestId); + setLoading(false); + return; + const result: any = await fal.subscribe(endpointId, { input: JSON.parse(input), logs: true, diff --git a/libs/client/package.json b/libs/client/package.json index 608083e..2d11093 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.13.0", + "version": "0.14.0-alpha.0", "license": "MIT", "repository": { "type": "git", diff --git a/libs/client/src/config.ts b/libs/client/src/config.ts index 5395082..195a1cd 100644 --- a/libs/client/src/config.ts +++ b/libs/client/src/config.ts @@ -13,6 +13,7 @@ export type Config = { proxyUrl?: string; requestMiddleware?: RequestMiddleware; responseHandler?: ResponseHandler; + fetch?: typeof fetch; }; export type RequiredConfig = Required; diff --git a/libs/client/src/request.ts b/libs/client/src/request.ts index 19af2a2..618eb7f 100644 --- a/libs/client/src/request.ts +++ b/libs/client/src/request.ts @@ -14,6 +14,7 @@ export async function dispatchRequest( credentials: credentialsValue, requestMiddleware, responseHandler, + fetch = global.fetch, } = getConfig(); const userAgent = isBrowser() ? {} : { 'User-Agent': getUserAgent() }; const credentials = diff --git a/libs/client/src/storage.ts b/libs/client/src/storage.ts index 1388823..3544815 100644 --- a/libs/client/src/storage.ts +++ b/libs/client/src/storage.ts @@ -76,6 +76,7 @@ type KeyValuePair = [string, any]; export const storageImpl: StorageSupport = { upload: async (file: Blob) => { + const { fetch = global.fetch } = getConfig(); const { upload_url: uploadUrl, file_url: url } = await initiateUpload(file); const response = await fetch(uploadUrl, { method: 'PUT', diff --git a/libs/client/src/streaming.ts b/libs/client/src/streaming.ts index 6dd7aa9..1ee1df4 100644 --- a/libs/client/src/streaming.ts +++ b/libs/client/src/streaming.ts @@ -1,5 +1,6 @@ import { createParser } from 'eventsource-parser'; import { getTemporaryAuthToken } from './auth'; +import { getConfig } from './config'; import { buildUrl } from './function'; import { ApiError, defaultResponseHandler } from './response'; import { storageImpl } from './storage'; @@ -83,6 +84,7 @@ export class FalStream { private start = async () => { const { url, options } = this; const { input, method = 'post' } = options; + const { fetch = global.fetch } = getConfig(); try { const response = await fetch(url, { method: method.toUpperCase(), From f0a48d49ae1666481e743da873967846a20fb4e1 Mon Sep 17 00:00:00 2001 From: Daniel Rochetti Date: Fri, 19 Jul 2024 00:12:10 -0700 Subject: [PATCH 2/4] fix(samples): remove test call --- apps/demo-nextjs-app-router/app/queue/page.tsx | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/apps/demo-nextjs-app-router/app/queue/page.tsx b/apps/demo-nextjs-app-router/app/queue/page.tsx index 8bad3e7..e663a3b 100644 --- a/apps/demo-nextjs-app-router/app/queue/page.tsx +++ b/apps/demo-nextjs-app-router/app/queue/page.tsx @@ -49,20 +49,6 @@ export default function Home() { setLoading(true); const start = Date.now(); try { - const requestId = await fal.queue.submit( - 'fal-ai/fast-animatediff/turbo/text-to-video', - { - input: { - prompt: prompt, - }, - webhookUrl: - 'https://webhook.site/6679a19d-9dbc-4496-8c2d-53679e417861?userId=1234', - } - ); - console.log(requestId); - setLoading(false); - return; - const result: any = await fal.subscribe(endpointId, { input: JSON.parse(input), logs: true, From 6f56b48d3ce6b918a37ad5174da9b734bb245fea Mon Sep 17 00:00:00 2001 From: Daniel Rochetti Date: Fri, 19 Jul 2024 01:05:28 -0700 Subject: [PATCH 3/4] fix: proxy middleware order --- libs/client/package.json | 2 +- libs/client/src/config.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/client/package.json b/libs/client/package.json index 2d11093..e7a4831 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.14.0-alpha.0", + "version": "0.14.0-alpha.1", "license": "MIT", "repository": { "type": "git", diff --git a/libs/client/src/config.ts b/libs/client/src/config.ts index 195a1cd..00315bf 100644 --- a/libs/client/src/config.ts +++ b/libs/client/src/config.ts @@ -65,8 +65,8 @@ export function config(config: Config) { configuration = { ...configuration, requestMiddleware: withMiddleware( - configuration.requestMiddleware, - withProxy({ targetUrl: config.proxyUrl }) + withProxy({ targetUrl: config.proxyUrl }), + configuration.requestMiddleware ), }; } From 30ed3bd4fb9d45084cebd2a83f877b253de39254 Mon Sep 17 00:00:00 2001 From: Daniel Rochetti Date: Mon, 22 Jul 2024 12:51:41 -0700 Subject: [PATCH 4/4] chore(demo): switch to queue polling by default --- apps/demo-nextjs-app-router/app/queue/page.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/demo-nextjs-app-router/app/queue/page.tsx b/apps/demo-nextjs-app-router/app/queue/page.tsx index e663a3b..e979728 100644 --- a/apps/demo-nextjs-app-router/app/queue/page.tsx +++ b/apps/demo-nextjs-app-router/app/queue/page.tsx @@ -52,8 +52,9 @@ export default function Home() { const result: any = await fal.subscribe(endpointId, { input: JSON.parse(input), logs: true, - mode: 'streaming', - // pollInterval: 1000, + // mode: "streaming", + mode: 'polling', + pollInterval: 1000, onQueueUpdate(update) { console.log('queue update'); console.log(update);