scryfall-client
Version:
A module for making requests to scryfall
156 lines (155 loc) • 6.68 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import SingularEntity from "./singular-entity";
import { get } from "../lib/api-request";
var SCRYFALL_CARD_BACK_IMAGE_URL = "http://cards.scryfall.io/back.png";
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
var Card = /** @class */ (function (_super) {
__extends(Card, _super);
function Card(scryfallObject) {
var _this = _super.call(this, scryfallObject) || this;
_this._tokens = (_this.all_parts || []).filter(function (part) {
return part.component === "token";
});
_this._hasTokens = Boolean(_this._tokens.length);
_this.card_faces = _this.card_faces || [
{
object: "card_face",
},
];
_this.card_faces.forEach(function (face) {
face.artist = face.artist || _this.artist;
face.color_indicator = face.color_indicator || _this.color_indicator;
face.colors = face.colors || _this.colors;
face.flavor_text = face.flavor_text || _this.flavor_text;
face.flavor_name = face.flavor_name || _this.flavor_name;
face.illustration_id = face.illustration_id || _this.illustration_id;
face.image_uris = face.image_uris || _this.image_uris;
face.loyalty = face.loyalty || _this.loyalty;
face.mana_cost = face.mana_cost || _this.mana_cost;
face.name = face.name || _this.name;
face.oracle_id = face.oracle_id || _this.oracle_id;
face.oracle_text = face.oracle_text || _this.oracle_text;
face.power = face.power || _this.power;
face.printed_name = face.printed_name || _this.printed_name;
face.printed_text = face.printed_text || _this.printed_text;
face.printed_type_line = face.printed_type_line || _this.printed_type_line;
face.toughness = face.toughness || _this.toughness;
face.type_line = face.type_line || _this.type_line;
face.watermark = face.watermark || _this.watermark;
if (face.oracle_text &&
face.oracle_text.toLowerCase().indexOf("token") > -1) {
// if the tokens are missing from the all_parts attribute
// we still want to check the oracle text, it may only
// be missing in the particular printing and can be found
// by looking for another printing
_this._hasTokens = true;
}
});
// some boolean fields are incositently applied
// IE, they only are applied if the value is true
// and omitted if the value is false
_this.booster = _this.booster || false;
_this.content_warning = _this.content_warning || false;
// flavor names of cards are not on the top level if the card
// has multiple sides, so we create a flavor name in the same
// style as the regular card name: the two sides, separated by //
if (_this.card_faces.length > 1 && _this.card_faces[0].flavor_name) {
_this.flavor_name = _this.card_faces
.map(function (face) { return face.flavor_name; })
.join(" // ");
}
_this._isDoublesided =
_this.layout === "transform" || _this.layout === "double_faced_token";
return _this;
}
Card.prototype.getRulings = function () {
return get(this.rulings_uri);
};
Card.prototype.getSet = function () {
return get(this.set_uri);
};
Card.prototype.getPrints = function () {
return get(this.prints_search_uri);
};
Card.prototype.isLegal = function (format) {
var legality = this.legalities[format];
return legality === "legal" || legality === "restricted";
};
Card.prototype.getImage = function (type) {
if (type === void 0) { type = "normal"; }
var imageObject = this.card_faces[0].image_uris;
if (!imageObject) {
throw new Error("Could not find image uris for card.");
}
return imageObject[type];
};
Card.prototype.getBackImage = function (type) {
if (type === void 0) { type = "normal"; }
if (!this._isDoublesided) {
return SCRYFALL_CARD_BACK_IMAGE_URL;
}
var imageObject = this.card_faces[1].image_uris;
if (!imageObject) {
throw new Error("An unexpected error occured when attempting to show back side of card.");
}
return imageObject[type];
};
Card.prototype.getPrice = function (type) {
var prices = this.prices;
if (!type) {
return (prices.usd ||
prices.usd_foil ||
prices.usd_etched ||
prices.eur ||
prices.eur_foil ||
prices.tix ||
"");
}
return prices[type] || "";
};
Card.prototype.getTokens = function () {
var _this = this;
if (!this._hasTokens) {
return Promise.resolve([]);
}
return Promise.all(this._tokens.map(function (token) {
return get(token.uri);
})).then(function (tokens) {
if (tokens.length > 0) {
return tokens;
}
return _this.getPrints().then(function (prints) {
var printWithTokens = prints.find(function (print) {
return print._tokens.length > 0;
});
if (printWithTokens) {
return printWithTokens.getTokens();
}
return [];
});
});
};
Card.prototype.getTaggerUrl = function () {
return ("https://tagger.scryfall.com/card/" +
this.set +
"/" +
this.collector_number);
};
return Card;
}(SingularEntity));
export default Card;