diff --git a/src/utils/url.utils.ts b/src/utils/url.utils.ts index b067808..82c4a14 100644 --- a/src/utils/url.utils.ts +++ b/src/utils/url.utils.ts @@ -1,4 +1,5 @@ -import { LocalStorageConstants, AppIDConstants } from "../constants"; +import { LocalStorageConstants, AppIDConstants, URLConstants } from "../constants"; +import { queryParameters } from "../constants/url.constants"; import { getActiveLoginid, getAppId, getEnvironmentFromLoginid } from "./websocket.utils"; /** @@ -29,10 +30,16 @@ export const getLoginInfoFromURL = () => { const currencyRegex = key.match(/^cur(\d+)/); if (accountRegex) { - loginInfo[+accountRegex[1] - 1] = { ...(loginInfo[+accountRegex[1] - 1] || {}), loginid: value }; + loginInfo[+accountRegex[1] - 1] = { + ...(loginInfo[+accountRegex[1] - 1] || {}), + loginid: value, + }; } if (tokenRegex) { - loginInfo[+tokenRegex[1] - 1] = { ...(loginInfo[+tokenRegex[1] - 1] || {}), token: value }; + loginInfo[+tokenRegex[1] - 1] = { + ...(loginInfo[+tokenRegex[1] - 1] || {}), + token: value, + }; } if (currencyRegex) { loginInfo[+currencyRegex[1] - 1] = { @@ -44,7 +51,7 @@ export const getLoginInfoFromURL = () => { } const filteredLoginInfo = loginInfo.filter((login) => - ["loginid", "token", "currency"].every((k) => Object.keys(login).includes(k)) + ["loginid", "token", "currency"].every((k) => Object.keys(login).includes(k)), ) as AccountInfo[]; return { loginInfo: filteredLoginInfo, paramsToDelete }; @@ -104,3 +111,57 @@ export const getWebsocketURL = () => { return `wss://${serverURL}/websockets/v3?app_id=${getAppId()}&l=${language}&brand=${AppIDConstants.appBrand}`; }; + +/** + * Extracts query parameters from the URL by parsing the current window's URL search parameters for the specified key. + * It returns the query parameters associated with the given key. + * + * @returns {string | null} A string containing query parameter associated with the given key. + */ +export const getURLParameters = (key: queryParameters) => { + const searchParams = new URLSearchParams(window.location.search); + return searchParams.get(key); +}; + +/** + * Takes a 'URL path' as input and removes certain characters from the beginning and end of the path. + * + * Removes the following: + * - Any leading forward slash (/) at the beginning of the path. + * - Any trailing forward slash (/) at the end of the path. + * - Any characters that are not alphanumeric, hyphen, underscore, dot, forward slash, parentheses, or hash symbol. + * + * @param {string} path - The URL path that needs to be normalized. + * @returns {string} Returns the formatted path without the specified characters. + */ +export const normalizePath = (path: string) => path.replace(/(^\/|\/$|[^a-zA-Z0-9-_./()#])/g, ""); + +/** + * Generates a static URL for the deriv.com project based on the provided parameters. + * This function is necessary because deriv.com URLs differ from those used in app.deriv.com + * + * @param {string} path - The path to be appended to the base URL. + * @param {boolean} [isDocument=false] - Specifies whether the path represents a document. + * @param {boolean} [isEU=false] - Specifies whether the URL should be generated for the EU production environment. + * @returns {string} Returns the formatted static URL. + */ +export const generateDerivStaticURL = (path: string, isDocument?: boolean, isEU?: boolean) => { + const { derivComProductionEU, derivComProduction } = URLConstants; + const { i18nLanguage } = LocalStorageConstants; + + const host = isEU ? derivComProductionEU : derivComProduction; + let lang = localStorage.getItem(i18nLanguage)?.toLowerCase(); + + if (lang && lang !== "en") { + lang = `/${lang}`; + } else lang = ""; + + if (isDocument) return `${host}/${normalizePath(path)}`; + + // Deriv.com uses '-' instead of '_' for language separation. + if (lang.includes("_")) { + lang = lang.replace("_", "-"); + } + + return `${host}${lang}/${normalizePath(path)}`; +};