UNPKG

scryfall-sdk

Version:

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

115 lines (114 loc) 5.11 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const Cached_1 = require("../util/Cached"); const MagicQuerier_1 = require("../util/MagicQuerier"); let axios; if (typeof fetch === "undefined") { try { axios = require("axios").default; } catch (_a) { } } var BulkDataTypes; (function (BulkDataTypes) { BulkDataTypes[BulkDataTypes["oracle_cards"] = 0] = "oracle_cards"; BulkDataTypes[BulkDataTypes["unique_artwork"] = 1] = "unique_artwork"; BulkDataTypes[BulkDataTypes["default_cards"] = 2] = "default_cards"; BulkDataTypes[BulkDataTypes["all_cards"] = 3] = "all_cards"; BulkDataTypes[BulkDataTypes["rulings"] = 4] = "rulings"; })(BulkDataTypes || (BulkDataTypes = {})); class BulkData extends MagicQuerier_1.default { /** * Returns a stream for the given bulk data if it has been updated since the last download time. If it hasn't, returns `undefined` * @param lastDownload The last time this bulk data was downloaded. If you want to re-download the data regardless of * the last time it was downloaded, set this to `0`. */ downloadByType(type, lastDownload) { return __awaiter(this, void 0, void 0, function* () { return this.download(type, lastDownload); }); } /** * Returns a stream for the given bulk data if it has been updated since the last download time. If it hasn't, returns `undefined` * @param lastDownload The last time this bulk data was downloaded. If you want to re-download the data regardless of * the last time it was downloaded, set this to `0`. */ downloadById(id, lastDownload) { return __awaiter(this, void 0, void 0, function* () { return this.download(id, lastDownload); }); } //////////////////////////////////// // Definitions // definitions() { return __awaiter(this, void 0, void 0, function* () { return (yield this.query("bulk-data")).data; }); } definitionByType(type) { return __awaiter(this, void 0, void 0, function* () { return this.definition(type); }); } definitionById(id) { return __awaiter(this, void 0, void 0, function* () { return this.definition(id); }); } //////////////////////////////////// // Internals // download(idOrType, lastDownload) { return __awaiter(this, void 0, void 0, function* () { const definition = yield this.definition(idOrType); if (new Date(lastDownload).getTime() > new Date(definition.updated_at).getTime()) return undefined; if (axios) { const result = yield axios.request({ method: "GET", url: definition.download_uri, responseType: "stream", }); return result.data; } else { const result = yield fetch(definition.download_uri, { method: "GET", headers: Object.assign(Object.assign({}, !MagicQuerier_1.default.agent ? undefined : { 'User-Agent': MagicQuerier_1.default.agent, }), { Accept: "*/*" }), }); return result.body; } }); } definition(idOrType) { return this.query(["bulk-data", idOrType]); } } __decorate([ Cached_1.default ], BulkData.prototype, "definitions", null); __decorate([ Cached_1.default ], BulkData.prototype, "definitionByType", null); __decorate([ Cached_1.default ], BulkData.prototype, "definitionById", null); exports.default = new BulkData;