-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathgetEnv.ts
More file actions
29 lines (23 loc) · 793 Bytes
/
getEnv.ts
File metadata and controls
29 lines (23 loc) · 793 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { env } from "std-env";
/**
* Get an environment variable with optional default value. Runtime agnostic.
*
* @param name The name of the environment variable.
* @param defaultValue The default value to return if the environment variable is not set.
* @returns The value of the environment variable, or the default value if the environment variable is not set.
*
*/
export function getEnvVar(name: string, defaultValue?: string): string | undefined {
return env[name] ?? defaultValue;
}
export function getNumberEnvVar(name: string, defaultValue?: number): number | undefined {
const value = getEnvVar(name);
if (value === undefined) {
return defaultValue;
}
const parsed = Number(value);
if (isNaN(parsed)) {
return defaultValue;
}
return parsed;
}