From 8ff317b32d9e242aa0ff97b0b3af054a72d6bc85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Gerhard=20Lindesva=CC=88rd?= Date: Thu, 11 Apr 2024 11:37:51 +0200 Subject: [PATCH] clean up unused code --- apps/api/src/utils/parseReferrer.ts | 58 ----------------------------- apps/api/src/utils/url.ts | 48 ------------------------ 2 files changed, 106 deletions(-) delete mode 100644 apps/api/src/utils/parseReferrer.ts delete mode 100644 apps/api/src/utils/url.ts diff --git a/apps/api/src/utils/parseReferrer.ts b/apps/api/src/utils/parseReferrer.ts deleted file mode 100644 index adec1df9..00000000 --- a/apps/api/src/utils/parseReferrer.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { stripTrailingSlash } from '@openpanel/common'; - -import referrers from '../referrers'; - -function getHostname(url: string | undefined) { - if (!url) { - return ''; - } - - try { - return new URL(url).hostname; - } catch (e) { - return ''; - } -} - -export function parseReferrer(url: string | undefined) { - const hostname = getHostname(url); - const match = referrers[hostname] ?? referrers[hostname.replace('www.', '')]; - - return { - name: match?.name ?? '', - type: match?.type ?? 'unknown', - url: stripTrailingSlash(url ?? ''), - }; -} - -export function getReferrerWithQuery( - query: Record | undefined -) { - if (!query) { - return null; - } - - const source = query.utm_source ?? query.ref ?? query.utm_referrer ?? ''; - - if (source === '') { - return null; - } - - const match = Object.values(referrers).find( - (referrer) => referrer.name.toLowerCase() === source.toLowerCase() - ); - - if (match) { - return { - name: match.name, - type: match.type, - url: '', - }; - } - - return { - name: source, - type: 'unknown', - url: '', - }; -} diff --git a/apps/api/src/utils/url.ts b/apps/api/src/utils/url.ts deleted file mode 100644 index c6d3723d..00000000 --- a/apps/api/src/utils/url.ts +++ /dev/null @@ -1,48 +0,0 @@ -export function parseSearchParams( - params: URLSearchParams -): Record | undefined { - const result: Record = {}; - for (const [key, value] of params.entries()) { - result[key] = value; - } - return Object.keys(result).length ? result : undefined; -} - -export function parsePath(path?: string): { - query?: Record; - path: string; - hash?: string; -} { - if (!path) { - return { - path: '', - }; - } - - try { - const url = new URL(path); - return { - query: parseSearchParams(url.searchParams), - path: url.pathname, - hash: url.hash || undefined, - }; - } catch (error) { - return { - path, - }; - } -} - -export function isSameDomain( - url1: string | undefined, - url2: string | undefined -) { - if (!url1 || !url2) { - return false; - } - try { - return new URL(url1).hostname === new URL(url2).hostname; - } catch (e) { - return false; - } -}