1export interface Me {2 account_id: string;3 handle: string | null;4}56export function element<T extends HTMLElement = HTMLElement>(id: string): T {7 const found = document.getElementById(id);8 if (!(found instanceof HTMLElement)) throw new Error(`#${id} is missing`);9 return found as T;10}1112export function query<T extends Element = HTMLElement>(13 selector: string,14 root: ParentNode = document,15): T {16 const found = root.querySelector(selector);17 if (!found) throw new Error(`${selector} is missing`);18 return found as T;19}2021export const escapeHtml = (value: unknown) =>22 String(value).replace(/[&<>"']/g, (character) =>23 ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[character]24 ?? character));2526export function setPageTitle(suffix = '') {27 document.title = suffix ? `dev.site - ${suffix}` : 'dev.site';28}2930export async function api<T>(path: string, options: RequestInit = {}): Promise<T> {31 const response = await fetch(path, {32 credentials: 'same-origin',33 headers: { 'Content-Type': 'application/json' },34 ...options,35 });36 if (!response.ok) {37 const body = await response.json().catch(() => ({})) as { error?: string };38 throw new Error(body.error ?? `${response.status}`);39 }40 return (response.status === 204 ? null : await response.json()) as T;41}4243export function errorMessage(error: unknown): string {44 return error instanceof Error ? error.message : String(error);45}