Skip to content

Commit

Permalink
wip: update config getter
Browse files Browse the repository at this point in the history
  • Loading branch information
mathysth committed Mar 17, 2024
1 parent aac8d69 commit 30c8067
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
39 changes: 38 additions & 1 deletion packages/core/src/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
import type { LoggerService } from '@services/logger.service';
import { injectable } from 'inversify';
import { Env } from 'src';

@injectable()
export class CosmosConfig {}
export class CosmosConfig {
constructor(private readonly loggerService: LoggerService) {}

/**
* Get env variables
*/
public get<T>(value: string): T {
const envValue = process.env[value];

if (envValue === 'true') {
return true as T;
}

if (envValue === 'false') {
return false as T;
}

if (!Number.isNaN(Number(envValue))) {
return Number(envValue) as T;
}

return envValue as T;
}

/**
* Validate if env key exist and then return it
*/
public getEnvKey(key: string) {
const envKey = Env.EnvKeys[key];
if (envKey) {
return envKey;
} else {
void this.loggerService.pino.warn(`${key} does not exist in you'r environnement`);
}
}
}
20 changes: 19 additions & 1 deletion packages/core/src/env/env.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
import { injectable } from 'inversify';
import z, { type SafeParseError, type ZodError } from 'zod';
// Fixes the return error from the Zod library.

/**
* Fixes the return error from the Zod library.
*/
function hashError(safeParseReturn: any): safeParseReturn is SafeParseError<ZodError<any>> {
return safeParseReturn?.error;
}

@injectable()
export class Env {
/**
* Access to zod library
*/
public static validator = z;

/**
* Mapping of env key
*/
public static EnvKeys: { [key: string]: any } = {};

/**
* Process and validate env variables
*/
public process(variables: { [key: string]: any }) {
for (const [envVariable, enValues] of Object.entries(variables)) {
Env.EnvKeys[envVariable] = envVariable;
}

const schema = z.object(variables);
const parsed = schema.safeParse(process.env);

Expand Down

0 comments on commit 30c8067

Please sign in to comment.