From 8e0e87602d227dba1418b3e7ecbbe42c5eb9120b Mon Sep 17 00:00:00 2001 From: Carlos Rincon <“carlos.r@affinidi.com”> Date: Thu, 25 Jul 2024 14:34:59 +0200 Subject: [PATCH] fix: allow using package on the frontend without depending on process --- packages/common/helpers/environment.ts | 34 ++++++++++++++------------ packages/common/helpers/logger.ts | 2 +- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/packages/common/helpers/environment.ts b/packages/common/helpers/environment.ts index 42d41309..703c1095 100644 --- a/packages/common/helpers/environment.ts +++ b/packages/common/helpers/environment.ts @@ -31,26 +31,28 @@ const envToIotUrl = { export class EnvironmentUtils { static fetchEnvironment(): Environment { - const backendEnv = process.env.AFFINIDI_TDK_ENVIRONMENT - if (backendEnv) { - return backendEnv as Environment - } - - const nextPublicEnv = process.env.NEXT_PUBLIC_AFFINIDI_TDK_ENVIRONMENT - if (nextPublicEnv) { - return nextPublicEnv as Environment - } - + try { + if (typeof process !== 'undefined' && process.env) { + const backendEnv = process.env.AFFINIDI_TDK_ENVIRONMENT + if (backendEnv) { + return backendEnv as Environment + } + const nextPublicEnv = process.env.NEXT_PUBLIC_AFFINIDI_TDK_ENVIRONMENT + if (nextPublicEnv) { + return nextPublicEnv as Environment + } + } + } catch (error) {} return Environment.PRODUCTION } - static fetchApiGwUrl(): string { - const env = this.fetchEnvironment() + static fetchApiGwUrl(env?: Environment): string { + env ??= this.fetchEnvironment() return `${envToApiGwUrl[env]}` } - static fetchElementsAuthTokenUrl(): string { - const env = this.fetchEnvironment() + static fetchElementsAuthTokenUrl(env?: Environment): string { + env ??= this.fetchEnvironment() return `${envToElementsAuthTokenUrl[env]}` } @@ -58,8 +60,8 @@ export class EnvironmentUtils { return DEFAULT_REGION } - static fetchIotUrl(): string { - const env = this.fetchEnvironment() + static fetchIotUrl(env?: Environment): string { + env ??= this.fetchEnvironment() return `${envToIotUrl[env]}` } } diff --git a/packages/common/helpers/logger.ts b/packages/common/helpers/logger.ts index 888fb141..69247dea 100644 --- a/packages/common/helpers/logger.ts +++ b/packages/common/helpers/logger.ts @@ -5,7 +5,7 @@ export class Logger { EnvironmentUtils.fetchEnvironment() !== Environment.PRODUCTION static debug(message: string, ...optionalParams: any[]): void { - if (this.isDebugEnabled) { + if (this.isDebugEnabled && typeof console !== 'undefined' && console.log) { console.log(`TDK: ${message}`, ...optionalParams) } }