@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 81 lines 2.66 KB
Raw
1 /**
2 * A Railway variable-reference template (`${{Service.KEY}}` or
3 * `${{shared.NAME}}`). Pass as {@link Variable} `value` or `Service.env`.
4 *
5 * Distinct from the unexported resource-prop `Ref<T>`. Never export that
6 * `Ref`.
7 */
8 export type VariableRef = string;
9
10 /**
11 * Something `Railway.ref` can take a namespace from: `"shared"`, an
12 * explicit service name, or a resource's `LogicalId`.
13 */
14 export type VariableRefSource =
15 | string
16 | {
17 readonly LogicalId: string;
18 };
19
20 const SHARED = "shared";
21
22 const namespaceOf = (resource: VariableRefSource): string => {
23 if (typeof resource === "string") {
24 const trimmed = resource.trim();
25 return trimmed.length > 0 ? trimmed : SHARED;
26 }
27 const logical = typeof resource.LogicalId === "string" ? resource.LogicalId.trim() : "";
28 return logical.length > 0 ? logical : SHARED;
29 };
30
31 /**
32 * Emit a Railway variable-reference template (`${{Service.KEY}}` or
33 * `${{shared.NAME}}`). Distinct from the unexported resource-prop
34 * `Ref<T>` — never export that `Ref`.
35 *
36 * `Railway.ref(Db, "DATABASE_URL")` returns `${{LogicalName.DATABASE_URL}}`
37 * where `LogicalName` is `resource.LogicalId` (`Postgres("Db")` → `Db`).
38 * Shared variables use the `"shared"` namespace:
39 * `Railway.ref("shared", "SENTRY_DSN")` returns `${{shared.SENTRY_DSN}}`.
40 *
41 * Store the string as a {@link Variable} `value` (or on `Service.env`).
42 * Railway keeps the template (`unrendered: true`) and interpolates it at
43 * build/runtime — it is not a resolved URI. Use {@link ConnectPostgres}
44 * when you want a typed client inside an Effect-native Service.
45 *
46 * Railway interpolates by service name. Set `name` on Postgres/Service to
47 * the LogicalId if you need the template to resolve to that service.
48 *
49 * @see https://docs.railway.com/variables/reference#template-syntax
50 * @see https://docs.railway.com/infrastructure-as-code/reference
51 *
52 * ### Reference a service variable
53 * Pass a resource (or its LogicalId) and the variable key. The result is
54 * a template string, not a URI.
55 *
56 * **Example:** Reference Postgres DATABASE_URL
57 * ```typescript
58 * const db = yield* Railway.Postgres("Db", { project: site });
59 * yield* Railway.Variable("DatabaseUrl", {
60 * project: site,
61 * service: api,
62 * name: "DATABASE_URL",
63 * value: Railway.ref(db, "DATABASE_URL"),
64 * });
65 * ```
66 *
67 * ### Shared variables
68 * `"shared"` is the environment-wide namespace (IaC `ctx.shared.NAME`).
69 *
70 * **Example:** Shared variable
71 * ```typescript
72 * env: {
73 * SENTRY_DSN: Railway.ref("shared", "SENTRY_DSN"),
74 * }
75 * ```
76 *
77 * @resource
78 * @product Project
79 */
80 export const ref = (resource: VariableRefSource, key: string): VariableRef =>
81 `\${{${namespaceOf(resource)}.${key}}}`;