Skip to content

Commit

Permalink
Merge pull request #11 from radekBednarik/feat/helpers
Browse files Browse the repository at this point in the history
feat: add helpers - provide method for sequential api call
  • Loading branch information
radekBednarik authored Nov 1, 2024
2 parents f811c23 + 3de895c commit 042d4e8
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 28 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bedna/czech-covid-data-api-lib",
"version": "1.3.0",
"version": "1.4.0",
"exports": "./src/index.ts",
"license": "MIT",
"tasks": {
Expand Down
6 changes: 3 additions & 3 deletions src/basic-overview/basic-overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export default class BasicOverview {
* @param param0.itemsPerPage
* @param param0.properties
*/
public async getBasicOverviewV3({
public getBasicOverviewV3 = async ({
page = 1,
itemsPerPage = 100,
properties,
}: {
page?: number;
itemsPerPage?: number;
properties?: string[];
} = {}): Promise<[BasicOverviewItemArr, null] | [null, GetApiError]> {
} = {}): Promise<[BasicOverviewItemArr, null] | [null, GetApiError]> => {
return await new ApiCallBuilder<BasicOverviewItemArr>({
token: this.token,
})
Expand All @@ -40,7 +40,7 @@ export default class BasicOverview {
{ itemsPerPage },
{ properties },
]).build();
}
};

/**
* Handles call to `/api/v3/zakladni-prehled/:date`.
Expand Down
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
6 changes: 3 additions & 3 deletions src/deaths/deaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class Deaths {
this.token = token;
}

public async getDeathsV3({
public getDeathsV3 = async ({
page = 1,
itemsPerPage = 100,
properties,
Expand Down Expand Up @@ -38,7 +38,7 @@ export default class Deaths {
vekLte?: string;
pohlavi?: string | string[];
krajNutsKod?: string;
} = {}): Promise<[DeathsItemArr, null] | [null, GetApiError]> {
} = {}): Promise<[DeathsItemArr, null] | [null, GetApiError]> => {
return await new ApiCallBuilder<DeathsItemArr>({
token: this.token,
})
Expand All @@ -58,7 +58,7 @@ export default class Deaths {
{ pohlavi },
{ krajNutsKod },
]).build();
}
};

public async getDeathsOfIdV3(
{ id }: { id: string },
Expand Down
12 changes: 12 additions & 0 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Sequential from "./sequential.ts";
export default class Helpers {
private readonly token: string;

public readonly sequential: Sequential;

constructor(token: string) {
this.token = token;

this.sequential = new Sequential();
}
}
42 changes: 42 additions & 0 deletions src/helpers/sequential.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { GetApiError } from "../common/api.ts";

export default class Sequential {
constructor() {}

public async callEndpoint<T>(
handler: (
{ page, itemsPerPage }: { page?: number; itemsPerPage?: number },
) => Promise<[T, null] | [null, GetApiError]>,
options: {
pages: { start?: number; end?: number };
itemsPerPage?: number;
waitAfterCall?: number;
},
): Promise<Array<[T, null] | [null, GetApiError]>> {
const data: Array<[T, null] | [null, GetApiError]> = [];

const startPage = options.pages.start || 1;
const endPage = options.pages.end || Infinity;

for (let page = startPage; page <= endPage; page++) {
const result = await handler({
page,
itemsPerPage: options.itemsPerPage,
});

if (Array.isArray(result[0]) && result[0].length === 0) {
break;
}

data.push(result);

if (typeof options.waitAfterCall === "number") {
await new Promise((resolve) =>
setTimeout(resolve, options.waitAfterCall)
);
}
}

return data;
}
}
66 changes: 66 additions & 0 deletions src/helpers/sequential_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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", () => {
it("happy - hospitalizations - should return data of all called pages", async () => {
const result = await client.helpers.sequential.callEndpoint(
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);
});

it("happy - deaths - should return data of all called pages", async () => {
const result = await client.helpers.sequential.callEndpoint(
client.deaths.getDeathsV3,
{ pages: { start: 5, end: 20 }, itemsPerPage: 1 },
);

expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(16);
expect(result[0]).toBeInstanceOf(Array);
expect(result[0][1]).toBeNull();
expect(result[0][0]).toHaveLength(1);
});

it("happy - deaths - do not provide pages.start and itemsPerPage options returns data", async () => {
const result = await client.helpers.sequential.callEndpoint(
client.deaths.getDeathsV3,
{ pages: { end: 2 } },
);

expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(2);
expect(result[0]).toBeInstanceOf(Array);
expect(result[0][1]).toBeNull();
expect(result[0][0]).toHaveLength(100);
});

it("happy - hospitalizations - should return data of all called pages with 1 second wait after each call", async () => {
const result = await client.helpers.sequential.callEndpoint(
client.hospitalization.getHospitalizationsV3,
{ pages: { end: 2 }, itemsPerPage: 1, waitAfterCall: 1000 },
);

expect(result).toBeInstanceOf(Array);
expect(result).toHaveLength(2);
expect(result[0]).toBeInstanceOf(Array);
expect(result[0][1]).toBeNull();
expect(result[0][0]).toHaveLength(1);
});
});
});
6 changes: 3 additions & 3 deletions src/hospitalizations/hospitalizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class Hospitalizations {
this.token = token;
}

public async getHospitalizationsV3(
public getHospitalizationsV3 = async (
{
page = 1,
itemsPerPage = 100,
Expand All @@ -25,7 +25,7 @@ export default class Hospitalizations {
datumAfter?: string;
datumStrictlyAfter?: string;
} = {},
): Promise<[HospitalizationsDataItemArr, null] | [null, GetApiError]> {
): Promise<[HospitalizationsDataItemArr, null] | [null, GetApiError]> => {
return await new ApiCallBuilder<HospitalizationsDataItemArr>({
token: this.token,
})
Expand All @@ -42,7 +42,7 @@ export default class Hospitalizations {
{ datumStrictlyAfter },
],
).build();
}
};

public async getHospitalizationOfId(
{ id }: { id: string },
Expand Down
6 changes: 3 additions & 3 deletions src/incidence/incidence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class Incidence {
this.token = token;
}

public async getIncidence714CzV3({
public getIncidence714CzV3 = async ({
page = 1,
itemsPerPage = 100,
properties,
Expand All @@ -23,7 +23,7 @@ export default class Incidence {
datumAfter?: string;
datumStrictlyBefore?: string;
datumStrictlyAfter?: string;
} = {}): Promise<[IncidenceDataItemArr, null] | [null, GetApiError]> {
} = {}): Promise<[IncidenceDataItemArr, null] | [null, GetApiError]> => {
const [data, err] = await new ApiCallBuilder({ token: this.token })
.provideEndpoint("/api/v3/incidence-7-14-cr").provideQueryParams([
{ page },
Expand All @@ -40,7 +40,7 @@ export default class Incidence {
}

return [data as IncidenceDataItemArr, null];
}
};

public async getIncidence714CzOfIdV3({
id,
Expand Down
6 changes: 3 additions & 3 deletions src/vaccination/vaccination-aggregated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class VaccinationAggregated {
this.token = token;
}

public async getVaccinationV3({
public getVaccinationV3 = async ({
page = 1,
itemsPerPage = 100,
properties,
Expand All @@ -34,7 +34,7 @@ export default class VaccinationAggregated {
vekovaSkupina?: string | string[];
} = {}): Promise<
[VaccinationAggregatedItemArr, null] | [null, GetApiError]
> {
> => {
return await new ApiCallBuilder<VaccinationAggregatedItemArr>({
token: this.token,
})
Expand All @@ -51,7 +51,7 @@ export default class VaccinationAggregated {
{ krajNazev },
{ vekovaSkupina },
]).build();
}
};

public async getVaccinationOfIdV3(
{ id }: { id: string },
Expand Down
6 changes: 3 additions & 3 deletions src/vaccination/vaccination-deaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class VaccinationDeaths {
this.token = token;
}

public async getVaccinationDeathsV3({
public getVaccinationDeathsV3 = async ({
page = 1,
itemsPerPage = 100,
properties,
Expand All @@ -24,7 +24,7 @@ export default class VaccinationDeaths {
datumStrictlyBefore?: string;
datumAfter?: string;
datumStrictlyAfter?: string;
} = {}): Promise<[VaccinationDeathsItemArr, null] | [null, GetApiError]> {
} = {}): Promise<[VaccinationDeathsItemArr, null] | [null, GetApiError]> => {
return await new ApiCallBuilder<VaccinationDeathsItemArr>({
token: this.token,
}).provideEndpoint("/api/v3/ockovani-umrti").provideQueryParams([
Expand All @@ -36,7 +36,7 @@ export default class VaccinationDeaths {
{ datumAfter },
{ datumStrictlyAfter },
]).build();
}
};

public async getVaccinationDeathsOfIdV3(
{ id }: { id: string },
Expand Down
6 changes: 3 additions & 3 deletions src/vaccination/vaccination-demographic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class VaccinationDemographic {
this.token = token;
}

public async getVaccinationDemographicDataV3({
public getVaccinationDemographicDataV3 = async ({
page = 1,
itemsPerPage = 100,
properties,
Expand All @@ -32,7 +32,7 @@ export default class VaccinationDemographic {
pohlavi?: "M" | "Z" | ("M" | "Z")[];
} = {}): Promise<
[VaccinationDemographicDataItemArr, null] | [null, GetApiError]
> {
> => {
return await new ApiCallBuilder<VaccinationDemographicDataItemArr>({
token: this.token,
}).provideEndpoint(
Expand All @@ -49,7 +49,7 @@ export default class VaccinationDemographic {
{ poradiDavky },
{ pohlavi },
]).build();
}
};

public async getVaccinationDemographicDataOfIdV3(
{ id }: { id: string },
Expand Down
6 changes: 3 additions & 3 deletions src/vaccination/vaccination-facilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class VaccinationFacilities {
this.token = token;
}

public async getVaccinationFacilitiesV3({
public getVaccinationFacilitiesV3 = async ({
page = 1,
itemsPerPage = 100,
properties,
Expand Down Expand Up @@ -42,7 +42,7 @@ export default class VaccinationFacilities {
praktickyLekarDospeli?: boolean;
} = {}): Promise<
[VaccinationFacilitiesItemArr, null] | [null, GetApiError]
> {
> => {
return await new ApiCallBuilder<VaccinationFacilitiesItemArr>({
token: this.token,
}).provideEndpoint(
Expand All @@ -64,7 +64,7 @@ export default class VaccinationFacilities {
{ praktickyLekarDeti },
{ praktickyLekarDospeli },
]).build();
}
};

public async getVaccinationFacilitiesOfIdV3(
{ id }: { id: string },
Expand Down
6 changes: 3 additions & 3 deletions src/vaccination/vaccination-places.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class VaccinationPlaces {
this.token = token;
}

public async getVaccinationPlacesV3({
public getVaccinationPlacesV3 = async ({
page = 1,
itemsPerPage = 100,
properties,
Expand Down Expand Up @@ -36,7 +36,7 @@ export default class VaccinationPlaces {
zarizeniKod?: string | string[];
zarizeniNazev?: string | string[];
poradiDavky?: 1 | 2 | 3 | number[];
} = {}): Promise<[VaccinationPlacesItemArr, null] | [null, GetApiError]> {
} = {}): Promise<[VaccinationPlacesItemArr, null] | [null, GetApiError]> => {
return await new ApiCallBuilder<VaccinationPlacesItemArr>({
token: this.token,
})
Expand All @@ -55,7 +55,7 @@ export default class VaccinationPlaces {
{ zarizeniNazev },
{ poradiDavky },
]).build();
}
};

public async getVaccinationPlacesOfIdV3(
{ id }: { id: string },
Expand Down

0 comments on commit 042d4e8

Please sign in to comment.