Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: Page is not being passed to queryPage from Cards.search #68

Merged
merged 2 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading