UNPKG

diginext-utils

Version:
38 lines (37 loc) 1.27 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const getUrlBypassCors_1 = __importDefault(require("./getUrlBypassCors")); const tryLoad = async (url) => { const response = await fetch(url, { // mode: "cors", cache: "force-cache", }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const imageBlob = await (response === null || response === void 0 ? void 0 : response.blob()); const blobUrl = URL.createObjectURL(imageBlob); return blobUrl; }; async function loadImageAsBlobUrl(imageUrl, addVerion = true) { try { const imageBlob = await tryLoad(imageUrl); return imageBlob; } catch (error) { } if (addVerion) { try { const url = (0, getUrlBypassCors_1.default)(imageUrl); if (!url) throw new Error("imageUrl error"); const imageBlob = await tryLoad(url); return imageBlob; } catch (error) { } } return imageUrl; } exports.default = loadImageAsBlobUrl;