diggy 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,49 @@
1
- import type { AnyDNSRecord, BuildInDNSResolver, DNSResolver } from "./types";
1
+ import { type BuildInDNSResolver } from "./get-resolver";
2
+ import type { DNSResolver } from "./resolvers/DNSResolver";
3
+ import type { AnyDNSRecord } from "./types";
4
+ /**
5
+ * Fetches DNS records for a given `host` and type. When no `type` is specified,
6
+ * it retrieves all available DNS records for the host.
7
+ *
8
+ * @example Fetch all DNS records for a host
9
+ * ```ts
10
+ * import { getDnsRecords } from "diggy";
11
+ * const records = await getDnsRecords("example.com");
12
+ * console.log(records);
13
+ * ```
14
+ *
15
+ * @example Get A records for a host
16
+ * ```ts
17
+ * import { getDnsRecords } from "diggy";
18
+ * const aRecords = await getDnsRecords("example.com", "A");
19
+ * console.log(aRecords);
20
+ * ```
21
+ *
22
+ * @example Change the DNS resolver (built-in resolvers)
23
+ * ```ts
24
+ * import { getDnsRecords } from "diggy";
25
+ * const records = await getDnsRecords("example.com", "A", "google");
26
+ * console.log(records);
27
+ * ```
28
+ *
29
+ * @example Use Node.js DNS resolver
30
+ * ```ts
31
+ * import { getDnsRecords } from "diggy";
32
+ * const records = await getDnsRecords("example.com", "A", "nodejs");
33
+ * console.log(records);
34
+ * ```
35
+ *
36
+ * @example Use a custom DNS resolver
37
+ * ```ts
38
+ * import { getDnsRecords } from "diggy";
39
+ * import { customResolver } from "./my-custom-resolver";
40
+ * const records = await getDnsRecords("example.com", "A", customResolver);
41
+ * console.log(records);
42
+ * ```
43
+ *
44
+ * @param host - The domain or host for which to fetch DNS records e.g. "example.com".
45
+ * @param type - The type of DNS record to fetch (e.g., "A", "AAAA", "MX"). If not specified, all records will be fetched.
46
+ * @param resolver - The DNS resolver to use. It can be a built-in resolver name (like "google" or "cloudflare"), a custom resolver function, or a string URL for a DoH resolver.
47
+ * @group Main Functions
48
+ */
2
49
  export declare function getDnsRecords(host: string, type?: string, resolver?: string | BuildInDNSResolver | DNSResolver): Promise<AnyDNSRecord[]>;
@@ -1,6 +1,51 @@
1
1
  import { getResolver } from "./get-resolver";
2
2
  import { resolveAllRecords } from "./utils/resolve-all-records";
3
3
  import { toDnsType } from "./utils/to-dns-type";
