UNPKG

3.39 kBTypeScriptView Raw
1import { ValidationError, SanitizationResultValidIp } from "./sanitize";
2import { NO_HOSTNAME } from "./from-url";
3export declare const RESERVED_TOP_LEVEL_DOMAINS: string[];
4export declare type Label = string;
5export declare enum ParseResultType {
6 /**
7 * This parse result is returned in case the given hostname does not adhere to [RFC 1034](https://tools.ietf.org/html/rfc1034).
8 */
9 Invalid = "INVALID",
10 /**
11 * This parse result is returned if the given hostname was an IPv4 or IPv6.
12 */
13 Ip = "IP",
14 /**
15 * This parse result is returned when the given hostname
16 * - is the root domain (the empty string `""`)
17 * - belongs to the top-level domain `localhost`, `local`, `example`, `invalid` or `test`
18 */
19 Reserved = "RESERVED",
20 /**
21 * This parse result is returned when the given hostname is valid and does not belong to a reserved top-level domain, but is not listed in the public suffix list.
22 */
23 NotListed = "NOT_LISTED",
24 /**
25 * This parse result is returned when the given hostname belongs to a top-level domain that is listed in the public suffix list.
26 */
27 Listed = "LISTED"
28}
29declare type ParseResultCommon<Type extends ParseResultType> = {
30 /**
31 * The type of the parse result. Use switch or if to distinguish between different results.
32 */
33 type: Type;
34 /**
35 * The original hostname that was passed to parseDomain().
36 */
37 hostname: Type extends ParseResultType.Invalid ? string | typeof NO_HOSTNAME : string;
38};
39export declare type ParseResultInvalid = ParseResultCommon<ParseResultType.Invalid> & {
40 /**
41 * An array of validation errors.
42 */
43 errors: Array<ValidationError>;
44};
45declare type ParseResultCommonValidDomain = {
46 /**
47 * An array of labels that were separated by a dot character in the given hostname.
48 */
49 labels: Array<Label>;
50};
51export declare type ParseResultIp = ParseResultCommon<ParseResultType.Ip> & Pick<SanitizationResultValidIp, "ipVersion">;
52export declare type ParseResultReserved = ParseResultCommon<ParseResultType.Reserved> & ParseResultCommonValidDomain;
53export declare type ParseResultNotListed = ParseResultCommon<ParseResultType.NotListed> & ParseResultCommonValidDomain;
54declare type ParseResultListedDomains = {
55 /**
56 * An array of labels that belong to the subdomain. Can be empty if there was no subdomain in the given hostname.
57 */
58 subDomains: Array<Label>;
59 /**
60 * The first label that belongs to the user-controlled section of the hostname. Can be undefined if just a top-level domain was passed to parseDomain().
61 */
62 domain: Label | undefined;
63 /**
64 * An array of labels that are controlled by the domain registrar.
65 */
66 topLevelDomains: Array<Label>;
67};
68export declare type ParseResultListed = ParseResultCommon<ParseResultType.Listed> & ParseResultCommonValidDomain & ParseResultListedDomains & {
69 /**
70 * The parse result according to ICANN only without private top-level domains.
71 */
72 icann: ParseResultListedDomains;
73};
74export declare type ParseResult = ParseResultInvalid | ParseResultIp | ParseResultReserved | ParseResultNotListed | ParseResultListed;
75/**
76 * Splits the given hostname in topLevelDomains, a domain and subDomains.
77 */
78export declare const parseDomain: (hostname: string | typeof NO_HOSTNAME) => ParseResult;
79export {};