UNPKG

@tgwf/co2

Version:
8 lines (7 loc) 4.59 kB
{ "version": 3, "sources": ["../../src/hosting-api.js"], "sourcesContent": ["\"use strict\";\n\nimport { getApiRequestHeaders } from \"./helpers/index.js\";\nimport hostingJSON from \"./hosting-json.js\";\n\n/**\n * Check if a string or array of domains is hosted by a green web host by querying the Green Web Foundation API.\n * @param {string|array} domain - The domain to check, or an array of domains to be checked.\n * @param {string | DomainCheckOptions} optionsOrAgentId - Optional. An object of domain check options, or a string\n * representing the app, site, or organisation that is making the request.\n */\n\nfunction check(domain, optionsOrAgentId) {\n const options =\n typeof optionsOrAgentId === \"string\"\n ? { userAgentIdentifier: optionsOrAgentId }\n : optionsOrAgentId;\n\n if (options?.db && options.verbose) {\n throw new Error(\"verbose mode cannot be used with a local lookup database\");\n }\n // is it a single domain or an array of them?\n if (typeof domain === \"string\") {\n return checkAgainstAPI(domain, options);\n } else {\n return checkDomainsAgainstAPI(domain, options);\n }\n}\n\n/**\n * Check if a domain is hosted by a green web host by querying the Green Web Foundation API.\n * @param {string} domain - The domain to check.\n * @param {DomainCheckOptions} options\n * @returns - A boolean indicating whether the domain is hosted by a green web host if `options.verbose` is false,\n * otherwise an object representing the domain host information.\n */\nasync function checkAgainstAPI(domain, options = {}) {\n const req = await fetch(\n `https://api.thegreenwebfoundation.org/greencheck/${domain}`,\n {\n headers: getApiRequestHeaders(options.userAgentIdentifier),\n }\n );\n if (options?.db) {\n return hostingJSON.check(domain, options.db);\n }\n const res = await req.json();\n return options.verbose ? res : res.green;\n}\n\n/**\n * Check if an array of domains is hosted by a green web host by querying the Green Web Foundation API.\n * @param {array} domains - An array of domains to check.\n * @param {DomainCheckOptions} options\n * @returns - An array of domains that are hosted by a green web host if `options.verbose` is false,\n * otherwise a dictionary of domain to host information.\n */\n\nasync function checkDomainsAgainstAPI(domains, options = {}) {\n try {\n const apiPath = \"https://api.thegreenwebfoundation.org/v2/greencheckmulti\";\n const domainsString = JSON.stringify(domains);\n\n const req = await fetch(`${apiPath}/${domainsString}`, {\n headers: getApiRequestHeaders(options.userAgentIdentifier),\n });\n\n const allGreenCheckResults = await req.json();\n\n return options.verbose\n ? allGreenCheckResults\n : greenDomainsFromResults(allGreenCheckResults);\n } catch (e) {\n return options.verbose ? {} : [];\n }\n}\n\n/**\n * Extract the green domains from the results of a green check.\n * @param {object} greenResults - The results of a green check.\n * @returns {array} - An array of domains that are hosted by a green web host.\n */\nfunction greenDomainsFromResults(greenResults) {\n const entries = Object.entries(greenResults);\n const greenEntries = entries.filter(([key, val]) => val.green);\n return greenEntries.map(([key, val]) => val.url);\n}\n\nexport default {\n check,\n};\n"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,qBAAqC;AACrC,0BAAwB;AASxB,SAAS,MAAM,QAAQ,kBAAkB;AACvC,QAAM,UACJ,OAAO,qBAAqB,WACxB,EAAE,qBAAqB,iBAAiB,IACxC;AAEN,OAAI,mCAAS,OAAM,QAAQ,SAAS;AAClC,UAAM,IAAI,MAAM,0DAA0D;AAAA,EAC5E;AAEA,MAAI,OAAO,WAAW,UAAU;AAC9B,WAAO,gBAAgB,QAAQ,OAAO;AAAA,EACxC,OAAO;AACL,WAAO,uBAAuB,QAAQ,OAAO;AAAA,EAC/C;AACF;AASA,eAAe,gBAAgB,QAAQ,UAAU,CAAC,GAAG;AACnD,QAAM,MAAM,MAAM;AAAA,IAChB,oDAAoD;AAAA,IACpD;AAAA,MACE,aAAS,qCAAqB,QAAQ,mBAAmB;AAAA,IAC3D;AAAA,EACF;AACA,MAAI,mCAAS,IAAI;AACf,WAAO,oBAAAA,QAAY,MAAM,QAAQ,QAAQ,EAAE;AAAA,EAC7C;AACA,QAAM,MAAM,MAAM,IAAI,KAAK;AAC3B,SAAO,QAAQ,UAAU,MAAM,IAAI;AACrC;AAUA,eAAe,uBAAuB,SAAS,UAAU,CAAC,GAAG;AAC3D,MAAI;AACF,UAAM,UAAU;AAChB,UAAM,gBAAgB,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