Skip to content

Instantly share code, notes, and snippets.

@321zeno
Created October 8, 2020 17:43
Show Gist options
  • Save 321zeno/2d944146432d6670f1d56a47b1fb10f9 to your computer and use it in GitHub Desktop.
Save 321zeno/2d944146432d6670f1d56a47b1fb10f9 to your computer and use it in GitHub Desktop.
Clockify Helper
async function clockifyRequest(endpoint, token, method = 'GET', body) {
const fetchParams = {
headers: {
'X-Api-Key': token,
'Accept': 'application/json',
'Content-Type': 'application/json'
},
method: method,
}
if (!!body) {
fetchParams.body = JSON.stringify(body);
}
let response = await fetch(endpoint, fetchParams);
return response.json();
}
async function startTimer(description, token) {
let user = await clockifyRequest(`https://api.clockify.me/api/v1/user`, token)
// check if the user has any running entries
let inProgress = await clockifyRequest(`https://api.clockify.me/api/v1/workspaces/${user.activeWorkspace}/user/${user.id}/time-entries?in-progress=1`, token);
// stop any running entries
if (!!inProgress.length) {
await clockifyRequest(`https://api.clockify.me/api/v1/workspaces/${user.activeWorkspace}/user/${user.id}/time-entries`, token, 'PATCH', {
end: new Date()
});
}
// finally start a new entry
await clockifyRequest(`https://api.clockify.me/api/v1/workspaces/${user.activeWorkspace}/user/${user.id}/time-entries`, token, 'POST', {
start: new Date(),
description: description
});
}
const token = 'XXXXXXXXXXXXX';
;(async () => {
try {
await startTimer('Make it happen', token)
} catch (error) {
console.trace(error);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment