From 8c8a014d8772bdbc4cc81b28ee3d00930e84245b Mon Sep 17 00:00:00 2001 From: radekBednarik Date: Fri, 1 Nov 2024 12:51:34 +0100 Subject: [PATCH] dev: wip --- src/client/client.ts | 5 +++++ src/helpers/helpers.ts | 7 +++++-- src/helpers/sequential.ts | 25 +++++++++++++++++++++++++ src/helpers/sequential_test.ts | 28 ++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 src/helpers/sequential.ts create mode 100644 src/helpers/sequential_test.ts diff --git a/src/client/client.ts b/src/client/client.ts index abfb2c8..f6fcd94 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -7,6 +7,7 @@ import VaccinationPlaces from "../vaccination/vaccination-places.ts"; import VaccinationFacilities from "../vaccination/vaccination-facilities.ts"; import VaccinationDeaths from "../vaccination/vaccination-deaths.ts"; import VaccinationDemographic from "../vaccination/vaccination-demographic.ts"; +import Helpers from "../helpers/helpers.ts"; /** * Represents single entrypoint for calling supported @@ -35,6 +36,8 @@ export default class Client { public readonly vaccinationDeaths: VaccinationDeaths; /** @property vaccinationDemographic - represents instance of the VaccinationDemographic class */ public readonly vaccinationDemographic: VaccinationDemographic; + /** @property helpers - represents instance of the Helpers class */ + public readonly helpers: Helpers; /** @property instance - holds the singleton instance of the class */ private static instance: Client; @@ -61,6 +64,8 @@ export default class Client { this.vaccinationFacilities = new VaccinationFacilities(this.token); this.vaccinationDeaths = new VaccinationDeaths(this.token); this.vaccinationDemographic = new VaccinationDemographic(this.token); + + this.helpers = new Helpers(this.token); } /** diff --git a/src/helpers/helpers.ts b/src/helpers/helpers.ts index 0f5d5f8..bc96c53 100644 --- a/src/helpers/helpers.ts +++ b/src/helpers/helpers.ts @@ -1,9 +1,12 @@ -import type { GetApiError } from "../common/api.ts"; - +import Sequential from "./sequential.ts"; export default class Helpers { private readonly token: string; + public readonly sequentialSameApi: Sequential; + constructor(token: string) { this.token = token; + + this.sequentialSameApi = new Sequential(); } } diff --git a/src/helpers/sequential.ts b/src/helpers/sequential.ts new file mode 100644 index 0000000..f3a9068 --- /dev/null +++ b/src/helpers/sequential.ts @@ -0,0 +1,25 @@ +import type { GetApiError } from "../common/api.ts"; + +export default class Sequential { + constructor() {} + + public async call( + handler: ( + { page, itemsPerPage }: { page: number; itemsPerPage: number }, + ) => Promise<[T, null] | [null, GetApiError]>, + options: { pages: { start: number; end: number }; itemsPerPage: number }, + ) { + const data: Array<[T, null] | [null, GetApiError]> = []; + + for (let page = options.pages.start; page <= options.pages.end; page++) { + const result = await handler({ + page, + itemsPerPage: options.itemsPerPage, + }); + + data.push(result); + } + + return data; + } +} diff --git a/src/helpers/sequential_test.ts b/src/helpers/sequential_test.ts new file mode 100644 index 0000000..fe7e0a8 --- /dev/null +++ b/src/helpers/sequential_test.ts @@ -0,0 +1,28 @@ +import { beforeAll, describe, it } from "@std/testing/bdd"; +import { expect } from "@std/expect"; +import "@std/dotenv/load"; +import Client from "../index.ts"; + +let client: Client; + +describe("helpers", () => { + beforeAll(() => { + client = Client.getInstance({ token: Deno.env.get("CLIENT_TOKEN") }); + }); + + describe("call sequentially same api endpoint - hospitalizations", () => { + it("happy - should return data of all called pages", async () => { + console.log(client) + const result = await client.helpers.sequentialSameApi.call( + client.hospitalization.getHospitalizationsV3, + { pages: { start: 1, end: 10 }, itemsPerPage: 1 }, + ); + + expect(result).toBeInstanceOf(Array); + expect(result).toHaveLength(10); + expect(result[0]).toBeInstanceOf(Array); + expect(result[0][1]).toBeNull(); + expect(result[0][0]).toHaveLength(1); + }); + }); +});