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