UNPKG

archidekt

Version:
254 lines (237 loc) 7.63 kB
import axios from 'axios'; var archidekt = axios.create({ baseURL: 'https://archidekt.com/api/' }); function __async(g) { return new Promise(function (s, j) { function c(a, x) { try { var r = g[x ? "throw" : "next"](a); } catch (e) { j(e); return; } r.done ? s(r.value) : Promise.resolve(r.value).then(c, d); } function d(e) { c(e, 1); } c(); }); } var deckURI = function deckURI(id) { var small = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var uri = "decks/".concat(id, "/"); if (small) uri += 'small/'; return uri; }; var fetchDeckById = function fetchDeckById(id) { var small = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; return __async( /*#__PURE__*/ regeneratorRuntime.mark(function _callee() { var response; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return archidekt.get(deckURI(id, small)); case 2: response = _context.sent; return _context.abrupt("return", response); case 4: case "end": return _context.stop(); } } }, _callee); })()); }; var formatMap = { 'standard': 1, 'modern': 2, 'commander/edh': 3, 'legacy': 4, 'vintage': 5, 'pauper': 6, 'custom': 7, 'frontier': 8, 'future standard': 9, 'penny dreadful': 10, '1v1 commander': 11, 'dual commander': 12, 'brawl': 13 }; var validFormats = new Set(Object.keys(formatMap)); var isValidFormat = function isValidFormat(maybeFormat) { var maybe = maybeFormat.toLowerCase(); return validFormats.has(maybe); }; var toAPIFormat = function toAPIFormat(format) { var f = format.toLowerCase(); if (isValidFormat(f)) return formatMap[f]; return null; }; var URIFormats = function URIFormats(formats) { var processed = formats.map(function (format) { return toAPIFormat(format); }).filter(function (format) { return format != null; }); return processed; }; var colorMap = { '{w}': 'white', '{b}': 'black', '{u}': 'blue', '{r}': 'red', '{g}': 'green', '{c}': 'colorless', 'w': 'white', 'b': 'black', 'u': 'blue', 'r': 'red', 'g': 'green', 'c': 'colorless' }; var oracleColorRegex = /\{(\w+)\}/gi; var extractOracleColors = function extractOracleColors(str) { return str.match(oracleColorRegex).map(function (match) { return match.slice(1, -1).toLowerCase(); }); }; var extractOracleColor = function extractOracleColor(str) { var colors = extractOracleColors(str); return colors ? colors[0] : colors; }; var isValidColor = function isValidColor(maybeColor) { var mc = maybeColor.toLowerCase(); if (validColors.has(mc)) return mc; if (colorMap[mc] !== undefined) return colorMap[mc]; var oc = extractOracleColor(mc); if (colorMap[oc] !== undefined) return colorMap[oc]; return false; }; var validColors = new Set(["white", "blue", "black", "red", "green", "colorless"]); var capitalize = function capitalize(str) { return str.slice(0, 1).toUpperCase() + str.slice(1); }; var toAPIColor = function toAPIColor(color) { var c = color.toLowerCase(); var v = isValidColor(c); if (v) return capitalize(v); return null; }; var URIColors = function URIColors(colors) { var processed = colors.map(function (color) { return toAPIColor(color); }).filter(function (color) { return color != null; }); return processed; }; var quoteCard = function quoteCard(card) { return "\"".concat(card, "\""); }; var URICards = function URICards(cards) { var processed = cards.map(function (card) { return quoteCard(card); }); return processed; }; var URICommanders = function URICommanders(commanders) { var processed = commanders.map(function (card) { return quoteCard(card); }); return processed; }; var searchDecks = function searchDecks(_ref) { var _ref$logicalAnd = _ref.logicalAnd, logicalAnd = _ref$logicalAnd === void 0 ? false : _ref$logicalAnd, _ref$owner = _ref.owner, owner = _ref$owner === void 0 ? null : _ref$owner, _ref$colors = _ref.colors, colors = _ref$colors === void 0 ? ['White', 'Blue', 'Black', 'Green', 'Red', 'Colorless'] : _ref$colors, _ref$cards = _ref.cards, cards = _ref$cards === void 0 ? null : _ref$cards, _ref$orderBy = _ref.orderBy, orderBy = _ref$orderBy === void 0 ? 'createdAt' : _ref$orderBy, _ref$descending = _ref.descending, descending = _ref$descending === void 0 ? true : _ref$descending, _ref$formats = _ref.formats, formats = _ref$formats === void 0 ? null : _ref$formats, _ref$pageSize = _ref.pageSize, pageSize = _ref$pageSize === void 0 ? 10 : _ref$pageSize, _ref$commanders = _ref.commanders, commanders = _ref$commanders === void 0 ? null : _ref$commanders; return __async( /*#__PURE__*/ regeneratorRuntime.mark(function _callee() { var params, response; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: params = processSearchParameters({ logicalAnd: logicalAnd, owner: owner, colors: colors, cards: cards, orderBy: orderBy, descending: descending, formats: formats, pageSize: pageSize, commanders: commanders }); _context.next = 3; return archidekt.get('decks/cards/', { params: params }); case 3: response = _context.sent; return _context.abrupt("return", response); case 5: case "end": return _context.stop(); } } }, _callee); })()); }; var processSearchParameters = function processSearchParameters(_ref2) { var _ref2$logicalAnd = _ref2.logicalAnd, logicalAnd = _ref2$logicalAnd === void 0 ? false : _ref2$logicalAnd, _ref2$owner = _ref2.owner, owner = _ref2$owner === void 0 ? null : _ref2$owner, _ref2$colors = _ref2.colors, colors = _ref2$colors === void 0 ? ['White', 'Blue', 'Black', 'Green', 'Red', 'Colorless'] : _ref2$colors, _ref2$cards = _ref2.cards, cards = _ref2$cards === void 0 ? null : _ref2$cards, _ref2$orderBy = _ref2.orderBy, orderBy = _ref2$orderBy === void 0 ? 'createdAt' : _ref2$orderBy, _ref2$descending = _ref2.descending, descending = _ref2$descending === void 0 ? true : _ref2$descending, _ref2$formats = _ref2.formats, formats = _ref2$formats === void 0 ? null : _ref2$formats, _ref2$pageSize = _ref2.pageSize, pageSize = _ref2$pageSize === void 0 ? 10 : _ref2$pageSize, _ref2$commanders = _ref2.commanders, commanders = _ref2$commanders === void 0 ? null : _ref2$commanders; var params = {}; if (logicalAnd) params.andcolors = true; if (colors) params.colors = URIColors(colors); if (owner) params.owner = owner; if (cards) params.cards = URICards(cards); if (orderBy) params.orderBy = "".concat(descending ? '-' : '').concat(orderBy); if (formats) params.formats = URIFormats(formats); if (commanders) params.commanders = URICommanders(commanders); if (pageSize) params.pageSize = pageSize; return params; }; var archidekt$1 = { api: archidekt, fetchDeckById: fetchDeckById, searchDecks: searchDecks }; export default archidekt$1;