| 268 | * |
| 269 | * Use when you need to transform a parsed config value with a function that can |
| 270 | * produce a `ConfigError` (e.g. parsing a URL, checking a range). |
| 271 | * |
| 272 | * **Example** (Wrapping a value in an effectful transformation) |
| 273 | * |
| 274 | * ```ts import.meta.vitest |
| 275 | * import { Config, ConfigProvider, Effect } from "effect" |
| 276 | * |
| 277 | * const trimmed = Config.string("name").pipe( |
| 278 | * Config.mapOrFail((s) => Effect.succeed(s.trim())) |
| 279 | * ) |
| 280 | * const provider = ConfigProvider.fromUnknown({ name: " Alice " }) |
| 281 | * Effect.runSync(trimmed.parse(provider)) // => "Alice" |
| 282 | * ``` |
| 283 | * |
| 284 | * @see {@link map} – when the transformation cannot fail |
| 285 | * |
| 286 | * @category mapping |
| 287 | * @since 2.0.0 |
| 288 | */ |
| 289 | export const mapOrFail: { |
| 290 | <A, B>(f: (a: A) => Effect.Effect<B, ConfigError>): (self: Config<A>) => Config<B> |
| 291 | <A, B>(self: Config<A>, f: (a: A) => Effect.Effect<B, ConfigError>): Config<B> |
| 292 | } = dual(2, <A, B>(self: Config<A>, f: (a: A) => Effect.Effect<B, ConfigError>): Config<B> => { |
| 293 | return make((provider, pathPrefix) => |
| 294 | Effect.flatMap(evaluateAt(self, provider, pathPrefix), (resolution) => |
| 295 | resolution._tag === "Resolved" |
| 296 | ? f(resolution.value).pipe( |
| 297 | Effect.mapEager((value) => resolved(value, resolution.hasInput)), |
| 298 | Effect.mapErrorEager((error) => evaluationFailure(error, resolution.hasInput)) |
| 299 | ) |