All files / src/api/bns fetchers.ts

100% Statements 37/37
100% Branches 0/0
100% Functions 12/12
100% Lines 37/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159                        10x               10x 1x 1x                 10x 1x 1x                 10x 1x 1x                 10x 1x   1x                 10x 1x   1x                 10x 1x 1x                 10x 1x 1x                 10x 1x 1x                 10x         1x 1x                 10x         1x 1x                 10x 1x 1x                 10x       1x 1x    
import {
  BnsGetNamespacePriceResponse,
  BnsGetNamePriceResponse,
  BnsGetAllNamespacesResponse,
  BnsGetNameInfoResponse,
  BnsError,
  BnsFetchHistoricalZoneFileResponse,
  BnsFetchFileZoneResponse,
  BnsNamesOwnByAddressResponse,
  BnsGetSubdomainAtTx,
} from '@stacks/stacks-blockchain-api-types';
import { BaseListParams } from '../types';
import { fetchJson, generateUrl, v1Endpoint, v2Endpoint } from '../utils';
 
/**
 * Get the price of a namespace. The amount given will be in the smallest possible units of the currency.
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/bns#fetchnamespaceprice
 */
 
export async function fetchNamespacePrice({ url, tld }: BaseListParams & { tld: string }) {
  const path = generateUrl(`${v2Endpoint(url)}/prices/namespaces/${tld}`, {});
  return fetchJson<BnsGetNamespacePriceResponse>(path);
}
 
/**
 * Get the price of a name. The amount given will be in the smallest possible units of the currency.
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/bns#fetchnameprice
 */
 
export async function fetchNamePrice({ url, name }: BaseListParams & { name: string }) {
  const path = generateUrl(`${v2Endpoint(url)}/prices/names/${name}`, {});
  return fetchJson<BnsGetNamePriceResponse>(path);
}
 
/**
 * Fetch a list of all namespaces known to the node.
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/bns#fetchnamespaces
 */
 
export async function fetchNamespaces({ url }: BaseListParams) {
  const path = generateUrl(`${v1Endpoint(url)}/namespaces`, {});
  return fetchJson<BnsGetAllNamespacesResponse>(path);
}
 
/**
 * Fetch a list of names from the namespace.
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/bns#fetchnamesfromnamespaces
 */
 
export async function fetchNamesFromNamespaces({ url, tld }: BaseListParams & { tld: string }) {
  const path = generateUrl(`${v1Endpoint(url)}/namespaces/${tld}/names`, {});
  // TODO: validate? string^([a-z0-9-_.+]{3,37})$
  return fetchJson<string[]>(path);
}
 
/**
 * Fetch a list of all names known to the node.
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/bns#fetchnames
 */
 
export async function fetchNames({ url, page }: BaseListParams & { page: number }) {
  const path = generateUrl(`${v1Endpoint(url)}/names?page=${page}`, {});
  // TODO: validate? string^([a-z0-9-_.+]{3,37})$
  return fetchJson<string[]>(path);
}
 
/**
 * Get Name Details
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/bns#fetchname
 */
 
export async function fetchName({ url, name }: BaseListParams & { name: string }) {
  const path = generateUrl(`${v1Endpoint(url)}/names/${name}`, {});
  return fetchJson<BnsGetNameInfoResponse>(path);
}
 
/**
 * Get a history of all blockchain records of a registered name.
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/bns#fetchnamehistory
 */
 
export async function fetchNameHistory({ url, name }: BaseListParams & { name: string }) {
  const path = generateUrl(`${v1Endpoint(url)}/names/${name}/history`, {});
  return fetchJson<BnsFetchHistoricalZoneFileResponse>(path);
}
 
/**
 * Fetch Zone File
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/bns#fetchzonefile
 */
 
export async function fetchZoneFile({ url, name }: BaseListParams & { name: string }) {
  const path = generateUrl(`${v1Endpoint(url)}/names/${name}/zonefile`, {});
  return fetchJson<BnsError | BnsFetchFileZoneResponse>(path);
}
 
/**
 * Get Historical Zone File
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/bns#fetchhistoricalzonefile
 */
 
export async function fetchHistoricalZoneFile({
  url,
  name,
  zoneFileHash,
}: BaseListParams & { name: string; zoneFileHash: string }) {
  const path = generateUrl(`${v1Endpoint(url)}/names/${name}/zonefile/${zoneFileHash}`, {});
  return fetchJson<BnsError | BnsFetchHistoricalZoneFileResponse>(path);
}
 
/**
 * Retrieves a list of names owned by the address provided.
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/bns#fetchnamesbyaddress
 */
 
export async function fetchNamesByAddress({
  url,
  blockchain,
  address,
}: BaseListParams & { blockchain: string; address: string }) {
  const path = generateUrl(`${v1Endpoint(url)}/addresses/${blockchain}/${address}`, {});
  return fetchJson<BnsError | BnsNamesOwnByAddressResponse>(path);
}
 
/**
 * Fetch a list of all subdomains known to the node.
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/bns#fetchallsubdomains
 */
 
export async function fetchAllSubdomains({ url, page }: BaseListParams & { page: number }) {
  const path = generateUrl(`${v1Endpoint(url)}/subdomains/`, { page: page });
  return fetchJson<BnsError | string[]>(path);
}
 
/**
 * Get Subdomain at Transaction
 *
 * @see https://docs.micro-stacks.dev/modules/core/api/bns#fetchsubdomainattransaction
 */
 
export async function fetchSubdomainAtTransaction({
  url,
  txid,
}: BaseListParams & { txid: string }) {
  const path = generateUrl(`${v1Endpoint(url)}/subdomains/${txid}`, {});
  return fetchJson<BnsError | BnsGetSubdomainAtTx>(path);
}