import { fetchJson, type ApiRequestOptions } from "../client"; import type { SSHTestRequest, SSHTestResult, SSHSession, SSHAgentReadinessResponse, SSHProbeShellsResponse, } from "@/lib/types/http-ssh"; export async function testSSHConnection( request: SSHTestRequest, options?: ApiRequestOptions, ): Promise { return fetchJson("/api/v1/ssh/test", { ...options, init: { // Caller overrides come first so an extension can add e.g. an extra // header or signal, then the required POST + JSON body + Content-Type // win — otherwise a caller passing `init.headers` would clobber the // Content-Type and break server-side parsing. Same ordering inside the // inner `headers` literal: spread first, JSON content-type last. ...(options?.init ?? {}), method: "POST", headers: { ...(options?.init?.headers ?? {}), "Content-Type": "application/json" }, body: JSON.stringify(request), }, }); } export async function listSSHSessions( executorId: string, options?: ApiRequestOptions, ): Promise { return fetchJson( `/api/v1/ssh/executors/${encodeURIComponent(executorId)}/sessions`, options, ); } export async function probeSSHAgents( executorId: string, body?: { shell?: string }, options?: ApiRequestOptions, ): Promise { return fetchJson( `/api/v1/ssh/executors/${encodeURIComponent(executorId)}/probe-agents`, { ...options, init: { ...(options?.init ?? {}), method: "POST", headers: { ...(options?.init?.headers ?? {}), "Content-Type": "application/json" }, body: JSON.stringify(body ?? {}), }, }, ); } export async function probeSSHShells( executorId: string, options?: ApiRequestOptions, ): Promise { return fetchJson( `/api/v1/ssh/executors/${encodeURIComponent(executorId)}/probe-shells`, { ...options, init: { ...(options?.init ?? {}), method: "POST", headers: { ...(options?.init?.headers ?? {}), "Content-Type": "application/json" }, }, }, ); }