@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, 04:45 UTC

main
ts 119 lines 4.23 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as AWSBindings from "../AWSBindings.ts";
3 import type { Key } from "./Key.ts";
4
5 /**
6 * Shared scaffolding for the AWS Payment Cryptography HTTP bindings.
7 *
8 * NOT exported from `index.ts` — every thin `{Op}Http.ts` in this service is
9 * a `Layer.effect(Cap, make…HttpBinding({ … }))` over one of the builders
10 * below. Everything except the operation, the IAM action, and (for two-key
11 * operations) the injected request fields is boilerplate.
12 *
13 * Payment Cryptography data-plane operations authorize against the key ARN,
14 * so every builder grants `actions` on the bound {@link Key}(s) and the
15 * runtime callable injects the key ARN(s) into every request.
16 */
17
18 /**
19 * Build the impl Effect for an operation scoped to a single {@link Key}
20 * whose input carries the key ARN in a `KeyIdentifier` field (all
21 * single-key Payment Cryptography operations use this field name).
22 */
23 export const makePaymentCryptographyKeyHttpBinding = <
24 I extends { KeyIdentifier: string },
25 A,
26 E,
27 R,
28 >(options: {
29 /** Fully-qualified binding tag, e.g. `AWS.PaymentCryptography.EncryptData`. */
30 tag: string;
31 /** The distilled operation; `KeyIdentifier` is injected from the key. */
32 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
33 /** IAM actions granted on the bound key's ARN. */
34 actions: readonly `payment-cryptography:${string}`[];
35 }) =>
36 Effect.gen(function* () {
37 const bind = yield* AWSBindings.Bind;
38 const op = yield* AWSBindings.withCredentials(options.operation);
39
40 return Effect.fn(function* <K extends Key>(key: K) {
41 // Outputs yield a DEFERRED effect — resolve again per invocation below.
42 const KeyArn = yield* key.keyArn;
43 if (!globalThis.__ALCHEMY_RUNTIME__) {
44 yield* bind`${options.tag}(${key})`({
45 policyStatements: [
46 {
47 Effect: "Allow",
48 Action: [...options.actions],
49 Resource: [key.keyArn],
50 },
51 ],
52 });
53 }
54 return Effect.fn(`${options.tag}(${key.LogicalId})`)(function* (
55 request?: Omit<I, "KeyIdentifier">,
56 ) {
57 return yield* op({
58 ...request,
59 KeyIdentifier: yield* KeyArn,
60 } as unknown as I);
61 });
62 });
63 });
64
65 /**
66 * Build the impl Effect for an operation spanning two {@link Key}s (e.g.
67 * `ReEncryptData` with incoming + outgoing keys, `GeneratePinData` with
68 * generation + encryption keys). The deploy-time half grants `actions` on
69 * both key ARNs, and the runtime callable injects each key's ARN into the
70 * request field named by `keyFields` (in bind-argument order).
71 */
72 export const makePaymentCryptographyKeyPairHttpBinding = <
73 F1 extends string,
74 F2 extends string,
75 I extends Record<F1 | F2, string>,
76 A,
77 E,
78 R,
79 >(options: {
80 /** Fully-qualified binding tag, e.g. `AWS.PaymentCryptography.ReEncryptData`. */
81 tag: string;
82 /** The distilled operation; both key fields are injected. */
83 operation: Effect.Effect<(input: I) => Effect.Effect<A, E>, never, R>;
84 /** IAM actions granted on both bound keys' ARNs. */
85 actions: readonly `payment-cryptography:${string}`[];
86 /** Request fields carrying the two keys' ARNs, in bind-argument order. */
87 keyFields: readonly [F1, F2];
88 }) =>
89 Effect.gen(function* () {
90 const bind = yield* AWSBindings.Bind;
91 const op = yield* AWSBindings.withCredentials(options.operation);
92
93 return Effect.fn(function* <A1 extends Key, A2 extends Key>(first: A1, second: A2) {
94 const FirstArn = yield* first.keyArn;
95 const SecondArn = yield* second.keyArn;
96 if (!globalThis.__ALCHEMY_RUNTIME__) {
97 for (const key of [first, second]) {
98 yield* bind`${options.tag}(${key})`({
99 policyStatements: [
100 {
101 Effect: "Allow",
102 Action: [...options.actions],
103 Resource: [key.keyArn],
104 },
105 ],
106 });
107 }
108 }
109 return Effect.fn(`${options.tag}(${first.LogicalId}, ${second.LogicalId})`)(function* (
110 request: Omit<I, F1 | F2>,
111 ) {
112 return yield* op({
113 ...request,
114 [options.keyFields[0]]: yield* FirstArn,
115 [options.keyFields[1]]: yield* SecondArn,
116 } as unknown as I);
117 });
118 });
119 });