|
1 | 1 | import { ImageModelV1, NoSuchModelError, ProviderV1 } from '@ai-sdk/provider';
|
2 | 2 | import type { FetchFunction } from '@ai-sdk/provider-utils';
|
3 |
| -import { loadApiKey, withoutTrailingSlash } from '@ai-sdk/provider-utils'; |
| 3 | +import { withoutTrailingSlash } from '@ai-sdk/provider-utils'; |
4 | 4 | import { FalImageModel } from './fal-image-model';
|
5 | 5 | import { FalImageModelId, FalImageSettings } from './fal-image-settings';
|
6 | 6 |
|
7 | 7 | export interface FalProviderSettings {
|
8 | 8 | /**
|
9 | 9 | fal.ai API key. Default value is taken from the `FAL_API_KEY` environment
|
10 |
| -variable. |
| 10 | +variable, falling back to `FAL_KEY`. |
11 | 11 | */
|
12 | 12 | apiKey?: string;
|
13 | 13 |
|
@@ -46,16 +46,55 @@ Creates a model for image generation.
|
46 | 46 |
|
47 | 47 | const defaultBaseURL = 'https://fal.run';
|
48 | 48 |
|
| 49 | +function loadFalApiKey({ |
| 50 | + apiKey, |
| 51 | + description = 'fal.ai', |
| 52 | +}: { |
| 53 | + apiKey: string | undefined; |
| 54 | + description?: string; |
| 55 | +}): string { |
| 56 | + if (typeof apiKey === 'string') { |
| 57 | + return apiKey; |
| 58 | + } |
| 59 | + |
| 60 | + if (apiKey != null) { |
| 61 | + throw new Error(`${description} API key must be a string.`); |
| 62 | + } |
| 63 | + |
| 64 | + if (typeof process === 'undefined') { |
| 65 | + throw new Error( |
| 66 | + `${description} API key is missing. Pass it using the 'apiKey' parameter. Environment variables are not supported in this environment.`, |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + let envApiKey = process.env.FAL_API_KEY; |
| 71 | + if (envApiKey == null) { |
| 72 | + envApiKey = process.env.FAL_KEY; |
| 73 | + } |
| 74 | + |
| 75 | + if (envApiKey == null) { |
| 76 | + throw new Error( |
| 77 | + `${description} API key is missing. Pass it using the 'apiKey' parameter or set either the FAL_API_KEY or FAL_KEY environment variable.`, |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + if (typeof envApiKey !== 'string') { |
| 82 | + throw new Error( |
| 83 | + `${description} API key must be a string. The value of the environment variable is not a string.`, |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + return envApiKey; |
| 88 | +} |
| 89 | + |
49 | 90 | /**
|
50 | 91 | Create a fal.ai provider instance.
|
51 | 92 | */
|
52 | 93 | export function createFal(options: FalProviderSettings = {}): FalProvider {
|
53 | 94 | const baseURL = withoutTrailingSlash(options.baseURL ?? defaultBaseURL);
|
54 | 95 | const getHeaders = () => ({
|
55 |
| - Authorization: `Key ${loadApiKey({ |
| 96 | + Authorization: `Key ${loadFalApiKey({ |
56 | 97 | apiKey: options.apiKey,
|
57 |
| - environmentVariableName: 'FAL_API_KEY', |
58 |
| - description: 'fal.ai', |
59 | 98 | })}`,
|
60 | 99 | ...options.headers,
|
61 | 100 | });
|
|
0 commit comments