@tgwf/co2
Version:
Work out the co2 of your digital services
72 lines (71 loc) • 2.6 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
import { getApiRequestHeaders } from "./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: __spreadProps(__spreadValues({}, 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;
export {
check,
carbon_txt_default as default
};