43 lines
1.6 KiB
TypeScript
43 lines
1.6 KiB
TypeScript
import { ALLOWED_HOSTNAMES, RESERVED_SUBDOMAINS, WEBAPP_URL } from "@calcom/lib/constants";
|
|
|
|
/**
|
|
* return the org slug
|
|
* @param hostname
|
|
*/
|
|
export function getOrgSlug(hostname: string) {
|
|
if (!hostname.includes(".")) {
|
|
// A no-dot domain can never be org domain. It automatically handles localhost
|
|
return null;
|
|
}
|
|
// Find which hostname is being currently used
|
|
const currentHostname = ALLOWED_HOSTNAMES.find((ahn) => {
|
|
const url = new URL(WEBAPP_URL);
|
|
const testHostname = `${url.hostname}${url.port ? `:${url.port}` : ""}`;
|
|
return testHostname.endsWith(`.${ahn}`);
|
|
});
|
|
if (currentHostname) {
|
|
// Define which is the current domain/subdomain
|
|
const slug = hostname.replace(`.${currentHostname}` ?? "", "");
|
|
return slug.indexOf(".") === -1 ? slug : null;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function orgDomainConfig(hostname: string) {
|
|
const currentOrgDomain = getOrgSlug(hostname);
|
|
return {
|
|
currentOrgDomain,
|
|
isValidOrgDomain: currentOrgDomain !== null && !RESERVED_SUBDOMAINS.includes(currentOrgDomain),
|
|
};
|
|
}
|
|
|
|
export function subdomainSuffix() {
|
|
const urlSplit = WEBAPP_URL.replace("https://", "")?.replace("http://", "").split(".");
|
|
return urlSplit.length === 3 ? urlSplit.slice(1).join(".") : urlSplit.join(".");
|
|
}
|
|
|
|
export function getOrgFullDomain(slug: string, options: { protocol: boolean } = { protocol: true }) {
|
|
// TODO: It is a replacement for WEBAPP_URL and that doesn't have / in the end. Remove / after ensuring that it works reliably everywhere
|
|
return `${options.protocol ? `${new URL(WEBAPP_URL).protocol}//` : ""}${slug}.${subdomainSuffix()}/`;
|
|
}
|