This repository was archived by the owner on Nov 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrektor.js
More file actions
107 lines (85 loc) · 2.54 KB
/
Copy pathtrektor.js
File metadata and controls
107 lines (85 loc) · 2.54 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
if (typeof browser == "undefined") {
globalThis.browser = chrome
}
class TrelloGateway {
static ENDPOINT = "https://api.trello.com/1";
static API_KEY = "2379d540412e417f6f0696c1397f38a6";
#storage;
constructor(storage) {
this.#storage = storage;
}
getCard(id) {
return this.#request("get", `/cards/${id}`);
}
updateCard(id, data) {
return this.#request("put", `/cards/${id}`, data);
}
async #request(method, path, data = null) {
const url = this.constructor.ENDPOINT + path;
const { trello: token } = await this.#storage.get("trello");
const response = await fetch(url, {
method,
headers: {
"Authorization": `OAuth oauth_consumer_key="${this.constructor.API_KEY}", oauth_token="${token}"`,
"Content-Type": "application/json; charset=utf-8",
},
body: (data === null) ? null : JSON.stringify(data),
});
if (response.status === 401) {
throw new Error('Invalid or expired trello token.');
} else {
return response.json();
}
}
}
class TogglGateway {
static ENDPOINT = "https://api.track.toggl.com/api/v8";
#storage;
constructor(storage) {
this.#storage = storage;
}
getWorkspaces() {
return this.#request("get", "/workspaces");
}
getProjects(workspaceId) {
return this.#request("get", `/workspaces/${workspaceId}/projects`);
}
getTasks(projectId) {
return this.#request("get", `/projects/${projectId}/tasks`);
}
createTask(projectId, name) {
return this.#request("post", "/tasks", {
task: { name, pid: projectId },
});
}
getCurrentTimeEntry() {
return this.#request("get", "/time_entries/current");
}
startTimeEntry(taskId, description) {
return this.#request("post", "/time_entries/start", {
time_entry: { description, tid: taskId, created_with: "trektor" },
});
}
async #request(method, path, data = null) {
const url = this.constructor.ENDPOINT + path;
const { toggl: token } = await this.#storage.get("toggl");
const response = await fetch(url, {
method,
headers: {
"Authorization": `Basic ${btoa(`${token}:api_token`)}`,
"Content-Type": "application/json; charset=utf-8",
},
body: (data === null) ? null : JSON.stringify(data),
});
if (response.status === 403) {
throw new Error('Invalid toggl token.');
} else {
return response.json();
}
}
}
const trektor = {
trelloGateway: new TrelloGateway(browser.storage.local),
togglGateway: new TogglGateway(browser.storage.local),
browser: browser,
}