Suite of AT Protocol TypeScript libraries built on web standards
1import { parseIntWithFallback } from "./util.ts";
2import process from "node:process";
3
4// Detect runtime environment
5const isDeno = typeof Deno !== "undefined";
6
7// Runtime-agnostic environment variable getter
8const getEnvVar = (name: string): string | undefined => {
9 if (isDeno) {
10 return Deno.env.get(name);
11 } else {
12 try {
13 return process?.env?.[name];
14 } catch {
15 return undefined;
16 }
17 }
18};
19
20export const envInt = (name: string): number | undefined => {
21 const str = getEnvVar(name);
22 return parseIntWithFallback(str, undefined);
23};
24
25export const envStr = (name: string): string | undefined => {
26 const str = getEnvVar(name);
27 if (str === undefined || str.length === 0) return undefined;
28 return str;
29};
30
31export const envBool = (name: string): boolean | undefined => {
32 const str = getEnvVar(name);
33 if (str === "true" || str === "1") return true;
34 if (str === "false" || str === "0") return false;
35 return undefined;
36};
37
38export const envList = (name: string): string[] => {
39 const str = getEnvVar(name);
40 if (str === undefined || str.length === 0) return [];
41 return str.split(",");
42};