Skip to content

Commit

Permalink
feat(client): local credentials warning suppress option
Browse files Browse the repository at this point in the history
  • Loading branch information
drochetti committed Aug 7, 2024
1 parent 6edbf29 commit 0475feb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
45 changes: 45 additions & 0 deletions libs/client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,43 @@ import { defaultResponseHandler } from './response';
export type CredentialsResolver = () => string | undefined;

export type Config = {
/**
* The credentials to use for the fal serverless client. When using the
* client in the browser, it's recommended to use a proxy server to avoid
* exposing the credentials in the client's environment.
*
* By default it tries to use the `FAL_KEY` environment variable, when
* `process.env` is defined.
*
* @see https://fal.ai/docs/model-endpoints/server-side
* @see #suppressLocalCredentialsWarning
*/
credentials?: undefined | string | CredentialsResolver;
/**
* Suppresses the warning when the fal credentials are exposed in the
* browser's environment. Make sure you understand the security implications
* before enabling this option.
*/
suppressLocalCredentialsWarning?: boolean;
/**
* The URL of the proxy server to use for the client requests. The proxy
* server should forward the requests to the fal serverless rest api.
*/
proxyUrl?: string;
/**
* The request middleware to use for the client requests. By default it
* doesn't apply any middleware.
*/
requestMiddleware?: RequestMiddleware;
/**
* 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 44 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;
};

Expand Down Expand Up @@ -48,6 +81,7 @@ export const credentialsFromEnv: CredentialsResolver = () => {

const DEFAULT_CONFIG: Partial<Config> = {
credentials: credentialsFromEnv,
suppressLocalCredentialsWarning: false,
requestMiddleware: (request) => Promise.resolve(request),
responseHandler: defaultResponseHandler,
};
Expand All @@ -70,6 +104,17 @@ export function config(config: Config) {
),
};
}
const { credentials, suppressLocalCredentialsWarning } = configuration;
if (
typeof window !== 'undefined' &&
credentials &&
!suppressLocalCredentialsWarning
) {
console.warn(
"The fal credentials are exposed in the browser's environment. " +
"That's not recommended for production use cases."
);
}
}

/**
Expand Down
6 changes: 0 additions & 6 deletions libs/client/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ export async function dispatchRequest<Input, Output>(
url: targetUrl,
});
const authHeader = credentials ? { Authorization: `Key ${credentials}` } : {};
if (typeof window !== 'undefined' && credentials) {
console.warn(
"The fal credentials are exposed in the browser's environment. " +
"That's not recommended for production use cases."
);
}
const requestHeaders = {
...authHeader,
Accept: 'application/json',
Expand Down

0 comments on commit 0475feb

Please sign in to comment.