Skip to content

Commit 0dede67

Browse files
committed
fix(api): should close #1
1 parent d105925 commit 0dede67

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

src/ticktick/ticktick.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,29 @@ class TickTick {
1212
}
1313
}
1414

15-
public async getAccessToken(clientId: string, clientSecret: string, code: string, redirectUri: string): Promise<string> {
16-
const response = await fetch(this.AUTH_URL, {
17-
method: 'POST',
18-
body: new URLSearchParams({
15+
public getAccessToken(clientId: string, clientSecret: string, code: string, redirectUri: string): Promise<string> {
16+
return new Promise((resolve, reject) => {
17+
const xhr = new XMLHttpRequest();
18+
xhr.open('POST', this.AUTH_URL);
19+
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
20+
xhr.onload = () => {
21+
const data = JSON.parse(xhr.responseText);
22+
if (xhr.status >= 200 && xhr.status < 300) {
23+
this.accessToken = data.access_token;
24+
resolve(data.access_token);
25+
} else {
26+
reject(new Error(`Failed to get access token: ${data.error_description}`));
27+
}
28+
};
29+
xhr.onerror = () => reject(new Error("Network error getting access token"));
30+
xhr.send(new URLSearchParams({
1931
client_id: clientId,
2032
client_secret: clientSecret,
2133
code,
2234
redirect_uri: redirectUri,
2335
grant_type: 'authorization_code'
24-
})
36+
}));
2537
});
26-
27-
const data = await response.json();
28-
if (!response.ok) {
29-
throw new Error(`Failed to get access token: ${data.error_description}`);
30-
}
31-
32-
this.accessToken = data.access_token;
33-
return data.access_token;
3438
}
3539

3640
public setAccessToken(accessToken: string): void {
@@ -41,24 +45,25 @@ class TickTick {
4145
return `${this.WEB_URL}/#p/${encodeURIComponent(task.projectId)}/task/${encodeURIComponent(task.id)}`;
4246
}
4347

44-
public async createTask(newTask: NewTask): Promise<Task> {
45-
const url = `${this.BASE_URL}/task`;
46-
const response = await fetch(url, {
47-
method: 'POST',
48-
headers: {
49-
'Content-Type': 'application/json',
50-
'Authorization': `Bearer ${this.accessToken}`
51-
},
52-
body: JSON.stringify(newTask)
48+
public createTask(newTask: NewTask): Promise<Task> {
49+
return new Promise((resolve, reject) => {
50+
const xhr = new XMLHttpRequest();
51+
const url = `${this.BASE_URL}/task`;
52+
xhr.open('POST', url);
53+
xhr.setRequestHeader('Content-Type', 'application/json');
54+
xhr.setRequestHeader('Authorization', `Bearer ${this.accessToken}`);
55+
xhr.onload = () => {
56+
const data = JSON.parse(xhr.responseText);
57+
if (xhr.status >= 200 && xhr.status < 300) {
58+
data.taskUrl = this.generateTaskUrl(data);
59+
resolve(data);
60+
} else {
61+
reject(new Error(`Failed to create task: ${data.error_description}`));
62+
}
63+
};
64+
xhr.onerror = () => reject(new Error("Network error creating task"));
65+
xhr.send(JSON.stringify(newTask));
5366
});
54-
55-
const data = await response.json();
56-
if (!response.ok) {
57-
throw new Error(`Failed to create task: ${data.error_description}`);
58-
}
59-
60-
data.taskUrl = this.generateTaskUrl(data);
61-
return data;
6267
}
6368
}
6469

0 commit comments

Comments
 (0)