-
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.
- Loading branch information
1 parent
617ee69
commit 8c8a014
Showing
4 changed files
with
63 additions
and
2 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
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(); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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); | ||
}); | ||
}); | ||
}); |