From 774de8365ebfc07c98b54e3f39183cbd9824c484 Mon Sep 17 00:00:00 2001 From: Filip Znachor Date: Fri, 28 Jun 2024 18:47:33 +0200 Subject: [PATCH] Added listing support --- package.json | 2 +- src/APIList.ts | 89 +++++++++++++++++++++++++++++++++++++++ src/index.ts | 1 + src/models/Certificate.ts | 5 +++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/APIList.ts diff --git a/package.json b/package.json index 70963cd..455d99f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "oohost.js", - "version": "0.0.3", + "version": "0.0.4", "description": "TypeScript library for accessing the oohost API", "license": "MIT", "author": "oohost.cz", diff --git a/src/APIList.ts b/src/APIList.ts new file mode 100644 index 0000000..9505769 --- /dev/null +++ b/src/APIList.ts @@ -0,0 +1,89 @@ +import { API } from "./API"; + +export class APIList { + + 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 { + 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; + } + +} diff --git a/src/index.ts b/src/index.ts index 1b58939..fb8e408 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ export * from "./API"; +export * from "./APIList"; export * from "./models/Base"; export * from "./models/User"; export * from "./models/Certificate"; diff --git a/src/models/Certificate.ts b/src/models/Certificate.ts index 1ff667d..25c7acd 100644 --- a/src/models/Certificate.ts +++ b/src/models/Certificate.ts @@ -1,4 +1,5 @@ import { API, Response } from "../API"; +import { APIList } from "../APIList"; import { PromisedData } from "./Base"; import { UserInclude } from "./User"; @@ -27,6 +28,10 @@ export class Certificate { return await API.get("certs/edit", {id: this.id, ...data}); } + public static getList(): APIList { + return new APIList("certs/list"); + } + } export interface CertificateEdit {