Effect CommunityEC
Effect Community2y ago
10 replies
joepjoosten

Improving TypeScript Parsing Pattern

Is there a more idiomatic way to do this:

dxParser are Schema decoders, and every parser returns a different type.
I've tried Array.separate, but this only works on homogeneous type, not multiple types

Stream.mapEffect(body => pipe(
  Effect.all([
    d1Parser(body),
    d2Parser(body),
    d3Parser(body),
  ], { mode: 'either'}),
  Effect.andThen(([e1, e2, e3]) => {
    if(Either.isRight(e1) && Option.isSome(e1.right)) {
      return Option.getOrThrow(e1.right);
    }
    if(Either.isRight(e2) && Option.isSome(e2.right)) {
      return Option.getOrThrow(e2.right);
    }
    if(Either.isRight(e3) && Option.isSome(e3.right)) {
      return Option.getOrThrow(e3.right);
    }
    throw new Error('Could not parse event'); // <-- would like to return all errors from the parsers
  }
))),
Was this page helpful?