The best way to control Claude Code, Codex and more from any computer or phone

main
ts 71 lines 2.56 KB
Raw
1 import * as ApiGateway from "@alchemy.run/aws/ApiGateway";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Effect from "effect/Effect";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5
6 export class RestApiEventSourceFunction extends Lambda.Function<RestApiEventSourceFunction>()(
7 "RestApiEventSourceFunction",
8 ) {}
9
10 /**
11 * Fixture: a Lambda that owns a REST API and serves two routes through the
12 * `RestApiEventSource` (`onRestApiRoute`) binding. The event source
13 * materializes the `Resource` chain, `AWS_PROXY` `Method`s, and the invoke
14 * `Permission`; the `Deployment`/`Stage` below are ordered after them via
15 * the API's binding contract.
16 *
17 * Deliberately does NOT `yield*` any `api.*`/`stage.*` outputs: the API's
18 * full `create` settles only after every bound child (including the
19 * `Method`s that reference `host.functionArn`), so awaiting those outputs
20 * inside the host's own effect would deadlock the plan. The test discovers
21 * the REST API id out-of-band by its stack, stage, and logical-id tags.
22 */
23 export default RestApiEventSourceFunction.make(
24 {
25 main: import.meta.url,
26 functionUrl: true,
27 },
28 Effect.gen(function* () {
29 const api = yield* ApiGateway.RestApi("AgEsApi", {
30 endpointConfiguration: { types: ["REGIONAL"] },
31 });
32
33 // GET /items — static JSON response.
34 yield* ApiGateway.onRestApiRoute(api, { path: "/items", httpMethod: "GET" }, () =>
35 Effect.succeed({
36 statusCode: 200,
37 headers: { "content-type": "application/json" },
38 body: JSON.stringify({ items: ["alpha", "beta"] }),
39 }),
40 );
41
42 // POST /echo — proves the proxy event (method, resource, body) reaches
43 // the handler intact.
44 yield* ApiGateway.onRestApiRoute(api, { path: "/echo", httpMethod: "POST" }, (event) =>
45 Effect.sync(() => ({
46 statusCode: 200,
47 headers: { "content-type": "application/json" },
48 body: JSON.stringify({
49 method: event.httpMethod,
50 resource: event.resource,
51 echoed: event.body === null ? null : JSON.parse(event.body),
52 }),
53 })),
54 );
55
56 const deployment = yield* ApiGateway.Deployment("AgEsDep", {
57 restApi: api,
58 });
59 yield* ApiGateway.Stage("AgEsStage", {
60 restApi: api,
61 stageName: "test",
62 deploymentId: deployment.deploymentId,
63 });
64
65 return {
66 // Readiness probe only — the REST routes are served via the event
67 // source, not the function URL.
68 fetch: Effect.succeed(HttpServerResponse.text("ok")),
69 };
70 }).pipe(Effect.provide(Lambda.RestApiEventSource)),
71 );