I’m working on my first typescript library that makes some API calls and serves up data. I need to use some dynamic code to read environment variables that are defined in the app that uses this library and use them to set the url that I’m making API calls to. So far, my code is always returning the default value instead of reading the environment variables.
I have a config.ts file:
<code>export default class Config {
private getMyEnv(key: string, defaultValue: string): string {
return process.env[key] || defaultValue;
}
private setMyEnv(): string {
return this.getMyEnv('MY_ENV', 'prod');
}
public get DISPATCHER_API_HOST(): string {
const myEnv = this.setmyEnv();
return `https://url-${myEnv}.net/`;
}
}
</code>
<code>export default class Config {
private getMyEnv(key: string, defaultValue: string): string {
return process.env[key] || defaultValue;
}
private setMyEnv(): string {
return this.getMyEnv('MY_ENV', 'prod');
}
public get DISPATCHER_API_HOST(): string {
const myEnv = this.setmyEnv();
return `https://url-${myEnv}.net/`;
}
}
</code>
export default class Config {
private getMyEnv(key: string, defaultValue: string): string {
return process.env[key] || defaultValue;
}
private setMyEnv(): string {
return this.getMyEnv('MY_ENV', 'prod');
}
public get DISPATCHER_API_HOST(): string {
const myEnv = this.setmyEnv();
return `https://url-${myEnv}.net/`;
}
}
And an environment.d.ts:
<code>declare global {
namespace NodeJS {
interface ProcessEnv {
MY_ENV: string; }
}
}
export {};
</code>
<code>declare global {
namespace NodeJS {
interface ProcessEnv {
MY_ENV: string; }
}
}
export {};
</code>
declare global {
namespace NodeJS {
interface ProcessEnv {
MY_ENV: string; }
}
}
export {};