From 12887d4b876793ef060924e5bc10a08d39d6313a Mon Sep 17 00:00:00 2001 From: Daniel Rochetti Date: Tue, 23 Jan 2024 14:14:33 -0800 Subject: [PATCH] feat(proxy): env var resolution (#45) --- libs/proxy/package.json | 2 +- libs/proxy/src/index.ts | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/libs/proxy/package.json b/libs/proxy/package.json index 21e6a44..c2e89a4 100644 --- a/libs/proxy/package.json +++ b/libs/proxy/package.json @@ -1,6 +1,6 @@ { "name": "@fal-ai/serverless-proxy", - "version": "0.7.2", + "version": "0.7.3", "license": "MIT", "repository": { "type": "git", diff --git a/libs/proxy/src/index.ts b/libs/proxy/src/index.ts index c8b365c..1b90fde 100644 --- a/libs/proxy/src/index.ts +++ b/libs/proxy/src/index.ts @@ -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 { getHeader(name: string): HeaderValue; sendHeader(name: string, value: string): void; getBody(): Promise; + resolveApiKey?: () => Promise; } /** @@ -77,7 +77,9 @@ export async function handleRequest( 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'); }