UNPKG

@tgwf/co2

Version:
75 lines (74 loc) 2.69 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var carbon_txt_exports = {}; __export(carbon_txt_exports, { check: () => check, default: () => carbon_txt_default }); module.exports = __toCommonJS(carbon_txt_exports); var import_helpers = require("./helpers/index.js"); const processResponse = (res, verbose = false) => { var _a, _b, _c, _d; if (!res.success && !res.errors) { return { success: false, errors: [res.detail[0].msg] }; } if (verbose) { return res; } if (!res.success) { return { success: false, errors: res.errors }; } return { success: true, url: res.url, org: { disclosures: (_b = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.org) == null ? void 0 : _b.disclosures }, upstream: { services: (_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c.upstream) == null ? void 0 : _d.services } }; }; async function check(domain, options) { if (typeof domain !== "string") { throw new Error("Invalid domain. Domain must be a string."); } const agentId = options == null ? void 0 : options.userAgentIdentifier; const verbose = (options == null ? void 0 : options.verbose) || false; const apiKey = (options == null ? void 0 : options.apiKey) || null; if (!apiKey) { throw new Error("A valid API key is required."); } const validatorUrl = (options == null ? void 0 : options.customValidator) || "https://carbon-txt-api.greenweb.org/api/validate/domain/"; try { const req = await fetch(validatorUrl, { headers: { ...(0, import_helpers.getApiRequestHeaders)(agentId), "x-api-key": apiKey }, method: "POST", body: JSON.stringify({ domain }) }); const res = await req.json(); return processResponse(res, verbose); } catch (error) { return { success: false, errors: [error.message] }; } } var carbon_txt_default = check; //# sourceMappingURL=carbon-txt.js.map