Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 371e634

Browse files
authored
Merge pull request #5 from dubisdev/1.0.0
v. 1.0.0
2 parents 2fcedd2 + a9d54de commit 371e634

File tree

14 files changed

+261
-292
lines changed

14 files changed

+261
-292
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/node_modules
2-
testing.js
2+
testing.js
3+
/dist

.npmignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
testing.js
1+
testing.js
2+
3+
/core
4+
esbuild.js

core/TDSClient.js

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,16 @@
1-
import IManager from "./managers/IManager.js";
1+
import task from "./client_modules/task.js";
2+
import project from "./client_modules/project.js";
23

3-
export default class TDSClient {
4-
constructor(apiToken) {
5-
if (!apiToken) throw new Error("Missing api token");
4+
const TDSClient = (apiToken) => {
5+
if (!apiToken) throw new Error("Missing api token");
66

7-
this.headers = {
8-
headers: { Authorization: `Bearer ${apiToken}` },
9-
};
10-
}
7+
const headers = { Authorization: `Bearer ${apiToken}` };
118

12-
/**
13-
* Method for getting all todoist resources from one type (task, project, etc.).
14-
* The resource type is given by params
15-
*/
16-
async getAll({ type = "task" } = {}) {
17-
const TypeManager = new IManager(type, this.headers);
18-
return await TypeManager.getAll();
19-
}
9+
return {
10+
apiToken,
11+
task: task(headers),
12+
project: project(headers),
13+
};
14+
};
2015

21-
/**
22-
* Method for getting all todoist resources jsons from one type (task, project, etc.).
23-
* The resource type is given by params
24-
*/
25-
async getAllJSON({ type = "task" } = {}) {
26-
const TypeManager = new IManager(type, this.headers);
27-
return await TypeManager.getAllJSON();
28-
}
29-
30-
/**
31-
* Method for getting today tasks names
32-
*/
33-
async getTodayTasks() {
34-
const TypeManager = new IManager("task", this.headers);
35-
return await TypeManager.getToday();
36-
}
37-
38-
/**
39-
* Method for creating todoist resources (task, project, etc.). The resource type ond object are given by params.
40-
* If no params, creates a NO_CONTENT task
41-
* Returns true if all ok, then false.
42-
*/
43-
async create({ type = "task" } = {}, ObjectFromType) {
44-
const TypeManager = new IManager(type, this.headers);
45-
return await TypeManager.create(ObjectFromType);
46-
}
47-
48-
/**
49-
* Method for getting today tasks json
50-
*/
51-
async getTodayTasksJSON() {
52-
const TypeManager = new IManager("task", this.headers);
53-
return await TypeManager.getTodayJSON();
54-
}
55-
56-
/**
57-
* Method for getting a todoist resource from one type (task, project, etc.) by id.
58-
* The resource type is given by params.
59-
*/
60-
async get({ type, id } = {}) {
61-
const TypeManager = new IManager(type, this.headers);
62-
return await TypeManager.get(id);
63-
}
64-
65-
/**
66-
* Method for creating todoist resources (task, project, etc.). The resource type ond object are given by params.
67-
* If no params, creates a NO_CONTENT task
68-
*/
69-
async completeTask({ TaskObject = {}, id }) {
70-
const TypeManager = new IManager("task", this.headers);
71-
if (TaskObject.id) {
72-
return await TypeManager.completeTask(TaskObject.id);
73-
} else {
74-
return await TypeManager.completeTask(id);
75-
}
76-
}
77-
}
16+
export default TDSClient;

core/client_modules/project.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import Project from "../resources/Project.js";
2+
import axios from "axios";
3+
4+
const project = (headers) => {
5+
return {
6+
create: async (project = new Project()) => {
7+
return await axios.post(
8+
`https://api.todoist.com/rest/v1/projects`,
9+
project,
10+
{
11+
headers,
12+
}
13+
);
14+
},
15+
16+
getAll: async () => {
17+
let json = await getAllJSON(headers);
18+
let arrayProjects = [];
19+
json.map((project) => {
20+
arrayProjects.push(project.name);
21+
});
22+
return arrayProjects;
23+
},
24+
25+
getAllJSON: async () => await getAllJSON(headers),
26+
27+
get: async (id) => {
28+
let project = await getOneJSON(id, headers);
29+
return project;
30+
},
31+
};
32+
};
33+
export default project;
34+
35+
async function getAllJSON(headers) {
36+
return await axios
37+
.get(`https://api.todoist.com/rest/v1/projects`, {
38+
headers,
39+
})
40+
.then((res = {}) => res.data);
41+
}
42+
43+
async function getOneJSON(id, headers) {
44+
return await axios
45+
.get(`https://api.todoist.com/rest/v1/projects/${id}`, {
46+
headers,
47+
})
48+
.then((res = {}) => res.data);
49+
}

core/client_modules/task.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import Task from "../resources/Task.js";
2+
import axios from "axios";
3+
4+
async function getAllJSON(headers) {
5+
return await axios
6+
.get(`https://api.todoist.com/rest/v1/tasks`, { headers })
7+
.then((res = {}) => res.data);
8+
}
9+
10+
async function getOneJSON(id, headers) {
11+
return await axios
12+
.get(`https://api.todoist.com/rest/v1/tasks/${id}`, {
13+
headers,
14+
})
15+
.then((res = {}) => res.data);
16+
}
17+
18+
const task = (headers) => {
19+
return {
20+
create: async (task = new Task()) => {
21+
return await axios.post(`https://api.todoist.com/rest/v1/tasks`, task, {
22+
headers,
23+
});
24+
},
25+
26+
getAll: async () => {
27+
let json = await getAllJSON(headers);
28+
let arrayTasks = [];
29+
json.map((task) => {
30+
arrayTasks.push(task.content);
31+
});
32+
return arrayTasks;
33+
},
34+
35+
getAllJSON: async () => {
36+
return await getAllJSON(headers);
37+
},
38+
39+
getToday: async () => {
40+
let json = await getAllJSON(headers);
41+
let arrayTasks = [];
42+
43+
let todayTasksJson = json
44+
.filter((task) => task.due !== undefined)
45+
.filter(
46+
(task) => task.due.date === new Date().toISOString().substring(0, 10)
47+
);
48+
49+
todayTasksJson.map((task) => {
50+
arrayTasks.push(task.content);
51+
});
52+
53+
return arrayTasks;
54+
},
55+
56+
getTodayJSON: async () => {
57+
let json = await getAllJSON(headers);
58+
59+
let todayTasksJson = json
60+
.filter((task) => task.due !== undefined)
61+
.filter(
62+
(task) => task.due.date === new Date().toISOString().substring(0, 10)
63+
);
64+
65+
return todayTasksJson;
66+
},
67+
68+
get: async (id) => {
69+
return await getOneJSON(id, headers);
70+
},
71+
72+
completeTask: async (id) => {
73+
return await axios.post(
74+
`https://api.todoist.com/rest/v1/tasks/${id}/close`,
75+
{},
76+
{
77+
headers,
78+
}
79+
);
80+
},
81+
};
82+
};
83+
84+
export default task;

core/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import TDSClient from "./TDSClient.js";
2+
3+
export { Task, Project } from "./resources/index.js";
4+
export default TDSClient;

core/managers/IManager.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

core/managers/ProjectsManager.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)