Skip to content

Commit

Permalink
fix: allow using package on the frontend without depending on process
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Rincon committed Jul 25, 2024
1 parent 2be9495 commit 8e0e876
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
34 changes: 18 additions & 16 deletions packages/common/helpers/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,37 @@ 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]}`
}

static fetchRegion(): string {
return DEFAULT_REGION
}

static fetchIotUrl(): string {
const env = this.fetchEnvironment()
static fetchIotUrl(env?: Environment): string {
env ??= this.fetchEnvironment()
return `${envToIotUrl[env]}`
}
}
2 changes: 1 addition & 1 deletion packages/common/helpers/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 8e0e876

Please sign in to comment.