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

import { Endpoint } from "@distilled.cloud/aws";
import * as Region from "@distilled.cloud/aws/Region";
import * as sfn from "@distilled.cloud/aws/sfn";
import type { SourceError } from "effect/ConfigProvider";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { makeStateMachineArnHttpBinding } from "./BindingHttp.ts";
import { StartSyncExecution } from "./StartSyncExecution.ts";

/**
 * `StartSyncExecution` must target the `sync-states.{region}` endpoint
 * (the Smithy `hostPrefix: "sync-"` endpoint trait) — the regular
 * `states.{region}` endpoint rejects it. distilled applies the host
 * prefix itself; the explicit Endpoint here additionally pins the
 * correct host for runtimes bundled against a distilled build that
 * predates host-prefix support.
 */
const startSyncExecutionOnSyncEndpoint = Effect.gen(function* () {
  const regionEffect = yield* Region.Region;
  // No region: no pin; the call itself then fails with MissingRegion.
  const syncStatesEndpoint: Effect.Effect<string | undefined, SourceError> = regionEffect.pipe(
    Effect.map((region): string | undefined => `https://sync-states.${region}.amazonaws.com`),
    Effect.catchTag("Alchemy::AWS::MissingRegion", () => Effect.undefined),
  );
  const op = yield* sfn.startSyncExecution;
  // The endpoint must be pinned per call, not around the yield: the
  // yield-time snapshot is only a fallback — the calling fiber's ambient
  // `Endpoint` (e.g. the runtime's `Endpoint.fromEnv`) wins over it.
  return (input: sfn.StartSyncExecutionInput) =>
    op(input).pipe(Effect.provideService(Endpoint.Endpoint, syncStatesEndpoint));
});

export const StartSyncExecutionHttp = Layer.effect(
  StartSyncExecution,
  makeStateMachineArnHttpBinding({
    tag: "AWS.StepFunctions.StartSyncExecution",
    operation: startSyncExecutionOnSyncEndpoint,
    actions: ["states:StartSyncExecution"],
  }),
);