Skip to content

Commit

Permalink
fix(client): global reference when accessing fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
drochetti committed Aug 7, 2024
1 parent 6edbf29 commit eb76a5a
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.0",
"version": "0.14.1-alpha.2",
"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,12 +8,23 @@ 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 = {
credentials?: undefined | string | CredentialsResolver;
proxyUrl?: string;
requestMiddleware?: RequestMiddleware;
responseHandler?: ResponseHandler<any>;

Check warning on line 26 in libs/client/src/config.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
fetch?: typeof fetch;
fetch?: FetchType;
};

export type RequiredConfig = Required<Config>;
Expand Down Expand Up @@ -60,7 +71,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 All @@ -80,7 +95,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 eb76a5a

Please sign in to comment.