0

Search Tasks

by
Published May 7, 2024

Search tasks by name, label, project and/or section. [See Docs](https://developer.todoist.com/rest/v2/#get-active-tasks)

Script todoist Verified

The script

Submitted by hugo697 Bun
Verified 398 days ago
1
type Todoist = {
2
	Token: string
3
}
4
interface Task {
5
	id: string
6
	project_id?: string
7
	section_id?: string
8
	content: string
9
	labels: string[]
10
}
11
export async function main(
12
	resource: Todoist,
13
	args: {
14
		projectId?: string
15
		sectionId?: string
16
		label?: string
17
		filter?: string
18
	}
19
) {
20
	const response = await fetch('https://api.todoist.com/rest/v2/tasks', {
21
		method: 'GET',
22
		headers: {
23
			Authorization: `Bearer ${resource.Token}`
24
		}
25
	})
26
	const tasks = (await response.json()) as Task[]
27
	const filteredTasks = tasks.filter((task) => {
28
		let matches = true
29
		if (args.projectId) matches = matches && task.project_id === args.projectId
30
		if (args.sectionId) matches = matches && task.section_id === args.sectionId
31
		if (args.label) matches = matches && task.labels.includes(args.label)
32
		if (args.filter) matches = matches && new RegExp(args.filter).test(task.content)
33
		return matches
34
	})
35
	return filteredTasks
36
}
37