Using `@effect/platform` FileSystem with mTLS in HttpClient setup

Hi all! 👋
I'm building a custom HttpClient that uses mTLS where I need to read the certificate, key and passphrase from the file system when creating the agent layer. Right now, I'm using fs.readFileSync, but I'd like to use @effect/platform's FileSystem readFileString to stay within the Effect ecosystem. The challenge is that I somehow need to execute the effect (the file read operations) before creating the layer. Of course, I can simply call Effect.runSync or something like that, but I'd like to know if there is a better approach. That would be also amazing if I can make it an adapter so that I can switch the underlying implementation (e.g. read from S3, process.env, etc.).
This is what I have tried so far:
const program = Effect.gen(function* () {
  const client = yield* HttpClient.HttpClient;

  const response = yield* client.post('https://...', {
    acceptJson: true,
    body: yield* HttpBody.json({ ... }),
  });

  const json = yield* response.json;
  console.dir(json, { depth: null });
}).pipe(
  Effect.provide(NodeHttpClient.layerWithoutAgent),
  Effect.provide(
    NodeHttpClient.makeAgentLayer({
      cert: readFileSync('cert.pem', 'utf-8').trim(),
      key: readFileSync('key.pem', 'utf-8').trim(),
      passphrase: readFileSync('pass.txt', 'utf-8').trim(),
    })
  )
);

await Effect.runPromise(program);
Was this page helpful?