@tgwf/co2
Version:
Work out the co2 of your digital services
78 lines (77 loc) • 3.15 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var hosting_api_exports = {};
__export(hosting_api_exports, {
default: () => hosting_api_default
});
module.exports = __toCommonJS(hosting_api_exports);
var import_helpers = require("./helpers/index.js");
var import_hosting_json = __toESM(require("./hosting-json.js"));
function check(domain, optionsOrAgentId) {
const options = typeof optionsOrAgentId === "string" ? { userAgentIdentifier: optionsOrAgentId } : optionsOrAgentId;
if ((options == null ? void 0 : options.db) && options.verbose) {
throw new Error("verbose mode cannot be used with a local lookup database");
}
if (typeof domain === "string") {
return checkAgainstAPI(domain, options);
} else {
return checkDomainsAgainstAPI(domain, options);
}
}
async function checkAgainstAPI(domain, options = {}) {
const req = await fetch(
`https://api.thegreenwebfoundation.org/greencheck/${domain}`,
{
headers: (0, import_helpers.getApiRequestHeaders)(options.userAgentIdentifier)
}
);
if (options == null ? void 0 : options.db) {
return import_hosting_json.default.check(domain, options.db);
}
const res = await req.json();
return options.verbose ? res : res.green;
}
async function checkDomainsAgainstAPI(domains, options = {}) {
try {
const apiPath = "https://api.thegreenwebfoundation.org/v2/greencheckmulti";
const domainsString = JSON.stringify(domains);
const req = await fetch(`${apiPath}/${domainsString}`, {
headers: (0, import_helpers.getApiRequestHeaders)(options.userAgentIdentifier)
});
const allGreenCheckResults = await req.json();
return options.verbose ? allGreenCheckResults : greenDomainsFromResults(allGreenCheckResults);
} catch (e) {
return options.verbose ? {} : [];
}
}
function greenDomainsFromResults(greenResults) {
const entries = Object.entries(greenResults);
const greenEntries = entries.filter(([key, val]) => val.green);
return greenEntries.map(([key, val]) => val.url);
}
var hosting_api_default = {
check
};
//# sourceMappingURL=hosting-api.js.map
;