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

import * as Effect from "effect/Effect";
import * as AWSBindings from "../AWSBindings.ts";
import type { CertificateAuthority } from "./CertificateAuthority.ts";

/**
 * Options for {@link makeACMPCAHttpBinding}.
 *
 * @internal shared HTTP scaffolding for the ACM PCA capability bindings —
 * not exported from the service index.
 */
export interface ACMPCAHttpBindingOptions<
  Req extends { CertificateAuthorityArn: string },
  A,
  E,
  R,
> {
  /**
   * The ACM PCA action name, e.g. `"IssueCertificate"`. Used verbatim for
   * the IAM action (`acm-pca:{action}`), the bind label, and the tracing
   * span name.
   */
  action: string;
  /**
   * The distilled ACM PCA operation. Every ACM PCA data-plane operation is
   * keyed by `CertificateAuthorityArn`, which the binding injects from the
   * bound {@link CertificateAuthority}.
   */
  operation: Effect.Effect<(request: Req) => Effect.Effect<A, E>, never, R>;
}

/**
 * Build the implementation Effect for an ACM PCA HTTP capability binding.
 *
 * Every ACM PCA capability has the same shape — resolve the CA's ARN,
 * register an IAM policy statement for `acm-pca:{action}` scoped to that
 * CA at deploy time, and return a runtime callable that injects
 * `CertificateAuthorityArn` into the operation's request. Only the
 * operation and the action name differ, so each `{Op}Http.ts` is a thin
 * `Layer.effect(Cap, makeACMPCAHttpBinding({ ... }))`.
 *
 * @internal
 */
export const makeACMPCAHttpBinding = <Req extends { CertificateAuthorityArn: string }, A, E, R>(
  options: ACMPCAHttpBindingOptions<Req, A, E, R>,
) =>
  Effect.gen(function* () {
    const bind = yield* AWSBindings.Bind;
    const operation = yield* AWSBindings.withCredentials(options.operation);

    return Effect.fn(function* (certificateAuthority: CertificateAuthority) {
      const Arn = yield* certificateAuthority.certificateAuthorityArn;
      if (!globalThis.__ALCHEMY_RUNTIME__) {
        yield* bind`AWS.ACMPCA.${options.action}(${certificateAuthority})`({
          policyStatements: [
            {
              Effect: "Allow",
              Action: [`acm-pca:${options.action}`],
              Resource: [certificateAuthority.certificateAuthorityArn],
            },
          ],
        });
      }
      return Effect.fn(`AWS.ACMPCA.${options.action}(${certificateAuthority.LogicalId})`)(
        function* (
          request: Omit<Req, "CertificateAuthorityArn"> = {} as Omit<
            Req,
            "CertificateAuthorityArn"
          >,
        ) {
          return yield* operation({
            ...request,
            CertificateAuthorityArn: yield* Arn,
          } as Req);
        },
      );
    });
  });