@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 111 lines 3.57 KB
Raw
1 import * as DynamoDB from "@distilled.cloud/aws/dynamodb";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as AWSBindings from "../AWSBindings.ts";
5 import { GrantTables, tablesRegion } from "./BindingHttp.ts";
6 import {
7 TransactWriteItems,
8 type TransactWriteItemsRequest,
9 type TransactWriteItemsTables,
10 } from "./TransactWriteItems.ts";
11
12 export const TransactWriteItemsHttp = Layer.effect(
13 TransactWriteItems,
14 Effect.gen(function* () {
15 const grantTables = yield* GrantTables;
16 return Effect.fn(function* (...tables: TransactWriteItemsTables) {
17 const sortedTables = sortTables(tables);
18 const tableNames = new Map(
19 yield* Effect.forEach(sortedTables, (table) =>
20 Effect.gen(function* () {
21 return [table.LogicalId, yield* table.tableName] as const;
22 }),
23 ),
24 );
25
26 const getTableName = Effect.fn(function* (tableId: string) {
27 const TableName = tableNames.get(tableId);
28 if (!TableName) {
29 return yield* Effect.die(
30 new Error(`TransactWriteItems request references unbound table '${tableId}'`),
31 );
32 }
33 return yield* TableName;
34 });
35
36 yield* grantTables(
37 `AWS.DynamoDB.TransactWriteItems(${sortedTables.map((table) => table.LogicalId).join(", ")})`,
38 () => [
39 {
40 Effect: "Allow",
41 Action: [
42 "dynamodb:ConditionCheckItem",
43 "dynamodb:DeleteItem",
44 "dynamodb:PutItem",
45 "dynamodb:UpdateItem",
46 ],
47 Resource: sortedTables.map((table) => table.tableArn),
48 },
49 ],
50 );
51 const transactWriteItems = yield* AWSBindings.withCredentials(
52 DynamoDB.transactWriteItems,
53 tablesRegion(sortedTables),
54 );
55
56 return Effect.fn(
57 `AWS.DynamoDB.TransactWriteItems(${sortedTables.map((table) => table.LogicalId).join(", ")})`,
58 )(function* (request: TransactWriteItemsRequest) {
59 const transactItems = yield* Effect.forEach(request.TransactItems, (item) =>
60 Effect.gen(function* () {
61 if (item.ConditionCheck) {
62 return {
63 ConditionCheck: {
64 ...item.ConditionCheck,
65 TableName: yield* getTableName(item.ConditionCheck.Table),
66 },
67 };
68 }
69 if (item.Delete) {
70 return {
71 Delete: {
72 ...item.Delete,
73 TableName: yield* getTableName(item.Delete.Table),
74 },
75 };
76 }
77 if (item.Put) {
78 return {
79 Put: {
80 ...item.Put,
81 TableName: yield* getTableName(item.Put.Table),
82 },
83 };
84 }
85 if (item.Update) {
86 return {
87 Update: {
88 ...item.Update,
89 TableName: yield* getTableName(item.Update.Table),
90 },
91 };
92 }
93 return yield* Effect.die(
94 new Error("TransactWriteItems request item must include one DynamoDB operation"),
95 );
96 }),
97 );
98
99 return yield* transactWriteItems({
100 ...request,
101 TransactItems: transactItems,
102 });
103 });
104 });
105 }),
106 );
107
108 const sortTables = (tables: TransactWriteItemsTables) =>
109 [...new Map(tables.map((table) => [table.LogicalId, table] as const)).values()].sort((a, b) =>
110 a.LogicalId.localeCompare(b.LogicalId),
111 ) as TransactWriteItemsTables;