The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { Input } from "alchemy/Input"; |
| 2 | import * as Output from "alchemy/Output"; |
| 3 | import * as Data from "effect/Data"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import type { App } from "../App.ts"; |
| 6 | import type { Bucket } from "../Bucket.ts"; |
| 7 | import type { Database } from "../Database.ts"; |
| 8 | import type { Project } from "../Project.ts"; |
| 9 | |
| 10 | export class RefsOperationFailed extends Data.TaggedError("RefsOperationFailed")<{ |
| 11 | readonly message: string; |
| 12 | readonly cause?: unknown; |
| 13 | }> {} |
| 14 | |
| 15 | export type InputObject<T extends object> = { |
| 16 | [Key in keyof T]: Input<T[Key]>; |
| 17 | }; |
| 18 | |
| 19 | export const isInputObject = <T extends object>( |
| 20 | value: Input<T>, |
| 21 | ): value is InputObject<T> & Input<T> => |
| 22 | typeof value === "object" && value !== null && !Output.isOutput(value) && !Effect.isEffect(value); |
| 23 | |
| 24 | export const isPrismaDevId = (value: unknown): value is string => |
| 25 | typeof value === "string" && value.startsWith("dev:"); |
| 26 | |
| 27 | export const concreteIdOf = (value: unknown): string | undefined => |
| 28 | typeof value === "string" && !isPrismaDevId(value) ? value : undefined; |
| 29 | |
| 30 | export const concreteIdsChanged = (oldId: string | undefined, newId: string | undefined) => |
| 31 | oldId !== undefined && newId !== undefined && newId !== oldId; |
| 32 | |
| 33 | const resolveId = (label: string, value: unknown) => |
| 34 | typeof value === "string" |
| 35 | ? Effect.succeed(value) |
| 36 | : Effect.fail(new RefsOperationFailed({ message: `Unable to resolve Prisma ${label}.` })); |
| 37 | |
| 38 | export const unresolvedProjectIdOf = (project: string | Project | undefined) => |
| 39 | concreteIdOf(typeof project === "string" ? project : (project?.projectId as unknown)); |
| 40 | |
| 41 | export const resolveProjectId = (project: string | Project) => |
| 42 | resolveId("project id", typeof project === "string" ? project : (project.projectId as unknown)); |
| 43 | |
| 44 | export const unresolvedDatabaseIdOf = (database: string | Database | undefined) => |
| 45 | concreteIdOf(typeof database === "string" ? database : (database?.databaseId as unknown)); |
| 46 | |
| 47 | export const resolveDatabaseId = (database: string | Database) => |
| 48 | resolveId( |
| 49 | "database id", |
| 50 | typeof database === "string" ? database : (database.databaseId as unknown), |
| 51 | ); |
| 52 | |
| 53 | export const unresolvedBucketIdOf = (bucket: string | Bucket | undefined) => |
| 54 | concreteIdOf(typeof bucket === "string" ? bucket : (bucket?.bucketId as unknown)); |
| 55 | |
| 56 | export const resolveBucketId = (bucket: string | Bucket) => |
| 57 | resolveId("bucket id", typeof bucket === "string" ? bucket : (bucket.bucketId as unknown)); |
| 58 | |
| 59 | export const unresolvedAppIdOf = (app: string | App | undefined) => |
| 60 | concreteIdOf(typeof app === "string" ? app : (app?.appId as unknown)); |
| 61 | |
| 62 | export const resolveAppId = (app: string | App) => |
| 63 | resolveId("app id", typeof app === "string" ? app : (app.appId as unknown)); |