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

main
ts 89 lines 2.86 KB
Raw
1 import * as connectivity from "@distilled.cloud/cloudflare/connectivity";
2 import * as Output from "alchemy/Output";
3 import * as Effect from "effect/Effect";
4 import * as Option from "effect/Option";
5 import * as Stream from "effect/Stream";
6 import { CloudflareEnvironment } from "../CloudflareEnvironment.ts";
7 import { formatVpcService, type Attributes } from "./VpcService.ts";
8
9 export type VpcServiceLookupProps =
10 | {
11 /**
12 * The Cloudflare-assigned ID for the VPC service.
13 */
14 serviceId: string;
15 }
16 | {
17 /**
18 * The display name of the VPC service.
19 */
20 name: string;
21 };
22
23 /**
24 * The resolved value of a {@link lookup} — the service's {@link Attributes}
25 * branded with the VpcService resource `Type`, so Worker binding
26 * classification treats it exactly like the managed resource.
27 */
28 export interface VpcServiceLookup extends Attributes {
29 readonly Type: "Cloudflare.VpcService.VpcService";
30 }
31
32 const toLookup = (attrs: Attributes): VpcServiceLookup => ({
33 ...attrs,
34 Type: "Cloudflare.VpcService.VpcService",
35 });
36
37 /**
38 * Look up an existing Cloudflare VPC service (managed outside this stack)
39 * without managing its lifecycle — the data-source form (what Terraform
40 * calls a data source and Pulumi an invoke). Reads the service by
41 * `serviceId` or `name` and returns an `Output` of its {@link Attributes},
42 * resolved during plan/deploy and inert inside deployed bundles. Place it
43 * in a Worker's `env` to attach a `vpc_service` binding.
44 * **Example:** Look up by ID
45 * ```typescript
46 * const service = Cloudflare.VpcService.lookup({
47 * serviceId: "123e4567-e89b-12d3-a456-426614174000",
48 * });
49 * ```
50 *
51 * **Example:** Look up by name
52 * ```typescript
53 * const service = Cloudflare.VpcService.lookup({ name: "my-vpc-service" });
54 * ```
55 *
56 * **Example:** Bind to a Worker
57 * ```typescript
58 * const worker = yield* Cloudflare.Worker("Worker", {
59 * main: "./src/worker.ts",
60 * env: { VPC: Cloudflare.VpcService.lookup({ name: "my-vpc-service" }) },
61 * });
62 * ```
63 *
64 * @resource
65 * @product Workers VPC
66 * @category Network
67 */
68 export const lookup = (props: VpcServiceLookupProps) =>
69 Output.fromEffect(
70 Effect.gen(function* () {
71 const { accountId } = yield* yield* CloudflareEnvironment;
72 if ("name" in props) {
73 const match = yield* connectivity.listDirectoryServices.items({ accountId }).pipe(
74 Stream.filter((s) => s.name === props.name),
75 Stream.runHead,
76 Effect.map(Option.getOrUndefined),
77 );
78 if (!match) {
79 return yield* Effect.die(`VPC service "${props.name}" not found`);
80 }
81 return toLookup(formatVpcService(match, accountId));
82 }
83 const result = yield* connectivity.getDirectoryService({
84 accountId,
85 serviceId: props.serviceId,
86 });
87 return toLookup(formatVpcService(result, accountId));
88 }).pipe(Effect.orDie),
89 );