UNPKG

chargebee-js

Version:

Javascript Client SDK to implement Chargebee on frontend.

156 lines (150 loc) 4.86 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const SCRIPT_URL = "https://js.chargebee.com/v2/chargebee.js"; const SCRIPT_URL_REGEX = /^https:\/\/js\.chargebee\.com\/v2\/chargebee\.js\/?(\?.*)?$/; const EXISTING_SCRIPT_MESSAGE = "loadChargebee was called but a script already exists in the document;"; const EXISTING_INSTANCE_MESSAGE = "loadChargebee was called but an instance is already created;"; /** * Find Chargebee Script tag in document * @returns HTMLScriptElement */ const findScript = () => { const scripts = document.querySelectorAll(`script[src^="${SCRIPT_URL}"]`); for (let i = 0; i < scripts.length; i++) { const script = scripts[i]; if (!SCRIPT_URL_REGEX.test(script.src)) { continue; } return script; } return null; }; /** * Inject Chargebee Script tag in document if doesn't exists * @returns HTMLScriptElement */ const injectScript = () => { const script = document.createElement("script"); script.src = `${SCRIPT_URL}`; const headOrBody = document.head || document.body; if (!headOrBody) { throw new Error("Expected document.body not to be null. Chargebee requires a <body> element."); } headOrBody.appendChild(script); return script; }; let cbPromise$1 = null; let onErrorListener = null; let onLoadListener = null; const onError = (reject) => () => { reject(new Error("Failed to load Chargebee")); }; const onLoad = (resolve, reject) => () => { if (window.Chargebee) { resolve(window.Chargebee); } else { reject(new Error("Chargebee not available")); } }; /** * Load Chargebee script * @returns Chargebee */ const loadScript = () => { // Ensure that we only attempt to load Chargebee at most once if (cbPromise$1 !== null) { return cbPromise$1; } cbPromise$1 = new Promise((resolve, reject) => { var _a; if (typeof window === "undefined" || typeof document === "undefined") { // Resolve to null when imported server side. This makes the module // safe to import in an isomorphic code base. resolve(null); return; } if (window.Chargebee) { console.warn(EXISTING_SCRIPT_MESSAGE); } if (window.Chargebee) { resolve(window.Chargebee); return; } try { let script = findScript(); if (!script) { script = injectScript(); } else if (script && onLoadListener !== null && onErrorListener !== null) { // remove event listeners script.removeEventListener("load", onLoadListener); script.removeEventListener("error", onErrorListener); // if script exists, but we are reloading due to an error, // reload script to trigger 'load' event (_a = script.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(script); script = injectScript(); } onLoadListener = onLoad(resolve, reject); onErrorListener = onError(reject); script.addEventListener("load", onLoadListener); script.addEventListener("error", onErrorListener); } catch (error) { reject(error); return; } }); // Resets cbPromise on error return cbPromise$1.catch((error) => { cbPromise$1 = null; return Promise.reject(error); }); }; let cbPromise; let loadCalled = false; const getCbPromise = () => { if (cbPromise) { return cbPromise; } cbPromise = loadScript().catch((error) => { // clear cache on error cbPromise = null; return Promise.reject(error); }); return cbPromise; }; // Execute our own script injection after a tick to give users time to do their // own script injection. Promise.resolve() .then(() => getCbPromise()) .catch((error) => { if (!loadCalled) { console.warn(error); } }); const loadChargebee = (config) => { loadCalled = true; config.site; // if previous attempts are unsuccessful, will re-load script return getCbPromise().then((maybeChargebee) => initChargebee(maybeChargebee, config)); }; const initChargebee = (maybeChargebee, config) => { if (maybeChargebee === null) { return null; } if (maybeChargebee.inited) { console.warn(EXISTING_INSTANCE_MESSAGE); return maybeChargebee.getInstance(); } return maybeChargebee.init(config); }; var index = { loadChargebee }; exports.default = index; exports.initChargebee = initChargebee; exports.loadChargebee = loadChargebee; module.exports = Object.assign(exports.default, exports);