ChiriVulpes / scryfall-sdk

A Node.js SDK for https://scryfall.com/docs/api, written in TypeScript.
MIT License
99 stars 16 forks source link

Pagination for /cards endpoint using Cards.all() #8

Closed lucasmfredmark closed 6 years ago

lucasmfredmark commented 6 years ago

First of all, thanks for making this library! Definitely simplifies things 👍

It doesn't seem like it's possible to request a specific page to retrieve using Cards.all(). Every time this function is called it starts getting results from page 1. It would be nice if I could provide a page parameter to the function - I can then limit the returned items myself and create my own pagination logic.

What do you think about this? I'll happily help implementing this, but my TypeScript knowledge is not that great 😄

ChiriVulpes commented 6 years ago

Honestly I didn't even consider this functionality, I was only thinking about pagination in search results. Maybe there's a way you could use that method instead? I don't remember all the stuff you could do with it off the top of my head but I expect there's something you could give it that would match every card

I'm on holiday right now, I'll probably be able to get to this by the end of the month

lucasmfredmark commented 6 years ago

The search endpoint requires a search query parameter - I have also tried giving it parameters that would match every card, but I did not find a way to make it work that way.

If you were to implement this in your library, you could make the Cards.all() function accept query parameters and conditionally only request the specified page if the query parameter is there - otherwise just do the recursive behavior like now.

Would be cool to have this implemented! 😄

ChiriVulpes commented 6 years ago

This is published as v1.3.0

It doesn't seem like there's a way to change the size of a page, so I didn't allow an options object. Cards.all() now accepts one parameter which is the starting page. (1 is the first page, not 0)

To only get the cards in the page you start in, you can use the following:

const cardsFromPage15 = await Scry.Cards.all(15).cancelAfterPage().waitForAll();