chore: refactor domain parsing to be more general (#3448)

* change the api of domain parsing to just parseDomain and getCookieDomain
* adds getBaseDomain as the method to get the domain after any official subdomains
This commit is contained in:
Nan Yu
2022-05-31 18:48:23 -07:00
committed by GitHub
parent 876f788f59
commit 41e425756d
16 changed files with 216 additions and 237 deletions

View File

@@ -1,32 +1,32 @@
import env from "../env";
import { parseDomain } from "./domains";
const env = typeof window !== "undefined" ? window.env : process.env;
export function cdnPath(path: string): string {
return `${env.CDN_URL}${path}`;
}
// TODO: HACK: if this is called server-side, it will always return false.
// - The only call sites to this function and isExternalUrl are on the client
// - The reason this is in a shared util is because it's used in an editor plugin
// which is also in the shared code
export function isInternalUrl(href: string) {
// empty strings are never internal
if (href === "") {
return false;
}
// relative paths are always internal
if (href[0] === "/") {
return true;
}
const outline =
typeof window !== "undefined"
? parseDomain(window.location.href)
: undefined;
const parsed = parseDomain(href);
if (
parsed &&
outline &&
parsed.subdomain === outline.subdomain &&
parsed.domain === outline.domain &&
parsed.tld === outline.tld
) {
return true;
}
return false;
const domain = parseDomain(href);
return outline?.host === domain.host;
}
export function isExternalUrl(href: string) {