33 lines
1.5 KiB
TypeScript
33 lines
1.5 KiB
TypeScript
import type { DomainRoutingConfig } from "@hivegrid/config";
|
|
import { adminPath, appPath, type PathInput } from "./CanonicalUrlUtils";
|
|
|
|
export type PublicDomainRoutingConfig = Pick<DomainRoutingConfig, "protocol" | "webAppHost" | "adminAppHost" | "apiHost" | "appBaseDomain">;
|
|
|
|
function absoluteHostPath(host: string, path: string, protocol: DomainRoutingConfig["protocol"]) {
|
|
return `${protocol}://${host}${path}`;
|
|
}
|
|
|
|
export function absoluteAdminPath(routing: PublicDomainRoutingConfig | undefined, path?: PathInput) {
|
|
const targetPath = adminPath(path, { mode: "host-aware" });
|
|
return routing ? absoluteHostPath(routing.adminAppHost, targetPath, routing.protocol) : targetPath;
|
|
}
|
|
|
|
export function absoluteAppPath(routing: PublicDomainRoutingConfig | undefined, tenantSlug: string, path?: PathInput) {
|
|
const targetPath = appPath({ tenantSlug, path, mode: "host-aware" });
|
|
return routing ? absoluteHostPath(routing.webAppHost, targetPath, routing.protocol) : targetPath;
|
|
}
|
|
|
|
export function absoluteAppRootPath(routing: PublicDomainRoutingConfig | undefined) {
|
|
return routing ? absoluteHostPath(routing.webAppHost, "/", routing.protocol) : "/";
|
|
}
|
|
|
|
export function absolutePublicAppPath(routing: PublicDomainRoutingConfig | undefined, path: string) {
|
|
const normalizedPath = path.startsWith("/") ? path : `/${path}`;
|
|
const appRoot = absoluteAppRootPath(routing);
|
|
return isAbsoluteHttpUrl(appRoot) ? new URL(normalizedPath, appRoot).toString() : normalizedPath;
|
|
}
|
|
|
|
export function isAbsoluteHttpUrl(value: string) {
|
|
return /^https?:\/\//i.test(value);
|
|
}
|