diff --git a/src/api/Cards.ts b/src/api/Cards.ts index 007da2a..856ac74 100644 --- a/src/api/Cards.ts +++ b/src/api/Cards.ts @@ -631,6 +631,15 @@ class Cards extends MagicQuerier { /** * Returns a MagicEmitter of every card in the Scryfall database that matches the given query. */ + public search (query: string, options?: SearchOptions): MagicEmitter; + /** + * Returns a MagicEmitter of every card in the Scryfall database that matches the given query. + */ + public search (query: string, page?: number): MagicEmitter; + /** + * Returns a MagicEmitter of every card in the Scryfall database that matches the given query. + */ + public search (query: string, options?: SearchOptions | number): MagicEmitter; public search (query: string, options?: SearchOptions | number) { const emitter = new MagicEmitter() .map(Card.construct); diff --git a/src/api/Migrations.ts b/src/api/Migrations.ts index 0613ea4..1771c6b 100644 --- a/src/api/Migrations.ts +++ b/src/api/Migrations.ts @@ -23,7 +23,7 @@ export interface Migration { } class Migrations extends MagicQuerier { - public all (page?: number) { + public all (page = 1) { const emitter = new MagicEmitter(); this.queryPage(emitter, "migrations", {}, page) diff --git a/src/tests/Main.ts b/src/tests/Main.ts index 13391f8..5bc8877 100644 --- a/src/tests/Main.ts +++ b/src/tests/Main.ts @@ -155,24 +155,17 @@ describe("Scry", function () { }).timeout(15000); it("should support pagination of searches", async () => { - let firstPageCard: Scry.Card; - let secondPageCard: Scry.Card; - - await Promise.all([ - new Promise((resolve, reject) => { - const emitter = Scry.Cards.search("type:creature"); - emitter.on("data", card => (firstPageCard = card, emitter.cancel())) - .on("end", () => reject(new Error("Did not expect to reach this point"))) - .on("cancel", resolve) - .on("error", reject); - }), - new Promise((resolve, reject) => { - const emitter = Scry.Cards.search("type:creature", 2).cancelAfterPage(); - emitter.on("data", card => secondPageCard = card) - .on("end", () => reject(new Error("Did not expect to reach this point"))) - .on("cancel", resolve) - .on("error", reject); - }), + const [firstPageCard, secondPageCard] = await Promise.all([ + new Promise((resolve, reject) => Scry.Cards.search("type:creature") + .cancelAfterPage() + .waitForAll() + .then(cards => cards[0]) + .then(resolve, reject)), + new Promise((resolve, reject) => Scry.Cards.search("type:creature", 2) + .cancelAfterPage() + .waitForAll() + .then(cards => cards[0]) + .then(resolve, reject)), ]); expect(firstPageCard!.id).not.eq(secondPageCard!.id); diff --git a/src/util/MagicQuerier.ts b/src/util/MagicQuerier.ts index e842a61..034545d 100644 --- a/src/util/MagicQuerier.ts +++ b/src/util/MagicQuerier.ts @@ -88,7 +88,7 @@ export default class MagicQuerier { return result; } - protected async queryPage (emitter: MagicEmitter, apiPath: string, query: any, page = 1): Promise { + protected async queryPage (emitter: MagicEmitter, apiPath: string, query: any, page = query?.page as number | undefined ?? 1): Promise { let error: SearchError | undefined; const results = await this.query>(apiPath, { ...query, page }) .catch(err => error = err);