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

main
ts 59 lines 1.94 KB
Raw
1 import * as Effect from "effect/Effect";
2 import * as GCPBindings from "../GCPBindings.ts";
3 import { grantFor, type BindingIam, type GcpHttpOp } from "../HttpBinding.ts";
4 import type { Subscription } from "./Subscription.ts";
5 import type { Topic } from "./Topic.ts";
6
7 /**
8 * Shared HTTP scaffolding for Pub/Sub bindings.
9 * NOT exported from index.ts.
10 */
11 export const makeTopicHttpBinding = <I extends { topic: string }, A, E>(options: {
12 tag: string;
13 iam: BindingIam;
14 operation: GcpHttpOp<I, A, E>;
15 }) =>
16 Effect.gen(function* () {
17 const bind = yield* GCPBindings.Bind;
18 const run = yield* GCPBindings.withCredentials(options.operation);
19 return Effect.fn(function* (topic: Topic) {
20 const name = yield* topic.name;
21 if (!globalThis.__ALCHEMY_RUNTIME__) {
22 yield* bind`${options.tag}(${topic})`({
23 iam: [grantFor(options.iam, topic.name)],
24 });
25 }
26 return Effect.fn(`${options.tag}(${topic.LogicalId})`)(function* (request: Omit<I, "topic">) {
27 return yield* run({
28 ...request,
29 topic: yield* name,
30 } as I);
31 });
32 });
33 });
34
35 export const makeSubscriptionHttpBinding = <I extends { subscription: string }, A, E>(options: {
36 tag: string;
37 iam: BindingIam;
38 operation: GcpHttpOp<I, A, E>;
39 }) =>
40 Effect.gen(function* () {
41 const bind = yield* GCPBindings.Bind;
42 const run = yield* GCPBindings.withCredentials(options.operation);
43 return Effect.fn(function* (subscription: Subscription) {
44 const name = yield* subscription.name;
45 if (!globalThis.__ALCHEMY_RUNTIME__) {
46 yield* bind`${options.tag}(${subscription})`({
47 iam: [grantFor(options.iam, subscription.name)],
48 });
49 }
50 return Effect.fn(`${options.tag}(${subscription.LogicalId})`)(function* (
51 request: Omit<I, "subscription">,
52 ) {
53 return yield* run({
54 ...request,
55 subscription: yield* name,
56 } as I);
57 });
58 });
59 });