From 3965a5d184ebbd72310e6688b6f3d6609fb781fe Mon Sep 17 00:00:00 2001 From: Daniel Rochetti Date: Wed, 7 Aug 2024 10:18:27 -0700 Subject: [PATCH] feat(client): local credentials warning suppress option (#79) * feat(client): local credentials warning suppress option * chore(client): bump version and release alpha --- libs/client/package.json | 2 +- libs/client/src/config.ts | 45 ++++++++++++++++++++++++++++++++++++++ libs/client/src/request.ts | 6 ----- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/libs/client/package.json b/libs/client/package.json index 49deeb9..6015b2c 100644 --- a/libs/client/package.json +++ b/libs/client/package.json @@ -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.0", "license": "MIT", "repository": { "type": "git", diff --git a/libs/client/src/config.ts b/libs/client/src/config.ts index 00315bf..06aa358 100644 --- a/libs/client/src/config.ts +++ b/libs/client/src/config.ts @@ -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; + /** + * The fetch implementation to use for the client requests. By default it uses + * the global `fetch` function. + */ fetch?: typeof fetch; }; @@ -48,6 +81,7 @@ export const credentialsFromEnv: CredentialsResolver = () => { const DEFAULT_CONFIG: Partial = { credentials: credentialsFromEnv, + suppressLocalCredentialsWarning: false, requestMiddleware: (request) => Promise.resolve(request), responseHandler: defaultResponseHandler, }; @@ -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." + ); + } } /** diff --git a/libs/client/src/request.ts b/libs/client/src/request.ts index bf94f30..40dd8c4 100644 --- a/libs/client/src/request.ts +++ b/libs/client/src/request.ts @@ -32,12 +32,6 @@ export async function dispatchRequest( 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',