**Persisting Cookies with HttpClient in Effect**

Hey! New to Effect. Was just playing around with HttpClient. Is there anyway to persist cookies for following requests?

import {
  HttpClient,
  HttpClientRequest,
  HttpClientResponse,
} from "@effect/platform";
import { Context, Effect, Layer } from "effect";
import * as Model from "./Model";

const makeHttpClient = Effect.gen(function* () {
  const defaultClient = yield* HttpClient.HttpClient;

  return defaultClient.pipe(
    HttpClient.mapRequest((req) =>
      req.pipe(HttpClientRequest.prependUrl("http://acme.com/api"))
    )
  );
});

class TestHttpClient extends Context.Tag("TestHttpClient")<
  TestHttpClient,
  Effect.Effect.Success<typeof makeHttpClient>
>() {
  public static readonly live = Layer.effect(this, makeHttpClient).pipe(
    Layer.provide(HttpClient.layer)
  );
}

const request = Effect.gen(function* () {
  const client = yield* TestHttpClient;

  yield* client(
    HttpClientRequest.post("/login", {}).pipe(
      HttpClientRequest.unsafeJsonBody({
        email: "hello@foo.com",
        password: "13123",
      })
    )
  ).pipe(
    Effect.flatMap(HttpClientResponse.schemaBodyJson(Model.LoginResponse)),
    Effect.tap(Effect.log)
  );
// need cookies for following requests
  yield* client(
    HttpClientRequest.get("/get-token")
  ).pipe(
    Effect.flatMap(HttpClientResponse.schemaBodyJson(Model.TokenResponse)),
    Effect.tap(Effect.log)
  );
});

request.pipe(
  Effect.provide(TestHttpClient.live),
  Effect.scoped,
  Effect.runPromise
);
Was this page helpful?