| 1 | import { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { type EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection"; |
| 3 | |
| 4 | export interface SavedRemoteConnection { |
| 5 | readonly environmentId: EnvironmentId; |
| 6 | readonly environmentLabel: string; |
| 7 | readonly pairingUrl: string; |
| 8 | readonly displayUrl: string; |
| 9 | readonly httpBaseUrl: string; |
| 10 | readonly wsBaseUrl: string; |
| 11 | readonly bearerToken: string | null; |
| 12 | readonly authenticationMethod?: "bearer" | "dpop"; |
| 13 | readonly dpopAccessToken?: string; |
| 14 | readonly relayManaged?: true; |
| 15 | } |
| 16 | |
| 17 | export type RemoteClientConnectionState = EnvironmentConnectionPhase; |
| 18 | |
| 19 | export function isRelayManagedConnection( |
| 20 | connection: Pick<SavedRemoteConnection, "authenticationMethod" | "relayManaged">, |
| 21 | ): boolean { |
| 22 | return connection.relayManaged === true || connection.authenticationMethod === "dpop"; |
| 23 | } |
| 24 | |
| 25 | export function toStableSavedRemoteConnection( |
| 26 | connection: SavedRemoteConnection, |
| 27 | ): SavedRemoteConnection { |
| 28 | if (!isRelayManagedConnection(connection) || !connection.dpopAccessToken) { |
| 29 | return connection; |
| 30 | } |
| 31 | |
| 32 | const { dpopAccessToken: _, ...stableConnection } = connection; |
| 33 | return stableConnection; |
| 34 | } |