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
| 1 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 2 | import * as SimpleDB from "@alchemy.run/aws/SimpleDB"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import path from "pathe"; |
| 8 | |
| 9 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 10 | |
| 11 | export class SimpleDBTestFunction extends Lambda.Function<Lambda.Function>()( |
| 12 | "SimpleDBTestFunction", |
| 13 | ) {} |
| 14 | |
| 15 | export default SimpleDBTestFunction.make( |
| 16 | { |
| 17 | main, |
| 18 | functionUrl: true, |
| 19 | }, |
| 20 | Effect.gen(function* () { |
| 21 | const domain = yield* SimpleDB.Domain("BindingsDomain", {}); |
| 22 | |
| 23 | const putAttributes = yield* SimpleDB.PutAttributes(domain); |
| 24 | const getAttributes = yield* SimpleDB.GetAttributes(domain); |
| 25 | const deleteAttributes = yield* SimpleDB.DeleteAttributes(domain); |
| 26 | const batchPutAttributes = yield* SimpleDB.BatchPutAttributes(domain); |
| 27 | const batchDeleteAttributes = yield* SimpleDB.BatchDeleteAttributes(domain); |
| 28 | const domainMetadata = yield* SimpleDB.DomainMetadata(domain); |
| 29 | const select = yield* SimpleDB.Select(domain); |
| 30 | const listDomains = yield* SimpleDB.ListDomains(); |
| 31 | |
| 32 | return { |
| 33 | fetch: Effect.gen(function* () { |
| 34 | const request = yield* HttpServerRequest; |
| 35 | const url = new URL(request.originalUrl); |
| 36 | const pathname = url.pathname; |
| 37 | |
| 38 | if (request.method === "POST" && pathname === "/put") { |
| 39 | const body = (yield* request.json) as unknown as { |
| 40 | item: string; |
| 41 | attributes: { name: string; value: string; replace?: boolean }[]; |
| 42 | }; |
| 43 | yield* putAttributes({ |
| 44 | ItemName: body.item, |
| 45 | Attributes: body.attributes.map((a) => ({ |
| 46 | Name: a.name, |
| 47 | Value: a.value, |
| 48 | Replace: a.replace ?? true, |
| 49 | })), |
| 50 | }); |
| 51 | return yield* HttpServerResponse.json({ success: true }); |
| 52 | } |
| 53 | |
| 54 | if (request.method === "GET" && pathname === "/get") { |
| 55 | const item = url.searchParams.get("item"); |
| 56 | if (!item) { |
| 57 | return HttpServerResponse.text("Missing item", { status: 400 }); |
| 58 | } |
| 59 | const result = yield* getAttributes({ |
| 60 | ItemName: item, |
| 61 | ConsistentRead: true, |
| 62 | }); |
| 63 | return yield* HttpServerResponse.json({ |
| 64 | attributes: result.Attributes ?? [], |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | if (request.method === "GET" && pathname === "/select") { |
| 69 | const where = url.searchParams.get("where"); |
| 70 | const result = yield* select({ |
| 71 | SelectExpression: (name) => |
| 72 | where ? `select * from \`${name}\` where ${where}` : `select * from \`${name}\``, |
| 73 | ConsistentRead: true, |
| 74 | }); |
| 75 | return yield* HttpServerResponse.json({ |
| 76 | items: result.Items ?? [], |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | if (request.method === "POST" && pathname === "/batch-put") { |
| 81 | const body = (yield* request.json) as unknown as { |
| 82 | items: { |
| 83 | item: string; |
| 84 | attributes: { name: string; value: string }[]; |
| 85 | }[]; |
| 86 | }; |
| 87 | yield* batchPutAttributes({ |
| 88 | Items: body.items.map((i) => ({ |
| 89 | ItemName: i.item, |
| 90 | Attributes: i.attributes.map((a) => ({ |
| 91 | Name: a.name, |
| 92 | Value: a.value, |
| 93 | Replace: true, |
| 94 | })), |
| 95 | })), |
| 96 | }); |
| 97 | return yield* HttpServerResponse.json({ success: true }); |
| 98 | } |
| 99 | |
| 100 | if (request.method === "POST" && pathname === "/batch-delete") { |
| 101 | const body = (yield* request.json) as unknown as { |
| 102 | items: string[]; |
| 103 | }; |
| 104 | yield* batchDeleteAttributes({ |
| 105 | Items: body.items.map((item) => ({ ItemName: item })), |
| 106 | }); |
| 107 | return yield* HttpServerResponse.json({ success: true }); |
| 108 | } |
| 109 | |
| 110 | if (request.method === "DELETE" && pathname === "/delete") { |
| 111 | const body = (yield* request.json) as unknown as { |
| 112 | item: string; |
| 113 | attributes?: string[]; |
| 114 | }; |
| 115 | yield* deleteAttributes({ |
| 116 | ItemName: body.item, |
| 117 | ...(body.attributes |
| 118 | ? { Attributes: body.attributes.map((name) => ({ Name: name })) } |
| 119 | : {}), |
| 120 | }); |
| 121 | return yield* HttpServerResponse.json({ success: true }); |
| 122 | } |
| 123 | |
| 124 | if (request.method === "GET" && pathname === "/domains") { |
| 125 | const result = yield* listDomains({ MaxNumberOfDomains: 100 }); |
| 126 | return yield* HttpServerResponse.json({ |
| 127 | domainNames: result.DomainNames ?? [], |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | if (request.method === "GET" && pathname === "/metadata") { |
| 132 | const result = yield* domainMetadata(); |
| 133 | return yield* HttpServerResponse.json({ |
| 134 | itemCount: result.ItemCount, |
| 135 | attributeNameCount: result.AttributeNameCount, |
| 136 | attributeValueCount: result.AttributeValueCount, |
| 137 | }); |
| 138 | } |
| 139 | |
| 140 | return yield* HttpServerResponse.json( |
| 141 | { error: "Not found", method: request.method, pathname }, |
| 142 | { status: 404 }, |
| 143 | ); |
| 144 | }).pipe(Effect.orDie), |
| 145 | }; |
| 146 | }).pipe( |
| 147 | Effect.provide( |
| 148 | Layer.mergeAll( |
| 149 | SimpleDB.BatchDeleteAttributesHttp, |
| 150 | SimpleDB.BatchPutAttributesHttp, |
| 151 | SimpleDB.DeleteAttributesHttp, |
| 152 | SimpleDB.DomainMetadataHttp, |
| 153 | SimpleDB.GetAttributesHttp, |
| 154 | SimpleDB.ListDomainsHttp, |
| 155 | SimpleDB.PutAttributesHttp, |
| 156 | SimpleDB.SelectHttp, |
| 157 | ), |
| 158 | ), |
| 159 | ), |
| 160 | ); |