-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathconfig.ts
More file actions
155 lines (143 loc) · 4.47 KB
/
config.ts
File metadata and controls
155 lines (143 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import {
withMiddleware,
withProxy,
type RequestMiddleware,
} from "./middleware";
import type { ResponseHandler } from "./response";
import { defaultResponseHandler } from "./response";
import { DEFAULT_RETRY_OPTIONS, type RetryOptions } from "./retry";
import { isBrowser } from "./runtime";
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 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 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>;
/**
* The fetch implementation to use for the client requests. By default it uses
* the global `fetch` function.
*/
fetch?: FetchType;
/**
* Retry configuration for handling transient errors like rate limiting and server errors.
* When not specified, a default retry configuration is used.
*/
retry?: Partial<RetryOptions>;
};
export type RequiredConfig = Required<Config>;
/**
* Checks if the required FAL environment variables are set.
*
* @returns `true` if the required environment variables are set,
* `false` otherwise.
*/
function hasEnvVariables(): boolean {
return (
typeof process !== "undefined" &&
process.env &&
(typeof process.env.FAL_KEY !== "undefined" ||
(typeof process.env.FAL_KEY_ID !== "undefined" &&
typeof process.env.FAL_KEY_SECRET !== "undefined"))
);
}
export const credentialsFromEnv: CredentialsResolver = () => {
if (!hasEnvVariables()) {
return undefined;
}
if (typeof process.env.FAL_KEY !== "undefined") {
return process.env.FAL_KEY;
}
return process.env.FAL_KEY_ID
? `${process.env.FAL_KEY_ID}:${process.env.FAL_KEY_SECRET}`
: undefined;
};
const DEFAULT_CONFIG: Partial<Config> = {
credentials: credentialsFromEnv,
suppressLocalCredentialsWarning: false,
requestMiddleware: (request) => Promise.resolve(request),
responseHandler: defaultResponseHandler,
retry: DEFAULT_RETRY_OPTIONS,
};
/**
* Configures the fal client.
*
* @param config the new configuration.
*/
export function createConfig(config: Config): RequiredConfig {
let configuration = {
...DEFAULT_CONFIG,
...config,
fetch: config.fetch ?? resolveDefaultFetch(),
// Merge retry configuration with defaults
retry: {
...DEFAULT_RETRY_OPTIONS,
...(config.retry || {}),
},
} as RequiredConfig;
if (config.proxyUrl) {
configuration = {
...configuration,
requestMiddleware: withMiddleware(
configuration.requestMiddleware,
withProxy({ targetUrl: config.proxyUrl }),
),
};
}
const { credentials: resolveCredentials, suppressLocalCredentialsWarning } =
configuration;
const credentials =
typeof resolveCredentials === "function"
? resolveCredentials()
: resolveCredentials;
if (isBrowser() && credentials && !suppressLocalCredentialsWarning) {
console.warn(
"The fal credentials are exposed in the browser's environment. " +
"That's not recommended for production use cases.",
);
}
return configuration;
}
/**
* @returns the URL of the fal REST api endpoint.
*/
export function getRestApiUrl(): string {
return "https://rest.fal.ai";
}