@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 165 lines 5.64 KB
Raw
1 import * as ag from "@distilled.cloud/aws/api-gateway";
2 import { isResolved } from "alchemy/Diff";
3 import type { Input } from "alchemy/Input";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Effect from "effect/Effect";
7 import * as Stream from "effect/Stream";
8 import type { Providers } from "../Providers.ts";
9
10 export interface UsagePlanKeyProps {
11 /** ID of the usage plan to add the key to. */
12 usagePlanId: Input<string>;
13 /** ID of the API key to associate. */
14 keyId: Input<string>;
15 /**
16 * @default "API_KEY"
17 */
18 keyType?: string;
19 }
20
21 /** @resource */
22 export interface UsagePlanKey extends Resource<
23 "AWS.ApiGateway.UsagePlanKey",
24 UsagePlanKeyProps,
25 {
26 usagePlanId: string;
27 keyId: string;
28 keyType: string;
29 name: string | undefined;
30 },
31 never,
32 Providers
33 > {}
34
35 /**
36 * Associates an API key with a usage plan.
37 *
38 * ### Usage plan keys
39 * **Example:** Associate key with plan
40 * ```typescript
41 * yield* ApiGateway.UsagePlanKey("PlanKey", {
42 * usagePlanId: plan.id,
43 * keyId: key.id,
44 * });
45 * ```
46 */
47 const UsagePlanKeyResource = Resource<UsagePlanKey>("AWS.ApiGateway.UsagePlanKey");
48
49 export { UsagePlanKeyResource as UsagePlanKey };
50
51 export const UsagePlanKeyProvider = () =>
52 Provider.effect(
53 UsagePlanKeyResource,
54 Effect.gen(function* () {
55 return {
56 stables: ["usagePlanId", "keyId"] as const,
57 diff: Effect.fn(function* ({ news: newsIn, olds }) {
58 if (!isResolved(newsIn)) return;
59 const news = newsIn as UsagePlanKeyProps;
60 if (news.usagePlanId !== olds.usagePlanId || news.keyId !== olds.keyId) {
61 return { action: "replace" } as const;
62 }
63 }),
64 read: Effect.fn(function* ({ output }) {
65 if (!output) return undefined;
66 const k = yield* ag
67 .getUsagePlanKey({
68 usagePlanId: output.usagePlanId,
69 keyId: output.keyId,
70 })
71 .pipe(Effect.catchTag("NotFoundException", () => Effect.undefined));
72 if (!k?.id) return undefined;
73 return {
74 usagePlanId: output.usagePlanId,
75 keyId: k.id,
76 keyType: k.type ?? "API_KEY",
77 name: k.name,
78 };
79 }),
80 reconcile: Effect.fn(function* ({ news: newsIn, output, session }) {
81 if (!isResolved(newsIn)) {
82 return yield* Effect.die("UsagePlanKey props were not resolved");
83 }
84 const news = newsIn as Input.ResolveProps<UsagePlanKeyProps>;
85 const usagePlanId = (output?.usagePlanId ?? news.usagePlanId) as string;
86 const keyId = (output?.keyId ?? news.keyId) as string;
87
88 // Observe — fetch the live link. UsagePlanKey is a pure
89 // association (no patchable fields apart from the natural-key
90 // tuple, which is modeled as `replace` in `diff`), so once it
91 // exists there's nothing to sync.
92 let observed = yield* ag
93 .getUsagePlanKey({ usagePlanId, keyId })
94 .pipe(Effect.catchTag("NotFoundException", () => Effect.undefined));
95
96 // Ensure — create the link if missing.
97 if (!observed?.id) {
98 const created = yield* ag.createUsagePlanKey({
99 usagePlanId: news.usagePlanId as string,
100 keyId: news.keyId as string,
101 keyType: news.keyType ?? "API_KEY",
102 });
103 yield* session.note(`Linked key ${keyId} to usage plan ${usagePlanId}`);
104 observed = yield* ag.getUsagePlanKey({
105 usagePlanId,
106 keyId: created.id ?? keyId,
107 });
108 }
109
110 return {
111 usagePlanId,
112 keyId: observed.id!,
113 keyType: observed.type ?? "API_KEY",
114 name: observed.name,
115 };
116 }),
117 // UsagePlanKey is a sub-resource keyed by {usagePlanId, keyId}. There
118 // is no account-wide enumeration, so enumerate every usage plan first
119 // (getUsagePlans) then list keys per plan (getUsagePlanKeys).
120 list: () =>
121 Effect.gen(function* () {
122 const usagePlanIds = yield* ag.getUsagePlans.pages({}).pipe(
123 Stream.runCollect,
124 Effect.map((chunk) =>
125 Array.from(chunk).flatMap((page) =>
126 (page.items ?? [])
127 .filter((p): p is ag.UsagePlan & { id: string } => p.id != null)
128 .map((p) => p.id),
129 ),
130 ),
131 );
132 const rows = yield* Effect.forEach(
133 usagePlanIds,
134 (usagePlanId) =>
135 ag.getUsagePlanKeys.pages({ usagePlanId }).pipe(
136 Stream.runCollect,
137 Effect.map((chunk) =>
138 Array.from(chunk).flatMap((page) =>
139 (page.items ?? [])
140 .filter((k): k is ag.UsagePlanKey & { id: string } => k.id != null)
141 .map((k) => ({
142 usagePlanId,
143 keyId: k.id,
144 keyType: k.type ?? "API_KEY",
145 name: k.name,
146 })),
147 ),
148 ),
149 ),
150 { concurrency: 10 },
151 );
152 return rows.flat();
153 }),
154 delete: Effect.fn(function* ({ output, session }) {
155 yield* ag
156 .deleteUsagePlanKey({
157 usagePlanId: output.usagePlanId,
158 keyId: output.keyId,
159 })
160 .pipe(Effect.catchTag("NotFoundException", () => Effect.void));
161 yield* session.note(`Removed key from usage plan`);
162 }),
163 };
164 }),
165 );