UNPKG

connect-transfer-react-native-sdk

Version:

Connect Transfer React Native SDK for Mastercard Open Banking Connect

99 lines (92 loc) 3.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.openLink = exports.getURL = exports.getTranslation = exports.formatCurrentDateTime = exports.extractUrlData = void 0; require("react-native-url-polyfill/auto"); var _reactNativeInappbrowserReborn = require("react-native-inappbrowser-reborn"); var _routes = require("../services/api/routes"); var _constants = require("../constants"); /** * Extracts base URL, query parameters string, and query parameters as an object from a given partner URL. * * @param {string} url - The URL from which to extract data. * @returns {{ baseURL: string; queryParams: string; queryParamsObject: Record<string, string> }} * - An object containing: * - baseURL: The base URL of the partner URL. * - queryParams: The raw query parameters string, including the '?' prefix. * - queryParamsObject: An object mapping query parameter keys to their respective values. */ const extractUrlData = (url = '') => { try { if (!url) { console.warn('Invalid URL: URL is empty/null/undefined'); return; } const urlPattern = /^https:\/\//i; if (!urlPattern.test(url)) { console.warn('Invalid URL: Must start with https://'); return; } const urlObject = new URL(url); // Get base URL (protocol + hostname + port) const baseURL = `${urlObject.protocol}//${urlObject.host}`; // Get query string const queryParams = urlObject.search; // Parse query parameters into an object const queryParamsObject = {}; urlObject.searchParams.forEach((value, key) => { if (key) { queryParamsObject[key] = value; } }); return { baseURL, queryParams, queryParamsObject }; } catch (error) { console.warn('Error parsing URL:', error); return; } }; /** * Formats the current date and time to "yyyy-MM-dd'T'HH:mm:ssZ" format. * @returns {string} The formatted current date and time. */ exports.extractUrlData = extractUrlData; const formatCurrentDateTime = () => { const date = new Date(); // Format the date to "yyyy-MM-dd'T'HH:mm:ssZ" const formattedDate = date.toISOString().replace(/\.\d{3}Z$/, 'Z'); return formattedDate; }; /** * Generate the URL based on the current app language and the type of webpage (Terms or Privacy). * @param {string} language - The current app language ('en' or 'es'). * @param {string} type - The type of page ('termsOfUse' or 'privacyPolicy'). * @returns {string} - The URL for the specified page. */ exports.formatCurrentDateTime = formatCurrentDateTime; const getURL = (language, type) => { const key = language === 'es' ? _constants.WEBPAGE_API_KEYS[`${type}_ES`] : _constants.WEBPAGE_API_KEYS[`${type}_EN`]; return (0, _routes.generateRoute)(key); }; /** * Open a URL in the InAppBrowser or fallback to the default browser. * @param {string} url - The URL to open. */ exports.getURL = getURL; const openLink = async url => { try { if (await _reactNativeInappbrowserReborn.InAppBrowser.isAvailable()) { await _reactNativeInappbrowserReborn.InAppBrowser.open(url); } } catch (error) { console.warn('Failed to open link:', error); } }; exports.openLink = openLink; const getTranslation = (text, esJson) => esJson[text] || text; exports.getTranslation = getTranslation; //# sourceMappingURL=utils.js.map