Remove comments that only restate the code, keeping the section banners and the notes that carry a reason, a measured fact or an external contract. ButtonOptions.width claimed to be a fixed width but only set minWidth, so the Graph and Texture pickers grew with the name they showed. It is now what it promises, with a separate minWidth for the buttons that have two forms. Narrow buttons shrink their padding, otherwise a 24px icon button had 8px left for its glyph and truncate dropped it entirely. __gm.labelsDropped() guards that class: the overflow guard only saw text that was too wide, never a label truncated to nothing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
80 lines
2.9 KiB
TypeScript
80 lines
2.9 KiB
TypeScript
import type { Project } from '../model/Project';
|
|
import { statusMessage } from './http';
|
|
import {
|
|
type LoadedProject,
|
|
type ProjectStore,
|
|
type ProjectSummary,
|
|
StaleRevisionError,
|
|
} from './ProjectStore';
|
|
import { deserializeProject, serializeProject } from './serialize';
|
|
|
|
/** Routing goes through the `r` query parameter because the site's nginx config rejects PATH_INFO. */
|
|
export class HttpProjectStore implements ProjectStore {
|
|
constructor(private readonly endpoint: string) {}
|
|
|
|
async list(): Promise<ProjectSummary[]> {
|
|
return this.request<ProjectSummary[]>('GET', 'projects');
|
|
}
|
|
|
|
async load(id: string): Promise<LoadedProject> {
|
|
const body = await this.request<{ revision: number; project: unknown }>(
|
|
'GET',
|
|
`projects/${encodeURIComponent(id)}`,
|
|
);
|
|
return { project: deserializeProject(body.project, id), revision: body.revision };
|
|
}
|
|
|
|
async save(project: Project, revision: number): Promise<number> {
|
|
const body = await this.request<{ revision: number }>(
|
|
'PUT',
|
|
`projects/${encodeURIComponent(project.id)}`,
|
|
{
|
|
revision,
|
|
project: serializeProject(project),
|
|
},
|
|
);
|
|
return body.revision;
|
|
}
|
|
|
|
async create(name: string): Promise<LoadedProject> {
|
|
const body = await this.request<{ revision: number; project: unknown }>('POST', 'projects', { name });
|
|
const project = deserializeProject(body.project);
|
|
return { project, revision: body.revision };
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
await this.request<void>('DELETE', `projects/${encodeURIComponent(id)}`);
|
|
}
|
|
|
|
/** 409 becomes a `StaleRevisionError`, every other failure an `Error` with the server's message. */
|
|
private async request<T>(method: string, route: string, payload?: unknown): Promise<T> {
|
|
const url = `${this.endpoint}?r=${encodeURIComponent(route)}`;
|
|
const response = await fetch(url, {
|
|
method,
|
|
headers: payload !== undefined ? { 'Content-Type': 'application/json' } : {},
|
|
body: payload !== undefined ? JSON.stringify(payload) : undefined,
|
|
});
|
|
if (response.status === 204) return undefined as T;
|
|
const text = await response.text();
|
|
let data: unknown;
|
|
try {
|
|
data = text ? JSON.parse(text) : null;
|
|
} catch {
|
|
// nginx rejects an oversized body itself and answers with an HTML page, so a failure is
|
|
// explained by its status rather than as "invalid JSON".
|
|
if (response.ok) throw new Error(`API ${method} ${route}: invalid JSON (${response.status})`);
|
|
data = null;
|
|
}
|
|
if (response.status === 409) {
|
|
const rev = (data as { revision?: number } | null)?.revision ?? -1;
|
|
throw new StaleRevisionError(rev);
|
|
}
|
|
if (!response.ok) {
|
|
const message =
|
|
(data as { error?: string } | null)?.error ?? statusMessage(response.status, response.statusText);
|
|
throw new Error(`API ${method} ${route}: ${message} (${response.status})`);
|
|
}
|
|
return data as T;
|
|
}
|
|
}
|