@tgwf/co2 0.18.1-0 → 0.18.2-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -40,8 +40,8 @@ const processResponse = (res, verbose = false) => {
40
40
  return {
41
41
  success: true,
42
42
  url: res.url,
43
- disclosures: (_b = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.org) == null ? void 0 : _b.disclosures,
44
- upstream: (_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c.upstream) == null ? void 0 : _d.services
43
+ org: { disclosures: (_b = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.org) == null ? void 0 : _b.disclosures },
44
+ upstream: { services: (_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c.upstream) == null ? void 0 : _d.services }
45
45
  };
46
46
  };
47
47
  async function check(domain, options) {
@@ -50,16 +50,25 @@ async function check(domain, options) {
50
50
  }
51
51
  const agentId = options == null ? void 0 : options.userAgentIdentifier;
52
52
  const verbose = (options == null ? void 0 : options.verbose) || false;
53
- const req = await fetch(
54
- `https://carbon-txt-api.greenweb.org/api/validate/domain/`,
55
- {
56
- headers: (0, import_helpers.getApiRequestHeaders)(agentId),
53
+ const apiKey = (options == null ? void 0 : options.apiKey) || null;
54
+ if (!apiKey) {
55
+ throw new Error("A valid API key is required.");
56
+ }
57
+ const validatorUrl = (options == null ? void 0 : options.customValidator) || "https://carbon-txt-api.greenweb.org/api/validate/domain/";
58
+ try {
59
+ const req = await fetch(validatorUrl, {
60
+ headers: {
61
+ ...(0, import_helpers.getApiRequestHeaders)(agentId),
62
+ "x-api-key": apiKey
63
+ },
57
64
  method: "POST",
58
65
  body: JSON.stringify({ domain })
59
- }
60
- );
61
- const res = await req.json();
62
- return processResponse(res, verbose);
66
+ });
67
+ const res = await req.json();
68
+ return processResponse(res, verbose);
69
+ } catch (error) {
70
+ return { success: false, errors: [error.message] };
71
+ }
63
72
  }
64
73
  var carbon_txt_default = check;
65
74
  //# sourceMappingURL=carbon-txt.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/carbon-txt.js"],
