diff options
Diffstat (limited to 'u')
-rw-r--r-- | u/fn/either.ts | 14 | ||||
-rw-r--r-- | u/process/env.ts | 7 | ||||
-rw-r--r-- | u/server/activity/health.ts | 12 | ||||
-rw-r--r-- | u/trace/itrace.ts | 4 |
4 files changed, 19 insertions, 18 deletions
diff --git a/u/fn/either.ts b/u/fn/either.ts index bf90f16..54f9fc2 100644 --- a/u/fn/either.ts +++ b/u/fn/either.ts @@ -32,14 +32,13 @@ export class Either<E, T> implements IEither<E, T> { private readonly self: Left<E> | Right<T>; private constructor( - err?: E, - ok?: T, + init: { err?: E, ok?: T }, public readonly _tag: IEitherTag = iEitherTag, ) { this.self = <Left<E> | Right<T>>{ - isLeft: typeof err !== "undefined", - isRight: typeof ok !== "undefined", - value: typeof err !== "undefined" ? err : ok!, + isLeft: "err" in init, + isRight: "ok" in init, + value: init.err ?? init.ok!, }; } @@ -86,10 +85,11 @@ export class Either<E, T> implements IEither<E, T> { } static left<E, T>(e: E): IEither<E, T> { - return new Either<E, T>(e, undefined); + return new Either<E, T>({ err: e}); } + static right<E, T>(t: T): IEither<E, T> { - return new Either<E, T>(undefined, t); + return new Either<E, T>({ok: t}); } static fromFailable<E, T>(s: Supplier<T>): IEither<E, T> { diff --git a/u/process/env.ts b/u/process/env.ts index c0d893c..470cb39 100644 --- a/u/process/env.ts +++ b/u/process/env.ts @@ -1,11 +1,10 @@ import { Either, type IEither } from "@emprespresso/pengueno"; export const getRequiredEnv = <V extends string>(name: V): IEither<Error, V> => - Either.fromFailable<Error, V>(() => Deno.env.get(name) as V) // could throw when no permission. + Either.fromFailable<Error, V | undefined>(() => Deno.env.get(name) as V | undefined) // could throw when no permission. .flatMap( - (v) => - (v && Either.right(v)) || - Either.left(new Error(`environment variable "${name}" is required D:`)), + (v) => (v && Either.right(v)) || + Either.left(new Error(`environment variable "${name}" is required D:`)) ); type ObjectFromList<T extends ReadonlyArray<string>, V = string> = { diff --git a/u/server/activity/health.ts b/u/server/activity/health.ts index b9dedf9..3b4a23a 100644 --- a/u/server/activity/health.ts +++ b/u/server/activity/health.ts @@ -35,16 +35,18 @@ export class HealthCheckActivityImpl implements IHealthCheckActivity { return req .bimap(TraceUtil.withFunctionTrace(this.checkHealth)) .bimap(TraceUtil.withMetricTrace(healthCheckMetric)) - .flatMap((r) => r.move(HealthCheckInput.CHECK).map(this.check)) + .flatMap((r) => + r.move(HealthCheckInput.CHECK).map((input) => this.check(input)), + ) .peek( TraceUtil.promiseify((h) => h.get().fold(({ isLeft, value }) => { - if (isLeft) { - h.trace.trace(healthCheckMetric.failure); - h.trace.addTrace(LogLevel.ERROR).trace(`${value}`); + if (!isLeft) { + h.trace.trace(healthCheckMetric.success); return; } - h.trace.trace(healthCheckMetric.success); + h.trace.trace(healthCheckMetric.failure); + h.trace.addTrace(LogLevel.ERROR).trace(`${value}`); }), ), ) diff --git a/u/trace/itrace.ts b/u/trace/itrace.ts index 35164b5..c3cb8ad 100644 --- a/u/trace/itrace.ts +++ b/u/trace/itrace.ts @@ -16,7 +16,7 @@ export type ITraceableMapper<T, _T, TraceWith, W = ITraceable<T, TraceWith>> = ( export interface ITraceable<T, Trace = BaseTraceWith> { readonly trace: ITrace<Trace>; get: Supplier<T>; - move: <_T>(u: _T) => ITraceable<_T, Trace>; + move: <_T>(t: _T) => ITraceable<_T, Trace>; map: <_T>(mapper: ITraceableMapper<T, _T, Trace>) => ITraceable<_T, Trace>; bimap: <_T>( mapper: ITraceableMapper< @@ -48,7 +48,7 @@ export class TraceableImpl<T, TraceWith> implements ITraceable<T, TraceWith> { public flatMap<_T>( mapper: ITraceableMapper<T, ITraceable<_T, TraceWith>, TraceWith>, ): ITraceable<_T, TraceWith> { - return mapper(this); + return mapper(this); } public flatMapAsync<_T>( |