我仍在学习和玩耍,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,
);