diff --git a/cmd/pkgserver/ui/static/index.ts b/cmd/pkgserver/ui/static/index.ts index ab1a8315..1fa3cd29 100644 --- a/cmd/pkgserver/ui/static/index.ts +++ b/cmd/pkgserver/ui/static/index.ts @@ -1,4 +1,10 @@ -class PackageIndexEntry { +function assertGetElementById(id: string): HTMLElement { + let elem = document.getElementById(id) + if(elem == null) throw new ReferenceError(`element with ID '${id}' missing from DOM`) + return elem +} + +interface PackageIndexEntry { name: string size: number | null description: string | null @@ -34,7 +40,7 @@ function toByteSizeString(bytes: number): string { const API_VERSION = 1 const ENDPOINT = `/api/v${API_VERSION}` -class InfoPayload { +interface InfoPayload { count: number hakurei_version: string } @@ -42,9 +48,9 @@ class InfoPayload { async function infoRequest(): Promise { const res = await fetch(`${ENDPOINT}/info`) const payload = await res.json() - return payload as InfoPayload + return payload } -class GetPayload { +interface GetPayload { values: PackageIndexEntry[] } @@ -57,7 +63,7 @@ enum SortOrders { async function getRequest(limit: number, index: number, sort: SortOrders): Promise { const res = await fetch(`${ENDPOINT}/get?limit=${limit}&index=${index}&sort=${sort.valueOf()}`) const payload = await res.json() - return payload as GetPayload + return payload } class State { entriesPerPage: number = 10 @@ -96,16 +102,16 @@ class State { } updatePage() { let page = Math.ceil(((this.getEntryIndex() + this.getEntriesPerPage()) - 1) / this.getEntriesPerPage()) - document.getElementById("page-number").innerText = String(page) + assertGetElementById("page-number").innerText = String(page) } updateRange() { let max = Math.min(this.getEntryIndex() + this.getEntriesPerPage(), this.getMaxEntries()) - document.getElementById("entry-counter").innerText = `${this.getEntryIndex() + 1}-${max} of ${this.getMaxEntries()}` + assertGetElementById("entry-counter").innerText = `${this.getEntryIndex() + 1}-${max} of ${this.getMaxEntries()}` } updateListings() { getRequest(this.getEntriesPerPage(), this.getEntryIndex(), this.getSortOrder()) .then(res => { - let table = document.getElementById("pkg-list") + let table = assertGetElementById("pkg-list") table.innerHTML = '' res.values.forEach((row) => { table.appendChild(toHTML(row)) @@ -141,15 +147,15 @@ document.addEventListener("DOMContentLoaded", () => { infoRequest() .then(res => { STATE.setMaxEntries(res.count) - document.getElementById("hakurei-version").innerText = res.hakurei_version + assertGetElementById("hakurei-version").innerText = res.hakurei_version STATE.updateRange() STATE.updateListings() }) - document.getElementById("count").addEventListener("change", (event) => { + assertGetElementById("count").addEventListener("change", (event) => { STATE.setEntriesPerPage(parseInt((event.target as HTMLSelectElement).value)) }) - document.getElementById("sort").addEventListener("change", (event) => { + assertGetElementById("sort").addEventListener("change", (event) => { STATE.setSortOrder(parseInt((event.target as HTMLSelectElement).value)) }) -}) \ No newline at end of file +}) diff --git a/cmd/pkgserver/ui/static/tsconfig.json b/cmd/pkgserver/ui/static/tsconfig.json index 8589a396..02161c32 100644 --- a/cmd/pkgserver/ui/static/tsconfig.json +++ b/cmd/pkgserver/ui/static/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "strict": true, "target": "ES2024" } -} \ No newline at end of file +}