-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from deriv-com/added-country-utils
Prince/ added country utility
- Loading branch information
Showing
9 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { describe, beforeEach, afterEach, it, expect, vi } from "vitest"; | ||
import { getCountry } from "../country.utils"; | ||
|
||
describe("getCountry", () => { | ||
beforeEach(() => { | ||
global.fetch = vi.fn(); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
it("should return the country code in lowercase when available", async () => { | ||
// Mock fetch response | ||
(global.fetch as vi.Mock).mockResolvedValue({ | ||
text: async () => "loc=US\nother=info\n", | ||
}); | ||
|
||
const country = await getCountry(); | ||
expect(country).toBe("us"); | ||
}); | ||
|
||
it("should return an empty string if the loc field is not present", async () => { | ||
(global.fetch as vi.Mock).mockResolvedValue({ | ||
text: async () => "other=info\n", | ||
}); | ||
|
||
const country = await getCountry(); | ||
expect(country).toBe(""); | ||
}); | ||
|
||
it("should return an empty string if the fetch fails", async () => { | ||
(global.fetch as vi.Mock).mockRejectedValue(new Error("Fetch failed")); | ||
|
||
const country = await getCountry(); | ||
expect(country).toBe(""); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import Cookies from "js-cookie"; | ||
import { cloudflareTrace } from "../constants/url.constants"; | ||
|
||
type TraceData = { | ||
loc?: string; | ||
}; | ||
|
||
/** | ||
* Fetches the country information based on Cloudflare's trace data or a fallback from cookies. | ||
* This function attempts to retrieve the country location by first fetching trace data from Cloudflare | ||
* and then falling back to the location stored in the cookies if the fetch fails. | ||
* | ||
* @returns {Promise<string>} A Promise that resolves to a string representing the country code in lowercase. | ||
* Returns an empty string if no country data is available or if an error occurs. | ||
* | ||
* @example | ||
* // Returns the country code in lowercase based on Cloudflare's trace data or cookies. | ||
* getCountry().then(country => console.log(country)); | ||
*/ | ||
|
||
export const getCountry = async (): Promise<string> => { | ||
try { | ||
const response = await fetch(cloudflareTrace).catch(() => null); | ||
if (!response) return ""; | ||
const text = await response.text().catch(() => ""); | ||
const entries = text ? text.split("\n").map((v) => v.split("=", 2)) : []; | ||
const data: TraceData = entries.length ? Object.fromEntries(entries) : {}; | ||
return data.loc?.toLowerCase() || JSON.parse(Cookies.get("website_status") || "{}")?.loc || ""; | ||
} catch (error) { | ||
return ""; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
sidebar_position: 2 | ||
--- | ||
|
||
# CountryUtils | ||
|
||
### getCountry | ||
|
||
Fetches the country code based on Cloudflare's trace data or a fallback from cookies. | ||
This function attempts to retrieve the country location by first fetching trace data from Cloudflare | ||
and then falling back to the location stored in the cookies if the fetch fails. | ||
|
||
It returns a string representing the country code in lowercase. | ||
|
||
#### Example | ||
|
||
```js | ||
getCountry().then((country) => console.log(country)); | ||
``` |