Skip to content

Commit e585c9c

Browse files
committed
fix Config.nested mapping
1 parent 365a683 commit e585c9c

5 files changed

Lines changed: 70 additions & 1 deletion

File tree

.changeset/bright-trees-count.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect-app": patch
3+
---
4+
5+
fix config nested

packages/effect-app/src/Config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { type Config, make } from "effect/Config"
2+
import { dual } from "effect/Function"
3+
4+
import * as ConfigProvider from "./ConfigProvider.js"
5+
6+
export const nested: {
7+
(name: string): <A>(self: Config<A>) => Config<A>
8+
<A>(self: Config<A>, name: string): Config<A>
9+
} = dual(
10+
2,
11+
<A>(self: Config<A>, name: string): Config<A> => make((provider) => self.parse(ConfigProvider.nested(provider, name)))
12+
)
13+
14+
export * from "effect/Config"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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"

packages/effect-app/src/client/apiClientFactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import * as Config from "effect/Config"
32
import { flow } from "effect/Function"
43
import * as Layer from "effect/Layer"
54
import * as ManagedRuntime from "effect/ManagedRuntime"
65
import * as Predicate from "effect/Predicate"
76
import * as Schema from "effect/Schema"
87
import * as Struct from "effect/Struct"
98
import { Rpc, RpcClient, RpcGroup, RpcSerialization } from "effect/unstable/rpc"
9+
import * as Config from "../Config.js"
1010
import * as Effect from "../Effect.js"
1111
import { HttpClient, HttpClientRequest } from "../http.js"
1212
import * as Option from "../Option.js"

packages/effect-app/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export * as Fnc from "./Function.js"
66
export * as Utils from "./utils.js"
77

88
export * as Array from "./Array.js"
9+
export * as Config from "./Config.js"
10+
export * as ConfigProvider from "./ConfigProvider.js"
911
export * as Effect from "./Effect.js"
1012
export * as Layer from "./Layer.js"
1113
export * as NonEmptySet from "./NonEmptySet.js"

0 commit comments

Comments
 (0)