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", "name": "@fal-ai/serverless-client",
"description": "The fal serverless JS/TS client", "description": "The fal serverless JS/TS client",
"version": "0.14.1-alpha.0", "version": "0.14.1-alpha.3",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -8,6 +8,17 @@ import { defaultResponseHandler } from './response';
export type CredentialsResolver = () => string | undefined; 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 = { export type Config = {
/** /**
* The credentials to use for the fal serverless client. When using the * 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 fetch implementation to use for the client requests. By default it uses
* the global `fetch` function. * the global `fetch` function.
*/ */
fetch?: typeof fetch; fetch?: FetchType;
}; };
export type RequiredConfig = Required<Config>; export type RequiredConfig = Required<Config>;
@ -94,7 +105,11 @@ let configuration: RequiredConfig;
* @param config the new configuration. * @param config the new configuration.
*/ */
export function config(config: Config) { export function config(config: Config) {
configuration = { ...DEFAULT_CONFIG, ...config } as RequiredConfig; configuration = {
...DEFAULT_CONFIG,
...config,
fetch: config.fetch ?? resolveDefaultFetch(),
} as RequiredConfig;
if (config.proxyUrl) { if (config.proxyUrl) {
configuration = { configuration = {
...configuration, ...configuration,
@ -125,7 +140,10 @@ export function config(config: Config) {
export function getConfig(): RequiredConfig { export function getConfig(): RequiredConfig {
if (!configuration) { if (!configuration) {
console.info('Using default configuration for the fal client'); console.info('Using default configuration for the fal client');
return { ...DEFAULT_CONFIG } as RequiredConfig; return {
...DEFAULT_CONFIG,
fetch: resolveDefaultFetch(),
} as RequiredConfig;
} }
return configuration; return configuration;
} }

View File

@ -20,7 +20,7 @@ export async function dispatchRequest<Input, Output>(
credentials: credentialsValue, credentials: credentialsValue,
requestMiddleware, requestMiddleware,
responseHandler, responseHandler,
fetch = global.fetch, fetch,
} = getConfig(); } = getConfig();
const userAgent = isBrowser() ? {} : { 'User-Agent': getUserAgent() }; const userAgent = isBrowser() ? {} : { 'User-Agent': getUserAgent() };
const credentials = const credentials =

View File

@ -76,7 +76,7 @@ type KeyValuePair = [string, any];
export const storageImpl: StorageSupport = { export const storageImpl: StorageSupport = {
upload: async (file: Blob) => { upload: async (file: Blob) => {
const { fetch = global.fetch } = getConfig(); const { fetch } = getConfig();
const { upload_url: uploadUrl, file_url: url } = await initiateUpload(file); const { upload_url: uploadUrl, file_url: url } = await initiateUpload(file);
const response = await fetch(uploadUrl, { const response = await fetch(uploadUrl, {
method: 'PUT', 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 // if we are in the browser, we need to get a temporary token
// to authenticate the request // to authenticate the request
const token = await getTemporaryAuthToken(endpointId); const token = await getTemporaryAuthToken(endpointId);
const { fetch = global.fetch } = getConfig(); const { fetch } = getConfig();
const parsedUrl = new URL(this.url); const parsedUrl = new URL(this.url);
parsedUrl.searchParams.set('fal_jwt_token', token); parsedUrl.searchParams.set('fal_jwt_token', token);
const response = await fetch(parsedUrl.toString(), { const response = await fetch(parsedUrl.toString(), {