minauth
Version:
A TypeScript library for building authentication systems on top of the Mina blockchain and other zero-knowledge proofs solutions.
62 lines • 1.72 kB
JavaScript
import * as E from 'fp-ts/lib/Either.js';
/**
* Convert a decoder from the idiomatic typescript interface to the functional style.
*/
export const tsToFpDecoder = (tsDec) => {
return {
__interface_tag: 'fp',
decode: (i) => E.fromNullable('unable to parse')(tsDec.decode(i))
};
};
/**
* Convert an encoder from the idiomatic typescript interface to the functional style
*/
export const tsToFpEncoder = (tsEnc) => {
return {
__interface_tag: 'fp',
encode: tsEnc.encode
};
};
/**
* This laws should hold
*/
export const __encDecLaw = (input, enc, dec) => E.match(() => false, (decoded) => decoded === input)(dec.decode(enc.encode(input)));
/**
* This is an encoder that just casts the object using "as unknown"
*/
export const noOpEncoder = (i) => {
return {
__interface_tag: i,
encode: (inp) => inp
};
};
/**
* Create a decoder out of a zod schema parser
*/
export const wrapZodDec = (i, s) => {
const mkDecoder = (onFailure, onSuccess) => (o) => {
const parseResult = s.safeParse(o);
return parseResult.success
? onSuccess(parseResult)
: onFailure(parseResult);
};
const fpDec = {
__interface_tag: 'fp',
decode: mkDecoder(({ error }) => E.left(String(error)), ({ data }) => E.right(data))
};
const tsDec = {
__interface_tag: 'ts',
decode: mkDecoder(() => undefined, ({ data }) => data)
};
return (i === 'fp' ? fpDec : tsDec);
};
/**
* Combine a decoder and an encoder into a single object.
*/
export const combineEncDec = (enc, dec) => {
return {
...enc,
...dec
};
};
//# sourceMappingURL=encodedecoder.js.map