4
+ /**
5
+ * Fetches DNS records for a given `host` and type. When no `type` is specified,
6
+ * it retrieves all available DNS records for the host.
7
+ *
8
+ * @example Fetch all DNS records for a host
9
+ * ```ts
10
+ * import { getDnsRecords } from "diggy";
11
+ * const records = await getDnsRecords("example.com");
12
+ * console.log(records);
13
+ * ```
14
+ *
15
+ * @example Get A records for a host
16
+ * ```ts
17
+ * import { getDnsRecords } from "diggy";
18
+ * const aRecords = await getDnsRecords("example.com", "A");
19
+ * console.log(aRecords);
20
+ * ```
21
+ *
22
+ * @example Change the DNS resolver (built-in resolvers)
23
+ * ```ts
24
+ * import { getDnsRecords } from "diggy";
25
+ * const records = await getDnsRecords("example.com", "A", "google");
26
+ * console.log(records);
27
+ * ```
28
+ *
29
+ * @example Use Node.js DNS resolver
30
+ * ```ts
31
+ * import { getDnsRecords } from "diggy";
32
+ * const records = await getDnsRecords("example.com", "A", "nodejs");
33
+ * console.log(records);
34
+ * ```
35
+ *
36
+ * @example Use a custom DNS resolver
37
+ * ```ts
38
+ * import { getDnsRecords } from "diggy";
39
+ * import { customResolver } from "./my-custom-resolver";
40
+ * const records = await getDnsRecords("example.com", "A", customResolver);
41
+ * console.log(records);
42
+ * ```
43
+ *
44
+ * @param host - The domain or host for which to fetch DNS records e.g. "example.com".
45
+ * @param type - The type of DNS record to fetch (e.g., "A", "AAAA", "MX"). If not specified, all records will be fetched.
46
+ * @param resolver - The DNS resolver to use. It can be a built-in resolver name (like "google" or "cloudflare"), a custom resolver function, or a string URL for a DoH resolver.
47
+ * @group Main Functions
48
+ */
4
49
  export function getDnsRecords(host, type, resolver) {
5
50
  const dnsResolver = getResolver(resolver);
6
51
  const dnsType = toDnsType(type);
@@ -1,2 +1,35 @@
1
- import type { BuildInDNSResolver, DNSResolver } from "./types";
1
+ import type { DNSResolver } from "./resolvers/DNSResolver";
2
+ /**
3
+ * Represents the built-in DNS resolvers available in the library.
4
+ * @enum {string}
5
+ * @property {string} nodejs - Uses Node.js's built-in DNS resolver.
6
+ * @property {string} dig - Uses the `dig` command-line tool for DNS resolution.
7
+ * @property {string} google - Uses Google's DNS over HTTPS (DoH) resolver.
8
+ * @property {string} cloudflare - Uses Cloudflare's DNS over HTTPS (DoH) resolver.
9
+ * @group Resolvers
10
+ */
11
+ export declare enum BuildInDNSResolver {
12
+ nodejs = "nodejs",
13
+ dig = "dig",
14
+ google = "google",
15
+ cloudflare = "cloudflare"
16
+ }
17
+ /**
18
+ * Returns a DNS resolver function based on the provided resolver name or function.
19
+ *
20
+ * If a `string` is provided, it must match one of the built-in resolvers.
21
+ * If a `function` is provided, it will be returned as is.
22
+ * If no resolver is provided, the default resolver (Google's DoH) will be used
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * import { getResolver } from "diggy";
27
+ * const resolver = getResolver("google"); // Returns the Google DoH resolver
28
+ * ```
29
+ *
30
+ *
31
+ * @param {string | BuildInDNSResolver | DNSResolver} [resolver] - The name of the built-in resolver or a custom resolver function.
32
+ * @return {DNSResolver} The DNS resolver function.
33
+ * @group Main Functions
34
+ */
2
35
  export declare function getResolver(resolver?: string | BuildInDNSResolver | DNSResolver): DNSResolver;
@@ -1,12 +1,46 @@
1
1
  import { digResolver } from "./resolvers/dig-resolver";
2
2
  import { dohResolver } from "./resolvers/doh-resolver";
3
3
  import { nodeResolver } from "./resolvers/node-resolver";
4
+ /**
5
+ * Represents the built-in DNS resolvers available in the library.
6
+ * @enum {string}
7
+ * @property {string} nodejs - Uses Node.js's built-in DNS resolver.
8
+ * @property {string} dig - Uses the `dig` command-line tool for DNS resolution.
9
+ * @property {string} google - Uses Google's DNS over HTTPS (DoH) resolver.
10
+ * @property {string} cloudflare - Uses Cloudflare's DNS over HTTPS (DoH) resolver.
11
+ * @group Resolvers
12
+ */
13
+ export var BuildInDNSResolver;
14
+ (function (BuildInDNSResolver) {
15
+ BuildInDNSResolver["nodejs"] = "nodejs";
16
+ BuildInDNSResolver["dig"] = "dig";
17
+ BuildInDNSResolver["google"] = "google";
18
+ BuildInDNSResolver["cloudflare"] = "cloudflare";
19
+ })(BuildInDNSResolver || (BuildInDNSResolver = {}));
4
20
  const resolvers = {
5
21
  nodejs: nodeResolver(),
6
22
  dig: digResolver(),
7
23
  google: dohResolver("https://dns.google/resolve"),
8
24
  cloudflare: dohResolver("https://cloudflare-dns.com/dns-query"),
9
25
  };
26
+ /**
27
+ * Returns a DNS resolver function based on the provided resolver name or function.
28
+ *
29
+ * If a `string` is provided, it must match one of the built-in resolvers.
30
+ * If a `function` is provided, it will be returned as is.
31
+ * If no resolver is provided, the default resolver (Google's DoH) will be used
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * import { getResolver } from "diggy";
36
+ * const resolver = getResolver("google"); // Returns the Google DoH resolver
37
+ * ```
38
+ *
39
+ *
40
+ * @param {string | BuildInDNSResolver | DNSResolver} [resolver] - The name of the built-in resolver or a custom resolver function.
41
+ * @return {DNSResolver} The DNS resolver function.
42
+ * @group Main Functions
43
+ */
10
44
  export function getResolver(resolver) {
11
45
  if (typeof resolver === "function") {
12
46
  return resolver;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,11 @@
1
1
  export * from "./get-dns-records";
2
2
  export * from "./get-resolver";
3
+ export * from "./resolvers/DNSResolver";
4
+ export * from "./resolvers/dig-resolver";
5
+ export * from "./resolvers/doh-resolver";
6
+ export * from "./resolvers/dot-resolver";
7
+ export * from "./resolvers/node-resolver";
3
8
  export * from "./types";
9
+ export * from "./utils/resolve-all-records";
10
+ export * from "./utils/to-dns-record";
11
+ export * from "./utils/to-dns-type";
package/dist/index.js CHANGED
@@ -1,3 +1,11 @@
1
1
  export * from "./get-dns-records";
2
2
  export * from "./get-resolver";
3
+ export * from "./resolvers/DNSResolver";
4
+ export * from "./resolvers/dig-resolver";
5
+ export * from "./resolvers/doh-resolver";
6
+ export * from "./resolvers/dot-resolver";
7
+ export * from "./resolvers/node-resolver";
3
8
  export * from "./types";
9
+ export * from "./utils/resolve-all-records";
10
+ export * from "./utils/to-dns-record";
11
+ export * from "./utils/to-dns-type";
@@ -0,0 +1,11 @@
1
+ import type { AnyDNSRecord, DNSRecordType } from "../types";
2
+ /**
3
+ * DNSResolver is a function type that takes a host and a DNS record type,
4
+ * and returns a promise that resolves to an array of DNS records.
5
+ * It is used to fetch DNS records for a given host and type.
6
+ *
7
+ * @param host - The domain or host for which to fetch DNS records (e.g., "example.com").
8
+ * @param type - The type of DNS record to fetch (e.g., "A", "AAAA", "MX").
9
+ * @group Resolvers
10
+ */
11
+ export type DNSResolver = (host: string, type: DNSRecordType) => Promise<AnyDNSRecord[]>;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,2 +1,24 @@
1
- import type { DNSResolver } from "../types";
1
+ import type { DNSResolver } from "./DNSResolver";
2
+ /**
3
+ * Returns a DNS resolver that uses the `dig` command to resolve DNS records.
4
+ *
5
+ * ```ts
6
+ * import { digResolver } from "diggy";
7
+ * const resolver = digResolver();
8
+ * const records = await resolver("example.com", "A");
9
+ * console.log(records);
10
+ * ```
11
+ *
12
+ * You can also specify the DNS server to use by passing it as an argument:
13
+ *
14
+ * ```ts
15
+ * import { digResolver } from "diggy";
16
+ * const resolver = digResolver("1.1.1.1");
17
+ * const records = await resolver("example.com", "A");
18
+ * console.log(records);
19
+ *```
20
+ *
21
+ * @param server - The DNS server to use (optional). If not provided, the default system resolver will be used.
22
+ * @group Resolvers
23
+ */
2
24
  export declare function digResolver(server?: string): DNSResolver;
@@ -1,4 +1,26 @@
1
1
  import { toDnsRecord } from "../utils/to-dns-record";
2
+ /**
3
+ * Returns a DNS resolver that uses the `dig` command to resolve DNS records.
4
+ *
5
+ * ```ts
6
+ * import { digResolver } from "diggy";
7
+ * const resolver = digResolver();
8
+ * const records = await resolver("example.com", "A");
9
+ * console.log(records);
10
+ * ```
11
+ *
12
+ * You can also specify the DNS server to use by passing it as an argument:
13
+ *
14
+ * ```ts
15
+ * import { digResolver } from "diggy";
16
+ * const resolver = digResolver("1.1.1.1");
17
+ * const records = await resolver("example.com", "A");
18
+ * console.log(records);
19
+ *```
20
+ *
21
+ * @param server - The DNS server to use (optional). If not provided, the default system resolver will be used.
22
+ * @group Resolvers
23
+ */
2
24
  export function digResolver(server) {
3
25
  return async (host, type) => {
4
26
  const args = [];
@@ -1,2 +1,17 @@
1
- import type { DNSResolver } from "../types";
1
+ import type { DNSResolver } from "./DNSResolver";
2
+ /**
3
+ * Returns a DNS resolver that uses DNS over HTTPS (DoH) to resolve DNS records.
4
+ *
5
+ * ```ts
6
+ * import { dohResolver } from "diggy";
7
+ * const resolver = dohResolver("https://dns.google/resolve");
8
+ * const records = await resolver("example.com", "A");
9
+ * console.log(records);
10
+ * ```
11
+ *
12
+ * > 💡 **Tip:** Find more public [DoH endpoints here](https://github.com/curl/curl/wiki/DNS-over-HTTPS)
13
+ *
14
+ * @param url - The URL of the DoH server to use (e.g., `https://dns.google/resolve`).
15
+ * @group Resolvers
16
+ */
2
17
  export declare function dohResolver(url: string): DNSResolver;
@@ -1,21 +1,19 @@
1
1
  import { toDnsRecord } from "../utils/to-dns-record";
2
- const DNSTypeNumbers = new Map([
3
- [1, "A"],
4
- [2, "NS"],
5
- [5, "CNAME"],
6
- [6, "SOA"],
7
- [12, "PTR"],
8
- [15, "MX"],
9
- [16, "TXT"],
10
- [24, "SIG"],
11
- [25, "KEY"],
12
- [28, "AAAA"],
13
- [33, "SRV"],
14
- [35, "NAPTR"],
15
- [43, "DS"],
16
- [48, "DNSKEY"],
17
- [257, "CAA"],
18
- ]);
2
+ /**
3
+ * Returns a DNS resolver that uses DNS over HTTPS (DoH) to resolve DNS records.
4
+ *
5
+ * ```ts
6
+ * import { dohResolver } from "diggy";
7
+ * const resolver = dohResolver("https://dns.google/resolve");
8
+ * const records = await resolver("example.com", "A");
9
+ * console.log(records);
10
+ * ```
11
+ *
12
+ * > 💡 **Tip:** Find more public [DoH endpoints here](https://github.com/curl/curl/wiki/DNS-over-HTTPS)
13
+ *
14
+ * @param url - The URL of the DoH server to use (e.g., `https://dns.google/resolve`).
15
+ * @group Resolvers
16
+ */
19
17
  export function dohResolver(url) {
20
18
  const dnsUrl = new URL(url);
21
19
  return async (host, type) => {
@@ -40,3 +38,20 @@ export function dohResolver(url) {
40
38
  });
41
39
  };
42
40
  }
41
+ const DNSTypeNumbers = new Map([
42
+ [1, "A"],
43
+ [2, "NS"],
44
+ [5, "CNAME"],
45
+ [6, "SOA"],
46
+ [12, "PTR"],
47
+ [15, "MX"],
48
+ [16, "TXT"],
49
+ [24, "SIG"],
50
+ [25, "KEY"],
51
+ [28, "AAAA"],
52
+ [33, "SRV"],
53
+ [35, "NAPTR"],
54
+ [43, "DS"],
55
+ [48, "DNSKEY"],
56
+ [257, "CAA"],
57
+ ]);
@@ -1,2 +1,26 @@
1
- import { type DNSResolver } from "../types";
1
+ import type { DNSResolver } from "./DNSResolver";
2
+ /**
3
+ * Returns a DNS resolver that uses Node.js's built-in DNS module.
4
+ *
5
+ * @example
6
+ * Without any additional configuration, it will use the system's default DNS servers.
7
+ *
8
+ * ```ts
9
+ * import { nodeResolver } from "diggy";
10
+ * const resolver = nodeResolver();
11
+ * const records = await resolver("example.com", "A");
12
+ * ```
13
+ *
14
+ * @example
15
+ * You can specify a list of DNS servers to use.
16
+ *
17
+ * ```ts
18
+ * import { nodeResolver } from "diggy";
19
+ * const resolver = nodeResolver(["8.8.8.8", "1.1.1.1"]);
20
+ * const records = await resolver("example.com", "A");
21
+ * ```
22
+ *
23
+ * @param servers - An array of DNS server addresses to use. If empty, the system's default DNS servers will be used.
24
+ * @group Resolvers
25
+ */
2
26
  export declare function nodeResolver(servers?: string[]): DNSResolver;
@@ -1,5 +1,29 @@
1
1
  import * as _dns from "node:dns/promises";
2
2
  import { DNSRecordType } from "../types";
3
+ /**
4
+ * Returns a DNS resolver that uses Node.js's built-in DNS module.
5
+ *
6
+ * @example
7
+ * Without any additional configuration, it will use the system's default DNS servers.
8
+ *
9
+ * ```ts
10
+ * import { nodeResolver } from "diggy";
11
+ * const resolver = nodeResolver();
12
+ * const records = await resolver("example.com", "A");
13
+ * ```
14
+ *
15
+ * @example
16
+ * You can specify a list of DNS servers to use.
17
+ *
18
+ * ```ts
19
+ * import { nodeResolver } from "diggy";
20
+ * const resolver = nodeResolver(["8.8.8.8", "1.1.1.1"]);
21
+ * const records = await resolver("example.com", "A");
22
+ * ```
23
+ *
24
+ * @param servers - An array of DNS server addresses to use. If empty, the system's default DNS servers will be used.
25
+ * @group Resolvers
26
+ */
3
27
  export function nodeResolver(servers = []) {
4
28
  const dns = new _dns.Resolver();
5
29
  if (servers.length > 0) {
package/dist/types.d.ts CHANGED
@@ -1,5 +1,21 @@
1
- export type DNSResolver = (host: string, type: DNSRecordType) => Promise<AnyDNSRecord[]>;
2
- export type BuildInDNSResolver = "nodejs" | "dig" | "google" | "cloudflare";
1
+ /**
2
+ * Represents the type of DNS records that can be queried.
3
+ *
4
+ * @enum {string}
5
+ * @property {string} A - IPv4 address record.
6
+ * @property {string} AAAA - IPv6 address record.
7
+ * @property {string} CAA - Certification Authority Authorization record.
8
+ * @property {string} CNAME - Canonical Name record, used for aliasing one domain to another.
9
+ * @property {string} NAPTR - Naming Authority Pointer record, used for service discovery.
10
+ * @property {string} MX - Mail Exchange record, used for email routing.
11
+ * @property {string} NS - Name Server record, indicating the authoritative DNS servers for a domain.
12
+ * @property {string} PTR - Pointer record, used for reverse DNS lookups.
13
+ * @property {string} SOA - Start of Authority record, providing information about the DNS zone.
14
+ * @property {string} SRV - Service record, used to define the location of services.
15
+ * @property {string} TXT - Text record, used for arbitrary text data, often for verification purposes.
16
+ *
17
+ * @group DNS Records
18
+ */
3
19
  export declare enum DNSRecordType {
4
20
  A = "A",
5
21
  AAAA = "AAAA",
@@ -13,10 +29,27 @@ export declare enum DNSRecordType {
13
29
  SRV = "SRV",
14
30
  TXT = "TXT"
15
31
  }
32
+ /**
33
+ * Represents the data for a DNS MX (Mail Exchange) record.
34
+ * @property exchange - The mail server that will handle emails for the domain.
35
+ * @property priority - The priority of the mail server, where lower values indicate higher priority.
36
+ * @group DNS Records
37
+ */
16
38
  export type MxRecordData = {
17
39
  exchange: string;
18
40
  priority: number;
19
41
  };
42
+ /**
43
+ * Represents the data for a DNS SOA (Start of Authority) record.
44
+ * @property nsname - The name server for the domain.
45
+ * @property hostmaster - The email address of the domain administrator (formatted as a string with a dot instead of an '@').
46
+ * @property serial - The serial number of the SOA record.
47
+ * @property refresh - The time interval (in seconds) before the zone should be refreshed.
48
+ * @property retry - The time interval (in seconds) before a retry should be attempted if the refresh fails.
49
+ * @property expire - The time interval (in seconds) before the zone expires if it cannot be refreshed.
50
+ * @property minttl - The minimum time-to-live (TTL) for the zone, indicating how long the record should be cached by resolvers.
51
+ * @group DNS Records
52
+ */
20
53
  export type SoaRecordData = {
21
54
  nsname: string;
22
55
  hostmaster: string;
@@ -26,11 +59,28 @@ export type SoaRecordData = {
26
59
  expire: number;
27
60
  minttl: number;
28
61
  };
62
+ /**
63
+ * Represents the data for a DNS CAA (Certification Authority Authorization) record.
64
+ * @property flags - The flags for the CAA record, indicating whether the record is critical or not.
65
+ * @property tag - The tag for the CAA record, indicating the type of authorization (e.g., "issue", "issuewild", "iodef").
66
+ * @property value - The value for the CAA record, which specifies the authorized certificate authority or other information.
67
+ * @group DNS Records
68
+ */
29
69
  export type CaaRecordData = {
30
70
  flags: number;
31
71
  tag: string;
32
72
  value: string;
33
73
  };
74
+ /**
75
+ * Represents the data for a DNS NAPTR (Naming Authority Pointer) record.
76
+ * @property order - The order of the NAPTR record, used to determine the sequence in which records should be processed.
77
+ * @property preference - The preference of the NAPTR record, used to determine the order of preference among multiple records.
78
+ * @property flags - The flags for the NAPTR record, which can indicate various properties (e.g., "U" for URI, "S" for service).
79
+ * @property service - The service type for the NAPTR record, indicating the type of service provided (e.g., "SIP+D2U").
80
+ * @property regexp - The regular expression for the NAPTR record, which can be used to transform the domain name.
81
+ * @property replacement - The replacement string for the NAPTR record, which can be used to replace the domain name.
82
+ * @group DNS Records
83
+ */
34
84
  export type NaptrRecordData = {
35
85
  order: number;
36
86
  preference: number;
@@ -39,16 +89,32 @@ export type NaptrRecordData = {
39
89
  regexp: string;
40
90
  replacement: string;
41
91
  };
92
+ /**
93
+ * Represents the data for a DNS SRV (Service) record.
94
+ * @property priority - The priority of the SRV record, used to determine the order in which services should be used.
95
+ * @property weight - The weight of the SRV record, used to distribute traffic among multiple services with the same priority.
96
+ * @property port - The port number on which the service is running.
97
+ * @property name - The target domain name of the service, which can be a hostname or an IP address.
98
+ * @group DNS Records
99
+ */
42
100
  export type SrvRecordData = {
43
101
  priority: number;
44
102
  weight: number;
45
103
  port: number;
46
104
  name: string;
47
105
  };
106
+ /**
107
+ * @group DNS Records
108
+ */
48
109
  export type DNSRecord<T = string | string[]> = {
49
110
  name: string;
50
111
  type: DNSRecordType;
51
112
  ttl?: number;
52
113
  data: T;
53
114
  };
115
+ /**
116
+ * Represents any DNS record type, including A, AAAA, CAA, CNAME, NAPTR, MX, NS, PTR, SOA, SRV, and TXT records.
117
+ * This type is a union of the generic DNSRecord type and specific record types like MxRecordData.
118
+ * @group DNS Records
119
+ */
54
120
  export type AnyDNSRecord = DNSRecord | DNSRecord<MxRecordData> | DNSRecord<SoaRecordData> | DNSRecord<CaaRecordData> | DNSRecord<NaptrRecordData> | DNSRecord<SrvRecordData>;
package/dist/types.js CHANGED
@@ -1,3 +1,21 @@
1
+ /**
2
+ * Represents the type of DNS records that can be queried.
3
+ *
4
+ * @enum {string}
5
+ * @property {string} A - IPv4 address record.
6
+ * @property {string} AAAA - IPv6 address record.
7
+ * @property {string} CAA - Certification Authority Authorization record.
8
+ * @property {string} CNAME - Canonical Name record, used for aliasing one domain to another.
9
+ * @property {string} NAPTR - Naming Authority Pointer record, used for service discovery.
10
+ * @property {string} MX - Mail Exchange record, used for email routing.
11
+ * @property {string} NS - Name Server record, indicating the authoritative DNS servers for a domain.
12
+ * @property {string} PTR - Pointer record, used for reverse DNS lookups.
13
+ * @property {string} SOA - Start of Authority record, providing information about the DNS zone.
14
+ * @property {string} SRV - Service record, used to define the location of services.
15
+ * @property {string} TXT - Text record, used for arbitrary text data, often for verification purposes.
16
+ *
17
+ * @group DNS Records
18
+ */
1
19
  export var DNSRecordType;
2
20
  (function (DNSRecordType) {
3
21
  DNSRecordType["A"] = "A";
@@ -1,2 +1,12 @@
1
- import { type AnyDNSRecord, type DNSResolver } from "../types";
1
+ import type { DNSResolver } from "../resolvers/DNSResolver";
2
+ import { type AnyDNSRecord } from "../types";
3
+ /**
4
+ * Resolves all DNS records for a given host using the specified resolver.
5
+ * This function attempts to resolve all types of DNS records defined in `DNSRecordType`.
6
+ * If a record type cannot be resolved, it will be skipped.
7
+ *
8
+ * @param host - The domain or host for which to resolve DNS records (e.g., "example.com").
9
+ * @param resolver - The DNS resolver function to use. It should accept a host and a DNS record type,
10
+ * @group Utilities
11
+ */
2
12
  export declare function resolveAllRecords(host: string, resolver: DNSResolver): Promise<AnyDNSRecord[]>;
@@ -1,4 +1,13 @@
1
1
  import { DNSRecordType } from "../types";
2
+ /**
3
+ * Resolves all DNS records for a given host using the specified resolver.
4
+ * This function attempts to resolve all types of DNS records defined in `DNSRecordType`.
5
+ * If a record type cannot be resolved, it will be skipped.
6
+ *
7
+ * @param host - The domain or host for which to resolve DNS records (e.g., "example.com").
8
+ * @param resolver - The DNS resolver function to use. It should accept a host and a DNS record type,
9
+ * @group Utilities
10
+ */
2
11
  export async function resolveAllRecords(host, resolver) {
3
12
  const allTypes = Object.values(DNSRecordType);
4
13
  const records = await Promise.allSettled(allTypes.map((t) => resolver(host, t)));
@@ -1,5 +1,16 @@
1
1
  import { type AnyDNSRecord } from "../types";
2
2
  type InputData = string | string[];
3
+ /**
4
+ * Converts input data to a DNS record format.
5
+ * This function takes a name, type, optional TTL, and data,
6
+ * and returns an object representing a DNS record.
7
+ *
8
+ * @param name - The name of the DNS record, e.g., "example.com".
9
+ * @param type - The type of DNS record, e.g., "A", "AAAA", "MX", etc.
10
+ * @param ttl - The time-to-live (TTL) for the DNS record, in seconds. Defaults to 0 if not provided.
11
+ * @param data - The data associated with the DNS record, which can vary based on the type.
12
+ * @group Utilities
13
+ */
3
14
  export declare function toDnsRecord({ name, type, ttl, data, }: {
4
15
  name: string;
5
16
  type: string;
@@ -39,6 +39,17 @@ function convertDataByType(type, data) {
39
39
  }
40
40
  return Array.isArray(data) ? data.join(" ") : data;
41
41
  }
42
+ /**
43
+ * Converts input data to a DNS record format.
44
+ * This function takes a name, type, optional TTL, and data,
45
+ * and returns an object representing a DNS record.
46
+ *
47
+ * @param name - The name of the DNS record, e.g., "example.com".
48
+ * @param type - The type of DNS record, e.g., "A", "AAAA", "MX", etc.
49
+ * @param ttl - The time-to-live (TTL) for the DNS record, in seconds. Defaults to 0 if not provided.
50
+ * @param data - The data associated with the DNS record, which can vary based on the type.
51
+ * @group Utilities
52
+ */
42
53
  export function toDnsRecord({ name, type, ttl, data, }) {
43
54
  return {
44
55
  // remove trailing dot from name
@@ -1,2 +1,16 @@
1
1
  import { DNSRecordType } from "../types";
2
+ /**
3
+ * Converts a string representation of a DNS record type to the corresponding `DNSRecordType` enum value.
4
+ * If the input string does not match any valid DNS record type, it returns `undefined`.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { toDnsType } from "diggy";
9
+ * console.log(toDnsType("A")); // Output: "A"
10
+ * console.log(toDnsType("MX")); // Output: "MX"
11
+ * console.log(toDnsType("INVALID")); // Output: undefined
12
+ * ```
13
+ * @param type - The string representation of the DNS record type (e.g., "A", "AAAA", "MX").
14
+ * @group Utilities
15
+ */
2
16
  export declare function toDnsType(type?: string): DNSRecordType | undefined;
@@ -1,4 +1,18 @@
1
1
  import { DNSRecordType } from "../types";
2
+ /**
3
+ * Converts a string representation of a DNS record type to the corresponding `DNSRecordType` enum value.
4
+ * If the input string does not match any valid DNS record type, it returns `undefined`.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { toDnsType } from "diggy";
9
+ * console.log(toDnsType("A")); // Output: "A"
10
+ * console.log(toDnsType("MX")); // Output: "MX"
11
+ * console.log(toDnsType("INVALID")); // Output: undefined
12
+ * ```
13
+ * @param type - The string representation of the DNS record type (e.g., "A", "AAAA", "MX").
14
+ * @group Utilities
15
+ */
2
16
  export function toDnsType(type) {
3
17
  if (!type)
4
18
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "diggy",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Multi-backend DNS resolver for Node.js/Browser — supports dig, DNS over HTTPS, and native Node.js DNS.",
5
5
  "repository": "git@github.com:OzzyCzech/diggy.git",
6
6
  "author": "Roman Ožana <roman@ozana.cz>",
@@ -39,13 +39,15 @@
39
39
  "prepare": "npm run build",
40
40
  "test": "tsc --noEmit && vitest",
41
41
  "format": "biome check --write .",
42
- "format:check": "biome check ."
42
+ "format:check": "biome check .",
43
+ "docs": "typedoc"
43
44
  },
44
45
  "devDependencies": {
45
46
  "@biomejs/biome": "^2.1.4",
46
47
  "@types/node": "^24.2.1",
47
48
  "np": "^10.2.0",
48
49
  "tsx": "^4.20.4",
50
+ "typedoc": "^0.28.10",
49
51
  "typescript": "^5.9.2",
50
52
  "vitest": "^3.2.4"
51
53
  }
package/readme.md CHANGED
@@ -60,15 +60,14 @@ const mxRecords = await getDnsRecords('example.com', 'MX');
60
60
 
61
61
  ```typescript
62
62
  import { type AnyDNSRecord, getDnsRecords } from 'diggy';
63
-
64
63
  const records: AnyDNSRecord[] = await getDnsRecords('example.com', 'A');
65
64
  ```
66
65
 
67
66
  ```typescript
68
67
  function getDnsRecords(
69
- host: string,
70
- type?: string,
71
- resolver?: string | BuildInDNSResolver | DNSResolver,
68
+ host: string,
69
+ type?: string,
70
+ resolver?: string | BuildInDNSResolver | DNSResolver,
72
71
  ): Promise<AnyDNSRecord[]>
73
72
  ```
74
73
 
@@ -78,9 +77,9 @@ function getDnsRecords(
78
77
  - `type` (string, optional): DNS record type (A, AAAA, MX, TXT, etc.). If omitted, returns all available records
79
78
  - `resolver` (string | BuildInDNSResolver | DNSResolver, optional): DNS resolver to use
80
79
 
81
- **Returns:** Promise resolving to an array of DNS records
80
+ **Returns:** Promise resolving to an array of DNS records, see [Response format](#response-format) for details.
82
81
 
83
- ## 🌍 Browser Usage
82
+ ### 🌍 Browser Usage
84
83
 
85
84
  You can also use Diggy in the browser via ESM imports. This allows you to fetch DNS records directly from client-side
86
85
  JavaScript. There are built-in resolvers for Google and Cloudflare DNS over HTTPS, which work seamlessly in the browser.
@@ -88,9 +87,8 @@ JavaScript. There are built-in resolvers for Google and Cloudflare DNS over HTTP
88
87
  ```html
89
88
 
90
89
  <script type="module">
91
- import { getDnsRecords } from 'https://esm.sh/diggy';
92
-
93
- const records = await getDnsRecords('ozana.cz');
90
+ import { getDnsRecords } from 'https://esm.sh/diggy';
91
+ const records = await getDnsRecords('ozana.cz');
94
92
  </script>
95
93
  ```
96
94
 
@@ -154,8 +152,8 @@ You can also **create your own custom resolver** by implementing the `DNSResolve
154
152
 
155
153
  ```typescript
156
154
  export type DNSResolver = (
157
- host: string,
158
- type: DNSRecordType,
155
+ host: string,
156
+ type: DNSRecordType,
159
157
  ) => Promise<AnyDNSRecord[]>;
160
158
  ```
161
159
 
@@ -173,7 +171,7 @@ export type DNSResolver = (
173
171
  | CAA | Certificate authority | SSL/TLS security |
174
172
  | NAPTR | Name authority pointer | ENUM, SIP routing |
175
173
 
176
- ## 📜 Response Format
174
+ ## Response Format
177
175
 
178
176
  DNS records are returned as an array of objects with the following structure:
179
177
 
@@ -181,19 +179,19 @@ DNS records are returned as an array of objects with the following structure:
181
179
  import { CaaRecordData, MxRecordData, SoaRecordData, SrvRecordData, NaptrRecordData } from "./types";
182
180
 
183
181
  interface AnyDNSRecord {
184
- name: string; // Domain name
185
- type: string; // Record type (A, AAAA, MX, etc.)
186
- ttl: number; // Time-to-live in seconds
187
-
188
- // Record data (format varies by type)
189
- data:
190
- | string
191
- | string[]
192
- | MxRecordData
193
- | SoaRecordData
194
- | CaaRecordData
195
- | NaptrRecordData
196
- | SrvRecordData;
182
+ name: string; // Domain name
183
+ type: string; // Record type (A, AAAA, MX, etc.)
184
+ ttl: number; // Time-to-live in seconds
185
+
186
+ // Record data (format varies by type)
187
+ data:
188
+ | string
189
+ | string[]
190
+ | MxRecordData
191
+ | SoaRecordData
192
+ | CaaRecordData
193
+ | NaptrRecordData
194
+ | SrvRecordData;
197
195
  }
198
196
  ```
199
197
 
@@ -201,41 +199,41 @@ interface AnyDNSRecord {
201
199
 
202
200
  ```json
203
201
  [
204
- {
205
- "name": "example.com",
206
- "type": "SOA",
207
- "ttl": 3600,
208
- "data": {
209
- "nsname": "ns1.example.com.",
210
- "hostmaster": "hostmaster.example.com.",
211
- "serial": 2025051204,
212
- "refresh": 10800,
213
- "retry": 3600,
214
- "expire": 604800,
215
- "minttl": 3600
216
- }
217
- },
218
- {
219
- "name": "example.cz",
220
- "type": "A",
221
- "ttl": 1800,
222
- "data": "66.33.66.33"
223
- },
224
- {
225
- "name": "example.cz",
226
- "type": "MX",
227
- "ttl": 60,
228
- "data": {
229
- "priority": 10,
230
- "exchange": "mail.example.com"
231
- }
232
- }
202
+ {
203
+ "name": "example.com",
204
+ "type": "SOA",
205
+ "ttl": 3600,
206
+ "data": {
207
+ "nsname": "ns1.example.com.",
208
+ "hostmaster": "hostmaster.example.com.",
209
+ "serial": 2025051204,
210
+ "refresh": 10800,
211
+ "retry": 3600,
212
+ "expire": 604800,
213
+ "minttl": 3600
214
+ }
215
+ },
216
+ {
217
+ "name": "example.cz",
218
+ "type": "A",
219
+ "ttl": 1800,
220
+ "data": "66.33.66.33"
221
+ },
222
+ {
223
+ "name": "example.cz",
224
+ "type": "MX",
225
+ "ttl": 60,
226
+ "data": {
227
+ "priority": 10,
228
+ "exchange": "mail.example.com"
229
+ }
230
+ }
233
231
  ]
234
232
  ```
235
233
 
236
234
  ## 🔧 Requirements
237
235
 
238
- - **Node.js**: Version 14 or higher
236
+ - **Node.js**: Version 22 or higher
239
237
  - **dig command**: Required only when using the 'dig' resolver
240
238
  - **Internet connection**: Required for DoH resolvers (google, cloudflare)
241
239