Effect CommunityEC
Effect Community2y ago
60 replies
Jonathan Clem

Simplifying Client Construction with Base URL and Default Headers

Can this client construction be cleaned up? It feels like a lot of boilerplate to create a client with a base URL and default headers. I'm mostly talking about how it feels awkward to have to wrap things like Http.request.prependUrl/setHeaders in two separate function calls (Http.client.mapRequest and
Effect.map
).

const githubClient = AuthStore.pipe(
  Effect.flatMap((authStore) => authStore.get(ProviderKey.GitHub)),
  Effect.flatMap((apiKey) =>
    Http.client.Client.pipe(
      Effect.map(Http.client.filterStatusOk),
      Effect.map(
        Http.client.mapRequest(
          Http.request.prependUrl("https://api.github.com"),
        ),
      ),
      Effect.map(
        Http.client.mapRequest(
          Http.request.setHeaders({
            authorization: `Bearer ${apiKey}`,
            "user-agent": "Node.js",
          }),
        ),
      ),
    ),
  ),
);
Was this page helpful?