summaryrefslogtreecommitdiffhomepage
path: root/src/features/workspaces/adapter/http.ts
blob: 6ebfee1b8f5ca1c215ec5c466222859d7588e04e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import type {
	DeleteWorkspaceResponse,
	EnsureWorkspaceRequest,
	SetWorkspaceDefaultComputerRequest,
	SetWorkspaceDefaultCwdRequest,
	SetWorkspaceTitleRequest,
	Workspace,
	WorkspaceEntry,
	WorkspaceListResponse,
	WorkspaceResponse,
} from "@dispatch/transport-contract";

/**
 * Workspace HTTP effects — the injected edge that talks to the backend's
 * workspace endpoints. Mirrors the store's fetch pattern: `httpBase` + an
 * injected `fetchImpl` (so it is testable without the network). Returns typed
 * `WorkspaceResult<T>` (`{ok,value}` | `{ok:false,error}`) for mutating ops so a
 * caller can surface the backend's `{ error }` reason; reads return data or a
 * safe empty/null on failure (non-fatal — the UI falls back gracefully).
 *
 * Endpoints ([email protected]):
 * - `GET /workspaces` → list
 * - `PUT /workspaces/:id` (create-on-miss, idempotent) → ensure
 * - `GET /workspaces/:id` (404 → null) → get
 * - `PUT /workspaces/:id/title` → rename
 * - `PUT /workspaces/:id/default-cwd` → set/clear default cwd
 * - `PUT /workspaces/:id/default-computer` → set/clear default computer (SSH handoff #2)
 * - `DELETE /workspaces/:id` (409 for "default") → delete
 */
export type WorkspaceResult<T> =
	| { readonly ok: true; readonly value: T }
	| { readonly ok: false; readonly error: string };

export interface WorkspaceHttp {
	list(): Promise<readonly WorkspaceEntry[]>;
	ensure(id: string, body?: EnsureWorkspaceRequest): Promise<WorkspaceResult<Workspace>>;
	get(id: string): Promise<Workspace | null>;
	setTitle(id: string, title: string): Promise<WorkspaceResult<Workspace>>;
	setDefaultCwd(id: string, defaultCwd: string | null): Promise<WorkspaceResult<Workspace>>;
	setDefaultComputer(id: string, computerId: string | null): Promise<WorkspaceResult<Workspace>>;
	delete(id: string): Promise<WorkspaceResult<{ closedCount: number }>>;
}

async function errText(res: Response): Promise<string> {
	try {
		const body = (await res.json()) as { error?: string };
		return body.error ?? `HTTP ${res.status}`;
	} catch {
		return `HTTP ${res.status}`;
	}
}

export function createWorkspaceHttp(httpBase: string, fetchImpl: typeof fetch): WorkspaceHttp {
	return {
		async list(): Promise<readonly WorkspaceEntry[]> {
			try {
				const res = await fetchImpl(`${httpBase}/workspaces`);
				if (!res.ok) return [];
				const data = (await res.json()) as WorkspaceListResponse;
				return data.workspaces;
			} catch {
				return [];
			}
		},

		async ensure(id, body): Promise<WorkspaceResult<Workspace>> {
			try {
				const res = await fetchImpl(`${httpBase}/workspaces/${encodeURIComponent(id)}`, {
					method: "PUT",
					headers: { "Content-Type": "application/json" },
					body: JSON.stringify(body ?? {}),
				});
				if (!res.ok) return { ok: false, error: await errText(res) };
				return { ok: true, value: (await res.json()) as WorkspaceResponse };
			} catch (err) {
				return {
					ok: false,
					error: err instanceof Error ? err.message : "Workspace request failed",
				};
			}
		},

		async get(id): Promise<Workspace | null> {
			try {
				const res = await fetchImpl(`${httpBase}/workspaces/${encodeURIComponent(id)}`);
				if (res.status === 404 || !res.ok) return null;
				return (await res.json()) as WorkspaceResponse;
			} catch {
				return null;
			}
		},

		async setTitle(id, title): Promise<WorkspaceResult<Workspace>> {
			try {
				const res = await fetchImpl(`${httpBase}/workspaces/${encodeURIComponent(id)}/title`, {
					method: "PUT",
					headers: { "Content-Type": "application/json" },
					body: JSON.stringify({ title } satisfies SetWorkspaceTitleRequest),
				});
				if (!res.ok) return { ok: false, error: await errText(res) };
				return { ok: true, value: (await res.json()) as WorkspaceResponse };
			} catch (err) {
				return { ok: false, error: err instanceof Error ? err.message : "Rename failed" };
			}
		},

		async setDefaultCwd(id, defaultCwd): Promise<WorkspaceResult<Workspace>> {
			try {
				const res = await fetchImpl(
					`${httpBase}/workspaces/${encodeURIComponent(id)}/default-cwd`,
					{
						method: "PUT",
						headers: { "Content-Type": "application/json" },
						body: JSON.stringify({ defaultCwd } satisfies SetWorkspaceDefaultCwdRequest),
					},
				);
				if (!res.ok) return { ok: false, error: await errText(res) };
				return { ok: true, value: (await res.json()) as WorkspaceResponse };
			} catch (err) {
				return { ok: false, error: err instanceof Error ? err.message : "Set default cwd failed" };
			}
		},

		async setDefaultComputer(id, computerId): Promise<WorkspaceResult<Workspace>> {
			try {
				const res = await fetchImpl(
					`${httpBase}/workspaces/${encodeURIComponent(id)}/default-computer`,
					{
						method: "PUT",
						headers: { "Content-Type": "application/json" },
						body: JSON.stringify({ computerId } satisfies SetWorkspaceDefaultComputerRequest),
					},
				);
				if (!res.ok) return { ok: false, error: await errText(res) };
				return { ok: true, value: (await res.json()) as WorkspaceResponse };
			} catch (err) {
				return {
					ok: false,
					error: err instanceof Error ? err.message : "Set default computer failed",
				};
			}
		},

		async delete(id): Promise<WorkspaceResult<{ closedCount: number }>> {
			try {
				const res = await fetchImpl(`${httpBase}/workspaces/${encodeURIComponent(id)}`, {
					method: "DELETE",
				});
				if (!res.ok) return { ok: false, error: await errText(res) };
				const data = (await res.json()) as DeleteWorkspaceResponse;
				return { ok: true, value: { closedCount: data.closedCount } };
			} catch (err) {
				return { ok: false, error: err instanceof Error ? err.message : "Delete failed" };
			}
		},
	};
}