0

Update Filter

by
Published Jun 6, 2022

Updates a filter. [See the docs here](https://developer.todoist.com/sync/v9/#update-a-filter)

Script todoist Verified

The script

Submitted by hugo697 Bun
Verified 398 days ago
1
import { v4 as uuidv4 } from 'uuid'
2

3
type Todoist = {
4
	Token: string
5
}
6
interface Response {
7
	full_sync: boolean
8
	sync_status: Sync_status
9
	sync_token: string
10
	temp_id_mapping: Temp_id_mapping
11
}
12
interface Sync_status {
13
	[key: string]: string
14
}
15
interface Temp_id_mapping {
16
	[key: string]: string
17
}
18

19
export async function main(
20
	resource: Todoist,
21
	filter: {
22
		id: number | string
23
		name?: string
24
		query?: string
25
		color?:
26
			| 'berry_red'
27
			| 'red'
28
			| 'orange'
29
			| 'yellow'
30
			| 'olive_green'
31
			| 'lime_green'
32
			| 'green'
33
			| 'mint_green'
34
			| 'teal'
35
			| 'sky_blue'
36
			| 'light_blue'
37
			| 'blue'
38
			| 'grape'
39
			| 'violet'
40
			| 'lavender'
41
			| 'magenta'
42
			| 'salmon'
43
			| 'charcoal'
44
			| 'grey'
45
			| 'taupe'
46
		item_order?: number
47
		is_favorite?: boolean
48
	}
49
) {
50
	const response = await fetch('https://api.todoist.com/sync/v9/sync', {
51
		method: 'POST',
52
		headers: {
53
			Authorization: `Bearer ${resource.Token}`,
54
			'Content-Type': 'application/json'
55
		},
56
		body: JSON.stringify({
57
			commands: [
58
				{
59
					type: 'filter_update',
60
					uuid: uuidv4(),
61
					args: {
62
						id: filter.id,
63
						name: filter.name,
64
						query: filter.query
65
					}
66
				}
67
			]
68
		})
69
	})
70

71
	const data = (await response.json()) as Response
72
	const tempId = Object.keys(data.temp_id_mapping)[0]
73
	const updatedFilterId = data.temp_id_mapping[tempId]
74
	return {
75
		id: updatedFilterId
76
	}
77
}
78