-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
81 lines (70 loc) · 1.83 KB
/
api.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
import axios from 'axios'
import { v4 } from 'uuid'
const baseURL = 'https://todoist.com'
const state = v4()
const instance = axios.create({
baseURL: baseURL,
timeout: 5000
})
class Api {
authorize() {
return new Promise((resolve, reject) => {
chrome.identity.launchWebAuthFlow(
{
url: `${baseURL}/oauth/authorize?client_id=${CLIENT_ID}&scope=data:read_write&state=${state}`,
interactive: true
},
responseUrl => {
if (responseUrl === undefined) {
return reject()
}
let url = new URL(responseUrl)
let code = url.searchParams.get('code')
instance
.post('/oauth/access_token', {
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
code: code
})
.then(response => {
if (response === undefined) {
return reject()
}
chrome.storage.sync.set({ token: response.data.access_token })
resolve()
})
}
)
})
}
getProjectList() {
let syncToken = '*'
let resourceTypes = '["projects"]'
let params = new URLSearchParams()
params.append('token', this.token)
params.append('sync_token', syncToken)
params.append('resource_types', resourceTypes)
return instance.post('/api/v8/sync', param)
}
addTask(content) {
let uuid = v4()
let temp_id = v4()
let params = new URLSearchParams()
params.append('token', this.token)
params.append(
'commands',
JSON.stringify([
{
type: 'item_add',
uuid: uuid,
temp_id: temp_id,
args: {
content: content
}
}
])
)
return instance.post('/api/v8/sync', params)
}
}
export default new Api()