sailfork / t3code-github / .repos/ alchemy-effect/ packages/ aws/ src/ Endpoint.ts

import { Endpoint } from "@distilled.cloud/aws";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { AWSEnvironment } from "./Environment.ts";

export const of = (endpoint: string) => Layer.succeed(Endpoint.Endpoint, Effect.succeed(endpoint));

/**
 * Derive a custom endpoint (if any) from the surrounding
 * {@link AWSEnvironment}. If the environment has no `endpoint` set, this
 * Layer is empty (the SDK uses its default endpoint resolver).
 */
export const fromEnvironment: Layer.Layer<never, never, AWSEnvironment> = Layer.effect(
  Endpoint.Endpoint,
  Effect.gen(function* () {
    return Effect.map(yield* AWSEnvironment, (env) => env.endpoint ?? undefined!);
  }),
);

/**
 * Explicitly "no custom endpoint" — the SDK falls back to its default
 * endpoint resolver.
 *
 * Use this (never `Layer.empty`) when an AWS call is made from *inside*
 * the construction of {@link AWSEnvironment}: leaving `Endpoint`
 * unprovided lets the call fall through to {@link fromEnvironment},
 * whose service Effect re-enters the in-flight `AWSEnvironment` cache and
 * deadlocks the fiber with no I/O and no timer pending.
 */
export const none = Layer.succeed(Endpoint.Endpoint, Effect.undefined);