diff --git a/libs/client/package.json b/libs/client/package.json index 6015b2c..36812fc 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.1-alpha.0", + "version": "0.14.1-alpha.3", "license": "MIT", "repository": { "type": "git", diff --git a/libs/client/src/config.ts b/libs/client/src/config.ts index 06aa358..120aba9 100644 --- a/libs/client/src/config.ts +++ b/libs/client/src/config.ts @@ -8,6 +8,17 @@ import { defaultResponseHandler } from './response'; export type CredentialsResolver = () => string | undefined; +type FetchType = typeof fetch; + +export function resolveDefaultFetch(): FetchType { + if (typeof fetch === 'undefined') { + throw new Error( + 'Your environment does not support fetch. Please provide your own fetch implementation.' + ); + } + return fetch; +} + export type Config = { /** * The credentials to use for the fal serverless client. When using the @@ -46,7 +57,7 @@ export type Config = { * The fetch implementation to use for the client requests. By default it uses * the global `fetch` function. */ - fetch?: typeof fetch; + fetch?: FetchType; }; export type RequiredConfig = Required; @@ -94,7 +105,11 @@ let configuration: RequiredConfig; * @param config the new configuration. */ export function config(config: Config) { - configuration = { ...DEFAULT_CONFIG, ...config } as RequiredConfig; + configuration = { + ...DEFAULT_CONFIG, + ...config, + fetch: config.fetch ?? resolveDefaultFetch(), + } as RequiredConfig; if (config.proxyUrl) { configuration = { ...configuration, @@ -125,7 +140,10 @@ export function config(config: Config) { export function getConfig(): RequiredConfig { if (!configuration) { console.info('Using default configuration for the fal client'); - return { ...DEFAULT_CONFIG } as RequiredConfig; + return { + ...DEFAULT_CONFIG, + fetch: resolveDefaultFetch(), + } as RequiredConfig; } return configuration; } diff --git a/libs/client/src/request.ts b/libs/client/src/request.ts index 40dd8c4..fc3e215 100644 --- a/libs/client/src/request.ts +++ b/libs/client/src/request.ts @@ -20,7 +20,7 @@ export async function dispatchRequest( credentials: credentialsValue, requestMiddleware, responseHandler, - fetch = global.fetch, + 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 3544815..a9a2ea1 100644 --- a/libs/client/src/storage.ts +++ b/libs/client/src/storage.ts @@ -76,7 +76,7 @@ type KeyValuePair = [string, any]; export const storageImpl: StorageSupport = { upload: async (file: Blob) => { - const { fetch = global.fetch } = getConfig(); + const { 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 bcca90b..2af54dd 100644 --- a/libs/client/src/streaming.ts +++ b/libs/client/src/streaming.ts @@ -129,7 +129,7 @@ export class FalStream { // if we are in the browser, we need to get a temporary token // to authenticate the request const token = await getTemporaryAuthToken(endpointId); - const { fetch = global.fetch } = getConfig(); + const { fetch } = getConfig(); const parsedUrl = new URL(this.url); parsedUrl.searchParams.set('fal_jwt_token', token); const response = await fetch(parsedUrl.toString(), {