Creating a custom reusable `HttpClient` using the Effect library is a good approach, and your imp...

Hi, just wanted to check if something like this is a good way to make a custom reusable HttpClient that I can then later use in other services?

class CoolHttpClient extends Effect.Service<CoolHttpClient>()(
  'CoolHttpClient',
  {
    effect: Effect.gen(function* () {
      return (yield* HttpClient.HttpClient).pipe(
        HttpClient.mapRequest(HttpClientRequest.setHeader('client', 'cool')),
      );
    }),
    dependencies: [NodeHttpClient.layerUndici],
  },
) {}

const program = Effect.gen(function* () {
  const client = yield* CoolHttpClient;
  const response = yield* client.get('https://example.com/api/v1/thing');
  const json = yield* response.json;
  console.log(json);
}).pipe(Effect.provide([CoolHttpClient.Default]));

Effect.runPromise(program);


I think custom tag + layer would be the other way? Or is there some other approach that's more 'best practices' that I've missed?
Was this page helpful?