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 {
7 CreateUsagePlanKey,
8 type CreateUsagePlanKeyRequest,
9} from "./CreateUsagePlanKey.ts";
10import type { UsagePlan } from "./UsagePlan.ts";
11
12/**
13 * HTTP implementation of the {@link CreateUsagePlanKey} binding. Grants
14 * `apigateway:POST` on the plan's `/keys` path and calls the API with the
15 * host Function's credentials.
16 */
17export const CreateUsagePlanKeyHttp = Layer.effect(
18 CreateUsagePlanKey,
19 Effect.gen(function* () {
20 const createUsagePlanKey = yield* ag.createUsagePlanKey;
21 return Effect.fn(function* <P extends UsagePlan>(usagePlan: P) {
22 const UsagePlanId = yield* usagePlan.id;
23 yield* registerApiGatewayBinding({
24 cap: "AWS.ApiGateway.CreateUsagePlanKey",
25 target: usagePlan,
26 verb: "POST",
27 paths: (region) => [
28 Output.interpolate`arn:aws:apigateway:${region}::/usageplans/${usagePlan.id}/keys`,
29 ],
30 });
31 return Effect.fn(
32 `AWS.ApiGateway.CreateUsagePlanKey(${usagePlan.LogicalId})`,
33 )(function* (request: CreateUsagePlanKeyRequest) {
34 return yield* createUsagePlanKey({
35 ...request,
36 keyType: request.keyType ?? "API_KEY",
37 usagePlanId: yield* UsagePlanId,
38 });
39 });
40 });
41 }),
42);