diff --git a/src/components/Options.tsx b/src/components/Options.tsx index ddf6dad..c785c5c 100644 --- a/src/components/Options.tsx +++ b/src/components/Options.tsx @@ -9,6 +9,7 @@ interface State { alertText: string; alertType: SuccessOrFailType; apiKey: string; + apiUrl: string; blacklist: string; displayAlert: boolean; hostname: string; @@ -25,6 +26,7 @@ export default function Options(): JSX.Element { alertText: config.alert.success.text, alertType: config.alert.success.type, apiKey: '', + apiUrl: config.apiUrl, blacklist: '', displayAlert: false, hostname: '', @@ -42,6 +44,7 @@ export default function Options(): JSX.Element { const restoreSettings = async (): Promise => { const items = await browser.storage.sync.get({ apiKey: config.apiKey, + apiUrl: config.apiUrl, blacklist: '', hostname: config.hostname, loggingStyle: config.loggingStyle, @@ -54,6 +57,7 @@ export default function Options(): JSX.Element { setState({ ...state, apiKey: items.apiKey as string, + apiUrl: items.apiUrl as string, blacklist: items.blacklist as string, hostname: items.hostname as string, loggingStyle: items.loggingStyle as string, @@ -82,6 +86,7 @@ export default function Options(): JSX.Element { setState({ ...state, loading: true }); const apiKey = state.apiKey; + const apiUrl = state.apiUrl; const theme = state.theme; const hostname = state.hostname; const loggingType = state.loggingType; @@ -95,6 +100,7 @@ export default function Options(): JSX.Element { // Sync options with google storage. await browser.storage.sync.set({ apiKey, + apiUrl, blacklist, hostname, loggingStyle, @@ -109,6 +115,7 @@ export default function Options(): JSX.Element { setState({ ...state, apiKey, + apiUrl, blacklist, displayAlert: true, hostname, @@ -273,6 +280,23 @@ export default function Options(): JSX.Element { +
+ + +
+ setState({ ...state, apiUrl: e.target.value })} + placeholder="https://wakatime.com/api/v1" + /> + https://wakatime.com/api/v1 +
+
+
browser.runtime.openOptionsPage()} style={{ cursor: 'pointer' }} /> diff --git a/src/config/config.test.ts b/src/config/config.test.ts index 09fc792..623fac0 100644 --- a/src/config/config.test.ts +++ b/src/config/config.test.ts @@ -25,13 +25,14 @@ describe('wakatime config', () => { }, }, "apiKey": "", + "apiUrl": "https://wakatime.com/api/v1", "colors": { "allGood": "", "lightTheme": "white", "notLogging": "gray", "notSignedIn": "red", }, - "currentUserApiUrl": "https://wakatime.com/api/v1/users/current", + "currentUserApiEndPoint": "/users/current", "detectionIntervalInSeconds": 60, "devSites": "https://codepen.io/ https://www.codewars.com/ @@ -43,7 +44,7 @@ describe('wakatime config', () => { https://stackoverflow.com/ https://www.udemy.com/ https://www.w3schools.com/", - "heartbeatApiUrl": "https://wakatime.com/api/v1/users/current/heartbeats", + "heartbeatApiEndPoint": "/users/current/heartbeats", "hostname": "", "loggingEnabled": true, "loggingStyle": "blacklist", @@ -67,7 +68,7 @@ describe('wakatime config', () => { "blacklisted", "whitelisted", ], - "summariesApiUrl": "https://wakatime.com/api/v1/users/current/summaries", + "summariesApiEndPoint": "/users/current/summaries", "theme": "light", "tooltips": { "allGood": "", diff --git a/src/config/config.ts b/src/config/config.ts index c74d6a9..08406ed 100644 --- a/src/config/config.ts +++ b/src/config/config.ts @@ -53,11 +53,12 @@ export interface Config { * API key use to query wakatime api */ apiKey: ''; + apiUrl: string; colors: Colors; /** * Url from which to detect if the user is logged in */ - currentUserApiUrl: string; + currentUserApiEndPoint: string; /** * Time for idle state of the browser * The user is considered idle if there was @@ -69,7 +70,7 @@ export interface Config { /** * Url to which to send the heartbeat */ - heartbeatApiUrl: string; + heartbeatApiEndPoint: string; hostname: string; /** @@ -91,7 +92,7 @@ export interface Config { /** * Get stats from the wakatime api */ - summariesApiUrl: string; + summariesApiEndPoint: string; /** * Options for theme */ @@ -118,6 +119,8 @@ const config: Config = { apiKey: '', + apiUrl: process.env.API_URL ?? 'https://wakatime.com/api/v1', + colors: { allGood: '', lightTheme: 'white', @@ -125,16 +128,14 @@ const config: Config = { notSignedIn: 'red', }, - currentUserApiUrl: - process.env.CURRENT_USER_API_URL ?? 'https://wakatime.com/api/v1/users/current', + currentUserApiEndPoint: process.env.CURRENT_USER_API_URL ?? '/users/current', detectionIntervalInSeconds: 60, devSites: 'https://codepen.io/\nhttps://www.codewars.com/\nhttps://dev.to/\nhttps://github.com/\nhttps://www.hackerrank.com/\nhttps://leetcode.com/\nhttps://developer.mozilla.org/en-US/\nhttps://stackoverflow.com/\nhttps://www.udemy.com/\nhttps://www.w3schools.com/', - heartbeatApiUrl: - process.env.HEART_BEAT_API_URL ?? 'https://wakatime.com/api/v1/users/current/heartbeats', + heartbeatApiEndPoint: process.env.HEARTBEAT_API_URL ?? '/users/current/heartbeats', hostname: '', @@ -152,8 +153,7 @@ const config: Config = { states: ['allGood', 'notLogging', 'notSignedIn', 'blacklisted', 'whitelisted'], - summariesApiUrl: - process.env.SUMMARIES_API_URL ?? 'https://wakatime.com/api/v1/users/current/summaries', + summariesApiEndPoint: process.env.SUMMARIES_API_URL ?? '/users/current/summaries', theme: 'light', diff --git a/src/core/WakaTimeCore.ts b/src/core/WakaTimeCore.ts index 029c908..7f6fa99 100644 --- a/src/core/WakaTimeCore.ts +++ b/src/core/WakaTimeCore.ts @@ -49,9 +49,14 @@ class WakaTimeCore { } async getTotalTimeLoggedToday(api_key = ''): Promise { + const items = await browser.storage.sync.get({ + apiUrl: config.apiUrl, + summariesApiEndPoint: config.summariesApiEndPoint, + }); + const today = moment().format('YYYY-MM-DD'); const summariesAxiosPayload: AxiosResponse = await axios.get( - config.summariesApiUrl, + `${items.apiUrl}${items.summariesApiEndPoint}`, { params: { api_key, @@ -70,8 +75,13 @@ class WakaTimeCore { */ async fetchApiKey(): Promise { try { + const items = await browser.storage.sync.get({ + apiUrl: config.apiUrl, + currentUserApiEndPoint: config.currentUserApiEndPoint, + }); + const apiKeyResponse: AxiosResponse = await axios.post( - `${config.currentUserApiUrl}/get_api_key`, + `${items.apiUrl}${items.currentUserApiEndPoint}/get_api_key`, ); return apiKeyResponse.data.data.api_key; } catch (err: unknown) { @@ -85,8 +95,12 @@ class WakaTimeCore { * @returns {*} */ async checkAuth(api_key = ''): Promise { + const items = await browser.storage.sync.get({ + apiUrl: config.apiUrl, + currentUserApiEndPoint: config.currentUserApiEndPoint, + }); const userPayload: AxiosResponse = await axios.get( - config.currentUserApiUrl, + `${items.apiUrl}${items.currentUserApiEndPoint}`, { params: { api_key } }, ); return userPayload.data.data; @@ -345,6 +359,11 @@ class WakaTimeCore { hostname = '', ): Promise { try { + const items = await browser.storage.sync.get({ + apiUrl: config.apiUrl, + heartbeatApiEndPoint: config.heartbeatApiEndPoint, + }); + const request: RequestInit = { body: JSON.stringify(payload), credentials: 'omit', @@ -355,7 +374,10 @@ class WakaTimeCore { 'X-Machine-Name': hostname, }; } - const response = await fetch(`${config.heartbeatApiUrl}?api_key=${apiKey}`, request); + const response = await fetch( + `${items.apiUrl}${items.heartbeatApiEndPoint}?api_key=${apiKey}`, + request, + ); await response.json(); } catch (err: unknown) { if (this.db) { diff --git a/src/manifests/chrome.json b/src/manifests/chrome.json index 38ef686..e6b60c8 100644 --- a/src/manifests/chrome.json +++ b/src/manifests/chrome.json @@ -26,5 +26,5 @@ "page": "options.html" }, "permissions": ["alarms", "tabs", "storage", "idle"], - "version": "3.0.7" + "version": "3.0.8" } diff --git a/src/manifests/firefox.json b/src/manifests/firefox.json index 796f710..46121eb 100644 --- a/src/manifests/firefox.json +++ b/src/manifests/firefox.json @@ -39,5 +39,5 @@ "storage", "idle" ], - "version": "3.0.7" + "version": "3.0.8" } diff --git a/src/reducers/currentUser.ts b/src/reducers/currentUser.ts index 86fc627..9ae4e18 100644 --- a/src/reducers/currentUser.ts +++ b/src/reducers/currentUser.ts @@ -1,7 +1,8 @@ -import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'; +import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'; import axios, { AxiosResponse } from 'axios'; -import { CurrentUser, User, UserPayload } from '../types/user'; +import browser from 'webextension-polyfill'; import config from '../config/config'; +import { CurrentUser, User, UserPayload } from '../types/user'; interface setUserAction { payload: User | undefined; @@ -14,9 +15,16 @@ export const name: NameType = 'currentUser'; export const fetchCurrentUser = createAsyncThunk( `[${name}]`, async (api_key = '') => { - const userPayload: AxiosResponse = await axios.get(config.currentUserApiUrl, { - params: { api_key }, + const items = await browser.storage.sync.get({ + apiUrl: config.apiUrl, + currentUserApiEndPoint: config.currentUserApiEndPoint, }); + const userPayload: AxiosResponse = await axios.get( + `${items.apiUrl}${items.currentUserApiEndPoint}`, + { + params: { api_key }, + }, + ); return userPayload.data.data; }, ); diff --git a/webpack.config.ts b/webpack.config.ts index a6f5b97..6d6d3d0 100644 --- a/webpack.config.ts +++ b/webpack.config.ts @@ -55,17 +55,12 @@ const getConfigByBrowser = (isProd: boolean, browser: BrowserTypes): webpack.Con ], }), new webpack.DefinePlugin({ - ['process.env.CURRENT_USER_API_URL']: JSON.stringify( - 'https://wakatime.com/api/v1/users/current', - ), - ['process.env.HEART_BEAT_API_URL']: JSON.stringify( - 'https://wakatime.com/api/v1/users/current/heartbeats', - ), + ['process.env.API_URL']: JSON.stringify('https://wakatime.com/api/v1'), + ['process.env.CURRENT_USER_API_URL']: JSON.stringify('/users/current'), + ['process.env.HEARTBEAT_API_URL']: JSON.stringify('/users/current/heartbeats'), ['process.env.LOGOUT_USER_URL']: JSON.stringify('https://wakatime.com/logout'), ['process.env.NODE_ENV']: JSON.stringify(isProd ? 'production' : 'development'), - ['process.env.SUMMARIES_API_URL']: JSON.stringify( - 'https://wakatime.com/api/v1/users/current/summaries', - ), + ['process.env.SUMMARIES_API_URL']: JSON.stringify('/users/current/summaries'), }), ], resolve: {