Skip to content

Commit

Permalink
Merge pull request #2 from knpwrs/require-env
Browse files Browse the repository at this point in the history
Add requireEnv function
  • Loading branch information
Pinta365 authored Mar 6, 2024
2 parents 3a0b606 + 61d39f4 commit 5773f1c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ export class UnsupportedEnvironmentError extends Error {
}
}

/**
* Error thrown when attempting to retrieve undefined environment variables.
*/
export class UndefinedEnvironmentError extends Error {
constructor(key: string) {
super(`${key} not defined in environment.`);
}
}

/**
* Error thrown when attempting to validate an environment variable.
*/
Expand Down
23 changes: 22 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* retrieving environment variables across Deno, Bun and Node.js
*/

import { EnvOptions, UnsupportedEnvironmentError, ValidationError, ValidatorFunction } from "./lib/helpers.ts";
import { EnvOptions, UndefinedEnvironmentError, UnsupportedEnvironmentError, ValidationError, ValidatorFunction } from "./lib/helpers.ts";
import { deepMerge } from "@cross/deepmerge";
import { getCurrentRuntime } from "@cross/runtime";
import { loadEnvFile } from "./lib/filehandler.ts";
Expand Down Expand Up @@ -103,6 +103,27 @@ export function getEnv(key: string): string | undefined {
}
}

/**
* Gets an environment variable across different supported runtimes and throws
* an error if the environment variable is not defined.
*
* @param {string} key - The name of the environment variable.
* @returns {string} The value of the environment variable, or undefined if not found.
* @throws {UndefinedEnvironmentError} if the current runtime is unsupported
* and the 'throwErrors' flag is set.
* @throws {UnsupportedEnvironmentError} if the current runtime is unsupported
* and the 'throwErrors' flag is set.
*/
export function requireEnv(key: string): string {
const value = getEnv(key);

if (!value) {
throw new UndefinedEnvironmentError(key);
}

return value;
}

/**
* Set an environment variable in supported runtimes.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getAllEnv,
getEnv,
hasEnv,
requireEnv,
setEnv,
setupEnv,
validateAndGetEnv,
Expand Down Expand Up @@ -32,6 +33,26 @@ Deno.test({
},
});

/** ==== requireEnv() ==== */
Deno.test({
name: "requireEnv() retrieves an existing environment variable",
fn() {
Deno.env.set("TEST_VARIABLE", "hello");

const value = requireEnv("TEST_VARIABLE");
assertEquals(value, "hello");

Deno.env.delete("TEST_VARIABLE");
},
});

Deno.test({
name: "requireEnv() throws for nonexistant keys",
fn: () => {
assertThrows(() => requireEnv("TEST_KEY_NONEXISTANT"), "TEST_KEY_NONEXISTANT not defined in environment.")
},
});

/** ==== getAllEnv() ==== */
Deno.test({
name: "getAllEnv() with prefix filters variables",
Expand Down

0 comments on commit 5773f1c

Please sign in to comment.