UNPKG

scryfall-client

Version:
51 lines (50 loc) 1.59 kB
"use strict"; import Card from "../models/card"; import Catalog from "../models/catalog"; import List from "../models/list"; import MagicSet from "../models/magic-set"; import GenericScryfallResponse from "../models/generic-scryfall-response"; var transformFunction; var noopTransformFunction = function (text) { // noop by default return text; }; export function setTextTransform(func) { transformFunction = func; } export function resetTextTransform() { setTextTransform(noopTransformFunction); } resetTextTransform(); // eslint-disable-next-line @typescript-eslint/no-explicit-any function wrapScryfallResponse(response) { if (typeof response === "string") { return transformFunction(response); } if (!response || typeof response !== "object") { return response; } if (response.object === "list" || response.object === "catalog") { response.data = response.data.map(wrapScryfallResponse); if (response.object === "list") { return new List(response); } else if (response.object === "catalog") { return new Catalog(response); } } Object.keys(response).forEach(function (key) { response[key] = wrapScryfallResponse(response[key]); }); if (response.object === "card") { return new Card(response); } else if (response.object === "set") { return new MagicSet(response); } else if (response.object) { return new GenericScryfallResponse(response); } return response; } export default wrapScryfallResponse;