Added listing support

This commit is contained in:
Filip Znachor 2024-06-28 18:47:33 +02:00
parent e7e4d346be
commit 774de8365e
4 changed files with 96 additions and 1 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "oohost.js", "name": "oohost.js",
"version": "0.0.3", "version": "0.0.4",
"description": "TypeScript library for accessing the oohost API", "description": "TypeScript library for accessing the oohost API",
"license": "MIT", "license": "MIT",
"author": "oohost.cz", "author": "oohost.cz",

89
src/APIList.ts Normal file
View file

@ -0,0 +1,89 @@
import { API } from "./API";
export class APIList<T> {
page: number = 0;
minPage: number = 0;
maxPage: number = 0;
totalCount: number | null = null;
endpoint: string;
count: number = 20;
list: T[] = [];
/**
* Create List for the specified endpoint
* @param endpoint API List endpoint
*/
constructor(endpoint: string) {
this.endpoint = endpoint;
}
/**
* Update List from the API
* @returns Updated List
*/
async update(): Promise<T[]> {
let res = await API.get(this.endpoint, {count: this.count, page: this.page});
if (res.success) {
const { total_count, list } = res.get();
this.totalCount = total_count;
this.maxPage = total_count ? Math.ceil(total_count/this.count)-1 : 0;
if (this.maxPage < this.page) {
this.page = this.maxPage;
return await this.update();
}
this.maxPage = total_count ? Math.ceil(total_count/this.count)-1 : 0;
this.list = list;
}
return this.list;
}
/**
* Set new List page
* @param page New page
* @returns Current List
*/
async setPage(page: number) {
let page_before = this.page;
this.page = Math.max(page, 0);
if (this.maxPage != null && this.page > this.maxPage) this.page = this.maxPage;
if (page_before != this.page) await this.update();
return this.list;
}
/**
* Move relatively by specified number of pages
* @param direction Number of pages
* @returns Current List
*/
async go(direction: number) {
return await this.setPage(this.page+direction);
}
/**
* Move to the next page
* @returns Current List
*/
async next() {
return await this.go(1);
}
/**
* Move to the previous page
* @returns Current List
*/
async prev() {
return await this.go(-1);
}
/**
* Check if List is empty
* @returns True, if List is empty
*/
empty() {
return this.totalCount == 0;
}
}

View file

@ -1,4 +1,5 @@
export * from "./API"; export * from "./API";
export * from "./APIList";
export * from "./models/Base"; export * from "./models/Base";
export * from "./models/User"; export * from "./models/User";
export * from "./models/Certificate"; export * from "./models/Certificate";

View file

@ -1,4 +1,5 @@
import { API, Response } from "../API"; import { API, Response } from "../API";
import { APIList } from "../APIList";
import { PromisedData } from "./Base"; import { PromisedData } from "./Base";
import { UserInclude } from "./User"; import { UserInclude } from "./User";
@ -27,6 +28,10 @@ export class Certificate {
return await API.get("certs/edit", {id: this.id, ...data}); return await API.get("certs/edit", {id: this.id, ...data});
} }
public static getList(): APIList<CertificateListItem> {
return new APIList("certs/list");
}
} }
export interface CertificateEdit { export interface CertificateEdit {