Skip to content

Commit

Permalink
fix: review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
niloofar-deriv committed Feb 27, 2024
1 parent 2a7a60d commit 53b46af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
17 changes: 10 additions & 7 deletions src/utils/__test__/url.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,23 +276,26 @@ describe("URLUtils.getDerivStaticURL", () => {
});

test("getDerivStaticURL with path and isEU true", () => {
const result = URLUtils.getDerivStaticURL("/p2p/", false, true);
const result = URLUtils.getDerivStaticURL("/p2p/", { isEU: true });
expect(result).toBe("https://eu.deriv.com/p2p");
});

test("getDerivStaticURL with path and isDocument true and default language", () => {
const result = URLUtils.getDerivStaticURL("/p2p/", true);
expect(result).toBe("https://deriv.com/p2p");
const result = URLUtils.getDerivStaticURL("regulatory/deriv-com-ltd-membership.pdf", { isDocument: true });
expect(result).toBe("https://deriv.com/regulatory/deriv-com-ltd-membership.pdf");
});

test("getDerivStaticURL with path and isDocument true and Spanish language", () => {
localStorage.getItem = vitest.fn(() => "ES");
const result = URLUtils.getDerivStaticURL("/p2p/", true);
expect(result).toBe("https://deriv.com/p2p");
const result = URLUtils.getDerivStaticURL("regulatory/deriv-com-ltd-membership.pdf", { isDocument: true });
expect(result).toBe("https://deriv.com/regulatory/deriv-com-ltd-membership.pdf");
});

test("getDerivStaticURL with path and isEU true and isDocument true", () => {
const result = URLUtils.getDerivStaticURL("/p2p/", true, true);
expect(result).toBe("https://eu.deriv.com/p2p");
const result = URLUtils.getDerivStaticURL("regulatory/deriv-com-ltd-membership.pdf", {
isDocument: true,
isEU: true,
});
expect(result).toBe("https://eu.deriv.com/regulatory/deriv-com-ltd-membership.pdf");
});
});
17 changes: 12 additions & 5 deletions src/utils/url.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { LocalStorageConstants, AppIDConstants, URLConstants } from "../constant
import { QueryParameters } from "../constants/url.constants";
import { getActiveLoginid, getAppId, getEnvironmentFromLoginid } from "./websocket.utils";

type DerivStaticURLOptions = {
isDocument?: boolean;
isEU?: boolean;
};

/**
* Defines the structure for account information.
* @typedef {Object} AccountInfo
Expand Down Expand Up @@ -142,16 +147,18 @@ export const normalizePath = (path: string) => path.replace(/(^\/|\/$|[^a-zA-Z0-
* 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.
* @param {DerivStaticURLOptions} [options] - Optional configuration for customising the Deriv Static URL, including:
* - `isDocument`: Specifies whether the path represents a document.
* - `isEU`: Specifies whether the URL should be generated for the EU production environment.
*
* @returns {string} Returns the formatted static URL.
*/
export const getDerivStaticURL = (path: string, isDocument?: boolean, isEU?: boolean) => {
const host = isEU ? URLConstants.derivComProductionEU : URLConstants.derivComProduction;
export const getDerivStaticURL = (path: string, options?: DerivStaticURLOptions) => {
const host = options?.isEU ? URLConstants.derivComProductionEU : URLConstants.derivComProduction;
let lang = localStorage.getItem(LocalStorageConstants.i18nLanguage)?.toLowerCase() ?? "en";

lang = lang === "en" ? "" : `/${lang.replace("_", "-")}`;

if (isDocument) return `${host}/${normalizePath(path)}`;
if (options?.isDocument) return `${host}/${normalizePath(path)}`;
return `${host}${lang}/${normalizePath(path)}`;
};

0 comments on commit 53b46af

Please sign in to comment.