/* eslint-disable no-console */ import FetchBuilder from "@web-atoms/fetch-builder"; import { BaseDomainRegistrar, IDomainSearchResult, ITransferResult } from "../DomainRegistrar.js"; import ServerLogger from "@entity-access/server-pages/dist/core/ServerLogger.js"; import EntityAccessError from "@entity-access/entity-access/dist/common/EntityAccessError.js"; const parsePrice = (p: any) => { let price = p["Price"] as string; if (!price) { return -1; } price = price.split(":").pop().trim(); const n = Number.parseFloat(price); if (Number.isNaN(n)) { return -1; } return n; }; export default class DynadotRegistrar extends BaseDomainRegistrar { async search(domain: string, currency: string): Promise { const client = await this.client(); const response = await client.queries({ command: "search", domain0: domain, show_price: 1, currency }).asJson<{ SearchResponse: { ResponseCode: any, SearchResults: { DomainName: string; Available: string; Price: string; }[] } }>(); const success = response.SearchResponse.ResponseCode === "0"; if (!success) { // print whole object. ServerLogger.error({ error: response, route: domain }); return { available: false, error: response }; } const result = response.SearchResponse.SearchResults[0]; const registration = parsePrice(result.Price); if (registration <= 0) { return { available: false, error: response }; } return { available: /yes|true/i.test(result.Available), registration }; } async register(domain: string, currency: string): Promise<{ status: "registered" | "failed"; error?: any; }> { const client = await this.client(); const result = await client.queries({ command: "register", domain, duration: 1, currency }).asJson<{ RegisterResponse: { ResponseCode: number, Status: "success" | "failed"} }>(); try { if (!/success/i.test(result.RegisterResponse.Status)) { console.error(`Domain ${domain} registration failed.\n${JSON.stringify(result)}`); throw new EntityAccessError(`Registration failed please contact support.`); } return { status: "registered" }; } catch (error) { console.error(`Domain ${domain} registration failed.\n${JSON.stringify(result)}`); throw new EntityAccessError(`Registration failed please contact support.`); } } transfer(domain: string, currency: string, transferCode: string): Promise { throw new Error("Method not implemented."); } updateTransfer(domain: string): Promise { throw new Error("Method not implemented."); } updateNameservers(domain: string, servers: string[]): Promise { throw new Error("Method not implemented."); } async client() { const key = this.integration.secret; let url = `https://api.dynadot.com/api3.json`; if (this.integration.sandbox) { url = `https://api-sandbox.dynadot.com/api3.json`; } return FetchBuilder.get(url) .queries({ key }) .header("accept", "application/json"); } }