-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from radekBednarik/feat/helpers
feat: add helpers - provide method for sequential api call
- Loading branch information
Showing
14 changed files
with
153 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters