1import * as ag from "@distilled.cloud/aws/api-gateway";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Output from "../../Output.ts";
5import { registerApiGatewayBinding } from "./BindingHttp.ts";
6import { GetUsage, type GetUsageRequest } from "./GetUsage.ts";
7import type { UsagePlan } from "./UsagePlan.ts";
8
9/**
10 * HTTP implementation of the {@link GetUsage} binding. Grants
11 * `apigateway:GET` on the plan's `/usage` path and calls the API with the
12 * host Function's credentials.
13 */
14export const GetUsageHttp = Layer.effect(
15 GetUsage,
16 Effect.gen(function* () {
17 const getUsage = yield* ag.getUsage;
18 return Effect.fn(function* <P extends UsagePlan>(usagePlan: P) {
19 const UsagePlanId = yield* usagePlan.id;
20 yield* registerApiGatewayBinding({
21 cap: "AWS.ApiGateway.GetUsage",
22 target: usagePlan,
23 verb: "GET",
24 paths: (region) => [
25 Output.interpolate`arn:aws:apigateway:${region}::/usageplans/${usagePlan.id}/usage`,
26 ],
27 });
28 return Effect.fn(`AWS.ApiGateway.GetUsage(${usagePlan.LogicalId})`)(
29 function* (request: GetUsageRequest) {
30 return yield* getUsage({
31 ...request,
32 usagePlanId: yield* UsagePlanId,
33 });
34 },
35 );
36 });
37 }),
38);