1
0
forked from rosa/hakurei

cmd/pkgserver: enable TypeScript's strict mode

This commit is contained in:
Kat
2026-03-23 06:12:57 +11:00
parent c7e195fe64
commit 6fffc4ccfc
2 changed files with 20 additions and 13 deletions

View File

@@ -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 name: string
size: number | null size: number | null
description: string | null description: string | null
@@ -34,7 +40,7 @@ function toByteSizeString(bytes: number): string {
const API_VERSION = 1 const API_VERSION = 1
const ENDPOINT = `/api/v${API_VERSION}` const ENDPOINT = `/api/v${API_VERSION}`
class InfoPayload { interface InfoPayload {
count: number count: number
hakurei_version: string hakurei_version: string
} }
@@ -42,9 +48,9 @@ class InfoPayload {
async function infoRequest(): Promise<InfoPayload> { async function infoRequest(): Promise<InfoPayload> {
const res = await fetch(`${ENDPOINT}/info`) const res = await fetch(`${ENDPOINT}/info`)
const payload = await res.json() const payload = await res.json()
return payload as InfoPayload return payload
} }
class GetPayload { interface GetPayload {
values: PackageIndexEntry[] values: PackageIndexEntry[]
} }
@@ -57,7 +63,7 @@ enum SortOrders {
async function getRequest(limit: number, index: number, sort: SortOrders): Promise<GetPayload> { async function getRequest(limit: number, index: number, sort: SortOrders): Promise<GetPayload> {
const res = await fetch(`${ENDPOINT}/get?limit=${limit}&index=${index}&sort=${sort.valueOf()}`) const res = await fetch(`${ENDPOINT}/get?limit=${limit}&index=${index}&sort=${sort.valueOf()}`)
const payload = await res.json() const payload = await res.json()
return payload as GetPayload return payload
} }
class State { class State {
entriesPerPage: number = 10 entriesPerPage: number = 10
@@ -96,16 +102,16 @@ class State {
} }
updatePage() { updatePage() {
let page = Math.ceil(((this.getEntryIndex() + this.getEntriesPerPage()) - 1) / this.getEntriesPerPage()) 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() { updateRange() {
let max = Math.min(this.getEntryIndex() + this.getEntriesPerPage(), this.getMaxEntries()) 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() { updateListings() {
getRequest(this.getEntriesPerPage(), this.getEntryIndex(), this.getSortOrder()) getRequest(this.getEntriesPerPage(), this.getEntryIndex(), this.getSortOrder())
.then(res => { .then(res => {
let table = document.getElementById("pkg-list") let table = assertGetElementById("pkg-list")
table.innerHTML = '' table.innerHTML = ''
res.values.forEach((row) => { res.values.forEach((row) => {
table.appendChild(toHTML(row)) table.appendChild(toHTML(row))
@@ -141,15 +147,15 @@ document.addEventListener("DOMContentLoaded", () => {
infoRequest() infoRequest()
.then(res => { .then(res => {
STATE.setMaxEntries(res.count) STATE.setMaxEntries(res.count)
document.getElementById("hakurei-version").innerText = res.hakurei_version assertGetElementById("hakurei-version").innerText = res.hakurei_version
STATE.updateRange() STATE.updateRange()
STATE.updateListings() STATE.updateListings()
}) })
document.getElementById("count").addEventListener("change", (event) => { assertGetElementById("count").addEventListener("change", (event) => {
STATE.setEntriesPerPage(parseInt((event.target as HTMLSelectElement).value)) 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)) STATE.setSortOrder(parseInt((event.target as HTMLSelectElement).value))
}) })
}) })

View File

@@ -1,5 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
"strict": true,
"target": "ES2024" "target": "ES2024"
} }
} }