How to Log the Response Body in an HTTP Client

import { ResponseError } from "@effect/platform/HttpClientError";
import { HttpClient } from "@effect/platform/index";
import { Duration, Effect, Option, Schedule } from "effect";

const retryAfterSchedule = Schedule.identity<unknown>().pipe(
    Schedule.map((error) =>
        Option.liftPredicate(error, (e) => e instanceof ResponseError).pipe(
            Option.flatMap((e) =>
                Duration.decodeUnknown(
                    e.response.headers["x-sn-rate-limit-retry-after"],
                ),
            ),
            Option.getOrElse(() => Duration.millis(1000)),
        ),
    ),
    Schedule.tapOutput((delay) =>
        Effect.logInfo(`Calculated retry delay: ${Duration.toMillis(delay)}ms`),
    ),
    Schedule.delayedSchedule,
    Schedule.intersect(Schedule.recurs(2)),
);

export class SolarNetworkClient extends Effect.Service<SolarNetworkClient>()(
    "http-client/solar-network-client",
    {
        effect: Effect.gen(function* () {
            const httpClient = (yield* HttpClient.HttpClient).pipe(
                HttpClient.tapRequest((request) =>
                    Effect.logInfo(
                        `➡️  HTTP Outgoing Request ${request.method} ${request.url}`,
                    ),
                ),
                HttpClient.tap((response) =>
                    Effect.logInfo(
                        `⬅️  HTTP ${response.status} ${JSON.stringify(response.toJSON())}`,
                    ),
                ),
            );

            return {
                client: httpClient.pipe(
                    HttpClient.transformResponse(Effect.retry(retryAfterSchedule)),
                ),
            };
        }),
    },
) {}

Here ive created a httpclient for a service, i wanted to add logging here but i cant seem to figure out how to log response.body here.
Was this page helpful?