Hyperlinkv0.8.0-beta.28

Config

Config.nestedconsteffect/Config.ts:1418
(name: string): <A>(self: Config<A>) => Config<A>
<A>(self: Config<A>, name: string): Config<A>

Scopes a config under a named prefix.

When to use

Use when you need to group related config keys under a common namespace.

Details

The prefix is prepended to every key the inner config reads. With fromUnknown this means an extra object level; with fromEnv it means a _-separated prefix on env var names.

Multiple nested calls compose: the outermost name becomes the outermost path segment.

Example (Nesting a struct config under "database")

import { Config, ConfigProvider, Effect } from "effect"

const dbConfig = Config.all({
  host: Config.string("host"),
  port: Config.number("port")
}).pipe(Config.nested("database"))

const provider = ConfigProvider.fromUnknown({
  database: { host: "localhost", port: "5432" }
})
// Effect.runSync(dbConfig.parse(provider))
// { host: "localhost", port: 5432 }

Example (Reading env vars with a nested prefix)

import { Config, ConfigProvider, Effect } from "effect"

const host = Config.string("host").pipe(Config.nested("database"))

const provider = ConfigProvider.fromEnv({
  env: { database_host: "localhost" }
})
// Effect.runSync(host.parse(provider)) // "localhost"
combinatorsallschema
Source effect/Config.ts:14188 lines
export const nested: {
  (name: string): <A>(self: Config<A>) => Config<A>
  <A>(self: Config<A>, name: string): Config<A>
} = dual(
  2,
  <A>(self: Config<A>, name: string): Config<A> =>
    make((provider, pathPrefix) => self.parse(provider, [...pathPrefix, name]))
)