@tgwf/co2
Version:
Work out the co2 of your digital services
56 lines (55 loc) • 1.99 kB
JavaScript
;
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 hosting_api_exports = {};
__export(hosting_api_exports, {
default: () => hosting_api_default
});
module.exports = __toCommonJS(hosting_api_exports);
function check(domain) {
if (typeof domain === "string") {
return checkAgainstAPI(domain);
} else {
return checkDomainsAgainstAPI(domain);
}
}
async function checkAgainstAPI(domain) {
const req = await fetch(`https://api.thegreenwebfoundation.org/greencheck/${domain}`);
const res = await req.json();
return res.green;
}
async function checkDomainsAgainstAPI(domains) {
try {
const apiPath = "https://api.thegreenwebfoundation.org/v2/greencheckmulti";
const domainsString = JSON.stringify(domains);
const req = await fetch(`${apiPath}/${domainsString}`);
const allGreenCheckResults = await req.json();
return greenDomainsFromResults(allGreenCheckResults);
} catch (e) {
return [];
}
}
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