Skip to content

Commit

Permalink
fix(client): global reference when accessing fetch (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
drochetti authored Aug 7, 2024
1 parent 3965a5d commit 4eb8111
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
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.14.1-alpha.0",
"version": "0.14.1-alpha.3",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
24 changes: 21 additions & 3 deletions libs/client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Config>;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion libs/client/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function dispatchRequest<Input, Output>(
credentials: credentialsValue,
requestMiddleware,
responseHandler,
fetch = global.fetch,
fetch,
} = getConfig();
const userAgent = isBrowser() ? {} : { 'User-Agent': getUserAgent() };
const credentials =
Expand Down
2 changes: 1 addition & 1 deletion libs/client/src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion libs/client/src/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class FalStream<Input, Output> {
// 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(), {
Expand Down

0 comments on commit 4eb8111

Please sign in to comment.