const makeApiHttpClient = Effect.gen(function* () {
const httpClient = (yield* HttpClient.HttpClient).pipe(
HttpClient.mapRequest(
HttpClientRequest.prependUrl(`${constants.API_URL}/api`),
),
HttpClient.filterStatusOk,
HttpClient.transformResponse(
Effect.catchTags({
RequestError: (error) =>
Effect.fail(
ErrorResult.make({
correlationId: ErrorCode.RequestError.toString(),
error: { code: ErrorCode.RequestError, details: error },
}),
),
ResponseError: (error) =>
Schema.decodeUnknownEither(ErrorResultResponse)(error).pipe(
Either.match({
onLeft: (err) =>
Effect.fail(
ErrorResult.make({
correlationId: "correlationId from headers X-correlation-id",
error: { code: ErrorCode.ResponseError, details: err },
}),
),
onRight: (errorResult) => Effect.fail(errorResult),
}),
),
}),
),
);
return { httpClient } as const;
}).pipe(Effect.provide(CustomFetchLive));
const makeApiHttpClient = Effect.gen(function* () {
const httpClient = (yield* HttpClient.HttpClient).pipe(
HttpClient.mapRequest(
HttpClientRequest.prependUrl(`${constants.API_URL}/api`),
),
HttpClient.filterStatusOk,
HttpClient.transformResponse(
Effect.catchTags({
RequestError: (error) =>
Effect.fail(
ErrorResult.make({
correlationId: ErrorCode.RequestError.toString(),
error: { code: ErrorCode.RequestError, details: error },
}),
),
ResponseError: (error) =>
Schema.decodeUnknownEither(ErrorResultResponse)(error).pipe(
Either.match({
onLeft: (err) =>
Effect.fail(
ErrorResult.make({
correlationId: "correlationId from headers X-correlation-id",
error: { code: ErrorCode.ResponseError, details: err },
}),
),
onRight: (errorResult) => Effect.fail(errorResult),
}),
),
}),
),
);
return { httpClient } as const;
}).pipe(Effect.provide(CustomFetchLive));