1import * as Effect from "effect/Effect";
2import * as Binding from "../../Binding.ts";
3import * as Output from "../../Output.ts";
4import { isBindingHost } from "../Lambda/Function.ts";
5import type { TargetGroup } from "./TargetGroup.ts";
6
7/**
8 * Shared scaffolding for the VPC Lattice runtime bindings.
9 *
10 * NOT exported from `index.ts` — every `{Op}Http.ts` in this service is a
11 * thin `Layer.effect(Cap, makeVpcLatticeTargetGroupHttpBinding({ … }))` over
12 * the builder below. Everything except the operation, the IAM action list,
13 * and the injected `targetGroupIdentifier` is boilerplate.
14 */
15
16/**
17 * Build the impl Effect for a target-group-scoped VPC Lattice operation: the
18 * runtime callable injects the bound {@link TargetGroup}'s ID as
19 * `targetGroupIdentifier` and the deploy-time half grants `actions` on the
20 * target group's ARN.
21 */
22export const makeVpcLatticeTargetGroupHttpBinding = <
23 I extends { targetGroupIdentifier?: string },
24 A,
25 E,
26 R,
27>(options: {
28 /** Fully-qualified binding tag, e.g. `AWS.VpcLattice.ListTargets`. */
29 tag: string;
30 /**
31 * The distilled operation; `targetGroupIdentifier` is injected from the
32 * target group.
33 */
34 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
35 /** IAM actions granted on the target group ARN. */
36 actions: readonly string[];
37}) =>
38 Effect.gen(function* () {
39 const op = yield* options.operation;
40
41 return Effect.fn(function* (targetGroup: TargetGroup) {
42 const targetGroupIdentifier = yield* targetGroup.targetGroupId;
43 if (!globalThis.__ALCHEMY_RUNTIME__) {
44 const host = yield* Binding.Host;
45 if (isBindingHost(host)) {
46 yield* host.bind`Allow(${host}, ${options.tag}(${targetGroup}))`({
47 policyStatements: [
48 {
49 Effect: "Allow",
50 Action: [...options.actions],
51 Resource: [Output.interpolate`${targetGroup.targetGroupArn}`],
52 },
53 ],
54 });
55 }
56 }
57 return Effect.fn(`${options.tag}(${targetGroup.LogicalId})`)(function* (
58 request: Omit<I, "targetGroupIdentifier">,
59 ) {
60 return yield* op({
61 ...request,
62 targetGroupIdentifier: yield* targetGroupIdentifier,
63 } as I);
64 });
65 });
66 });