From 6547f4916b428fadb29b2bdf8ecd97d5baf7d8f3 Mon Sep 17 00:00:00 2001 From: Sebastian Velez Date: Thu, 19 Jan 2023 05:45:17 -0500 Subject: [PATCH] chore: send api token to heartbeat end point --- src/core/WakaTimeCore.ts | 17 ++++++++--------- src/utils/user.ts | 2 +- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/core/WakaTimeCore.ts b/src/core/WakaTimeCore.ts index 0add85c..5e20e58 100644 --- a/src/core/WakaTimeCore.ts +++ b/src/core/WakaTimeCore.ts @@ -52,7 +52,7 @@ class WakaTimeCore { * Depending on various factors detects the current active tab URL or domain, * and sends it to WakaTime for logging. */ - async recordHeartbeat(): Promise { + async recordHeartbeat(apiKey: string): Promise { const items = await browser.storage.sync.get({ blacklist: '', loggingEnabled: config.loggingEnabled, @@ -90,6 +90,7 @@ class WakaTimeCore { url: currentActiveTab.url as string, }, debug, + apiKey, ); } else { await changeExtensionState('blacklisted'); @@ -103,7 +104,7 @@ class WakaTimeCore { items.whitelist as string, ); if (heartbeat.url) { - await this.sendHeartbeat(heartbeat, debug); + await this.sendHeartbeat(heartbeat, debug, apiKey); } else { await changeExtensionState('whitelisted'); console.log(`${currentActiveTab.url} is not on a whitelist.`); @@ -182,8 +183,8 @@ class WakaTimeCore { * @param heartbeat * @param debug */ - async sendHeartbeat(heartbeat: SendHeartbeat, debug: boolean): Promise { - let payload = null; + async sendHeartbeat(heartbeat: SendHeartbeat, debug: boolean, apiKey: string): Promise { + let payload; const loggingType = await this.getLoggingType(); // Get only the domain from the entity. @@ -191,14 +192,12 @@ class WakaTimeCore { if (loggingType == 'domain') { heartbeat.url = getDomainFromUrl(heartbeat.url); payload = this.preparePayload(heartbeat, 'domain', debug); - console.log(payload); - await this.sendAjaxRequestToApi(payload); + await this.sendPostRequestToApi(payload, apiKey); } // Send entity in heartbeat else if (loggingType == 'url') { payload = this.preparePayload(heartbeat, 'url', debug); - console.log(payload); - await this.sendAjaxRequestToApi(payload); + await this.sendPostRequestToApi(payload, apiKey); } } @@ -243,7 +242,7 @@ class WakaTimeCore { * @param method * @returns {*} */ - async sendAjaxRequestToApi(payload: Record, api_key = '') { + async sendPostRequestToApi(payload: Record, api_key = '') { try { const response = await axios.post(config.heartbeatApiUrl, payload, { params: { diff --git a/src/utils/user.ts b/src/utils/user.ts index d87b741..6166ac7 100644 --- a/src/utils/user.ts +++ b/src/utils/user.ts @@ -38,7 +38,7 @@ export const fetchUserData = async ( dispatch(setLoggingEnabled(items.loggingEnabled as boolean)); dispatch(setTotalTimeLoggedToday(totalTimeLoggedTodayResponse.text)); - await WakaTimeCore.recordHeartbeat(); + await WakaTimeCore.recordHeartbeat(apiKey); } catch (err: unknown) { await changeExtensionState('notSignedIn'); }