Skip to content

Commit

Permalink
chore: exported the component and added jsdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
prince-deriv committed Nov 6, 2024
1 parent 19658ca commit 3af8a83
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/utils/country.utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import Cookies from "js-cookie";
import { cloudflareTrace } from "../constants/url.constants";

interface TraceData {
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);
Expand Down
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as URLUtils from "./url.utils";
import * as WebSocketUtils from "./websocket.utils";
import * as BrandUtils from "./brand.utils";
import * as OSDetectionUtils from "./os-detect.utils";
import * as CountryUtils from "./country.utils";

export {
ImageUtils,
Expand All @@ -18,4 +19,5 @@ export {
WebSocketUtils,
BrandUtils,
OSDetectionUtils,
CountryUtils,
};

0 comments on commit 3af8a83

Please sign in to comment.