Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

refactor(url-utils): improve base domain validation logic to handle multi-part TLDs and update tests

Ademílson Tonato39a087e92384
diff --git a/apps/api/src/lib/__tests__/url-utils.test.ts b/apps/api/src/lib/__tests__/url-utils.test.ts
index 1c9ead0b6..84130ea42 100644
--- a/apps/api/src/lib/__tests__/url-utils.test.ts
+++ b/apps/api/src/lib/__tests__/url-utils.test.ts
@@ -5,6 +5,8 @@ describe("URL Utils", () => {
it("should return true for base domains", () => {
expect(isBaseDomain("https://example.com")).toBe(true);
expect(isBaseDomain("https://www.example.com")).toBe(true);
+ expect(isBaseDomain("https://example.co.uk")).toBe(true);
+ expect(isBaseDomain("https://www.example.co.uk")).toBe(true);
expect(isBaseDomain("http://example.com")).toBe(true);
expect(isBaseDomain("https://example.com/")).toBe(true);
});
diff --git a/apps/api/src/lib/url-utils.ts b/apps/api/src/lib/url-utils.ts
index d46ae24db..23735194e 100644
--- a/apps/api/src/lib/url-utils.ts
+++ b/apps/api/src/lib/url-utils.ts
@@ -13,24 +13,22 @@ export function isBaseDomain(url: string): boolean {
const urlObj = new URL(url);
const hostname = urlObj.hostname;
- // Remove www. prefix for consistency
- const cleanHostname = hostname.startsWith("www.")
- ? hostname.slice(4)
- : hostname;
-
- // Check if there are subdomains (more than 2 parts when split by dots)
- const parts = cleanHostname.split(".");
- if (parts.length > 2) {
- return false; // Has subdomains
- }
-
// Check if there's a path beyond the root
const pathname = urlObj.pathname;
if (pathname && pathname !== "/" && pathname.trim() !== "") {
return false; // Has a path
}
- return true;
+ // Remove www. prefix for consistency
+ const cleanHostname = hostname.startsWith("www.")
+ ? hostname.slice(4)
+ : hostname;
+
+ // Compare against extracted base domain (handles multi-part TLDs)
+ const baseDomain = extractBaseDomain(url);
+ if (!baseDomain) return false;
+
+ return cleanHostname === baseDomain;
} catch (error) {
// If URL parsing fails, consider it not a base domain
return false;