@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 219 lines 8.89 KB
Raw
1 import * as DataZone from "@alchemy.run/aws/DataZone";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import * as Result from "effect/Result";
9 import * as Stream from "effect/Stream";
10 import path from "pathe";
11
12 const main = path.resolve(import.meta.dirname, "handler.ts");
13
14 /**
15 * Deterministic domain name so the test can find the domain id out-of-band
16 * (needed to create a DataZone user profile for the function's role).
17 */
18 export const BINDINGS_DOMAIN_NAME = "alchemy-datazone-bindings-test";
19
20 /**
21 * Deterministic project name so the test can find the project id
22 * out-of-band (needed to add the function's user profile as a member —
23 * inventory search is project-scoped).
24 */
25 export const BINDINGS_PROJECT_NAME = "alchemy-datazone-bindings-project";
26
27 export class DataZoneTestFunction extends Lambda.Function<Lambda.Function>()(
28 "DataZoneTestFunction",
29 ) {}
30
31 export default DataZoneTestFunction.make(
32 {
33 main,
34 functionUrl: true,
35 timeout: Duration.seconds(30),
36 },
37 Effect.gen(function* () {
38 // The domain every domain-scoped binding is bound to.
39 const domain = yield* DataZone.Domain("BindingsDomain", {
40 name: BINDINGS_DOMAIN_NAME,
41 description: "alchemy datazone bindings fixture domain",
42 });
43
44 // A project for the project-scoped calls (inventory search requires an
45 // owning project; subscription listing requires a project filter).
46 const project = yield* DataZone.Project("BindingsProject", {
47 domainId: domain.domainId,
48 name: BINDINGS_PROJECT_NAME,
49 description: "alchemy datazone bindings fixture project",
50 });
51 const ProjectId = yield* project.projectId;
52
53 // Event source: subscribe the host to DataZone workflow events. The
54 // deploy proves the EventBridge rule + invoke permission wiring.
55 yield* DataZone.consumeDataZoneEvents(
56 { detailTypes: ["Subscription Request Created"] },
57 (events) =>
58 Stream.runForEach(events, (event) =>
59 Effect.log(`datazone event: ${event.detail.metadata?.id}`),
60 ),
61 );
62
63 const bound = {
64 search: yield* DataZone.Search(domain),
65 searchListings: yield* DataZone.SearchListings(domain),
66 searchTypes: yield* DataZone.SearchTypes(domain),
67 getListing: yield* DataZone.GetListing(domain),
68 getAsset: yield* DataZone.GetAsset(domain),
69 createAsset: yield* DataZone.CreateAsset(domain),
70 createAssetRevision: yield* DataZone.CreateAssetRevision(domain),
71 acceptPredictions: yield* DataZone.AcceptPredictions(domain),
72 rejectPredictions: yield* DataZone.RejectPredictions(domain),
73 postLineageEvent: yield* DataZone.PostLineageEvent(domain),
74 getLineageNode: yield* DataZone.GetLineageNode(domain),
75 postTimeSeriesDataPoints: yield* DataZone.PostTimeSeriesDataPoints(domain),
76 listTimeSeriesDataPoints: yield* DataZone.ListTimeSeriesDataPoints(domain),
77 getTimeSeriesDataPoint: yield* DataZone.GetTimeSeriesDataPoint(domain),
78 createSubscriptionRequest: yield* DataZone.CreateSubscriptionRequest(domain),
79 acceptSubscriptionRequest: yield* DataZone.AcceptSubscriptionRequest(domain),
80 rejectSubscriptionRequest: yield* DataZone.RejectSubscriptionRequest(domain),
81 getSubscription: yield* DataZone.GetSubscription(domain),
82 listSubscriptions: yield* DataZone.ListSubscriptions(domain),
83 listSubscriptionRequests: yield* DataZone.ListSubscriptionRequests(domain),
84 cancelSubscription: yield* DataZone.CancelSubscription(domain),
85 revokeSubscription: yield* DataZone.RevokeSubscription(domain),
86 startDataSourceRun: yield* DataZone.StartDataSourceRun(domain),
87 getDataSourceRun: yield* DataZone.GetDataSourceRun(domain),
88 listDataSourceRuns: yield* DataZone.ListDataSourceRuns(domain),
89 startMetadataGenerationRun: yield* DataZone.StartMetadataGenerationRun(domain),
90 getMetadataGenerationRun: yield* DataZone.GetMetadataGenerationRun(domain),
91 getIamPortalLoginUrl: yield* DataZone.GetIamPortalLoginUrl(domain),
92 listNotifications: yield* DataZone.ListNotifications(domain),
93 getUserProfile: yield* DataZone.GetUserProfile(domain),
94 };
95
96 /**
97 * Run a DataZone call and answer `{ ok: true, ... }` on success, or a
98 * 502 carrying the typed error tag on failure — so the test's retry
99 * loop surfaces the exact DataZone error instead of an opaque 500.
100 */
101 const respond = <A, E>(
102 effect: Effect.Effect<A, E>,
103 body: (value: A) => Record<string, unknown>,
104 ) =>
105 Effect.gen(function* () {
106 const result = yield* Effect.result(effect);
107 return yield* Result.isSuccess(result)
108 ? HttpServerResponse.json({ ok: true, ...body(result.success) })
109 : HttpServerResponse.json({ ok: false, error: String(result.failure) }, { status: 502 });
110 });
111
112 return {
113 fetch: Effect.gen(function* () {
114 const request = yield* HttpServerRequest;
115 const url = new URL(request.originalUrl);
116 const pathname = url.pathname;
117
118 if (request.method === "GET" && pathname === "/bindings") {
119 return yield* HttpServerResponse.json({
120 bound: Object.keys(bound),
121 });
122 }
123
124 // Inventory search: the domain id is injected from the binding;
125 // inventory assets are project-scoped, so the owning project is
126 // required.
127 if (request.method === "GET" && pathname === "/search") {
128 const owningProjectIdentifier = yield* ProjectId;
129 return yield* respond(
130 bound.search({ searchScope: "ASSET", owningProjectIdentifier }),
131 (result) => ({
132 totalMatchCount: result.totalMatchCount ?? 0,
133 items: (result.items ?? []).length,
134 }),
135 );
136 }
137
138 // Listing search over the published catalog.
139 if (request.method === "GET" && pathname === "/listings") {
140 return yield* respond(bound.searchListings({}), (result) => ({
141 items: (result.items ?? []).length,
142 }));
143 }
144
145 // Approved subscriptions (empty in a fresh domain). DataZone
146 // requires a project (or listing/principal) filter.
147 if (request.method === "GET" && pathname === "/subscriptions") {
148 const owningProjectId = yield* ProjectId;
149 return yield* respond(
150 bound.listSubscriptions({ status: "APPROVED", owningProjectId }),
151 (result) => ({ items: (result.items ?? []).length }),
152 );
153 }
154
155 // Pending subscription requests (empty in a fresh domain). Same
156 // project filter requirement as /subscriptions.
157 if (request.method === "GET" && pathname === "/requests") {
158 const owningProjectId = yield* ProjectId;
159 return yield* respond(
160 bound.listSubscriptionRequests({
161 status: "PENDING",
162 owningProjectId,
163 }),
164 (result) => ({ items: (result.items ?? []).length }),
165 );
166 }
167
168 // Single-use data portal sign-in URL.
169 if (request.method === "GET" && pathname === "/portal") {
170 return yield* respond(bound.getIamPortalLoginUrl(), (result) => ({
171 authCodeUrl: result.authCodeUrl,
172 userProfileId: result.userProfileId,
173 }));
174 }
175
176 return yield* HttpServerResponse.json(
177 { error: "Not found", method: request.method, pathname },
178 { status: 404 },
179 );
180 }).pipe(Effect.orDie),
181 };
182 }).pipe(
183 Effect.provide(
184 Layer.mergeAll(
185 Lambda.EventSource,
186 DataZone.SearchHttp,
187 DataZone.SearchListingsHttp,
188 DataZone.SearchTypesHttp,
189 DataZone.GetListingHttp,
190 DataZone.GetAssetHttp,
191 DataZone.CreateAssetHttp,
192 DataZone.CreateAssetRevisionHttp,
193 DataZone.AcceptPredictionsHttp,
194 DataZone.RejectPredictionsHttp,
195 DataZone.PostLineageEventHttp,
196 DataZone.GetLineageNodeHttp,
197 DataZone.PostTimeSeriesDataPointsHttp,
198 DataZone.ListTimeSeriesDataPointsHttp,
199 DataZone.GetTimeSeriesDataPointHttp,
200 DataZone.CreateSubscriptionRequestHttp,
201 DataZone.AcceptSubscriptionRequestHttp,
202 DataZone.RejectSubscriptionRequestHttp,
203 DataZone.GetSubscriptionHttp,
204 DataZone.ListSubscriptionsHttp,
205 DataZone.ListSubscriptionRequestsHttp,
206 DataZone.CancelSubscriptionHttp,
207 DataZone.RevokeSubscriptionHttp,
208 DataZone.StartDataSourceRunHttp,
209 DataZone.GetDataSourceRunHttp,
210 DataZone.ListDataSourceRunsHttp,
211 DataZone.StartMetadataGenerationRunHttp,
212 DataZone.GetMetadataGenerationRunHttp,
213 DataZone.GetIamPortalLoginUrlHttp,
214 DataZone.ListNotificationsHttp,
215 DataZone.GetUserProfileHttp,
216 ),
217 ),
218 ),
219 );