// The server, as the page talks to it. // // Every exchange that answers with the state payload carries the view the rows // are shown under, and says whether the view is still that one when the // answer arrives: the controls may have moved on while it was out, and then // its rows are for a view no longer on screen. // // `fetch` and `view` are handed in, so the tests can stand in for the server // and the controls. // The filter and search as a query string (M1-UI-005, M1-UI-006). The server // shows a row whose status is in the set, so an empty set asks for none; "all" // is the page's shorthand and never appears here. export function viewQuery(statuses, search) { return `?status=${statuses.join(",")}&q=${encodeURIComponent(search)}`; } const json = { "content-type": "application/json" }; export class Api { #fetch; #view; // `view` returns the query string for the controls as they are now. constructor(fetch, view) { this.#fetch = fetch; this.#view = view; } // The state under the current view, or null when the view changed while // the request was out: whatever changed it has asked again. async state() { const asked = this.#view(); const response = await this.#fetch("/api/state" + asked); const payload = await response.json(); return this.#view() === asked ? payload : null; } // An action answered with the state payload. The status says whether it was // accepted; the payload says what became of it either way (M1-UI-010). async send(method, path, body) { const asked = this.#view(); const response = await this.#fetch(path + asked, { method, headers: json, body }); const payload = await response.json(); return { accepted: response.ok, current: this.#view() === asked, payload }; } // Which aliases fit at the cursor in a condition (D9). Asking changes // nothing, so the answer is not the state payload and has no view. async complete(before) { const response = await this.#fetch("/api/complete", { method: "POST", headers: json, body: JSON.stringify({ before }), }); return response.json(); } async preview() { const response = await this.#fetch("/api/preview"); return response.json(); } }