ChiriVulpes / scryfall-sdk

A Node.js SDK for https://scryfall.com/docs/api, written in TypeScript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why add empty card_faces array?

furgin opened this issue · comments

In Cards.ts the constructor adds an almost empty card_faces array if the response from Scryfall is not present.

Why does this happen? I can't tell this is a mistake or I am misunderstanding how to use this array. Using the raw Scryfall API I would check if the faces array existed, to check where to find data about the card (either in the face or in the card object).

Hmm I don't quite remember, but looking at the code I think it's to allow using the same code no matter the type of card? I think in this case it allows being like "okay so no matter how many faces this card has, grab the first one, then do operations on the data in that face". The card face it adds won't have info printed with console logging, for reference, but I believe you can still access it bcuz its prototype is the card itself? I don't actually know if that works tbh. I'll mess with it when I get to PC

Cards that come back from the API with no faces array end up with a faces array with a single element that just has:

[ { "object": "card_face" } ]

I think this is a bug and should just be undefined to match the source API

I'm at PC now. Like I said, the face uses the card itself as its prototype, which results in the following:
image

You can access any data from the base card from the face, provided the face doesn't override it with different information. Since the data is not duplicated, and instead provided via the card face's prototype, it can't be logged. That's why in this screenshot, it looks like it just has "object": "card_face" rather than having everything. It does have everything, it just can't be logged.

If card faces matter to what you're working on, just get the data from the first face in favour of doing some kind of check to determine whether to use the face or the card itself.

If you don't think this will work, it might be helpful for me to know what exactly you're trying to do? Maybe we can work something out

I was hoping for a more consistent approach to processing data from a bulk stream (which is a raw JSON object) and the Scry.Card type that is collected through the API. They behave differently in this case where the raw object has no faces array but the object does, in most cases you can safely treat them as the same thing.

I was thinking of options how this could work,

  1. that they should behave close, ie have no array on the Scry.Card object like the json response (this is what I was trying to do)
  2. provide a way to get the raw json from the Card object (then write the logic based on this)
  3. provide a way to wrap up a raw json object in to a Scry.Card object so I can process a bulk stream as an object

For 3, I believe you're looking for Scry.Card.construct(raw)

I'm going to close this for now, feel free to reopen if you have more questions on the topic or still can't get something to work with the current way this stuff functions!