@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, 06:00 UTC

main
ts 113 lines 3.74 KB
Raw
1 import * as CostAndUsageReport from "@alchemy.run/aws/CostAndUsageReport";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import { Bucket } from "@alchemy.run/aws/S3";
4 import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import { HttpServerRequest } from "effect/http/HttpServerRequest";
7 import * as HttpServerResponse from "effect/http/HttpServerResponse";
8 import * as Layer from "effect/Layer";
9 import path from "pathe";
10
11 const main = path.resolve(import.meta.dirname, "handler.ts");
12
13 // Deterministic names — distinct from ReportDefinition.test.ts's fixtures so
14 // the two suites never collide.
15 export const REPORT_NAME = "alchemy-test-cur-bindings-report";
16 export const BUCKET_NAME = "alchemy-test-cur-bindings";
17
18 export class CurTestFunction extends Lambda.Function<Lambda.Function>()("CurTestFunction") {}
19
20 export default CurTestFunction.make(
21 {
22 main,
23 functionUrl: true,
24 timeout: Duration.seconds(30),
25 },
26 Effect.gen(function* () {
27 // The delivery bucket policy AWS validates before accepting a report
28 // definition (billingreports.amazonaws.com needs GetBucketAcl/
29 // GetBucketPolicy on the bucket and PutObject under it).
30 const bucket = yield* Bucket("CurBindingsBucket", {
31 bucketName: BUCKET_NAME,
32 forceDestroy: true,
33 policy: [
34 {
35 Effect: "Allow",
36 Principal: { Service: "billingreports.amazonaws.com" },
37 Action: ["s3:GetBucketAcl", "s3:GetBucketPolicy"],
38 Resource: `arn:aws:s3:::${BUCKET_NAME}`,
39 },
40 {
41 Effect: "Allow",
42 Principal: { Service: "billingreports.amazonaws.com" },
43 Action: ["s3:PutObject"],
44 Resource: `arn:aws:s3:::${BUCKET_NAME}/*`,
45 },
46 ],
47 });
48
49 const report = yield* CostAndUsageReport.ReportDefinition("CurBindingsReport", {
50 reportName: REPORT_NAME,
51 timeUnit: "DAILY",
52 format: "textORcsv",
53 compression: "GZIP",
54 s3Bucket: bucket.bucketName,
55 s3Prefix: "cur-bindings",
56 s3Region: bucket.region,
57 tags: { fixture: "cur-bindings" },
58 });
59
60 // --- account-level bindings ---
61 const describeReportDefinitions = yield* CostAndUsageReport.DescribeReportDefinitions();
62
63 // --- report-scoped bindings ---
64 const listReportTags = yield* CostAndUsageReport.ListTagsForResource(report);
65
66 const bound = {
67 describeReportDefinitions,
68 listReportTags,
69 };
70
71 return {
72 fetch: Effect.gen(function* () {
73 const request = yield* HttpServerRequest;
74 const url = new URL(request.originalUrl);
75 const pathname = url.pathname;
76
77 if (request.method === "GET" && pathname === "/bindings") {
78 return yield* HttpServerResponse.json({
79 bound: Object.keys(bound),
80 });
81 }
82
83 if (request.method === "GET" && pathname === "/reports") {
84 const result = yield* describeReportDefinitions();
85 const names = (result.ReportDefinitions ?? []).map((r) => r.ReportName);
86 return yield* HttpServerResponse.json({
87 count: names.length,
88 names,
89 });
90 }
91
92 if (request.method === "GET" && pathname === "/report-tags") {
93 const result = yield* listReportTags();
94 return yield* HttpServerResponse.json({
95 tags: Object.fromEntries((result.Tags ?? []).map((t) => [t.Key, t.Value])),
96 });
97 }
98
99 return yield* HttpServerResponse.json(
100 { error: "Not found", method: request.method, pathname },
101 { status: 404 },
102 );
103 }).pipe(Effect.orDie),
104 };
105 }).pipe(
106 Effect.provide(
107 Layer.mergeAll(
108 CostAndUsageReport.DescribeReportDefinitionsHttp,
109 CostAndUsageReport.ListTagsForResourceHttp,
110 ),
111 ),
112 ),
113 );