Skip to content
← Public packages

@kody/integrations-sh

Client for the public integrations.sh registry: search, detect, cached surface, and live discover.

src/domain.ts

22 lines · 757 B · TypeScript
const HOSTNAME_PATTERN =
	/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+$/

export function normalizeProviderDomain(domain: string): string {
	const normalized = domain.trim().toLowerCase()
	if (normalized.length === 0) {
		throw new Error('Provider domain is required')
	}
	if (/\s/.test(normalized)) {
		throw new Error('Provider domain must not contain whitespace')
	}
	if (/^[a-z][a-z0-9+.-]*:/i.test(normalized)) {
		throw new Error('Provider domain must not include a URL scheme')
	}
	if (normalized.includes('/')) {
		throw new Error('Provider domain must be a bare hostname without a path')
	}
	if (!HOSTNAME_PATTERN.test(normalized)) {
		throw new Error('Provider domain must look like a hostname')
	}
	return normalized
}