MCPcopy Create free account
hub / github.com/effect-app/libs / all

Function all

repos/effect/packages/effect/src/Config.ts:270–296  ·  view source on GitHub ↗
(
  arg: Arg
)

Source from the content-addressed store, hash-verified

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 */
289export 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 )

Callers

nothing calls this directly

Calls 3

parseMethod · 0.80
makeFunction · 0.70
mapMethod · 0.65

Tested by

no test coverage detected