Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: eslint config, dev modules and tsconfig; add: utils to ts

kruzhambus9fdb7d78e2af
diff --git a/package.json b/package.json
index 6fb48e9d0..7f54f4708 100644
--- a/package.json
+++ b/package.json
@@ -8,10 +8,6 @@
"@heroicons/react": "^2.0.16",
"@swetrix/sdk": "^1.0.0",
"@tailwindcss/forms": "^0.5.3",
- "@types/jest": "^29.5.0",
- "@types/node": "^18.15.3",
- "@types/react": "^18.0.28",
- "@types/react-dom": "^18.0.11",
"axios": "^1.3.4",
"axios-auth-refresh": "^3.3.6",
"billboard.js": "^3.7.5",
@@ -50,8 +46,7 @@
"redux-saga": "^1.2.3",
"spacetime": "^7.4.1",
"swetrix": "^2.1.0",
- "timezone-soft": "^1.4.1",
- "typescript": "^5.0.2"
+ "timezone-soft": "^1.4.1"
},
"scripts": {
"start": "react-scripts start",
@@ -78,6 +73,7 @@
"arrow-parens": "off",
"import/no-cycle": "off",
"import/no-named-as-default": "off",
+ "import/no-unresolved": [{ "devDependencies": true }],
"import/no-named-as-default-member": "off",
"import/prefer-default-export": "off",
"jsx-a11y/click-events-have-key-events": "off",
@@ -108,7 +104,8 @@
"extensions": [
".js",
".jsx",
- ".tsx"
+ ".tsx",
+ ".ts"
]
}
],
@@ -132,6 +129,12 @@
"moduleDirectory": [
"node_modules",
"src"
+ ],
+ "extensions": [
+ ".js",
+ ".jsx",
+ ".ts",
+ ".tsx"
]
}
}
@@ -155,6 +158,12 @@
},
"devDependencies": {
"@tailwindcss/typography": "^0.5.9",
+ "@types/jest": "^29.5.0",
+ "@types/lodash": "^4.14.191",
+ "@types/node": "^18.15.3",
+ "@types/react": "^18.0.28",
+ "@types/react-dom": "^18.0.11",
+ "@types/react-router-dom": "^5.3.3",
"autoprefixer": "^10.4.14",
"cross-env": "^7.0.3",
"eslint": "^8.36.0",
@@ -166,6 +175,7 @@
"eslint-plugin-react-hooks": "^4.6.0",
"postcss": "^8.4.21",
"source-map-explorer": "^2.5.3",
- "tailwindcss": "^3.2.7"
+ "tailwindcss": "^3.2.7",
+ "typescript": "^5.0.2"
}
}
diff --git a/src/utils/accessToken.js b/src/utils/accessToken.ts
similarity index 89%
rename from src/utils/accessToken.js
rename to src/utils/accessToken.ts
index 517e9b4d1..60c6c018c 100644
--- a/src/utils/accessToken.js
+++ b/src/utils/accessToken.ts
@@ -16,7 +16,7 @@ export const getAccessToken = () => {
return accessToken
}
-export const setAccessToken = (token, temporary = false) => {
+export const setAccessToken = (token: string, temporary: boolean = false) => {
if (temporary) {
sessionStorage.setItem(TOKEN, token)
return
diff --git a/src/utils/analytics.js b/src/utils/analytics.ts
similarity index 90%
rename from src/utils/analytics.js
rename to src/utils/analytics.ts
index 525308c76..37e684621 100644
--- a/src/utils/analytics.js
+++ b/src/utils/analytics.ts
@@ -14,7 +14,7 @@ const trackViews = () => {
}
}
-const trackCustom = (ev, unique = false) => {
+const trackCustom = (ev: string, unique = false) => {
if (!isSelfhosted) {
Swetrix.track({
ev, unique,
diff --git a/src/utils/cookie.js b/src/utils/cookie.ts
similarity index 68%
rename from src/utils/cookie.js
rename to src/utils/cookie.ts
index 5c7d15fb9..aa0e47144 100644
--- a/src/utils/cookie.js
+++ b/src/utils/cookie.ts
@@ -1,10 +1,10 @@
-import { isDevelopment } from '../redux/constants'
+import { isDevelopment } from 'redux/constants'
const COOKIE_DOMAIN = 'swetrix.com'
const COOKIE_SUFFIX = isDevelopment ? '' : `; domain=${COOKIE_DOMAIN}; secure`
-export const getCookie = key => {
+export const getCookie = (key: string) => {
const match = document.cookie.match(new RegExp(`(^| )${key}=([^;]+)`))
if (match) {
@@ -14,11 +14,11 @@ export const getCookie = key => {
return null
}
-export const setCookie = (key, value, maxAge = 3600, sameSite = 'strict') => {
+export const setCookie = (key: string, value: string, maxAge = 3600, sameSite = 'strict') => {
document.cookie = `${key}=${value}; max-age=${maxAge}; path=/; SameSite=${sameSite}${COOKIE_SUFFIX}`
}
-export const deleteCookie = (key) => {
+export const deleteCookie = (key: string) => {
document.cookie = `${key}=; max-age=0; path=/; SameSite=strict`
document.cookie = `${key}=; max-age=0; path=/; SameSite=strict${COOKIE_SUFFIX}`
}
diff --git a/src/utils/countries.js b/src/utils/countries.ts
similarity index 100%
rename from src/utils/countries.js
rename to src/utils/countries.ts
diff --git a/src/utils/generic.js b/src/utils/generic.ts
similarity index 80%
rename from src/utils/generic.js
rename to src/utils/generic.ts
index f665cc644..8744c51b5 100644
--- a/src/utils/generic.js
+++ b/src/utils/generic.ts
@@ -13,14 +13,14 @@ const formatterLookup = [
{ value: 1e9, symbol: 'B' },
]
-export const nFormatter = (num, digits = 1) => {
+export const nFormatter = (num: any, digits = 1) => {
const item = _find(formatterLookup.slice().reverse(), ({ value }) => num >= value)
return item ? _replace((num / item.value).toFixed(digits), rx, '$1') + item.symbol : '0'
}
// returns something like [123, 'k'], [5.5, 'M'], [425, null]
-export const nFormatterSeparated = (num, digits = 1) => {
+export const nFormatterSeparated = (num: any, digits = 1) => {
const item = _find(formatterLookup.slice().reverse(), ({ value }) => num >= value)
if (item) {
@@ -31,8 +31,8 @@ export const nFormatterSeparated = (num, digits = 1) => {
}
export const secondsTillNextMonth = () => {
- const now = new Date()
- const date = new Date()
+ const now: any = new Date()
+ const date: any = new Date()
date.setMonth(date.getMonth() + 1)
date.setDate(1)
@@ -41,12 +41,12 @@ export const secondsTillNextMonth = () => {
return 0 | (date - now) / 1000
}
-export const convertMsToSeconds = (ms) => {
+export const convertMsToSeconds = (ms: any) => {
return ms / 1000
}
// Returns an object like { h: 0, m: 0, s: 0 } based on the seconds parameter provided
-export const getTimeFromSeconds = (seconds) => {
+export const getTimeFromSeconds = (seconds: any) => {
const h = 0 | seconds / 3600
const m = 0 | (seconds % 3600) / 60
const s = 0 | seconds % 60
@@ -57,7 +57,7 @@ export const getTimeFromSeconds = (seconds) => {
}
}
-export const getStringFromTime = (time, showMS) => {
+export const getStringFromTime = (time: any, showMS: any) => {
const {
h, m, s, ms,
} = time
@@ -69,7 +69,7 @@ export const getStringFromTime = (time, showMS) => {
return `${h ? `${h}h ` : ''}${m ? `${m}m ` : ''}${s || (showMS && ms > 0) ? `${showMS ? _round(s + ms / 1000, 2) : s}s` : ''}`
}
-export const sumArrays = (...arrays) => {
+export const sumArrays = (...arrays: any) => {
return _map(arrays[0], (_, index) => {
return _reduce(arrays, (sum, array) => sum + array[index], 0)
})
diff --git a/src/utils/isoCountries.js b/src/utils/isoCountries.ts
similarity index 88%
rename from src/utils/isoCountries.js
rename to src/utils/isoCountries.ts
index 76873c9c2..40e5041fe 100644
--- a/src/utils/isoCountries.js
+++ b/src/utils/isoCountries.ts
@@ -1,12 +1,21 @@
import countries from 'i18n-iso-countries'
+// @ts-ignore
import countriesEn from 'i18n-iso-countries/langs/en.json'
+// @ts-ignore
import countriesDe from 'i18n-iso-countries/langs/de.json'
+// @ts-ignore
import countriesEl from 'i18n-iso-countries/langs/el.json'
+// @ts-ignore
import countriesHi from 'i18n-iso-countries/langs/hi.json'
+// @ts-ignore
import countriesUk from 'i18n-iso-countries/langs/uk.json'
+// @ts-ignore
import countriesZh from 'i18n-iso-countries/langs/zh.json'
+// @ts-ignore
import countriesRu from 'i18n-iso-countries/langs/ru.json'
+// @ts-ignore
import countriesSv from 'i18n-iso-countries/langs/sv.json'
+// @ts-ignore
import countriesPl from 'i18n-iso-countries/langs/pl.json'
countries.registerLocale(countriesEn)
diff --git a/src/utils/localstorage.js b/src/utils/localstorage.ts
similarity index 53%
rename from src/utils/localstorage.js
rename to src/utils/localstorage.ts
index c76b6f4e3..8fd108c42 100644
--- a/src/utils/localstorage.js
+++ b/src/utils/localstorage.ts
@@ -4,19 +4,25 @@
* to save performance on JSON-parsing and instead remember
* how any localStorage item was modified after first access
*/
-const currentLocalStorage = {}
-export const setItem = (key, value) => {
+type currentLocalStorageType = { [key: string]: string | null }
+const currentLocalStorage: currentLocalStorageType = {}
+
+type setItemType = (key: string, value: string) => void
+
+export const setItem: setItemType = (key, value) => {
currentLocalStorage[key] = value
localStorage.setItem(key, value)
}
-export const getItem = (key) => {
+type getItemType = (key: string) => string | null
+
+export const getItem: getItemType = (key) => {
if (currentLocalStorage[key]) {
return currentLocalStorage[key]
}
- const storedValue = localStorage.getItem(key)
+ const storedValue: any = localStorage.getItem(key)
try {
return JSON.parse(storedValue)
@@ -25,7 +31,9 @@ export const getItem = (key) => {
}
}
-export const removeItem = (key) => {
+type removeItemType = (key: string) => void
+
+export const removeItem: removeItemType = (key) => {
delete currentLocalStorage[key]
localStorage.removeItem(key)
}
diff --git a/src/utils/random.js b/src/utils/random.ts
similarity index 100%
rename from src/utils/random.js
rename to src/utils/random.ts
diff --git a/src/utils/refreshToken.js b/src/utils/refreshToken.ts
similarity index 88%
rename from src/utils/refreshToken.js
rename to src/utils/refreshToken.ts
index d5613375a..00b2f3a76 100644
--- a/src/utils/refreshToken.js
+++ b/src/utils/refreshToken.ts
@@ -8,7 +8,7 @@ export const getRefreshToken = () => {
return getCookie(REFRESH_TOKEN)
}
-export const setRefreshToken = (token) => {
+export const setRefreshToken = (token: string) => {
setCookie(REFRESH_TOKEN, token, STORE_REFRESH_TOKEN_FOR)
}
diff --git a/src/utils/validator.js b/src/utils/validator.js
deleted file mode 100644
index 65219114e..000000000
--- a/src/utils/validator.js
+++ /dev/null
@@ -1,6 +0,0 @@
-export const MIN_PASSWORD_CHARS = 8
-export const MAX_PASSWORD_CHARS = 50
-
-export const isValidEmail = text => text.match(/^\w+([.+-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,7})+$/)
-
-export const isValidPassword = text => text.length >= MIN_PASSWORD_CHARS
diff --git a/src/utils/validator.ts b/src/utils/validator.ts
new file mode 100644
index 000000000..732dd6395
--- /dev/null
+++ b/src/utils/validator.ts
@@ -0,0 +1,6 @@
+export const MIN_PASSWORD_CHARS = 8
+export const MAX_PASSWORD_CHARS = 50
+
+export const isValidEmail = (text: string) => text.match(/^\w+([.+-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,7})+$/)
+
+export const isValidPassword = (text: string) => text.length >= MIN_PASSWORD_CHARS
diff --git a/tsconfig.json b/tsconfig.json
index 7ffe32bae..217007fa7 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -39,7 +39,7 @@
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
- // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
+ "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */