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

Commit f5f3bab

Browse files
committed
Update to Github
0 parents  commit f5f3bab

File tree

11 files changed

+253
-0
lines changed

11 files changed

+253
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
testing.js

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testing.js

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Dubisdev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

api_services/IManager.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import TasksManager from "./TasksManager.js";
2+
/**
3+
* Interface for creating resources managers
4+
*/
5+
export default class IManager {
6+
constructor(type, headers) {
7+
switch (type) {
8+
case "task":
9+
return new TasksManager({ headers });
10+
}
11+
}
12+
}

api_services/TasksManager.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Task from "../todoist_resources/Task.js";
2+
import axios from "axios";
3+
4+
export default class TasksManager {
5+
constructor({ headers }) {
6+
this.headers = headers;
7+
}
8+
9+
create(task = new Task()) {
10+
axios
11+
.post(`https://api.todoist.com/rest/v1/tasks`, task, this.headers)
12+
.then(() => true);
13+
}
14+
15+
/**
16+
* returns an array with all today tasks
17+
*/
18+
async getAll() {
19+
let json = await getAllJson(this.headers);
20+
let arrayTasks = [];
21+
json.map((task) => {
22+
arrayTasks.push(task.content);
23+
});
24+
return arrayTasks;
25+
}
26+
27+
/**
28+
* returns an array with all today tasks
29+
*/
30+
async getToday() {
31+
let json = await getAllJson(this.headers);
32+
let arrayTasks = [];
33+
34+
let todayTasksJson = json
35+
.filter((task) => task.due !== undefined)
36+
.filter(
37+
(task) => task.due.date === new Date().toISOString().substring(0, 10)
38+
);
39+
40+
todayTasksJson.map((task) => {
41+
arrayTasks.push(task.content);
42+
});
43+
44+
return arrayTasks;
45+
}
46+
}
47+
48+
async function getAllJson(headers) {
49+
return await axios
50+
.get(`https://api.todoist.com/rest/v1/tasks`, headers)
51+
.then((res = {}) => res.data);
52+
}

api_services/tds_client.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import IManager from "./IManager.js";
2+
3+
export default class TDSClient {
4+
constructor(apiToken) {
5+
if (!apiToken) throw new Error("Missing api token");
6+
7+
this.headers = {
8+
headers: { Authorization: `Bearer ${apiToken}` },
9+
};
10+
}
11+
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+
}
20+
21+
/**
22+
* Method for getting today tasks
23+
*/
24+
async getTodayTasks() {
25+
const TypeManager = new IManager("task", this.headers);
26+
return await TypeManager.getToday();
27+
}
28+
29+
/**
30+
* Method for creating todoist resources (task, project, etc.). The resource type ond object are given by params.
31+
* If no params, creates a NO_CONTENT task
32+
*/
33+
create({ type = "task" } = {}, ObjectFromType) {
34+
const TypeManager = new IManager(type, this.headers);
35+
TypeManager.create(ObjectFromType);
36+
}
37+
}

index.js

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

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "todoist-rest-client",
3+
"version": "0.0.1",
4+
"description": "A simple todoist-rest-api client",
5+
"main": "index.js",
6+
"type": "module",
7+
"keywords": ["todoist", "rest-client"],
8+
"author": "Dubisdev",
9+
"license": "MIT",
10+
"dependencies": {
11+
"axios": "0.21.1"
12+
},
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/dubisdev/todoist-rest-client.git"
16+
},
17+
"bugs": {
18+
"url": "https://github.com/dubisdev/todoist-rest-client/issues"
19+
},
20+
"homepage": "https://github.com/dubisdev/npm-todoist-rest-api#readme"
21+
}

readme.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# TODOIST-REST-CLIENT
2+
3+
## _A simple todoist-rest-api client_
4+
5+
A todoist rest API client with:
6+
7+
## Implemented Features
8+
9+
- get Today Tasks
10+
- get All Tasks
11+
- create task (see example)
12+
13+
I'd implement more features in the future like:
14+
15+
- new resources
16+
- more tasks functions
17+
18+
> something important
19+
20+
## Installation
21+
22+
For installing the client:
23+
24+
```sh
25+
npm install todoist-rest-client
26+
```
27+
28+
## USE
29+
30+
```js
31+
import TDSClient from "todoist-rest-client";
32+
33+
const Client = new TDSClient(API_TOKEN); //get an api token from your todoist integrations page
34+
```
35+
36+
## API
37+
38+
### Client.create({type}, todoistResource)
39+
40+
This method allows creating todoist resources (tasks, projects, ...).
41+
If no params are given, it creates a _No_Content_ task in the inbox.
42+
If given type param, creates default todoistResourceType.
43+
44+
```js
45+
//Creating a todoist resource
46+
import TDSClient from "todoist-rest-client";
47+
48+
const Client = new TDSClient(API_TOKEN); //get an api token from your todoist integrations page
49+
50+
Client.create({ type: "task" });
51+
```
52+
53+
## Todoist Resources
54+
55+
Todoist resources are classes that allow you to easyly create resource objects for todoist.
56+
They can be imported directly from the package:
57+
58+
```js
59+
import {Task} from "todoist-rest-client";
60+
61+
const myTask = new Task({....});
62+
```
63+
64+
### Task
65+
66+
Tasks, are the basic pieces of todoist. Each task has a lot of properties: content, date, priority...
67+
68+
### Future Implementations
69+
70+
In future versions of this package, I'd like to implement more resources to work with projects, comments and all the Todoist's stuff.
71+
72+
## License
73+
74+
MIT

todoist_resources/Task.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default class Task {
2+
constructor({
3+
content = "_NO_CONTENT_",
4+
due_string = "",
5+
due_lang = "",
6+
priority = 1,
7+
} = {}) {
8+
this.priority = priority;
9+
this.content = content;
10+
this.due_string = due_string;
11+
this.due_lang = due_lang;
12+
}
13+
}

0 commit comments

Comments
 (0)