Skip to content

Commit

Permalink
Merge pull request #18 from goncalossilva/patch-1
Browse files Browse the repository at this point in the history
Use Todoist's  Sync API v9
  • Loading branch information
dubisdev authored Dec 15, 2022
2 parents 8508474 + b0cfb07 commit c05800d
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 86 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cerebro-cerebro-todoist",
"version": "2.8.1",
"version": "2.8.2",
"description": "Manage your Todoist tasks from CerebroApp",
"author": "David Jiménez <[email protected]> (https://dubis.dev)",
"license": "MIT",
Expand All @@ -10,16 +10,16 @@
],
"dependencies": {
"@cerebroapp/cerebro-ui": "1.0.4",
"@doist/todoist-api-typescript": "2.1.2",
"cerebro-command-router": "2.0.0",
"p-debounce": "4.0.0",
"semver": "7.3.7",
"todoist-rest-client": "3.0.10"
"semver": "7.3.8"
},
"devDependencies": {
"@types/react": "18.0.15",
"cerebro-build": "1.1.1",
"@types/react": "16.14.34",
"cerebro-build": "1.2.0",
"electron": "19.0.10",
"react": "15.7.0"
"react": "16.14.0"
},
"main": "dist/index.js",
"keywords": [
Expand All @@ -33,4 +33,4 @@
"clear": "cerebro-build clear",
"prepublish": "yarn clear && yarn build"
}
}
}
12 changes: 5 additions & 7 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import CerebroRouter from "cerebro-command-router";
import TDSClient from "todoist-rest-client";
import { createTask, updateChecker, startPageAfterUpdate } from "./services";
import { TodoistApi } from "@doist/todoist-api-typescript";
import { createTask, startPageAfterUpdate } from "./services";
import icon from "./icons";
import { NewTodayTask } from "./components";
import strings from "./lang";
Expand All @@ -9,8 +9,6 @@ import taskArrayGenerator from "./services/displayArrayGenerator";

if (!Notification.permission) Notification.requestPermission();

const initialize = () => updateChecker();

const plugin = ({ term, display, actions, settings, config, hide }) => {
if (!term.toLowerCase().includes("tds")) return;

Expand All @@ -23,7 +21,7 @@ const plugin = ({ term, display, actions, settings, config, hide }) => {
getPreview: () => <h3>{strings.noTokenFound}</h3>,
});
}
const client = TDSClient(token);
const client = new TodoistApi(token);

const myRouter = new CerebroRouter({ command: "tds", term, display, hide });

Expand All @@ -34,7 +32,7 @@ const plugin = ({ term, display, actions, settings, config, hide }) => {
icon: icon,
title: strings.workflow_new,
getPreview: () => <NewTodayTask />,
onSelect: () => createTask(client, { text: term }),
onSelect: () => createTask(token, { text: term }),
});

myRouter.route(
Expand Down Expand Up @@ -84,4 +82,4 @@ const plugin = ({ term, display, actions, settings, config, hide }) => {
});
};

export { icon, name, keyword, plugin as fn, settings, initialize };
export { icon, name, keyword, plugin as fn, settings };
18 changes: 9 additions & 9 deletions src/services/displayArrayGenerator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import pDebounce from "p-debounce";
import { TaskInfo } from "../components";

import lang from "../lang";
import { APITaskObject, TDSClient } from "todoist-rest-client/dist/definitions";
import type { TodoistApi, Task } from "@doist/todoist-api-typescript";
const strings = lang.displayArrayGenerator;
import { CerebroScreen } from "cerebro-command-router/dist/definitions";

type Options = {
type?: "today" | "view";
client: TDSClient;
client: TodoistApi;
term: string;
actions: object;
showOverdue?: boolean;
Expand All @@ -35,7 +35,7 @@ const todayTaskArrayGenerator: tTAG = async ({
term,
actions,
}) => {
let taskArray: APITaskObject[];
let taskArray: Task[];

const contentSearch = getSubCommandText(term);
let filter = contentSearch
Expand All @@ -44,8 +44,8 @@ const todayTaskArrayGenerator: tTAG = async ({

try {
taskArray = showOverdue
? await client.task.search({ filter })
: await client.extras.getTodayTaskJSON();
? await client.getTasks({ filter })
: await client.getTasks({ filter: "today" });
} catch (err) {
return handleErrors(err);
}
Expand All @@ -59,7 +59,7 @@ const todayTaskArrayGenerator: tTAG = async ({
if (taskArray.length <= 10) {
taskArray = await Promise.all(
taskArray.map(async (task) => {
let projectName = (await client.project.get(task.project_id)).name;
let projectName = (await client.getProject(task.projectId)).name;
return { ...task, projectName };
})
);
Expand All @@ -81,10 +81,10 @@ const filterTaskArrayGenerator = async ({ client, term, actions }: Options) => {

if (!filter) return Promise.resolve([{ title: strings.noFilterFound }]);

let fullTasksList: APITaskObject[];
let fullTasksList: Task[];

try {
fullTasksList = await client.task.search({ filter });
fullTasksList = await client.getTasks({ filter });
} catch (err) {
return handleErrors(err);
}
Expand All @@ -95,7 +95,7 @@ const filterTaskArrayGenerator = async ({ client, term, actions }: Options) => {
if (fullTasksList.length <= 10) {
fullTasksList = await Promise.all(
fullTasksList.map(async (task) => {
let projectName = (await client.project.get(task.project_id)).name;
let projectName = (await client.getProject(task.projectId)).name;
return { ...task, projectName };
})
);
Expand Down
3 changes: 1 addition & 2 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createTask } from "./taskServices";
import updateChecker from "./updateChecker";
import startPageAfterUpdate from "./startPageAfterUpdate";

export { createTask, updateChecker, startPageAfterUpdate };
export { createTask, startPageAfterUpdate };
2 changes: 1 addition & 1 deletion src/services/quickAdd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const quickAdd = (text: string, token: string) => {
return fetch("https://api.todoist.com/sync/v8/quick/add", {
return fetch("https://api.todoist.com/sync/v9/quick/add", {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down
16 changes: 8 additions & 8 deletions src/services/taskServices.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { notification } from "../components";
import { TDSClient, APITaskObject } from "todoist-rest-client/dist/definitions";
import type { TodoistApi, Task } from "@doist/todoist-api-typescript";

import { getSubCommandText } from "cerebro-command-router";
import lang from "../lang";
import { quickAdd } from "./quickAdd";

export async function createTask(Client: TDSClient, { text = "" } = {}) {
export async function createTask(token: string, { text = "" } = {}) {
let taskText = getSubCommandText(text);

try {
await quickAdd(taskText, Client.apiToken);
await quickAdd(taskText, token);
notification({ body: lang.notifications.taskCreated });
} catch (err) {
if (!err.status)
Expand All @@ -19,7 +19,7 @@ export async function createTask(Client: TDSClient, { text = "" } = {}) {
}
}

export const getTaskHour = (task: APITaskObject) => {
export const getTaskHour = (task: Task) => {
if (task?.due?.datetime) {
const hour = new Date(task.due.datetime)
.toTimeString()
Expand All @@ -29,14 +29,14 @@ export const getTaskHour = (task: APITaskObject) => {
} else return;
};

export const completeTask = async (Client: TDSClient, task: APITaskObject) => {
export const completeTask = async (Client: TodoistApi, task: Task) => {
if (!task) return;
await Client.task.close(task.id);
await Client.closeTask(task.id);
notification({ body: lang.notifications.taskCompleted });
};

export const deleteTask = async (Client: TDSClient, task: APITaskObject) => {
export const deleteTask = async (Client: TodoistApi, task: Task) => {
if (!task) return;
await Client.task.delete(task.id);
await Client.deleteTask(task.id);
notification({ body: lang.notifications.taskDeleted });
};
19 changes: 0 additions & 19 deletions src/services/updateChecker.ts

This file was deleted.

Loading

0 comments on commit c05800d

Please sign in to comment.