diff options
| author | Adam Malczewski <[email protected]> | 2026-06-26 22:21:55 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-06-26 22:23:39 +0900 |
| commit | c333fcec32b1f90bf0da6bb14d2609c20e38a74f (patch) | |
| tree | 0db3ec77a6838c4f800c362df0de3c6cd8544431 /src/features/workspaces/adapter/http.ts | |
| parent | 1285564f12238b22f6b39b9f3fbcecaca8456911 (diff) | |
| download | dispatch-web-c333fcec32b1f90bf0da6bb14d2609c20e38a74f.tar.gz dispatch-web-c333fcec32b1f90bf0da6bb14d2609c20e38a74f.zip | |
style: switch from tabs to 2-space indentation (incl. svelte)
Diffstat (limited to 'src/features/workspaces/adapter/http.ts')
| -rw-r--r-- | src/features/workspaces/adapter/http.ts | 242 |
1 files changed, 121 insertions, 121 deletions
diff --git a/src/features/workspaces/adapter/http.ts b/src/features/workspaces/adapter/http.ts index 6ebfee1..01fe677 100644 --- a/src/features/workspaces/adapter/http.ts +++ b/src/features/workspaces/adapter/http.ts @@ -1,13 +1,13 @@ import type { - DeleteWorkspaceResponse, - EnsureWorkspaceRequest, - SetWorkspaceDefaultComputerRequest, - SetWorkspaceDefaultCwdRequest, - SetWorkspaceTitleRequest, - Workspace, - WorkspaceEntry, - WorkspaceListResponse, - WorkspaceResponse, + DeleteWorkspaceResponse, + EnsureWorkspaceRequest, + SetWorkspaceDefaultComputerRequest, + SetWorkspaceDefaultCwdRequest, + SetWorkspaceTitleRequest, + Workspace, + WorkspaceEntry, + WorkspaceListResponse, + WorkspaceResponse, } from "@dispatch/transport-contract"; /** @@ -28,130 +28,130 @@ import type { * - `DELETE /workspaces/:id` (409 for "default") → delete */ export type WorkspaceResult<T> = - | { readonly ok: true; readonly value: T } - | { readonly ok: false; readonly error: string }; + | { 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 }>>; + 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}`; - } + 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 []; - } - }, + 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 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 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 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 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 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" }; - } - }, - }; + 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" }; + } + }, + }; } |
