3.0.0 • Published 10 months ago

@benjstephenson/config-ts v3.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
10 months ago

config-ts

Small library that reads config values from the node process environment. Config objects are described using a micro format which informs the library how to fetch them. Errors fetching values are accumulated when the config objects are instantiated and ultimately return an Either<NonEmptyList<string>, Config> which can be extracted unsafely using getConfigUnsafe.

Takes some inspiration from PureConfig and Zod

const databaseConfig = readFromEnvironment({
  dbName: { key: "DATABASE_NAME", type: 'string' },
  connectionString: { key: "DATABASE_CONN_STR", type: 'string' },
  autoCommit: { key: "DATABASE_AUTO_COMMIT", type: 'boolean', default: false }
})

const awsConfig = readFromEnvironment({
  region: { key: "AWS_REGION", type: 'string' },
  secret: { key: "AWS_SECRET", type: 'string' },
  services: { key: "AWS_ENABLED_SERVICES", type: 'list' }
})

// Grabs the type for use in the app code, type would be { region: string, secret: string, services: string[] }
type AwsConfig = Infer<typeof awsConfig>

// This will throw an exception of one or more config values couldn't be read.
const appConfig = getConfigUnsafe({
  databaseConfig,
  awsConfig
})

/*
 *  Resulting type of appConfig is
 {
   databaseConfig: { dbName: string, connectionString: string, autoCommit: boolean },
   awsConfig: { region: string, secret: string, services: string[] }
 }
*/
1.0.0

10 months ago

3.0.0

10 months ago

2.0.1

10 months ago

2.0.0

10 months ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago