import { DesktopSshEnvironmentTargetSchema, EnvironmentId } from "@t3tools/contracts"; import * as Option from "effect/Option"; import * as Schema from "effect/Schema"; import { BearerConnectionTarget, PrimaryConnectionTarget, RelayConnectionTarget, SshConnectionTarget, type ConnectionTarget, } from "./model.ts"; const ConnectionProfileBase = { connectionId: Schema.String, environmentId: EnvironmentId, label: Schema.String, }; export class BearerConnectionProfile extends Schema.TaggedClass()( "BearerConnectionProfile", { ...ConnectionProfileBase, httpBaseUrl: Schema.String, wsBaseUrl: Schema.String, }, ) {} export class SshConnectionProfile extends Schema.TaggedClass()( "SshConnectionProfile", { ...ConnectionProfileBase, target: DesktopSshEnvironmentTargetSchema, }, ) {} export const ConnectionProfile = Schema.Union([BearerConnectionProfile, SshConnectionProfile]); export type ConnectionProfile = typeof ConnectionProfile.Type; export interface ConnectionCatalogEntry { readonly target: ConnectionTarget; readonly profile: Option.Option; } export class BearerConnectionCredential extends Schema.TaggedClass()( "BearerConnectionCredential", { token: Schema.String, }, ) {} export const ConnectionCredential = Schema.Union([BearerConnectionCredential]); export type ConnectionCredential = typeof ConnectionCredential.Type; export class PrimaryConnectionRegistration extends Schema.TaggedClass()( "PrimaryConnectionRegistration", { target: PrimaryConnectionTarget, }, ) {} export class RelayConnectionRegistration extends Schema.TaggedClass()( "RelayConnectionRegistration", { target: RelayConnectionTarget, }, ) {} export class BearerConnectionRegistration extends Schema.TaggedClass()( "BearerConnectionRegistration", { target: BearerConnectionTarget, profile: BearerConnectionProfile, credential: BearerConnectionCredential, }, ) {} export class SshConnectionRegistration extends Schema.TaggedClass()( "SshConnectionRegistration", { target: SshConnectionTarget, profile: SshConnectionProfile, }, ) {} export const ConnectionRegistration = Schema.Union([ RelayConnectionRegistration, BearerConnectionRegistration, SshConnectionRegistration, ]); export type ConnectionRegistration = typeof ConnectionRegistration.Type; /** * Platform-managed registrations are reconciled from the host (the desktop * bootstrap IPC) rather than persisted by the user. They cover the primary * local environment plus any additional desktop-local backends running * alongside it (e.g. a parallel WSL backend). The primary stays on same-origin * cookie auth (`PrimaryConnectionRegistration`); secondary local backends live * on a separate loopback origin and authenticate with a bearer token minted * from their bootstrap credential (`BearerConnectionRegistration`). */ export const PlatformConnectionRegistration = Schema.Union([ PrimaryConnectionRegistration, BearerConnectionRegistration, ]); export type PlatformConnectionRegistration = typeof PlatformConnectionRegistration.Type; export function connectionRegistrationCatalogEntry( registration: ConnectionRegistration | PrimaryConnectionRegistration, ): ConnectionCatalogEntry { switch (registration._tag) { case "PrimaryConnectionRegistration": case "RelayConnectionRegistration": return { target: registration.target, profile: Option.none(), }; case "BearerConnectionRegistration": case "SshConnectionRegistration": return { target: registration.target, profile: Option.some(registration.profile), }; } }