Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(proxy): env var resolution #45

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/proxy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fal-ai/serverless-proxy",
"version": "0.7.2",
"version": "0.7.3",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
12 changes: 7 additions & 5 deletions libs/proxy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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>;
}

/**
Expand Down Expand Up @@ -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');
}
Expand Down