@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 193 lines 5.85 KB
Raw
1 import * as Redacted from "effect/Redacted";
2 import type { DatabaseStatus } from "../Types.ts";
3
4 /**
5 * Structural shapes for cloud values that reach shared helpers from more than
6 * one source.
7 *
8 * `Types.ts` narrows discriminants to literals (`type: "database"`) and types
9 * secrets as plain strings, while the generated distilled schemas type those
10 * fields as `string` and `string | Redacted.Redacted<string>`. Helpers used by
11 * both migrated and unmigrated resource files therefore accept the minimum
12 * each one actually reads, which both sides satisfy.
13 */
14
15 export interface ObservedEndpoint {
16 readonly host?: string;
17 readonly connectionString?: string | Redacted.Redacted<string> | undefined;
18 }
19
20 export interface ObservedConnection {
21 readonly endpoints?: {
22 readonly direct?: ObservedEndpoint | undefined;
23 readonly pooled?: ObservedEndpoint | undefined;
24 readonly accelerate?: ObservedEndpoint | undefined;
25 };
26 }
27
28 export interface ObservedDatabase {
29 readonly id: string;
30 readonly name: string;
31 readonly status: DatabaseStatus;
32 readonly defaultConnectionId: string | null;
33 readonly connections: ReadonlyArray<ObservedConnection & { readonly id: string }>;
34 readonly region?: { readonly id: string; readonly name: string } | null;
35 }
36
37 export interface ObservedConnectionRecord extends ObservedConnection {
38 readonly id: string;
39 readonly name: string;
40 readonly kind: "postgres" | "accelerate";
41 readonly createdAt: string;
42 readonly database: { readonly id: string };
43 }
44
45 export interface ObservedSourceRepository {
46 readonly id: string;
47 readonly projectId: string;
48 readonly repoId: number;
49 readonly provider: "github";
50 readonly repoFullName: string;
51 readonly defaultBranch: string;
52 readonly isPrivate: boolean;
53 readonly status: "active" | "archived";
54 readonly installationId: string;
55 readonly createdAt: string;
56 readonly updatedAt: string;
57 }
58
59 export interface ObservedEnvironmentVariable {
60 readonly id: string;
61 readonly projectId: string;
62 readonly branchId: string | null;
63 readonly class: "production" | "preview";
64 readonly key: string;
65 readonly valueKid: string;
66 readonly isManagedBySystem: boolean;
67 readonly createdAt: string;
68 readonly updatedAt: string;
69 }
70
71 export interface ObservedBucketKey {
72 readonly id: string;
73 readonly name: string;
74 }
75
76 export interface ObservedBucket {
77 readonly id: string;
78 readonly name: string;
79 readonly createdAt: string;
80 readonly project: { readonly id: string };
81 readonly branchId?: string | null;
82 readonly logicalId?: string | null;
83 }
84
85 export interface ObservedApp {
86 readonly id: string;
87 readonly name: string;
88 readonly projectId: string;
89 readonly region: { readonly id: string };
90 readonly branchId: string | null;
91 readonly latestDeploymentId: string | null;
92 readonly appEndpointDomain: string;
93 readonly createdAt: string;
94 readonly logicalId?: string | null;
95 }
96
97 export interface ObservedDeployment {
98 readonly id: string;
99 readonly foundryVersionId: string;
100 readonly status: string;
101 readonly previewDomain: string | null;
102 readonly createdAt?: string;
103 }
104
105 export interface ObservedBranch {
106 readonly id: string;
107 readonly gitName: string;
108 readonly isDefault: boolean;
109 readonly role: "production" | "preview";
110 readonly createdAt: string;
111 readonly updatedAt: string;
112 readonly project: { readonly id: string };
113 }
114
115 /**
116 * The Management API types a database's `source` discriminant as a plain
117 * string. {@link narrowDatabaseSource} turns an observed value back into the
118 * discriminated union the diffing logic needs, by checking the fields rather
119 * than asserting them.
120 */
121 export interface ObservedSource {
122 readonly type: string;
123 readonly databaseId?: string;
124 readonly backupId?: string;
125 }
126
127 export interface ObservedProjectDatabase extends ObservedDatabase {
128 readonly isDefault: boolean;
129 readonly branchId: string | null;
130 readonly createdAt: string;
131 readonly source: ObservedSource | null;
132 readonly project: { readonly id: string };
133 readonly logicalId?: string | null;
134 }
135
136 export interface ObservedProject {
137 readonly id: string;
138 readonly name: string;
139 readonly createdAt: string;
140 readonly defaultRegion: string | null;
141 readonly workspace: { readonly id: string };
142 }
143
144 /** Read a required possibly-redacted secret as a plain string. */
145 export const requiredSecretValue = (value: string | Redacted.Redacted<string>): string =>
146 typeof value === "string" ? value : Redacted.value(value);
147
148 /** Read a possibly-redacted secret as a plain string. */
149 export const secretValue = (
150 value: string | Redacted.Redacted<string> | undefined | null,
151 ): string | undefined =>
152 value === undefined || value === null
153 ? undefined
154 : typeof value === "string"
155 ? value
156 : Redacted.value(value);
157
158 /**
159 * Recover the discriminated source union from an observed database's
160 * string-typed `source`. A missing source reads as `empty`, matching how the
161 * diffing logic already treats it. An unrecognized shape reads as `unknown`,
162 * which deliberately matches no desired source, so the immutable-source
163 * refusals fire instead of silently accepting it.
164 */
165 export const narrowDatabaseSource = (
166 source: ObservedSource | undefined | null,
167 ):
168 | { readonly type: "empty" }
169 | { readonly type: "unknown" }
170 | { readonly type: "database"; readonly databaseId: string }
171 | {
172 readonly type: "backup";
173 readonly databaseId: string;
174 readonly backupId: string;
175 } => {
176 if (source == null) return { type: "empty" };
177 if (source.type === "empty") return { type: "empty" };
178 if (source.type === "database" && source.databaseId !== undefined) {
179 return { type: "database", databaseId: source.databaseId };
180 }
181 if (
182 source.type === "backup" &&
183 source.databaseId !== undefined &&
184 source.backupId !== undefined
185 ) {
186 return {
187 type: "backup",
188 databaseId: source.databaseId,
189 backupId: source.backupId,
190 };
191 }
192 return { type: "unknown" };
193 };