Improving TypeScript Code for Remote File Retrieval with Retry Policy

Is this there a better way to do this? It seems like I'm not doing something right...

import {
  FetchHttpClient,
  HttpClient,
  HttpClientRequest,
} from "@effect/platform";
import { Config, Context, Effect, Layer, Redacted } from "effect";
import { remoteRetryPolicy } from "../utils/remote-retry-policy";

const remoteFileKeyed = (remotePath: string, key: string, header: string) =>
  Effect.gen(function* () {
    const keyConfig = yield* Config.redacted(key);

    return yield* Effect.retry(
      HttpClient.HttpClient.pipe(
        Effect.tap(
          HttpClient.mapRequest(
            HttpClientRequest.setHeader(header, Redacted.value(keyConfig)),
          ),
        ),
        Effect.flatMap((httpClient) => httpClient.get(remotePath)),
      ),
      remoteRetryPolicy,
    ).pipe(Effect.scoped);
  });

export class RemoteFileKeyed extends Context.Tag("@myApp/RemoteFileKeyed")<
  RemoteFileKeyed,
  { readonly get: (remotePath: string) => ReturnType<typeof remoteFileKeyed> }
>() {
  static readonly apiKeyXYZLive = Layer.succeed(
    RemoteFileKeyed,
    RemoteFileKeyed.of({
      get: (remotePath: string) =>
        remoteFileKeyed(remotePath, "API_KEY", "X-TOKEN").pipe(
          Effect.provide(FetchHttpClient.layer),
        ),
    }),
  );
}
Was this page helpful?