Nicer display of node list
This commit is contained in:
parent
9b5f8f3f37
commit
cbd5624fb2
5 changed files with 144 additions and 12 deletions
frontend/src/utils
|
@ -1,4 +1,17 @@
|
|||
import type {TypeGuard} from "@/types/shared";
|
||||
import {toIsArray} from "@/types/shared";
|
||||
import type {Headers} from "request";
|
||||
import {parseInteger} from "@/utils/Numbers";
|
||||
|
||||
interface PagedListResult<T> {
|
||||
entries: T[];
|
||||
total: number;
|
||||
}
|
||||
|
||||
interface Response<T> {
|
||||
result: T;
|
||||
headers: Headers;
|
||||
}
|
||||
|
||||
class Api {
|
||||
private baseURL: string = import.meta.env.BASE_URL;
|
||||
|
@ -14,7 +27,7 @@ class Api {
|
|||
return this.baseURL + this.apiPrefix + path;
|
||||
}
|
||||
|
||||
async get<T>(path: string, isT: TypeGuard<T>): Promise<T> {
|
||||
private async doGet<T>(path: string, isT: TypeGuard<T>): Promise<Response<T>> {
|
||||
const url = this.toURL(path);
|
||||
const result = await fetch(url);
|
||||
const json = await result.json();
|
||||
|
@ -24,7 +37,26 @@ class Api {
|
|||
throw new Error(`API get result has wrong type. ${url} => ${json}`);
|
||||
}
|
||||
|
||||
return json;
|
||||
return {
|
||||
result: json,
|
||||
headers: result.headers,
|
||||
};
|
||||
}
|
||||
|
||||
async get<T>(path: string, isT: TypeGuard<T>): Promise<T> {
|
||||
const response = await this.doGet(path, isT);
|
||||
return response.result;
|
||||
}
|
||||
|
||||
async getPagedList<T>(path: string, isT: TypeGuard<T>): Promise<PagedListResult<T>> {
|
||||
const response = await this.doGet(path, toIsArray(isT));
|
||||
const totalStr = response.headers.get("x-total-count");
|
||||
const total = parseInteger(totalStr, 10);
|
||||
|
||||
return {
|
||||
entries: response.result,
|
||||
total,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
25
frontend/src/utils/Numbers.ts
Normal file
25
frontend/src/utils/Numbers.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
// TODO: Write tests!
|
||||
export function parseInteger(arg: any, radix: number): number {
|
||||
if (Number.isInteger(arg)) {
|
||||
return arg;
|
||||
}
|
||||
switch (typeof arg) {
|
||||
case "number":
|
||||
throw new Error(`Not an integer: ${arg}`);
|
||||
case "string":
|
||||
if (radix < 2 || radix > 36 || isNaN(radix)) {
|
||||
throw new Error(`Radix out of range: ${radix}`);
|
||||
}
|
||||
const str = (arg as string).trim();
|
||||
const num = parseInt(str, radix);
|
||||
if (isNaN(num)) {
|
||||
throw new Error(`Not a valid number (radix: ${radix}): ${str}`);
|
||||
}
|
||||
if (num.toString(radix).toLowerCase() !== str.toLowerCase()) {
|
||||
throw new Error(`Parsed integer does not match given string (radix: {radix}): ${str}`);
|
||||
}
|
||||
return num;
|
||||
default:
|
||||
throw new Error(`Cannot parse number (radix: ${radix}): ${arg}`);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue