11import EnvironmentVariableError from './EnvironmentVariableError' ;
22import { Parser } from './parsers' ;
33
4- export type Env < TSchema extends Schema > = Readonly <
5- { [ key in keyof TSchema ] : SchemaEntryType < TSchema [ key ] > }
6- > ;
4+ export type Env < T > = { [ K in keyof Schema < T > ] : T [ K ] } ;
75
86/**
97 * A schema defines the environment variable
108 * requirements.
119 */
12- export type Schema = Readonly < { [ name : string ] : SchemaEntry } > ;
10+ export type Schema < T > = { [ K in keyof T ] : SchemaEntry < T [ K ] > } ;
1311
14- export type SchemaEntry = Readonly <
15- {
16- parser : Parser ;
17- envVarName : string ;
18- } & SchemaEntryRequiredInfo
19- > ;
12+ export type SchemaEntry < T > = {
13+ parser : Parser < T > ;
14+ envVarName : string ;
15+ } & SchemaEntryRequiredInfo ;
2016
21- export type SchemaEntryRequiredInfo = Readonly <
17+ export type SchemaEntryRequiredInfo =
2218 | {
2319 required : true ;
2420 }
@@ -28,37 +24,31 @@ export type SchemaEntryRequiredInfo = Readonly<
2824 * The default value of the environment variable.
2925 */
3026 defaultEnvVarValue : string ;
31- }
32- > ;
33-
34- export type SchemaEntryType < TSchemaEntry extends SchemaEntry > = ReturnType <
35- TSchemaEntry [ 'parser' ]
36- > ;
27+ } ;
3728
3829/**
3930 * Returns an env object based on a schema.
4031 */
41- export function makeEnv < TSchema extends Schema > ( schema : TSchema ) : Env < TSchema > {
32+ export function makeEnv < T extends { [ key : string ] : any } > (
33+ schema : Schema < T > ,
34+ ) : Env < T > {
4235 const env = Object . entries ( schema ) . reduce ( ( acc , [ key , schemaEntry ] ) => {
43- const value = getValue ( key , schemaEntry ) ;
36+ const value = getValue ( key , schemaEntry as any ) ;
4437
4538 return { ...acc , [ key ] : value } ;
46- } , { } ) as Env < TSchema > ;
39+ } , { } ) as Env < T > ;
4740
4841 return env ;
4942}
5043
51- function getValue < TSchemaEntry extends SchemaEntry > (
52- key : string ,
53- schemaEntry : TSchemaEntry ,
54- ) : SchemaEntryType < TSchemaEntry > {
44+ function getValue < T > ( key : string , schemaEntry : SchemaEntry < T > ) : T {
5545 const serializedValue = getSerializedValue ( schemaEntry ) ;
5646 const value = parseSerializedValue ( key , serializedValue , schemaEntry ) ;
5747
5848 return value ;
5949}
6050
61- function getSerializedValue ( schemaEntry : SchemaEntry ) : string {
51+ function getSerializedValue < T > ( schemaEntry : SchemaEntry < T > ) : string {
6252 const envVarValue = process . env [ schemaEntry . envVarName ] ;
6353
6454 if ( envVarValue !== undefined ) {
@@ -82,12 +72,12 @@ function getSerializedValue(schemaEntry: SchemaEntry): string {
8272 return serializedValue ;
8373}
8474
85- function parseSerializedValue < TSchemaEntry extends SchemaEntry > (
75+ function parseSerializedValue < T > (
8676 key : string ,
8777 serializedValue : string ,
88- schemaEntry : TSchemaEntry ,
89- ) : SchemaEntryType < TSchemaEntry > {
90- let value : SchemaEntryType < TSchemaEntry > ;
78+ schemaEntry : SchemaEntry < T > ,
79+ ) : T {
80+ let value : T ;
9181
9282 try {
9383 value = schemaEntry . parser ( serializedValue ) ;
0 commit comments