chore: changes axios for fetch, service workers dont go along with axios

This commit is contained in:
Sebastian Velez
2023-01-23 11:33:36 -05:00
parent a828f15a6f
commit a0c3d35a95
2 changed files with 21 additions and 10 deletions

View File

@@ -1,11 +1,10 @@
import axios, { AxiosResponse } from 'axios';
import moment from 'moment';
import { Tabs } from 'webextension-polyfill';
import browser, { Tabs } from 'webextension-polyfill';
import { AxiosUserResponse, User } from '../types/user';
import config from '../config/config';
import { SummariesPayload, GrandTotal } from '../types/summaries';
import changeExtensionState from '../utils/changeExtensionState';
import inArray from '../utils/inArray';
import contains from '../utils/contains';
import { SendHeartbeat } from '../types/heartbeats';
import getDomainFromUrl from '../utils/getDomainFromUrl';
@@ -48,11 +47,23 @@ class WakaTimeCore {
return userPayload.data.data;
}
async getApiKey(): Promise<string> {
const storage = await browser.storage.sync.get({
apiKey: config.apiKey,
});
const apiKey = storage.apiKey as string;
return apiKey;
}
/**
* Depending on various factors detects the current active tab URL or domain,
* and sends it to WakaTime for logging.
*/
async recordHeartbeat(apiKey: string): Promise<void> {
async recordHeartbeat(): Promise<void> {
const apiKey = await this.getApiKey();
if (!apiKey) {
return changeExtensionState('notLogging');
}
const items = await browser.storage.sync.get({
blacklist: '',
loggingEnabled: config.loggingEnabled,
@@ -234,14 +245,14 @@ class WakaTimeCore {
* @param method
* @returns {*}
*/
async sendPostRequestToApi(payload: Record<string, unknown>, api_key = '') {
async sendPostRequestToApi(payload: Record<string, unknown>, apiKey = '') {
try {
const response = await axios.post(config.heartbeatApiUrl, payload, {
params: {
api_key,
},
const response = await fetch(`${config.heartbeatApiUrl}?api_key=${apiKey}`, {
body: JSON.stringify(payload),
method: 'POST',
});
return response.data;
const data = await response.json();
return data;
} catch (err: unknown) {
await changeExtensionState('notSignedIn');
}

View File

@@ -58,7 +58,7 @@ export const fetchUserData = async (
dispatch(setLoggingEnabled(items.loggingEnabled as boolean));
dispatch(setTotalTimeLoggedToday(totalTimeLoggedTodayResponse.text));
await WakaTimeCore.recordHeartbeat(apiKey);
await WakaTimeCore.recordHeartbeat();
} catch (err: unknown) {
await changeExtensionState('notSignedIn');
}