feat(proxy): env var resolution (#45)

This commit is contained in:
Daniel Rochetti 2024-01-23 14:14:33 -08:00 committed by GitHub
parent f5f22efd08
commit 12887d4b87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 6 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@fal-ai/serverless-proxy",
"version": "0.7.2",
"version": "0.7.3",
"license": "MIT",
"repository": {
"type": "git",

View File

@ -2,10 +2,9 @@ export const TARGET_URL_HEADER = 'x-fal-target-url';
export const DEFAULT_PROXY_ROUTE = '/api/fal/proxy';
const FAL_KEY = process.env.FAL_KEY || process.env.NEXT_PUBLIC_FAL_KEY;
const FAL_KEY_ID = process.env.FAL_KEY_ID || process.env.NEXT_PUBLIC_FAL_KEY_ID;
const FAL_KEY_SECRET =
process.env.FAL_KEY_SECRET || process.env.NEXT_PUBLIC_FAL_KEY_SECRET;
const FAL_KEY = process.env.FAL_KEY;
const FAL_KEY_ID = process.env.FAL_KEY_ID;
const FAL_KEY_SECRET = process.env.FAL_KEY_SECRET;
export type HeaderValue = string | string[] | undefined | null;
@ -24,6 +23,7 @@ export interface ProxyBehavior<ResponseType> {
getHeader(name: string): HeaderValue;
sendHeader(name: string, value: string): void;
getBody(): Promise<string | undefined>;
resolveApiKey?: () => Promise<string | undefined>;
}
/**
@ -77,7 +77,9 @@ export async function handleRequest<ResponseType>(
return behavior.respondWith(412, `Invalid ${TARGET_URL_HEADER} header`);
}
const falKey = getFalKey();
const falKey = behavior.resolveApiKey
? await behavior.resolveApiKey()
: getFalKey();
if (!falKey) {
return behavior.respondWith(401, 'Missing fal.ai credentials');
}