@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 99 lines 3.42 KB
Raw
1 import * as BackupSearch from "@alchemy.run/aws/BackupSearch";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
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 // Deterministic name so the crash-safe sweeper in Bindings.test.ts can find
12 // (and stop) any job a crashed prior run left RUNNING.
13 export const FIXTURE_SEARCH_JOB_NAME = "alchemy-test-backupsearch-bindings";
14
15 export class BackupSearchTestFunction extends Lambda.Function<Lambda.Function>()(
16 "BackupSearchTestFunction",
17 ) {}
18
19 export default BackupSearchTestFunction.make(
20 {
21 main,
22 functionUrl: true,
23 },
24 Effect.gen(function* () {
25 // The fixture search job scans S3 recovery points for a prefix that
26 // matches nothing — the job's lifecycle (and both list operations) are
27 // identical whether or not the account has indexed recovery points.
28 const search = yield* BackupSearch.SearchJob("BindingsSearch", {
29 name: FIXTURE_SEARCH_JOB_NAME,
30 searchScope: { backupResourceTypes: ["S3"] },
31 itemFilters: {
32 s3ItemFilters: [
33 {
34 objectKeys: [{ value: "alchemy-bindings-", operator: "BEGINS_WITH" }],
35 },
36 ],
37 },
38 tags: { fixture: "backup-search-bindings" },
39 });
40
41 // --- search-job-scoped bindings ---
42 const listSearchJobResults = yield* BackupSearch.ListSearchJobResults(search);
43 const listSearchJobBackups = yield* BackupSearch.ListSearchJobBackups(search);
44 const getSearchJob = yield* BackupSearch.GetSearchJob(search);
45
46 const bound = { listSearchJobResults, listSearchJobBackups, getSearchJob };
47
48 return {
49 fetch: Effect.gen(function* () {
50 const request = yield* HttpServerRequest;
51 const url = new URL(request.originalUrl);
52 const pathname = url.pathname;
53
54 if (request.method === "GET" && pathname === "/bindings") {
55 return yield* HttpServerResponse.json({
56 bound: Object.keys(bound),
57 });
58 }
59
60 if (request.method === "GET" && pathname === "/results") {
61 const page = yield* listSearchJobResults({ MaxResults: 25 });
62 return yield* HttpServerResponse.json({
63 count: page.Results.length,
64 nextToken: page.NextToken ?? null,
65 });
66 }
67
68 if (request.method === "GET" && pathname === "/job") {
69 const job = yield* getSearchJob();
70 return yield* HttpServerResponse.json({
71 status: job.Status ?? null,
72 name: job.Name ?? null,
73 });
74 }
75
76 if (request.method === "GET" && pathname === "/backups") {
77 const page = yield* listSearchJobBackups({ MaxResults: 25 });
78 return yield* HttpServerResponse.json({
79 count: page.Results.length,
80 statuses: page.Results.map((backup) => backup.Status ?? null),
81 });
82 }
83
84 return yield* HttpServerResponse.json(
85 { error: "Not found", method: request.method, pathname },
86 { status: 404 },
87 );
88 }).pipe(Effect.orDie),
89 };
90 }).pipe(
91 Effect.provide(
92 Layer.mergeAll(
93 BackupSearch.ListSearchJobResultsHttp,
94 BackupSearch.ListSearchJobBackupsHttp,
95 BackupSearch.GetSearchJobHttp,
96 ),
97 ),
98 ),
99 );