Skip to content

Commit

Permalink
Merge pull request #68 from thejoecode/main
Browse files Browse the repository at this point in the history
Bug fix: Page is not being passed to queryPage from Cards.search
  • Loading branch information
ChiriVulpes authored Oct 22, 2023
2 parents 7f79389 + c489edc commit 8db0221
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
9 changes: 9 additions & 0 deletions src/api/Cards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Card>;
/**
* Returns a MagicEmitter of every card in the Scryfall database that matches the given query.
*/
public search (query: string, page?: number): MagicEmitter<Card>;
/**
* Returns a MagicEmitter of every card in the Scryfall database that matches the given query.
*/
public search (query: string, options?: SearchOptions | number): MagicEmitter<Card>;
public search (query: string, options?: SearchOptions | number) {
const emitter = new MagicEmitter<Card>()
.map(Card.construct);
Expand Down
2 changes: 1 addition & 1 deletion src/api/Migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface Migration {
}

class Migrations extends MagicQuerier {
public all (page?: number) {
public all (page = 1) {
const emitter = new MagicEmitter<Migration>();

this.queryPage(emitter, "migrations", {}, page)
Expand Down
29 changes: 11 additions & 18 deletions src/tests/Main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Scry.Card>((resolve, reject) => Scry.Cards.search("type:creature")
.cancelAfterPage()
.waitForAll()
.then(cards => cards[0])
.then(resolve, reject)),
new Promise<Scry.Card>((resolve, reject) => Scry.Cards.search("type:creature", 2)
.cancelAfterPage()
.waitForAll()
.then(cards => cards[0])
.then(resolve, reject)),
]);

expect(firstPageCard!.id).not.eq(secondPageCard!.id);
Expand Down
2 changes: 1 addition & 1 deletion src/util/MagicQuerier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default class MagicQuerier {
return result;
}

protected async queryPage<T> (emitter: MagicEmitter<T>, apiPath: string, query: any, page = 1): Promise<void> {
protected async queryPage<T> (emitter: MagicEmitter<T>, apiPath: string, query: any, page = query?.page as number | undefined ?? 1): Promise<void> {
let error: SearchError | undefined;
const results = await this.query<List<T>>(apiPath, { ...query, page })
.catch(err => error = err);
Expand Down

0 comments on commit 8db0221

Please sign in to comment.