Skip to content

Instantly share code, notes, and snippets.

@graffhyrum
Last active April 22, 2025 17:48
Show Gist options
  • Save graffhyrum/d479b974bd900d76248316cacd662036 to your computer and use it in GitHub Desktop.
Save graffhyrum/d479b974bd900d76248316cacd662036 to your computer and use it in GitHub Desktop.
// src/process.d.ts
declare namespace NodeJS {
interface ProcessEnv {
MY_ENV_VAR: string;
}
}
/**
* ProcessEnvFacade is an object that provides methods to interact with environment variables.
* It provides methods to get, set, and validate environment variables.
*/
const ProcessEnvFacade = {
getValueOrThrow: (key: keyof NodeJS.ProcessEnv): string => {
const maybeKey = process.env[key];
if (!maybeKey) {
throw new Error(`Environment variable ${key} is not set`);
}
return maybeKey;
},
getValueOrExit: (key: keyof NodeJS.ProcessEnv): string => {
const maybeKey = process.env[key];
if (!maybeKey) {
console.error(`Environment variable ${key} is not set`);
process.exit(1);
}
return maybeKey;
},
getValue: (key: keyof NodeJS.ProcessEnv): string | undefined =>
process.env[key],
setValue: (key: keyof NodeJS.ProcessEnv, value: string) => {
process.env[key] = value;
},
};
export default ProcessEnvFacade;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment