forked from PipedreamHQ/pipedream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodoist.app.js
120 lines (119 loc) · 4.43 KB
/
todoist.app.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
const axios = require("axios");
const querystring = require("querystring");
const resourceTypes = require("./resource-types.js")
module.exports = {
type: "app",
app: "todoist",
propDefinitions: {
includeResourceTypes: {
type: "string[]",
label: "Resource Types",
description: "Select one or more resources to include",
async options() {
resourceTypes.unshift("all");
return resourceTypes;
},
},
selectProjects: {
type: "integer[]",
label: "Select Projects",
description:
"Filter for events that match one or more projects. Leave this blank to emit results for any project.",
optional: true,
async options() {
return (await this.getProjects()).map((project) => {
return { label: project.name, value: project.id };
});
},
},
},
methods: {
/**
* Make a request to Todoist's sync API.
* @params {Object} opts - An object representing the configuration options for this method
* @params {String} opts.path [opts.path=/sync/v8/sync] - The path for the sync request
* @params {String} opts.payload - The data to send in the API request at the POST body. This data will converted to `application/x-www-form-urlencoded`
* @returns {Object} When the request succeeds, an HTTP 200 response will be returned with a JSON object containing the requested resources and also a new `sync_token`.
*/
async _makeSyncRequest(opts) {
const { path = `/sync/v8/sync` } = opts;
delete opts.path;
opts.url = `https://api.todoist.com${path[0] === "/" ? "" : "/"}${path}`;
opts.payload.token = this.$auth.oauth_access_token;
opts.data = querystring.stringify(opts.payload);
delete opts.payload;
return await axios(opts);
},
/**
* Make a request to Todoist's REST API.
* @params {Object} opts - An object representing the Axios configuration options for this method
* @params {String} opts.path - The path for the REST API request
* @returns {*} The response may vary depending the specific API request.
*/
async _makeRestRequest(opts) {
const { path } = opts;
delete opts.path;
opts.url = `https://api.todoist.com${path[0] === "/" ? "" : "/"}${path}`;
opts.headers = {
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
};
return await axios(opts);
},
/**
* Check whether an array of project IDs contains the given proejct ID. This method is used in multiple sources to validate if an event matches the selection in the project filter.
* @params {Integer} project_id - The ID for a Todoist project
* @params {Array} selectedProjectIds - An array of Todoist project IDs
* @returns {Boolean} Returns `true` if the `project_id` matches a value in the arrar or if the array is empty. Otherwise returns `false`.
*/
isProjectInList(projectId, selectedProjectIds) {
return (
selectedProjectIds.length === 0 ||
selectedProjectIds.includes(projectId)
);
},
/**
* Public method to make a sync request.
* @params {Object} opts - The configuration for an axios request with a `path` key.
* @returns {Object} When the request succeeds, an HTTP 200 response will be returned with a JSON object containing the requested resources and also a new `sync_token`.
*/
async sync(opts) {
return (
await this._makeSyncRequest({
path: `/sync/v8/sync`,
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
payload: opts,
})
).data;
},
/**
* Get the list of project for the authenticated user
* @returns {Array} Returns a JSON-encoded array containing all user projects
*/
async getProjects() {
return (
await this._makeRestRequest({
path: `/rest/v1/projects`,
method: "GET",
})
).data;
},
async syncItems(db) {
return await this.syncResources(db, ["items"]);
},
async syncProjects(db) {
return await this.syncResources(db, ["projects"]);
},
async syncResources(db, resourceTypes) {
const syncToken = db.get("syncToken") || "*";
const result = await this.sync({
resource_types: JSON.stringify(resourceTypes),
sync_token: syncToken,
});
db.set("syncToken", result.sync_token);
return result;
},
},
};