import * as Either from "effect/Either" import type { Option } from "effect/Option" export type _R> = [T] extends [ Effect ] ? R : never export type _E> = [T] extends [ Effect ] ? E : never /** * @tsplus fluent effect/data/Option encaseInEffect */ export function encaseMaybeInEffect_( o: Option, onError: LazyArg ): Effect { return o.match({ onNone: () => Effect.fail(onError()), onSome: Effect.succeed }) } /** * @tsplus fluent effect/data/Option encaseInEither */ export function encaseMaybeEither_( o: Option, onError: LazyArg ): Either.Either { return o.match({ onNone: () => Either.left(onError()), onSome: Either.right }) } /** * @tsplus getter effect/io/Effect toNullable */ export function toNullable( self: Effect, E, R> ) { return self.map((_) => _.getOrNull) } /** * @tsplus fluent effect/io/Effect scope */ export function scope( scopedEffect: Effect, effect: Effect ): Effect> { return scopedEffect.zipRight(effect).scoped } /** * @tsplus fluent effect/io/Effect flatMapScoped */ export function flatMapScoped( scopedEffect: Effect, effect: (a: A) => Effect ): Effect> { return scopedEffect.flatMap(effect).scoped } // /** // * @tsplus fluent effect/io/Effect withScoped // */ // export function withScoped( // effect: Effect, // scopedEffect: Effect // ): Effect, E | E2, A2> { // return scopedEffect.zipRight(effect).scoped // } // /** // * @tsplus fluent effect/io/Effect withScoped // */ // export function withScopedFlatMap( // effect: (a: A) => Effect, // scopedEffect: Effect // ): Effect, E | E2, A2> { // return scopedEffect.flatMap(effect).scoped // } /** * Recovers from all errors. * * @tsplus static effect/io/Effect.Ops catchAllMap * @tsplus pipeable effect/io/Effect catchAllMap */ export function catchAllMap(f: (e: E) => A2) { return (self: Effect): Effect => self.catchAll((err) => Effect.sync(() => f(err))) } /** * Annotates each log in this effect with the specified log annotations. * @tsplus static effect/io/Effect.Ops annotateLogs */ export function annotateLogs(kvps: Record) { return (effect: Effect): Effect => FiberRef .currentLogAnnotations .get .flatMap((annotations) => Effect.suspend(() => pipe( effect, FiberRef.currentLogAnnotations.locally( HashMap.fromIterable([...annotations, ...kvps.$$.entries]) ) ) ) ) } /** * Annotates each log in this scope with the specified log annotation. * * @tsplus static effect/io/Effect.Ops annotateLogscoped */ export function annotateLogscoped(key: string, value: string) { return FiberRef .currentLogAnnotations .get .flatMap((annotations) => Effect.suspend(() => FiberRef.currentLogAnnotations.locallyScoped(annotations.set(key, value))) ) } /** * Annotates each log in this scope with the specified log annotations. * * @tsplus static effect/io/Effect.Ops annotateLogsScoped */ export function annotateLogsScoped(kvps: Record) { return FiberRef .currentLogAnnotations .get .flatMap((annotations) => Effect.suspend(() => FiberRef.currentLogAnnotations.locallyScoped(HashMap.fromIterable([...annotations, ...kvps.$$.entries])) ) ) } /** * @tsplus fluent function flow */ export function flow(f: (...args: Args) => B, g: (b: B) => C): (...args: Args) => C { return (...args) => g(f(...args)) } /** @tsplus fluent effect/platform/Http/Client request */ export const client: { ( client: HttpClient, req: Effect ): Effect (client: HttpClient, req: ClientRequest): Effect } = (client: HttpClient, req: Effect | ClientRequest) => Effect.isEffect(req) ? req.flatMap(client) : client(req)