1

我仍在学习和玩耍,fp-ts无法弄清楚这一点。我有一些 API 调用,我想将所有成功的响应和所有错误收集到数组中。

所以,我尝试使用array.sequence

TE.map(schedules =>
  array.sequence(TE.taskEither)(
    schedules.map(({ Program, ...schedule }) =>
      pipe(
        createProgramIfNotExist(Program),
        TE.map(createdProgram =>
          setRecordingSchedules(programsClient, { ...schedule, ProgramId: createdProgram.Id }),
        ),
        TE.flatten,
      ),
    ),
  ),
),
TE.flatten

这对响应很好,但我只收到来自 API 调用的最后一个错误。有没有办法将所有错误收集到一个数组中?

下面我编写了进行 API 调用的函数,以防万一我在那里遇到问题。

export const setRecordingSchedules = (
  fetcher: AxiosInstance,
  config: RecordingConfig,
): TE.TaskEither<Error, [ReturnType<typeof Schedule.codec.encode>, number]> => {
  const url = `/programs/${config.ProgramId}/recordingschedules`;
  return pipe(Schedule.codec.encode({ ...config, AutoPodcastConfig }), body =>
    pipe(
      TE.tryCatch(
        () => handleRateLimit(() => fetcher.put(url, body)),
        err => raiseUpdateError(unknownToError(err)),
      ),
      TE.map(({ status }) => [body, status]),
    ),
  );
};

export const createRecordingSchedule = (
  fetcher: AxiosInstance,
  program: Program.Type,
): TE.TaskEither<Error, Program.Type> =>
  pipe(
    Program.codec.encode(program),
    body =>
      pipe(
        TE.tryCatch(
          () => handleRateLimit(() => fetcher.post('/programs', body)),
          err => raiseCreateError(unknownToError(err)),
        ),
        TE.map(({ data }) =>
          pipe(
            Program.codec.decode({ ...data, Network: program.Network }),
            E.bimap(
              errors => ReportValidationError(errors, { ...data, Network: program.Network }),
              decoded => decoded,
            ),
            TE.fromEither,
          ),
        ),
      ),
    TE.flatten,
  );
4

2 回答 2

0

这个想法是转换TaskEither<E, A>[] -> Task<Either<E, A>[]>,然后给出一个合并函数Either<E, A>[] -> Either<E[], A[]>,你可以实现你想要的。合并功能也分几步完成:

  • Either<E[], A[]>
  • Either<E, A> -> Either<E[], A[]>- 将值或错误简单地包装到单例数组中
  • foldMap Either<E, A>[]通过使用上述包装器映射并使用上述幺半群折叠
import * as E from 'fp-ts/lib/Either';
import * as A from 'fp-ts/lib/Array';

// type Error = ...
// type Result = ...

// Make a validation monoid for `Either<E[], A[]>`
const validationMonoid = E.getValidationMonoid(A.getMonoid<Error>(), A.getMonoid<Result>());

// `Either<E, A> -> Either<E[], A[]>` - trivial wrap of value or error into singleton arrays
const validationWrap = E.bimap(x => [x], x => [x]);

// `foldMap` `Either<E, A>[]` by mapping with the above wrapper and folding using above monoid
const validationMerge = E.foldMap(validationMonoid)(validationWrap);

现在你可以做

const tasks: TaskEither<Error, Result>[] = ...;

const aggregatedResults: TaskEither<Error[], Result[]> = pipe(
  // convert `TaskEither<E, A>[] -> Task<Either<E, A>[]>`
  array.sequence(T.task)(tasks),
  // and then apply the merge
  T.map(validationMerge)
);
于 2020-03-22T09:54:23.713 回答
0

解决方案是使用traverse. 这个想法是获取验证任务 monoid 并将每个合并E到一个数组中。

array.traverse(TE.getTaskValidation(A.getMonoid<E>()))(xs, TE.mapLeft(A.of))

完整的工作解决方案:

import { array, getMonoid, of as arrayOf } from 'fp-ts/lib/Array';

TE.chain(schedules =>
  array.sequence(TE.taskEither)(
    schedules.map(({ Program, ...schedule }) =>
      pipe(
        createProgramIfNotExist(Program),
        TE.map(createdProgram =>
          setRecordingSchedules(programsClient, { ...schedule, ProgramId: createdProgram.Id }),
        ),
      ),
    ),
    xs => array.traverse(TE.getTaskValidation(getMonoid<E>()))(xs, TE.mapLeft(arrayOf)),
  ),
),
于 2020-04-04T18:35:09.557 回答