Skip to content

Commit

Permalink
feat(client): allow custom fetch impl (#74)
Browse files Browse the repository at this point in the history
* feat(client): allow custom fetch impl

* fix(samples): remove test call

* fix: proxy middleware order

* chore(demo): switch to queue polling by default
  • Loading branch information
drochetti authored Jul 22, 2024
1 parent cf300e9 commit 2839e79
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 5 deletions.
5 changes: 3 additions & 2 deletions apps/demo-nextjs-app-router/app/queue/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
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.13.0",
"version": "0.14.0-alpha.1",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
5 changes: 3 additions & 2 deletions libs/client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type Config = {
proxyUrl?: string;
requestMiddleware?: RequestMiddleware;
responseHandler?: ResponseHandler<any>;
fetch?: typeof fetch;
};

export type RequiredConfig = Required<Config>;
Expand Down Expand Up @@ -64,8 +65,8 @@ export function config(config: Config) {
configuration = {
...configuration,
requestMiddleware: withMiddleware(
configuration.requestMiddleware,
withProxy({ targetUrl: config.proxyUrl })
withProxy({ targetUrl: config.proxyUrl }),
configuration.requestMiddleware
),
};
}
Expand Down
1 change: 1 addition & 0 deletions libs/client/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export async function dispatchRequest<Input, Output>(
credentials: credentialsValue,
requestMiddleware,
responseHandler,
fetch = global.fetch,
} = getConfig();
const userAgent = isBrowser() ? {} : { 'User-Agent': getUserAgent() };
const credentials =
Expand Down
1 change: 1 addition & 0 deletions libs/client/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions libs/client/src/streaming.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -83,6 +84,7 @@ export class FalStream<Input, Output> {
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(),
Expand Down

0 comments on commit 2839e79

Please sign in to comment.