Skip to content

Commit

Permalink
Value preparation for env handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kaihaase committed Dec 19, 2024
1 parent 532d217 commit b7d782a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion spectaql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 21 additions & 4 deletions src/core/common/helpers/config.helper.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -62,15 +61,21 @@ export function merge(obj: Record<string, any>, ...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<string, any>; 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 || {};

Expand Down Expand Up @@ -140,7 +145,10 @@ export function getEnvironmentConfig(options: { config?: Record<string, any>; de
/**
* Get environment object from environment variables
*/
export function getEnvironmentObject(options?: { prefix?: string; processEnv?: Record<string, number | string> }) {
export function getEnvironmentObject(options?: {
prefix?: string;
processEnv?: Record<string, boolean | number | string>;
}) {
const config = {
prefix: 'NSC__',
processEnv: process.env,
Expand All @@ -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] || {};
Expand Down

0 comments on commit b7d782a

Please sign in to comment.