From 925510ee34f15bf5522491f1c1cf4241b2f5aef4 Mon Sep 17 00:00:00 2001 From: Simon Fishel Date: Sat, 16 Mar 2024 14:17:32 -0700 Subject: [PATCH] initial commit of tsconfig.json file and some initial jsdoc updates fixes #199 --- package-lock.json | 13 +++++++++++++ package.json | 3 ++- src/helpers/index.js | 7 +++++-- src/hosting-api.js | 10 +++++----- src/hosting.js | 4 ++-- tsconfig.json | 10 ++++++++++ 6 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 tsconfig.json diff --git a/package-lock.json b/package-lock.json index 03c912d..d816385 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "Apache-2.0", "devDependencies": { "@tgwf/url2green": "^0.4.0", + "@tsconfig/strictest": "^2.0.3", "all-contributors-cli": "^6.26.1", "esbuild": "^0.14.47", "esbuild-jest": "^0.5.0", @@ -1387,6 +1388,12 @@ "debug": "^4.1.1" } }, + "node_modules/@tsconfig/strictest": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@tsconfig/strictest/-/strictest-2.0.3.tgz", + "integrity": "sha512-MroLvRhMbqtXI5WBSwoomro6OQS4xnCoudUrMb20JO0vLKUs0bAaCEcvM/immEBSJjFAK1l6jW1oAO8q3Ancrg==", + "dev": true + }, "node_modules/@types/babel__core": { "version": "7.1.19", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.19.tgz", @@ -13229,6 +13236,12 @@ "debug": "^4.1.1" } }, + "@tsconfig/strictest": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@tsconfig/strictest/-/strictest-2.0.3.tgz", + "integrity": "sha512-MroLvRhMbqtXI5WBSwoomro6OQS4xnCoudUrMb20JO0vLKUs0bAaCEcvM/immEBSJjFAK1l6jW1oAO8q3Ancrg==", + "dev": true + }, "@types/babel__core": { "version": "7.1.19", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.19.tgz", diff --git a/package.json b/package.json index bd4ef74..0298f08 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "license": "Apache-2.0", "devDependencies": { "@tgwf/url2green": "^0.4.0", + "@tsconfig/strictest": "^2.0.3", "all-contributors-cli": "^6.26.1", "esbuild": "^0.14.47", "esbuild-jest": "^0.5.0", @@ -76,4 +77,4 @@ "type": "git", "url": "https://github.com/thegreenwebfoundation/co2.js.git" } -} +} \ No newline at end of file diff --git a/src/helpers/index.js b/src/helpers/index.js index 29db234..fa99c10 100644 --- a/src/helpers/index.js +++ b/src/helpers/index.js @@ -176,10 +176,13 @@ function parseOptions(options) { * Returns an object containing all the HTTP headers to use when making a request to the Green Web Foundation API. * @param {string} comment - Optional. The app, site, or organisation that is making the request. * - * @returns {import('http').OutgoingHttpHeaders} + * @returns {Record} */ function getApiRequestHeaders(comment = "") { - return { "User-Agent": `co2js/${process.env.CO2JS_VERSION} ${comment}` }; + if (!process.env["CO2JS_VERSION"]) { + return {}; + } + return { "User-Agent": `co2js/${process.env["CO2JS_VERSION"]} ${comment}` }; } export { formatNumber, parseOptions, getApiRequestHeaders }; diff --git a/src/hosting-api.js b/src/hosting-api.js index 60337a7..55a92fb 100644 --- a/src/hosting-api.js +++ b/src/hosting-api.js @@ -4,7 +4,7 @@ import { getApiRequestHeaders } from "./helpers/index.js"; /** * Check if a string or array of domains has been provided - * @param {string|array} domain - The domain to check, or an array of domains to be checked. + * @param {string | string[]} domain - The domain to check, or an array of domains to be checked. * @param {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request. */ @@ -21,7 +21,7 @@ function check(domain, userAgentIdentifier) { * Check if a domain is hosted by a green web host by querying the Green Web Foundation API. * @param {string} domain - The domain to check. * @param {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request. - * @returns {boolean} - A boolean indicating whether the domain is hosted by a green web host. + * @returns {Promise} - A boolean indicating whether the domain is hosted by a green web host. */ async function checkAgainstAPI(domain, userAgentIdentifier) { const req = await fetch( @@ -36,9 +36,9 @@ async function checkAgainstAPI(domain, userAgentIdentifier) { /** * Check if an array of domains is hosted by a green web host by querying the Green Web Foundation API. - * @param {array} domains - An array of domains to check. + * @param {string[]} domains - An array of domains to check. * @param {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request. - * @returns {array} - An array of domains that are hosted by a green web host. + * @returns {Promise} - An array of domains that are hosted by a green web host. */ async function checkDomainsAgainstAPI(domains, userAgentIdentifier) { @@ -61,7 +61,7 @@ async function checkDomainsAgainstAPI(domains, userAgentIdentifier) { /** * Extract the green domains from the results of a green check. * @param {object} greenResults - The results of a green check. - * @returns {array} - An array of domains that are hosted by a green web host. + * @returns {string[]} - An array of domains that are hosted by a green web host. */ function greenDomainsFromResults(greenResults) { const entries = Object.entries(greenResults); diff --git a/src/hosting.js b/src/hosting.js index d0020eb..e530614 100644 --- a/src/hosting.js +++ b/src/hosting.js @@ -4,9 +4,9 @@ import hostingAPI from "./hosting-api.js"; /** * Check if a domain is hosted by a green web host. - * @param {string|array} domain - The domain to check, or an array of domains to be checked. + * @param {string | string[]} domain - The domain to check, or an array of domains to be checked. * @param {string} userAgentIdentifier - Optional. The app, site, or organisation that is making the request. - * @returns {boolean|array} - A boolean if a string was provided, or an array of booleans if an array of domains was provided. + * @returns {Promise} - A boolean if a string was provided, or an array of strings if an array of domains was provided. */ function check(domain, userAgentIdentifier) { return hostingAPI.check(domain, userAgentIdentifier); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..06677f9 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "@tsconfig/strictest/tsconfig.json", + "include": ["src/**/*"], + "compilerOptions": { + "noEmit": true, + "strict": true, + "allowJs": true, + "checkJs": true, + } +} \ No newline at end of file