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

import * as ag from "@distilled.cloud/aws/api-gateway";
import * as Output from "alchemy/Output";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as AWSBindings from "../AWSBindings.ts";
import { RegisterApiGatewayBinding } from "./BindingHttp.ts";
import { GetUsage, type GetUsageRequest } from "./GetUsage.ts";
import type { UsagePlan } from "./UsagePlan.ts";

/**
 * HTTP implementation of the {@link GetUsage} binding. Grants
 * `apigateway:GET` on the plan's `/usage` path and calls the API with the
 * host Function's credentials.
 */
export const GetUsageHttp = Layer.effect(
  GetUsage,
  Effect.gen(function* () {
    const registerApiGatewayBinding = yield* RegisterApiGatewayBinding;
    const getUsage = yield* AWSBindings.withCredentials(ag.getUsage);
    return Effect.fn(function* <P extends UsagePlan>(usagePlan: P) {
      const UsagePlanId = yield* usagePlan.id;
      yield* registerApiGatewayBinding({
        cap: "AWS.ApiGateway.GetUsage",
        target: usagePlan,
        verb: "GET",
        paths: (region) => [
          Output.interpolate`arn:aws:apigateway:${region}::/usageplans/${usagePlan.id}/usage`,
        ],
      });
      return Effect.fn(`AWS.ApiGateway.GetUsage(${usagePlan.LogicalId})`)(function* (
        request: GetUsageRequest,
      ) {
        return yield* getUsage({
          ...request,
          usagePlanId: yield* UsagePlanId,
        });
      });
    });
  }),
);