minauth
Version:
A TypeScript library for building authentication systems on top of the Mina blockchain and other zero-knowledge proofs solutions.
48 lines • 2.3 kB
JavaScript
import { pipe } from 'fp-ts/lib/function.js';
import * as RTE from 'fp-ts/lib/ReaderTaskEither.js';
import * as IOE from 'fp-ts/lib/IOEither.js';
import * as E from 'fp-ts/lib/Either.js';
/**
* Construct a ReaderTaskEither from a function that performs side effects and
* may throw.
*/
export const tryCatchIO = (f, onThrow) => pipe(RTE.ask(), RTE.chain((env) => RTE.fromIOEither(IOE.tryCatch(() => f(env), onThrow))));
/**
* Convert a promise that may reject into a ReaderTaskEither.
*/
export const tryCatch = (f, onThrow) => pipe(RTE.ask(), RTE.chain((env) => RTE.fromTaskEither(() => f(env).then((res) => E.right(res), (err) => E.left(onThrow(err))))));
/**
* Given an error and a condition, throw the error when the condition is false.
*/
export const guard = (err) => (cond) => cond ? RTE.right(undefined) : RTE.left(err);
export const liftZodParseResult = (r, onThrow) => r.success ? RTE.right(r.data) : RTE.left(onThrow(r.error));
/**
* Assuming the reader environment is a string-indexable object, return the field
* in the environment of the given key.
*/
export const askRecordField = (key) => RTE.asks((env) => env[key]);
/**
* Returns the logger stored within RTE env.
*/
export const askLogger = () => askRecordField('logger');
/**
* Pass a function accepting the logger stored within RTE env
* to call it with the logger.
*/
export const useLogger = (f) => pipe(askRecordField('logger'), RTE.chain((logger) => RTE.fromIO(() => f(logger))), RTE.asUnit);
/**
* Pass a function accepting the logger stored within RTE env
* to call it with the logger.
* It will passthrough the given task either action
* - to be used within monadic actions chains.
*/
export const tapLogger = (f) => RTE.tap((x) => pipe(askRecordField('logger'), RTE.chain((logger) => RTE.fromIO(() => f(logger, x)))));
/**
* Returns a sublogger of the logger stored within RTE env.
*/
export const askSublogger = (loggerName) => pipe(askRecordField('logger'), RTE.chainIOK((logger) => () => logger.getSubLogger({ name: loggerName })));
/**
* Execute a computation in a modified environment.
*/
export const withRTE = (mapErr, mapEnv) => (f) => pipe(RTE.ask(), RTE.map(mapEnv), RTE.chain((r2) => pipe(RTE.fromTaskEither(f(r2)), RTE.mapLeft(mapErr))));
//# sourceMappingURL=readertaskeither.js.map