Files
OpenFlare/frontend/app/(main)/websites/components/resolve-zone-domain-input.ts
T
ryan d615d85a26 refactor: remove unused remarks and add quick domain create
Drop remark fields from Zone, Zone domains, proxy routes, WAF rule
groups and IP groups across models, APIs, UI and DB columns (keep
certificate/origin remarks). Add quick-create domain input for short
labels, @ apex and full FQDNs when binding domains.
2026-07-12 16:16:56 +08:00

56 lines
1.6 KiB
TypeScript

/**
* Resolve user input into a full FQDN under a Zone root.
*
* - `@` → apex (zone root itself), e.g. `example.com`
* - single label `name` → `name.example.com`
* - full FQDN must equal the root or end with `.root`
*/
export function resolveZoneDomainInput(
rawInput: string,
zoneRoot: string,
): {domain: string; error?: string} {
const input = rawInput.trim().toLowerCase();
const root = zoneRoot.trim().toLowerCase();
if (!root) {
return {domain: '', error: '请先选择 Zone'};
}
if (!input) {
return {domain: '', error: '请输入域名'};
}
if (input.includes('*')) {
return {domain: '', error: 'Zone 域名不支持通配符'};
}
if (input.includes('://') || input.includes('/') || input.includes('?') || input.includes('#')) {
return {domain: '', error: '域名格式不合法'};
}
if (input === '@') {
return {domain: root};
}
// Short label: no dots → prefix under zone root
if (!input.includes('.')) {
if (!/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/i.test(input)) {
return {domain: '', error: '子域名称格式不合法'};
}
return {domain: `${input}.${root}`};
}
// Full FQDN
if (input === root || input.endsWith(`.${root}`)) {
return {domain: input};
}
return {domain: '', error: `域名必须属于 Zone ${root}`};
}
/** Live preview string for the input helper text. */
export function previewZoneDomainInput(rawInput: string, zoneRoot: string): string {
const resolved = resolveZoneDomainInput(rawInput, zoneRoot);
if (resolved.error || !resolved.domain) {
return '';
}
return resolved.domain;
}