|
| 1 | +import { type ConfigProvider, make, type Path } from "effect/ConfigProvider" |
| 2 | +import { dual } from "effect/Function" |
| 3 | + |
| 4 | +/** |
| 5 | + * Scopes a provider so that all lookups are prefixed with the given path |
| 6 | + * segments. |
| 7 | + * |
| 8 | + * When to use: |
| 9 | + * - Namespacing config under a prefix like `"app"` or `"database"`. |
| 10 | + * - Reusing the same provider shape for multiple sub-configs. |
| 11 | + * |
| 12 | + * Accepts a single string or a full `Path` array. The prefix is prepended |
| 13 | + * *after* any `mapInput` transformation runs, so ordering matters when |
| 14 | + * composing with {@link mapInput} or {@link constantCase}. |
| 15 | + * |
| 16 | + * Supports both data-last and data-first calling conventions. |
| 17 | + * |
| 18 | + * **Example** (Nesting under a prefix) |
| 19 | + * |
| 20 | + * ```ts |
| 21 | + * import { ConfigProvider } from "effect" |
| 22 | + * |
| 23 | + * const provider = ConfigProvider.fromEnv({ |
| 24 | + * env: { APP_HOST: "localhost", APP_PORT: "3000" } |
| 25 | + * }) |
| 26 | + * |
| 27 | + * // Lookups for ["HOST"] now resolve to ["APP", "HOST"] |
| 28 | + * const scoped = ConfigProvider.nested(provider, "APP") |
| 29 | + * ``` |
| 30 | + * |
| 31 | + * @see {@link mapInput} – for arbitrary path transformations |
| 32 | + * |
| 33 | + * @category Combinators |
| 34 | + * @since 4.0.0 |
| 35 | + */ |
| 36 | +export const nested: { |
| 37 | + (prefix: string | Path): (self: ConfigProvider) => ConfigProvider |
| 38 | + (self: ConfigProvider, prefix: string | Path): ConfigProvider |
| 39 | +} = dual( |
| 40 | + 2, |
| 41 | + (self: ConfigProvider, prefix: string | Path): ConfigProvider => { |
| 42 | + let path: Path = typeof prefix === "string" ? [prefix] : prefix |
| 43 | + if (self.mapInput) path = self.mapInput(path) |
| 44 | + return make(self.get, self.mapInput, self.prefix ? [...self.prefix, ...path] : path) |
| 45 | + } |
| 46 | +) |
| 47 | + |
| 48 | +export * from "effect/ConfigProvider" |
0 commit comments