I am still learning and playing with fp-ts
and can't figure this out.
I have some API calls and I want to collect all the successful responses and all the errors into arrays.
So, I tried to use 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
which works fine for the responses, but I only receive the last error from the API calls. Is there any way to collect all the errors into one array?
Below I wrote the functions that are making the API calls, just in case I have an issue there.
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,
);