4
- "sourcesContent": ["\"use strict\";\n\nimport { getApiRequestHeaders } from \"./helpers/index.js\";\n\n/**\n * @module carbontxt-validator\n */\n\n/**\n * Process the response from the carbon.txt validator API\n * @param {object} res - The response object from the API\n * @param {boolean} verbose - Optional. Whether to return verbose results\n * @returns {object} - The processed response object\n */\nconst processResponse = (res, verbose = false) => {\n // This captures errors when an invalid domain is sent to the API\n if (!res.success && !res.errors) {\n return {\n success: false,\n errors: [res.detail[0].msg],\n };\n }\n\n if (verbose) {\n return res;\n }\n\n if (!res.success) {\n return { success: false, errors: res.errors };\n }\n\n return {\n success: true,\n url: res.url,\n disclosures: res?.data?.org?.disclosures,\n upstream: res?.data?.upstream?.services,\n };\n};\n\n/**\n * Perform a domain lookup using the Green Web Foundation carbon.txt validator endpoint\n * @param {string} domain - The domain to check, or an array of domains to be checked.\n * @param {string} options - Optional. An object of domain check options, or a string\n * @param {string} options.userAgentIdentifier - Optional. A string representing the app, site, or organisation that is making the request.\n * @param {string} options.verbose - Optional. A boolean indicating whether to return verbose results.\n */\n\nexport async function check(domain, options) {\n if (typeof domain !== \"string\") {\n throw new Error(\"Invalid domain. Domain must be a string.\");\n }\n\n const agentId = options?.userAgentIdentifier;\n const verbose = options?.verbose || false;\n\n const req = await fetch(\n `https://carbon-txt-api.greenweb.org/api/validate/domain/`,\n {\n headers: getApiRequestHeaders(agentId),\n method: \"POST\",\n body: JSON.stringify({ domain }),\n }\n );\n\n const res = await req.json();\n return processResponse(res, verbose);\n}\n\nexport default check;\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,qBAAqC;AAYrC,MAAM,kBAAkB,CAAC,KAAK,UAAU,UAAU;AAdlD;AAgBE,MAAI,CAAC,IAAI,WAAW,CAAC,IAAI,QAAQ;AAC/B,WAAO;AAAA,MACL,SAAS;AAAA,MACT,QAAQ,CAAC,IAAI,OAAO,CAAC,EAAE,GAAG;AAAA,IAC5B;AAAA,EACF;AAEA,MAAI,SAAS;AACX,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,IAAI,SAAS;AAChB,WAAO,EAAE,SAAS,OAAO,QAAQ,IAAI,OAAO;AAAA,EAC9C;AAEA,SAAO;AAAA,IACL,SAAS;AAAA,IACT,KAAK,IAAI;AAAA,IACT,cAAa,sCAAK,SAAL,mBAAW,QAAX,mBAAgB;AAAA,IAC7B,WAAU,sCAAK,SAAL,mBAAW,aAAX,mBAAqB;AAAA,EACjC;AACF;AAUA,eAAsB,MAAM,QAAQ,SAAS;AAC3C,MAAI,OAAO,WAAW,UAAU;AAC9B,UAAM,IAAI,MAAM,0CAA0C;AAAA,EAC5D;AAEA,QAAM,UAAU,mCAAS;AACzB,QAAM,WAAU,mCAAS,YAAW;AAEpC,QAAM,MAAM,MAAM;AAAA,IAChB;AAAA,IACA;AAAA,MACE,aAAS,qCAAqB,OAAO;AAAA,MACrC,QAAQ;AAAA,MACR,MAAM,KAAK,UAAU,EAAE,OAAO,CAAC;AAAA,IACjC;AAAA,EACF;AAEA,QAAM,MAAM,MAAM,IAAI,KAAK;AAC3B,SAAO,gBAAgB,KAAK,OAAO;AACrC;AAEA,IAAO,qBAAQ;",
4
+ "sourcesContent": ["\"use strict\";\n\nimport { getApiRequestHeaders } from \"./helpers/index.js\";\n\n/**\n * @module carbontxt-validator\n */\n\n/**\n * Process the response from the carbon.txt validator API\n * @param {object} res - The response object from the API\n * @param {boolean} verbose - Optional. Whether to return verbose results\n * @returns {object} - The processed response object\n */\nconst processResponse = (res, verbose = false) => {\n // This captures errors when an invalid domain is sent to the API\n if (!res.success && !res.errors) {\n return {\n success: false,\n errors: [res.detail[0].msg],\n };\n }\n\n if (verbose) {\n return res;\n }\n\n if (!res.success) {\n return { success: false, errors: res.errors };\n }\n\n return {\n success: true,\n url: res.url,\n org: { disclosures: res?.data?.org?.disclosures },\n upstream: { services: res?.data?.upstream?.services },\n };\n};\n\n/**\n * Perform a domain lookup using the Green Web Foundation carbon.txt validator endpoint\n * @param {string} domain - The domain to check.\n * @param {string} options - Optional. An object of domain check options, or a string\n * @param {string} options.userAgentIdentifier - Optional. A string representing the app, site, or organisation that is making the request.\n * @param {string} options.verbose - Optional. A boolean indicating whether to return verbose results.\n * @param {string} options.customValidator - Optional. A string representing the URL of the carbon.txt validator endpoint.\n * @param {string} options.apiKey - Optional. A string representing the API key to use for the request.\n */\n\nexport async function check(domain, options) {\n if (typeof domain !== \"string\") {\n throw new Error(\"Invalid domain. Domain must be a string.\");\n }\n\n const agentId = options?.userAgentIdentifier;\n const verbose = options?.verbose || false;\n const apiKey = options?.apiKey || null;\n\n if (!apiKey) {\n throw new Error(\"A valid API key is required.\");\n }\n\n const validatorUrl =\n options?.customValidator ||\n \"https://carbon-txt-api.greenweb.org/api/validate/domain/\";\n\n try {\n const req = await fetch(validatorUrl, {\n headers: {\n ...getApiRequestHeaders(agentId),\n \"x-api-key\": apiKey,\n },\n method: \"POST\",\n body: JSON.stringify({ domain }),\n });\n\n const res = await req.json();\n return processResponse(res, verbose);\n } catch (error) {\n return { success: false, errors: [error.message] };\n }\n}\n\nexport default check;\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,qBAAqC;AAYrC,MAAM,kBAAkB,CAAC,KAAK,UAAU,UAAU;AAdlD;AAgBE,MAAI,CAAC,IAAI,WAAW,CAAC,IAAI,QAAQ;AAC/B,WAAO;AAAA,MACL,SAAS;AAAA,MACT,QAAQ,CAAC,IAAI,OAAO,CAAC,EAAE,GAAG;AAAA,IAC5B;AAAA,EACF;AAEA,MAAI,SAAS;AACX,WAAO;AAAA,EACT;AAEA,MAAI,CAAC,IAAI,SAAS;AAChB,WAAO,EAAE,SAAS,OAAO,QAAQ,IAAI,OAAO;AAAA,EAC9C;AAEA,SAAO;AAAA,IACL,SAAS;AAAA,IACT,KAAK,IAAI;AAAA,IACT,KAAK,EAAE,cAAa,sCAAK,SAAL,mBAAW,QAAX,mBAAgB,YAAY;AAAA,IAChD,UAAU,EAAE,WAAU,sCAAK,SAAL,mBAAW,aAAX,mBAAqB,SAAS;AAAA,EACtD;AACF;AAYA,eAAsB,MAAM,QAAQ,SAAS;AAC3C,MAAI,OAAO,WAAW,UAAU;AAC9B,UAAM,IAAI,MAAM,0CAA0C;AAAA,EAC5D;AAEA,QAAM,UAAU,mCAAS;AACzB,QAAM,WAAU,mCAAS,YAAW;AACpC,QAAM,UAAS,mCAAS,WAAU;AAElC,MAAI,CAAC,QAAQ;AACX,UAAM,IAAI,MAAM,8BAA8B;AAAA,EAChD;AAEA,QAAM,gBACJ,mCAAS,oBACT;AAEF,MAAI;AACF,UAAM,MAAM,MAAM,MAAM,cAAc;AAAA,MACpC,SAAS;AAAA,QACP,OAAG,qCAAqB,OAAO;AAAA,QAC/B,aAAa;AAAA,MACf;AAAA,MACA,QAAQ;AAAA,MACR,MAAM,KAAK,UAAU,EAAE,OAAO,CAAC;AAAA,IACjC,CAAC;AAED,UAAM,MAAM,MAAM,IAAI,KAAK;AAC3B,WAAO,gBAAgB,KAAK,OAAO;AAAA,EACrC,SAAS,OAAO;AACd,WAAO,EAAE,SAAS,OAAO,QAAQ,CAAC,MAAM,OAAO,EAAE;AAAA,EACnD;AACF;AAEA,IAAO,qBAAQ;",
6
6
  "names": []
7
7
  }
@@ -197,7 +197,7 @@ Falling back to default value.`
197
197
  return adjustments;
198
198
  }
199
199
  function getApiRequestHeaders(comment = "") {
200
- return { "User-Agent": `co2js/${"0.18.1-0"} ${comment}` };
200
+ return { "User-Agent": `co2js/${"0.18.2-0"} ${comment}` };
201
201
  }
202
202
  function outputRating(co2e, swdmVersion) {
203
203
  let {
@@ -1,3 +1,22 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
1
20
  import { getApiRequestHeaders } from "./helpers/index.js";
2
21
  const processResponse = (res, verbose = false) => {
3
22
  var _a, _b, _c, _d;
@@ -16,8 +35,8 @@ const processResponse = (res, verbose = false) => {
16
35
  return {
17
36
  success: true,
18
37
  url: res.url,
19
- disclosures: (_b = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.org) == null ? void 0 : _b.disclosures,
20
- upstream: (_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c.upstream) == null ? void 0 : _d.services
38
+ org: { disclosures: (_b = (_a = res == null ? void 0 : res.data) == null ? void 0 : _a.org) == null ? void 0 : _b.disclosures },
39
+ upstream: { services: (_d = (_c = res == null ? void 0 : res.data) == null ? void 0 : _c.upstream) == null ? void 0 : _d.services }
21
40
  };
22
41
  };
23
42
  async function check(domain, options) {
@@ -26,16 +45,24 @@ async function check(domain, options) {
26
45
  }
27
46
  const agentId = options == null ? void 0 : options.userAgentIdentifier;
28
47
  const verbose = (options == null ? void 0 : options.verbose) || false;
29
- const req = await fetch(
30
- "https://carbon-txt-api.greenweb.org/api/validate/domain/",
31
- {
32
- headers: getApiRequestHeaders(agentId),
48
+ const apiKey = (options == null ? void 0 : options.apiKey) || null;
49
+ if (!apiKey) {
50
+ throw new Error("A valid API key is required.");
51
+ }
52
+ const validatorUrl = (options == null ? void 0 : options.customValidator) || "https://carbon-txt-api.greenweb.org/api/validate/domain/";
53
+ try {
54
+ const req = await fetch(validatorUrl, {
55
+ headers: __spreadProps(__spreadValues({}, getApiRequestHeaders(agentId)), {
56
+ "x-api-key": apiKey
57
+ }),
33
58
  method: "POST",
34
59
  body: JSON.stringify({ domain })
35
- }
36
- );
37
- const res = await req.json();
38
- return processResponse(res, verbose);
60
+ });
61
+ const res = await req.json();
62
+ return processResponse(res, verbose);
63
+ } catch (error) {
64
+ return { success: false, errors: [error.message] };
65
+ }
39
66
  }
40
67
  var carbon_txt_default = check;
41
68
  export {
@@ -164,7 +164,7 @@ function parseVisitTraceOptions(options = {}, version = 3, green = false) {
164
164
  return adjustments;
165
165
  }
166
166
  function getApiRequestHeaders(comment = "") {
167
- return { "User-Agent": "co2js/".concat("0.18.1-0", " ").concat(comment) };
167
+ return { "User-Agent": "co2js/".concat("0.18.2-0", " ").concat(comment) };
168
168
  }
169
169
  function outputRating(co2e, swdmVersion) {
170
170
  let {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tgwf/co2",
3
- "version": "0.18.1-0",
3
+ "version": "0.18.2-0",
4
4
  "description": "Work out the co2 of your digital services",
5
5
  "main": "dist/cjs/index-node.js",
6
6
  "module": "dist/esm/index.js",