Last active
April 22, 2025 17:48
-
-
Save graffhyrum/d479b974bd900d76248316cacd662036 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/process.d.ts | |
declare namespace NodeJS { | |
interface ProcessEnv { | |
MY_ENV_VAR: string; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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