From 8894cc213237420c78d9daca075b89490f58b7dc 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 | 32 ++++++++++++++++++++++---------- package.json | 6 +++++- src/helpers/index.js | 7 +++++-- src/hosting-api.js | 10 +++++----- src/hosting.js | 4 ++-- tsconfig.json | 11 +++++++++++ 6 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 tsconfig.json diff --git a/package-lock.json b/package-lock.json index 03c912d..bf7541b 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", @@ -22,7 +23,8 @@ "nock": "^13.2.4", "np": "^8.0.4", "pagexray": "^4.4.2", - "prettier": "^2.6.2" + "prettier": "^2.6.2", + "typescript": "^5.4.2" }, "engines": { "node": ">=14.0.0" @@ -1387,6 +1389,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", @@ -11662,11 +11670,10 @@ } }, "node_modules/typescript": { - "version": "5.1.6", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", - "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.2.tgz", + "integrity": "sha512-+2/g0Fds1ERlP6JsakQQDXjZdZMM+rqpamFZJEKh4kwTIn3iDkgKtby0CeNd5ATNZ4Ry1ax15TMx0W2V+miizQ==", "dev": true, - "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" @@ -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", @@ -20748,11 +20761,10 @@ } }, "typescript": { - "version": "5.1.6", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.1.6.tgz", - "integrity": "sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==", - "dev": true, - "peer": true + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.2.tgz", + "integrity": "sha512-+2/g0Fds1ERlP6JsakQQDXjZdZMM+rqpamFZJEKh4kwTIn3iDkgKtby0CeNd5ATNZ4Ry1ax15TMx0W2V+miizQ==", + "dev": true }, "union-value": { "version": "1.0.1", diff --git a/package.json b/package.json index 7b20fb8..fa6ed8d 100644 --- a/package.json +++ b/package.json @@ -18,11 +18,13 @@ "test:watch": "jest --watch src", "lint": "eslint src", "lint:fix": "eslint src --fix", + "check:types": "tsc --noEmit", "serve": "python -m http.server --directory ./public", "build": "npm run build:esm && npm run build:browser && npm run build:node", "build:esm": "node .esbuild.esm.js && ./fixup", "build:browser": "node .esbuild.browser.js", "build:node": "node .esbuild.node.js && ./fixup", + "emit:types": "tsc --emitDeclarationOnly --outDir dist/", "release:minor": "npm run build && npm run intensity-data && npm run format-data && np minor", "release:patch": "npm run build && npm run intensity-data && npm run format-data && np patch", "gitpod": "npm run build && cp ./dist/iife/index.js ./public && npm run serve", @@ -45,6 +47,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", @@ -56,7 +59,8 @@ "jest": "^28.1.0", "nock": "^13.2.4", "np": "^8.0.4", - "prettier": "^2.6.2" + "prettier": "^2.6.2", + "typescript": "^5.4.2" }, "jest": { "transform": { 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..339b120 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "@tsconfig/strictest/tsconfig.json", + "include": ["src/**/*"], + "compilerOptions": { + "strict": true, + "allowJs": true, + "checkJs": true, + "emitDeclarationOnly": true, + "declaration": true + } +} \ No newline at end of file