fix(client): global reference when accessing fetch (#80)

This commit is contained in:
Daniel Rochetti 2024-08-07 12:08:55 -07:00 committed by GitHub
parent 3965a5d184
commit 4eb8111cdc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 25 additions and 7 deletions

View File

@ -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",

View File

@ -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<Config>;
@ -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;
}

View File

@ -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 =

View File

@ -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',

View File

@ -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(), {