UNPKG

domainr-node

Version:

`domainr-node` is a [Domainr](https://domainr.com) API client written for Node that fully supports both the standard and enterprise versions.

158 lines (157 loc) 6.33 kB
/** * domainr-node * Copyright (C) 2021 Marvin Schopf * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * @license Apache-2.0 * @copyright 2021 Marvin Schopf * @author Marvin Schopf <marvin@schopf.biz> * */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import fetch from "node-fetch"; const apiBaseUrls = { standard: "https://domainr.p.rapidapi.com", enterprise: "https://api.domainr.com", }; function buildQueryString(params) { return Object.keys(params) .map((k) => params[k] ? encodeURIComponent(k) + "=" + encodeURIComponent(params[k]) : "") .join("&"); } function asyncForEach(array, callback) { return __awaiter(this, void 0, void 0, function* () { for (let index = 0; index < array.length; index++) { yield callback(array[index], index, array); } }); } export class DomainrClient { constructor(apiKey, options) { this.apiKey = apiKey; this.endpoint = options ? options.endpoint ? options.endpoint : "standard" : "standard"; } register(domain, registrar) { return __awaiter(this, void 0, void 0, function* () { const response = yield fetch(`${apiBaseUrls[this.endpoint]}/v2/register?${buildQueryString({ "mashape-key": this.endpoint === "standard" ? this.apiKey : false, client_id: this.endpoint === "enterprise" ? this.apiKey : false, domain: domain, registrar: registrar ? registrar : false, })}`, { redirect: "manual", }); if (response.status === 302) { if (response.headers.has("Location")) { // @ts-ignore return response.headers.get("Location"); } else { throw new Error("Domainr: No `Location` header in the response."); } } else { const json = yield response.json(); if (json && json.message) { throw new Error(`Domainr: ${json.message}`); } else { throw new Error(`Domainr: HTTP ${response.status} ${response.statusText}`); } } }); } status(domain) { return __awaiter(this, void 0, void 0, function* () { const response = yield fetch(`${apiBaseUrls[this.endpoint]}/v2/status?${this.endpoint === "enterprise" ? "client_id" : "mashape-key"}=${this.apiKey}&domain=${domain}`); if (response.status === 200) { let statuses = (yield response.json()).status; yield asyncForEach(statuses, (status, statusIndex) => __awaiter(this, void 0, void 0, function* () { statuses[statusIndex] = { domain: status.domain, zone: status.zone, status: status.status.split(" "), }; })); return statuses; } else { const json = yield response.json(); if (json && json.message) { throw new Error(`Domainr: ${json.message}`); } else { throw new Error(`Domainr: HTTP ${response.status} ${response.statusText}`); } } }); } search(query, options) { return __awaiter(this, void 0, void 0, function* () { const response = yield fetch(`${apiBaseUrls[this.endpoint]}/v2/search?${buildQueryString({ "mashape-key": this.endpoint === "standard" ? this.apiKey : false, client_id: this.endpoint === "enterprise" ? this.apiKey : false, query: query, location: options ? options.location ? options.location : false : false, registrar: options ? options.registrar ? options.registrar : false : false, defaults: options ? options.defaults ? options.defaults.join(",") : false : false, keywords: options ? options.keywords ? options.keywords.join(",") : false : false, })}`); if (response.status === 200) { const results = (yield response.json()).results; return results; } else { const json = yield response.json(); if (json && json.message) { throw new Error(`Domainr: ${json.message}`); } else { throw new Error(`Domainr: HTTP ${response.status} ${response.statusText}`); } } }); } } export default DomainrClient;