Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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 @@ -41,12 +52,12 @@
* The response handler to use for the client requests. By default it uses
* a built-in response handler that returns the JSON response.
*/
responseHandler?: ResponseHandler<any>;

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

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
/**
* 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 @@
* @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 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 @@ -7,7 +7,7 @@
navigator?.userAgent === 'Cloudflare-Workers';

type RequestOptions = {
responseHandler?: ResponseHandler<any>;

Check warning on line 10 in libs/client/src/request.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
};

export async function dispatchRequest<Input, Output>(
Expand All @@ -20,7 +20,7 @@
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 @@ -46,7 +46,7 @@
* @returns the file extension or `bin` if the content type is not recognized.
*/
function getExtensionFromContentType(contentType: string): string {
const [_, fileType] = contentType.split('/');

Check warning on line 49 in libs/client/src/storage.ts

View workflow job for this annotation

GitHub Actions / build

'_' is assigned a value but never used
return fileType.split(/[-;]/)[0] ?? 'bin';
}

Expand Down Expand Up @@ -76,7 +76,7 @@

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 All @@ -97,7 +97,7 @@
} else if (input instanceof Blob) {
return await storageImpl.upload(input);
} else if (isPlainObject(input)) {
const inputObject = input as Record<string, any>;

Check warning on line 100 in libs/client/src/storage.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const promises = Object.entries(inputObject).map(
async ([key, value]): Promise<KeyValuePair> => {
return [key, await storageImpl.transformInput(value)];
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 @@ -67,7 +67,7 @@

type FalStreamEventType = 'data' | 'error' | 'done';

type EventHandler<T = any> = (event: T) => void;

Check warning on line 70 in libs/client/src/streaming.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

/**
* The class representing a streaming response. With t
Expand Down Expand Up @@ -129,7 +129,7 @@
// 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 Expand Up @@ -212,7 +212,7 @@
this.emit('data', parsedData);

// also emit 'message'for backwards compatibility
this.emit('message' as any, parsedData);

Check warning on line 215 in libs/client/src/streaming.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
} catch (e) {
this.emit('error', e);
}
Expand Down Expand Up @@ -248,7 +248,7 @@
return;
};

private handleError = (error: any) => {

Check warning on line 251 in libs/client/src/streaming.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const apiError =
error instanceof ApiError
? error
Expand All @@ -267,7 +267,7 @@
this.listeners.get(type)?.push(listener);
};

private emit = (type: FalStreamEventType, event: any) => {

Check warning on line 270 in libs/client/src/streaming.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const listeners = this.listeners.get(type) || [];
for (const listener of listeners) {
listener(event);
Expand Down
Loading