🗝️ refactor: loadServiceKey to Support Stringified JSON and Env Var Renaming (#8317)

* feat: Enhance loadServiceKey to support stringified JSON input

* chore: Update GOOGLE_SERVICE_KEY_FILE_PATH to GOOGLE_SERVICE_KEY_FILE for consistency
This commit is contained in:
Danny Avila 2025-07-08 21:07:33 -04:00 committed by GitHub
parent e57fc83d40
commit 7e37211458
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 112 additions and 7 deletions

View file

@ -18,8 +18,8 @@ export interface GoogleServiceKey {
}
/**
* Load Google service key from file path or URL
* @param keyPath - The path or URL to the service key file
* Load Google service key from file path, URL, or stringified JSON
* @param keyPath - The path to the service key file, URL to fetch it from, or stringified JSON
* @returns The parsed service key object or null if failed
*/
export async function loadServiceKey(keyPath: string): Promise<GoogleServiceKey | null> {
@ -29,8 +29,17 @@ export async function loadServiceKey(keyPath: string): Promise<GoogleServiceKey
let serviceKey: unknown;
// Check if it's a stringified JSON (starts with '{')
if (keyPath.trim().startsWith('{')) {
try {
serviceKey = JSON.parse(keyPath);
} catch (error) {
logger.error('Failed to parse service key from stringified JSON', error);
return null;
}
}
// Check if it's a URL
if (/^https?:\/\//.test(keyPath)) {
else if (/^https?:\/\//.test(keyPath)) {
try {
const response = await axios.get(keyPath);
serviceKey = response.data;