scryfall-client
Version:
A module for making requests to scryfall
130 lines (129 loc) • 4.23 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
/* Cards - https://scryfall.com/docs/api/cards */
import { get, post } from "../lib/api-request";
import List from "../models/list";
// https://scryfall.com/docs/api/cards/search
export function search(searchString, options) {
if (options === void 0) { options = {}; }
var query = __assign({ q: searchString }, options);
return get("/cards/search", query);
}
// https://scryfall.com/docs/api/cards/named
export function getCardNamed(name, options) {
var _a;
if (options === void 0) { options = {}; }
var kind = options.kind || "fuzzy";
var query = (_a = {},
_a[kind] = name,
_a);
if (options.set) {
query.set = options.set;
}
return get("/cards/named", query);
}
// https://scryfall.com/docs/api/cards/autocomplete
export function autocomplete(searchString, options) {
if (options === void 0) { options = {}; }
var query = __assign({ q: searchString }, options);
return get("/cards/autocomplete", query);
}
// https://scryfall.com/docs/api/cards/random
export function random(searchString) {
if (!searchString) {
return get("/cards/random");
}
return get("/cards/random", {
q: searchString,
});
}
// https://scryfall.com/docs/api/cards/collection
export function getCollection(identifiers) {
var idBatches = identifiers.reduce(function (array, entry, i) {
if (i % 75 !== 0) {
return array;
}
return array.concat([identifiers.slice(i, i + 75)]);
}, []);
return Promise.all(idBatches.map(function (ids) {
return post("/cards/collection", {
identifiers: ids,
});
})).then(function (collectionResults) {
var warnings = [];
var notFound = [];
collectionResults.forEach(function (result) {
var list = result;
warnings.push.apply(warnings, list.warnings);
notFound.push.apply(notFound, list.not_found);
});
var collection = collectionResults.flat();
// coerce back into a List
return new List({
object: "list",
warnings: warnings,
not_found: notFound,
data: collection,
});
});
}
// https://scryfall.com/docs/api/cards/collector
export function getCardBySetCodeAndCollectorNumber(code, collectorNumber, lang) {
var url = "/cards/".concat(code, "/").concat(collectorNumber);
if (lang) {
url += "/".concat(lang);
}
return get(url);
}
// https://scryfall.com/docs/api/cards/multiverse
function getCardByMultiverseId(id) {
return get("/cards/multiverse/".concat(id));
}
// https://scryfall.com/docs/api/cards/mtgo
function getCardByMtgoId(id) {
return get("/cards/mtgo/".concat(id));
}
// https://scryfall.com/docs/api/cards/arena
function getCardByArenaId(id) {
return get("/cards/arena/".concat(id));
}
// https://scryfall.com/docs/api/cards/tcgplayer
function getCardByTcgPlayerId(id) {
return get("/cards/tcgplayer/".concat(id));
}
// https://scryfall.com/docs/api/cards/id
function getCardByScryfallId(id) {
return get("/cards/".concat(id));
}
export function getCard(idOrName, kind) {
if (kind === void 0) { kind = "scryfall"; }
var value = String(idOrName);
switch (kind) {
case "multiverse":
return getCardByMultiverseId(value);
case "arena":
return getCardByArenaId(value);
case "mtgo":
return getCardByMtgoId(value);
case "tcg":
return getCardByTcgPlayerId(value);
case "exactName":
return getCardNamed(value, { kind: "exact" });
case "name":
case "fuzzyName":
return getCardNamed(value, { kind: "fuzzy" });
case "id":
case "scryfall":
default:
return getCardByScryfallId(value);
}
}