All files / lib domain-utils.ts

100% Statements 35/35
100% Branches 31/31
100% Functions 7/7
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153                                21x 16x       23x             32x             20x   12x             20x                             6x   2x   1x   1x   2x                                     8x 8x 2x   6x 2x   4x 2x   2x                         8x 8x 2x   6x 2x   4x                             10x 3x     7x 4x 4x           3x 3x   3x            
/**
 * Centralized Domain & URL Utilities
 *
 * Single source of truth for all domain, project name, and URL construction
 * across the pro website publish workflow.
 *
 * Pure functions — takes environment as parameter, no process.env reads.
 */
 
export type Environment = "local" | "development" | "dev" | "preview" | "production";
 
/**
 * Get the base domain for the given environment.
 * Production uses interioring.com; all other environments use decorrocket.com.
 */
export function getBaseDomain(env: Environment): string {
	if (env === "production") return "interioring.com";
	return "decorrocket.com";
}
 
/** @deprecated Use getBaseDomain(env) for environment-aware domain resolution */
export const BASE_DOMAIN = "decorrocket.com";
 
/**
 * Resolve an environment string into a typed Environment value.
 * Treats missing/unknown values as "local".
 */
export function resolveEnvironment(env?: string): Environment {
	if (
		env === "production" ||
		env === "preview" ||
		env === "dev" ||
		env === "development" ||
		env === "local"
	) {
		return env;
	}
	return "local";
}
 
/**
 * Check if the environment is a local/development environment.
 */
export function isLocalEnvironment(env: Environment): boolean {
	return env === "local" || env === "development";
}
 
/**
 * Get the Cloudflare Pages project name for a pro.
 *
 * | Environment | Pattern                  |
 * |-------------|--------------------------|
 * | local       | local-pro-{slug}      |
 * | development | local-pro-{slug}      |
 * | dev         | dev-pro-{slug}        |
 * | preview     | preview-pro-{slug}    |
 * | production  | pro-{slug}            |
 */
export function getProjectName(proSlug: string, env: Environment): string {
	switch (env) {
		case "production":
			return `pro-${proSlug}`;
		case "preview":
			return `preview-pro-${proSlug}`;
		case "dev":
			return `dev-pro-${proSlug}`;
		default:
			return `local-pro-${proSlug}`;
	}
}
 
/**
 * Get the custom domain for a published pro site.
 *
 * | Environment | Pattern                              |
 * |-------------|--------------------------------------|
 * | local       | local-{subdomain}.decorrocket.com    |
 * | development | local-{subdomain}.decorrocket.com    |
 * | dev         | dev-{subdomain}.decorrocket.com      |
 * | preview     | preview-{subdomain}.decorrocket.com  |
 * | production  | {subdomain}.interioring.com           |
 */
export function getPublishedDomain(
	subdomain: string,
	env: Environment,
): string | null {
	const domain = getBaseDomain(env);
	if (isLocalEnvironment(env)) {
		return `local-${subdomain}.${domain}`;
	}
	if (env === "dev") {
		return `dev-${subdomain}.${domain}`;
	}
	if (env === "preview") {
		return `preview-${subdomain}.${domain}`;
	}
	return `${subdomain}.${domain}`;
}
 
/**
 * Get the SSR preview hostname (the pro-sites Astro SSR app).
 *
 * | Environment | Domain                              |
 * |-------------|-------------------------------------|
 * | dev         | pro-sites-dev.decorrocket.com       |
 * | preview     | pro-sites-preview.decorrocket.com   |
 * | production  | pro-preview.interioring.com         |
 */
export function getPreviewDomain(env: Environment): string {
	const domain = getBaseDomain(env);
	if (env === "dev") {
		return `pro-sites-dev.${domain}`;
	}
	if (env === "preview") {
		return `pro-sites-preview.${domain}`;
	}
	return `pro-preview.${domain}`;
}
 
/**
 * Build both preview and published URLs for a pro website.
 *
 * @param subdomain - Pro's chosen subdomain (null if not set)
 * @param env - Current environment
 * @param opts.localBaseUrl - Base URL for local dev (default: http://localhost:7004)
 */
export function getProSiteUrls(
	subdomain: string | null,
	env: Environment,
	opts?: { localBaseUrl?: string },
): { previewUrl: string | null; publishedUrl: string | null } {
	if (!subdomain) {
		return { previewUrl: null, publishedUrl: null };
	}
 
	if (isLocalEnvironment(env)) {
		const base = opts?.localBaseUrl || "http://localhost:7004";
		return {
			previewUrl: `${base}/?pro=${subdomain}`,
			publishedUrl: `${base}/?pro=${subdomain}`,
		};
	}
 
	const previewDomain = getPreviewDomain(env);
	const publishedDomain = getPublishedDomain(subdomain, env);
 
	return {
		previewUrl: `https://${previewDomain}/?pro=${subdomain}`,
		/* v8 ignore next -- getPublishedDomain always returns a string; null branch is defensive */
		publishedUrl: publishedDomain ? `https://${publishedDomain}` : null,
	};
}