@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 77 lines 2.47 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 TransactGetItems,
8 type TransactGetItemsRequest,
9 type TransactGetItemsTables,
10 } from "./TransactGetItems.ts";
11
12 export const TransactGetItemsHttp = Layer.effect(
13 TransactGetItems,
14 Effect.gen(function* () {
15 const grantTables = yield* GrantTables;
16 return Effect.fn(function* (...tables: TransactGetItemsTables) {
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(`TransactGetItems request references unbound table '${tableId}'`),
31 );
32 }
33 return yield* TableName;
34 });
35
36 yield* grantTables(
37 `AWS.DynamoDB.TransactGetItems(${sortedTables.map((table) => table.LogicalId).join(", ")})`,
38 () => [
39 {
40 Effect: "Allow",
41 Action: ["dynamodb:GetItem"],
42 Resource: sortedTables.map((table) => table.tableArn),
43 },
44 ],
45 );
46 const transactGetItems = yield* AWSBindings.withCredentials(
47 DynamoDB.transactGetItems,
48 tablesRegion(sortedTables),
49 );
50
51 return Effect.fn(
52 `AWS.DynamoDB.TransactGetItems(${sortedTables.map((table) => table.LogicalId).join(", ")})`,
53 )(function* (request: TransactGetItemsRequest) {
54 const transactItems = yield* Effect.forEach(request.TransactItems, ({ Get }) =>
55 Effect.gen(function* () {
56 return {
57 Get: {
58 ...Get,
59 TableName: yield* getTableName(Get.Table),
60 },
61 };
62 }),
63 );
64
65 return yield* transactGetItems({
66 ...request,
67 TransactItems: transactItems,
68 });
69 });
70 });
71 }),
72 );
73
74 const sortTables = (tables: TransactGetItemsTables) =>
75 [...new Map(tables.map((table) => [table.LogicalId, table] as const)).values()].sort((a, b) =>
76 a.LogicalId.localeCompare(b.LogicalId),
77 ) as TransactGetItemsTables;