import type { H3Event } from 'h3' const INVALID_SUBDOMAINS = new Set([ '0.0.0.0', 'www', 'api', 'admin', 'test', 'dev', 'development', 'staging', 'production', 'localhost', ]) const DEFAULT_TENANT = 'sistema' export function tenantFromHost(host: string | undefined): string { if (!host) return DEFAULT_TENANT const sub = host.split(':')[0]?.split('.')[0]?.toLowerCase() if (!sub || sub.length <= 1) return DEFAULT_TENANT if (INVALID_SUBDOMAINS.has(sub)) return DEFAULT_TENANT if (!/^[a-z0-9-]+$/.test(sub)) return DEFAULT_TENANT return sub } export function tenantFromEvent(event: H3Event): string { const host = getRequestHost(event, { xForwardedHost: true }) return tenantFromHost(host) }