@@ -167,10 +167,7 @@ export enum EnvVar {
167167/**
168168 * Gets an environment variable, but throws an error if it is not set.
169169 */
170- export function getRequiredEnvVar (
171- env : NodeJS . ProcessEnv ,
172- paramName : string ,
173- ) : string {
170+ function getRequiredEnvVar ( env : NodeJS . ProcessEnv , paramName : string ) : string {
174171 const value = env [ paramName ] ;
175172 if ( value === undefined || value . length === 0 ) {
176173 throw new Error ( `${ paramName } environment variable must be set` ) ;
@@ -180,6 +177,8 @@ export function getRequiredEnvVar(
180177
181178/**
182179 * Get an environment parameter, but throw an error if it is not set.
180+ *
181+ * @deprecated Use `getRequired` of a `ReadOnlyEnv` or `Env` instance instead.
183182 */
184183export function getRequiredEnvParam ( paramName : string ) : string {
185184 return getRequiredEnvVar ( process . env , paramName ) ;
@@ -188,7 +187,7 @@ export function getRequiredEnvParam(paramName: string): string {
188187/**
189188 * Gets an environment variable, but returns `undefined` if it is not set or empty.
190189 */
191- export function getOptionalEnvVarFrom (
190+ function getOptionalEnvVarFrom (
192191 env : NodeJS . ProcessEnv ,
193192 paramName : string ,
194193) : string | undefined {
@@ -201,31 +200,52 @@ export function getOptionalEnvVarFrom(
201200
202201/**
203202 * Get an environment variable, but return `undefined` if it is not set or empty.
203+ *
204+ * @deprecated Use `getOptional` of a `ReadOnlyEnv` or `Env` instance instead.
204205 */
205206export function getOptionalEnvVar ( paramName : string ) : string | undefined {
206207 return getOptionalEnvVarFrom ( process . env , paramName ) ;
207208}
208209
209- /** Gets an `Env` instance for `env`, which is `process.env` by default. */
210- export function getEnv ( env : NodeJS . ProcessEnv = process . env ) : Env {
211- return {
212- getRequired : ( name ) => getRequiredEnvVar ( env , name ) ,
213- getOptional : ( name ) => getOptionalEnvVarFrom ( env , name ) ,
214- entries : ( ) => Object . entries ( env ) ,
215- set : ( name , value ) => {
216- env [ name ] = value ;
217- } ,
218- } ;
219- }
210+ /**
211+ * An abstraction around read-only environment variables, to allow abstracting away from `process.env`
212+ * in tests, while clearly signalling in regular code that the consumer of the `ReadOnlyEnv` instance
213+ * will only read from it.
214+ */
215+ export class ReadOnlyEnv < T extends string | undefined = string | undefined > {
216+ constructor ( protected readonly vars : Record < string , T > ) { }
220217
221- /** A wrapper around an environment, to allow abstracting away from `process.env` in tests. */
222- export interface Env {
223218 /** Tries to get the value for `name` and throws if there isn't one. */
224- getRequired ( name : string ) : string ;
219+ public getRequired ( name : string ) : string {
220+ return getRequiredEnvVar ( this . vars , name ) ;
221+ }
222+
225223 /** Gets the value for `name`, or `undefined` if it isn't set or empty. */
226- getOptional ( name : string ) : string | undefined ;
224+ public getOptional ( name : string ) : string | undefined {
225+ return getOptionalEnvVarFrom ( this . vars , name ) ;
226+ }
227+
227228 /** Gets the entries of the underlying `ProcessEnv`. */
228- entries ( ) : Array < [ string , string | undefined ] > ;
229+ public entries ( ) : Array < [ string , T ] > {
230+ return Object . entries ( this . vars ) ;
231+ }
232+ }
233+
234+ /**
235+ * A wrapper around an environment, to allow abstracting away from `process.env` in tests.
236+ * Use `ReadOnlyEnv` instead if you only plan to read from the environment.
237+ * This type allows writing to the environment.
238+ */
239+ export class Env <
240+ T extends string | undefined = string | undefined ,
241+ > extends ReadOnlyEnv < T > {
229242 /** Sets an environment variable. */
230- set ( name : string , value : string ) : void ;
243+ public set ( name : string , value : T ) : void {
244+ this . vars [ name ] = value ;
245+ }
246+ }
247+
248+ /** Gets an `Env` instance for `env`, which is `process.env` by default. */
249+ export function getEnv ( env : NodeJS . ProcessEnv = process . env ) : Env {
250+ return new Env ( env ) ;
231251}
0 commit comments