From b7d782aedbcbbc327763e1bee300209d24d2a65f Mon Sep 17 00:00:00 2001 From: Kai Haase Date: Thu, 19 Dec 2024 09:54:24 +0100 Subject: [PATCH] Value preparation for env handling --- package-lock.json | 4 ++-- package.json | 2 +- spectaql.yml | 2 +- src/core/common/helpers/config.helper.ts | 25 ++++++++++++++++++++---- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3ccab94..fc64ec3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@lenne.tech/nest-server", - "version": "10.8.1", + "version": "10.8.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@lenne.tech/nest-server", - "version": "10.8.1", + "version": "10.8.2", "license": "MIT", "dependencies": { "@apollo/gateway": "2.9.3", diff --git a/package.json b/package.json index fa28d20..e6165a4 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@lenne.tech/nest-server", - "version": "10.8.1", + "version": "10.8.2", "description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).", "keywords": [ "node", diff --git a/spectaql.yml b/spectaql.yml index bb6239f..574f6a1 100644 --- a/spectaql.yml +++ b/spectaql.yml @@ -11,7 +11,7 @@ servers: info: title: lT Nest Server description: Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases). - version: 10.8.0 + version: 10.8.2 contact: name: lenne.Tech GmbH url: https://lenne.tech diff --git a/src/core/common/helpers/config.helper.ts b/src/core/common/helpers/config.helper.ts index fc07021..d2ca551 100644 --- a/src/core/common/helpers/config.helper.ts +++ b/src/core/common/helpers/config.helper.ts @@ -1,5 +1,4 @@ import * as dotenv from 'dotenv'; -import * as path from 'node:path'; import * as process from 'node:process'; import { join } from 'path'; @@ -62,15 +61,21 @@ export function merge(obj: Record, ...sources: any[]): any { * @param options options for processing * @param options.config config object with different environments as main keys (see config.env.ts) to merge environment configurations into (default: {}) * @param options.defaultEnv default environment to use if no NODE_ENV is set (default: 'local') + * @param options.envPath path to .env file (default: undefined => default of dotenv) */ export function getEnvironmentConfig(options: { config?: Record; defaultEnv?: string; envPath?: string }) { - const { config, defaultEnv } = { + const { config, defaultEnv, envPath } = { config: {}, defaultEnv: 'local', ...options, }; - dotenv.config({ path: options.envPath || path.resolve(process.cwd(), '.env') }); + if (envPath) { + dotenv.config({ path: envPath }); + } else { + dotenv.config(); + } + const env = process.env['NODE' + '_ENV'] || defaultEnv; const envConfig = config[env] || config.local || {}; @@ -140,7 +145,10 @@ export function getEnvironmentConfig(options: { config?: Record; de /** * Get environment object from environment variables */ -export function getEnvironmentObject(options?: { prefix?: string; processEnv?: Record }) { +export function getEnvironmentObject(options?: { + prefix?: string; + processEnv?: Record; +}) { const config = { prefix: 'NSC__', processEnv: process.env, @@ -167,6 +175,15 @@ export function getEnvironmentObject(options?: { prefix?: string; processEnv?: R for (let i = 0; i < path.length; i++) { const segment = path[i]; if (i === path.length - 1) { + // value preparation + if (value === 'true') { + value = true; + } else if (value === 'false') { + value = false; + } else if (!isNaN(Number(value))) { + value = Number(value); + } + current[segment] = value; } else { current = current[segment] = current[segment] || {};