UNPKG

@apihawk/billia-sdk

Version:

The ApiHawk Billia SDK

279 lines (278 loc) 10.5 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const punycode = require("punycode"); const qs = require("qs"); const tldjs = require("tldjs"); const billia_sdk_service_base_1 = require("../lib/billia-sdk-service-base"); class BilliaSDKDomain extends billia_sdk_service_base_1.BilliaSDKServiceBase { /** * Search domains. * * @param domain the domain to search for * @param tlds the TLDs to include * @param session user session */ search(domain, tlds, session) { return __awaiter(this, void 0, void 0, function* () { const query = (tlds ? tlds.map((tld, i) => `tlds[${i}]=${encodeURIComponent(tld)}`) : []).join('&'); let returnResults = []; return this.api .call({ url: `/domain/search?domain=${encodeURIComponent(domain)}&${query}`, method: 'GET', headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' }, session }) .then((result) => { const IDs = new Set(); returnResults = Object.values(result).map((item) => { if (item.available) { item.status = 'available'; } else { item.status = 'taken'; } IDs.add(item.product.product_id); return item; }); const filter = qs.stringify({ where: [ { field: 'product_id', where: 'and', type: 'in', values: Array.from(IDs) } ] }); return this.api.call({ url: '/billing/product?page_size=-1&' + filter, method: 'GET', headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' }, session }); }) .then((products) => { const productIdentifierIDs = {}; products._embedded.catalog_product.forEach((product) => { if (product.options) { const identifier = product.options.filter((option) => option.option_key === 'domain' || option.option_key === 'DomainName')[0]; if (identifier) { productIdentifierIDs[product.product_id] = identifier.option_id; } } }); returnResults.forEach((result) => { // try to parse 'visible_quantity' from a string: '[1, 2, 3]' to array [1, 2, 3] if (typeof result.product.visible_quantity === 'string') { try { result.product.visible_quantity = JSON.parse(result.product.visible_quantity); } catch (err) { result.product.visible_quantity = []; } } if (productIdentifierIDs[result.product.product_id]) { result.product.identifier = productIdentifierIDs[result.product.product_id]; } }); return Promise.resolve(returnResults); }); }); } /** * Search domains premium. * * @param domain the domain name * @param tlds list of TLDs * @param searchType * @param session user session */ searchPremium(domain, tlds, searchType, algorithm, idn, session) { return __awaiter(this, void 0, void 0, function* () { const queryArr = tlds ? tlds.map((tld, i) => `tlds[${i}]=${encodeURIComponent(tld)}`) : []; if (searchType) { queryArr.push(`search_type[]=${encodeURIComponent(searchType)}`); } if (algorithm) { queryArr.push(`algorithm=${encodeURIComponent(algorithm)}`); } if (idn) { queryArr.push(`idn=${encodeURIComponent(idn)}`); } const query = queryArr.join('&'); let returnResults = []; return this.api .call({ url: `/premiumdomain/search?domain=${encodeURIComponent(domain)}&${query}`, method: 'GET', headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' }, session }) .then((results) => { const IDs = new Set(); returnResults = Object.values(results); Object.values(results).forEach((result) => { IDs.add(result.product.product_id); }); if (!IDs.size) { return Promise.resolve({ _embedded: { catalog_product: [] } }); } const filter = qs.stringify({ where: [ { field: 'product_id', where: 'and', type: 'in', values: Array.from(IDs) } ] }); return this.api.call({ url: '/billing/product?page_size=-1&' + filter, method: 'GET', headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json', 'Accept-Response': 'Advanced' }, session }); }) .then((products) => { const productIdentifierIDs = {}; products._embedded.catalog_product.forEach((product) => { if (product.options) { const identifier = product.options.filter((option) => option.option_key === 'domain' || option.option_key === 'DomainName')[0]; if (identifier) { productIdentifierIDs[product.product_id] = identifier.option_id; } } }); returnResults.forEach((result) => { if (productIdentifierIDs[result.product.product_id]) { result.product.identifier = productIdentifierIDs[result.product.product_id]; } }); return Promise.resolve(returnResults); }); }); } /** * Checks if a string is valid hostname or domain * * @param hostname */ validHostOrDomain(hostname) { hostname = punycode.toASCII(hostname.toLowerCase()); if (tldjs.tldExists(hostname)) { if (tldjs.getDomain(hostname) !== hostname) { return false; } return tldjs.isValid(hostname); } else if (hostname.indexOf('.') === -1) { return tldjs.isValid(hostname); } else { return false; } } /** * Checks if a string is valid hostname. * * @param hostname */ validHostname(hostname) { hostname = punycode.toASCII(hostname.toLowerCase()); if (tldjs.tldExists(hostname) === false && hostname.indexOf('.') === -1) { return tldjs.isValid(hostname); } else { return false; } } /** * Check if a string is valid domain. * * @param domain */ validDomain(domain) { domain = punycode.toASCII(domain.toLowerCase()); if (tldjs.tldExists(domain)) { if (tldjs.getDomain(domain) !== domain) { return false; } return tldjs.isValid(domain); } return false; } /** * Checks if a string is valid domain or subdomain. * * @param domain */ validDomainOrSubdomain(domain) { domain = punycode.toASCII(domain.toLowerCase()); const parsed = tldjs.parse(domain); return (domain === tldjs.extractHostname(domain) && parsed.isValid && parsed.tldExists && !!parsed.domain); } /** * Checks if a domain is registered. * * @param domain */ isDomainRegesterd(domain) { return __awaiter(this, void 0, void 0, function* () { domain = punycode.toASCII(domain.toLowerCase()); if (this.validDomain(domain)) { return this.search(domain, []).then((domains) => { const result = Object.values(domains)[0]; if (result) { return !result.available; } return Promise.resolve(false); }); } else { return Promise.resolve(false); } }); } } exports.BilliaSDKDomain = BilliaSDKDomain;