Skip to content

Commit

Permalink
dev: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
radekBednarik committed Nov 1, 2024
1 parent 617ee69 commit 8c8a014
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand All @@ -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);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
25 changes: 25 additions & 0 deletions src/helpers/sequential.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { GetApiError } from "../common/api.ts";

export default class Sequential {
constructor() {}

public async call<T>(
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;
}
}
28 changes: 28 additions & 0 deletions src/helpers/sequential_test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 8c8a014

Please sign in to comment.