-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudflareWorker_toggl.js
executable file
·89 lines (84 loc) · 2.41 KB
/
cloudflareWorker_toggl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
async function startTogglTimer (key) {
const query = await fetch('https://www.toggl.com/api/v8/time_entries/start', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${btoa(`${key}:api_token`)}`
},
body: JSON.stringify({
time_entry: {
description: 'Newtelco - Work',
tags: ['Newtelco', 'AutoAdded'],
created_with: 'Newtelco Home'
}
})
})
.then(resp => resp.json())
.then((data) => {
return data
})
.catch(err => console.error(err))
return query
}
async function stopTogglTimer (key) {
const query = await fetch('https://www.toggl.com/api/v8/time_entries/current', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${btoa(`${key}:api_token`)}`
}
})
.then(resp => resp.json())
.then(async response => {
const currentId = response.data.id
await fetch(`https://www.toggl.com/api/v8/time_entries/${currentId}/stop`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${btoa(`${key}:api_token`)}`
}
})
.then(resp => resp.json())
.then((data) => {
return data
})
.catch(err => console.error(err))
})
.catch(err => console.error(err))
return query
}
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
// 'Access-Control-Allow-Origin': "https://home.newtelco.de",
'Access-Control-Allow-Methods': 'GET, HEAD, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type'
}
async function handleRequest (request) {
const url = new URL(request.url)
const params = new URLSearchParams(url.search)
const key = params.get('key')
const action = params.get('action')
const returnData = (resp) => {
return new Response(JSON.stringify(resp), {
headers: corsHeaders,
status: 200
})
}
if (action === 'start') {
const resp = await startTogglTimer(key)
return returnData(resp)
} else if (action === 'stop') {
const resp = await stopTogglTimer(key)
return returnData(resp)
}
}
addEventListener('fetch', event => {
if (event.request.method !== 'PUT' || event.request.method !== 'DELETE') {
return event.respondWith(handleRequest(event.request))
} else {
return new Response(null, {
status: 405,
statusText: 'Method Not Allowed'
})
}
})