feat: add dotenv and an option to check env variables

This commit is contained in:
Louis
2025-10-26 16:59:38 +01:00
parent bb00f74d17
commit 929f0e38f6
4 changed files with 11 additions and 7 deletions

2
package-lock.json generated
View File

@@ -22,7 +22,7 @@
"eslint": "^8.54.0", "eslint": "^8.54.0",
"nodemon": "^3.0.2", "nodemon": "^3.0.2",
"prettier": "^3.1.0", "prettier": "^3.1.0",
"ts-node": "^10.9.1", "ts-node": "^10.9.2",
"typescript": "^5.3.2" "typescript": "^5.3.2"
} }
}, },

View File

@@ -32,7 +32,7 @@
"eslint": "^8.54.0", "eslint": "^8.54.0",
"nodemon": "^3.0.2", "nodemon": "^3.0.2",
"prettier": "^3.1.0", "prettier": "^3.1.0",
"ts-node": "^10.9.1", "ts-node": "^10.9.2",
"typescript": "^5.3.2" "typescript": "^5.3.2"
} }
} }

View File

@@ -18,7 +18,6 @@ import dotenv from 'dotenv';
*/ */
abstract class Client { abstract class Client {
// log env variables to verify they are loaded // log env variables to verify they are loaded
@@ -26,11 +25,10 @@ abstract class Client {
private infisicalClient: InfisicalSDK; private infisicalClient: InfisicalSDK;
constructor() { constructor() {
dotenv.config();
this.infisicalClient = new InfisicalSDK({ this.infisicalClient = new InfisicalSDK({
siteUrl: process.env.INFISICAL_URL, siteUrl: process.env.INFISICAL_URL,
}); });
dotenv.config();
console.log("INFISICAL_URL:", process.env.INFISICAL_URL);
} }
init(): Effect.Effect<void, Error, never> { init(): Effect.Effect<void, Error, never> {
@@ -43,9 +41,13 @@ abstract class Client {
}) })
} }
getCredential(key: string): Effect.Effect<string, Error, never> { getCredential(key: string, checkEnv: boolean = false): Effect.Effect<string, Error, never> {
const self = this; const self = this;
return Effect.gen(function* (this: Client) { return Effect.gen(function* (this: Client) {
if (checkEnv && process.env[key]) {
yield* Effect.log('Using environment variable for key: ' + key);
return process.env[key]!;
}
const secret = yield* Effect.tryPromise({ const secret = yield* Effect.tryPromise({
try: () => self.infisicalClient.secrets().getSecret({ try: () => self.infisicalClient.secrets().getSecret({
environment: "dev", environment: "dev",

View File

@@ -24,7 +24,9 @@ class StravaClient extends Client {
const superInit = super.init(); const superInit = super.init();
return Effect.gen(function* () { return Effect.gen(function* () {
yield* superInit; yield* superInit;
const refreshToken = yield* self.getCredential('REFRESH_TOKEN'); // Retrieve credentials from Infisical, for refresh token as it represents user account
// There's an option to check env variables first
const refreshToken = yield* self.getCredential('REFRESH_TOKEN', true);
const clientId = yield* self.getCredential('CLIENT_ID'); const clientId = yield* self.getCredential('CLIENT_ID');
const clientSecret = yield* self.getCredential('CLIENT_SECRET'); const clientSecret = yield* self.getCredential('CLIENT_SECRET');
self.auth = makeStravaAuth( self.auth = makeStravaAuth(