@blockcore/dns 0.0.2 → 0.0.3

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.
package/README.md CHANGED
@@ -22,4 +22,28 @@
22
22
  npm install @blockcore/dns
23
23
  ```
24
24
 
25
- **Warning:** This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and does not provide a CommonJS export. If your project uses CommonJS, you'll have to [convert to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) or use the [dynamic `import()`](https://v8.dev/features/dynamic-import) function.
25
+ **Warning:** This package is native [ESM](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) and does not provide a CommonJS export. If your project uses CommonJS, you'll have to [convert to ESM](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c) or use the [dynamic `import()`](https://v8.dev/features/dynamic-import) function.
26
+
27
+
28
+ ## Usage
29
+
30
+ ```ts
31
+ import { BlockcoreDns } from '@blockcore/dns';
32
+
33
+ let dns = new BlockcoreDns();
34
+ let servers = await dns.getDnsServers();
35
+ ```
36
+
37
+ After getting the servers, either you can loop all of them or pick a single one:
38
+
39
+ ```ts
40
+ let server = servers[0];
41
+
42
+ dns.setActiveServer(dnsServer.url);
43
+
44
+ await dns.getServicesByType('Indexer');
45
+ await dns.getServicesByNetwork('CITY');
46
+ await dns.getServicesByTypeAndNetwork('Indexer', 'CITY');
47
+ await dns.getExternalIP();
48
+ ```
49
+
@@ -1,8 +1,17 @@
1
- import { BlockcoreDnsOptions } from './options.js';
1
+ import { BlockcoreDnsOptions, DnsListEntry, ServiceListEntry } from './types.js';
2
2
  export declare class BlockcoreDns {
3
3
  private options;
4
4
  static defaultOptions: BlockcoreDnsOptions;
5
5
  static create(): BlockcoreDns;
6
+ private activeServer?;
6
7
  constructor(options?: BlockcoreDnsOptions);
7
- getDnsServers(): Promise<unknown>;
8
+ setActiveServer(url: string): void;
9
+ getDnsServers(): Promise<DnsListEntry[]>;
10
+ getServicesByType(service: 'Indexer' | 'Explorer'): Promise<ServiceListEntry[]>;
11
+ getServicesByTypeAndNetwork(service: 'Indexer' | 'Explorer', symbol: string): Promise<ServiceListEntry[]>;
12
+ getServicesByNetwork(symbol: string): Promise<ServiceListEntry[]>;
13
+ getExternalIP(): Promise<string>;
14
+ private fetchText;
15
+ private fetchJson;
16
+ private fetchUrl;
8
17
  }
@@ -7,14 +7,51 @@ export class BlockcoreDns {
7
7
  writable: true,
8
8
  value: options
9
9
  });
10
+ Object.defineProperty(this, "activeServer", {
11
+ enumerable: true,
12
+ configurable: true,
13
+ writable: true,
14
+ value: void 0
15
+ });
10
16
  }
11
17
  static create() {
12
18
  const dns = new BlockcoreDns();
13
19
  return dns;
14
20
  }
21
+ setActiveServer(url) {
22
+ this.activeServer = url;
23
+ }
15
24
  async getDnsServers() {
16
- const url = `${this.options.baseUrl}/services/BLOCKCORE-DNS.json`;
17
- const response = await fetch(url, {
25
+ const url = `${this.options.baseUrl}/services/DNS.json`;
26
+ const servers = await this.fetchJson(url);
27
+ return servers;
28
+ }
29
+ async getServicesByType(service) {
30
+ const url = `${this.activeServer}/api/dns/services/service/${service}`;
31
+ return await this.fetchJson(url);
32
+ }
33
+ async getServicesByTypeAndNetwork(service, symbol) {
34
+ const url = `${this.activeServer}/api/dns/services/symbol/${symbol}/service/${service}`;
35
+ return await this.fetchJson(url);
36
+ }
37
+ async getServicesByNetwork(symbol) {
38
+ const url = `${this.activeServer}/api/dns/services/symbol/${symbol}`;
39
+ return await this.fetchJson(url);
40
+ }
41
+ async getExternalIP() {
42
+ const url = `${this.activeServer}/api/dns/ipaddress`;
43
+ return await this.fetchText(url);
44
+ }
45
+ async fetchText(url) {
46
+ const response = await this.fetchUrl(url);
47
+ return response.text();
48
+ }
49
+ async fetchJson(url) {
50
+ const response = await this.fetchUrl(url);
51
+ return response.json();
52
+ }
53
+ async fetchUrl(url) {
54
+ return await fetch(url, {
18
55
  method: 'GET',
19
56
  // mode: 'cors',
20
57
  // cache: 'no-cache',
@@ -25,8 +62,6 @@ export class BlockcoreDns {
25
62
  redirect: 'follow',
26
63
  referrerPolicy: 'no-referrer',
27
64
  });
28
- const servers = await response.json();
29
- return servers;
30
65
  }
31
66
  }
32
67
  Object.defineProperty(BlockcoreDns, "defaultOptions", {
package/dist/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
- export interface Dns {
2
- IP: string;
3
- }
1
+ export * from './BlockcoreDns.js';
2
+ export * from './types.js';
package/dist/index.js CHANGED
@@ -1 +1,2 @@
1
- export {};
1
+ export * from './BlockcoreDns.js';
2
+ export * from './types.js';
@@ -0,0 +1,14 @@
1
+ export interface DnsListEntry {
2
+ url: string;
3
+ contact: string;
4
+ }
5
+ export interface ServiceListEntry {
6
+ domain: string;
7
+ symbol: string;
8
+ service: string;
9
+ ttl: number;
10
+ online: boolean;
11
+ }
12
+ export interface BlockcoreDnsOptions {
13
+ baseUrl: string;
14
+ }
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blockcore/dns",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "type": "module",
5
5
  "exports": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
package/dist/options.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export interface BlockcoreDnsOptions {
2
- baseUrl: string;
3
- }