@@ -4,7 +4,7 @@ import { lastValueFrom } from 'rxjs'
44
55import type { Task } from '../types'
66
7- const LIMIT = 100
7+ const LIMIT = 200
88
99type SyncDue = {
1010 date : string
@@ -36,6 +36,12 @@ export type SyncTask = {
3636 due ?: SyncDue | null
3737}
3838
39+ type CompletedTasksResponse = {
40+ items : SyncTask [ ]
41+ total : number
42+ next_cursor ?: string
43+ }
44+
3945@Injectable ( )
4046export class TodoistService {
4147 constructor ( private readonly httpService : HttpService ) { }
@@ -47,37 +53,41 @@ export class TodoistService {
4753 token : string
4854 projectId : string
4955 } ) : Promise < Task [ ] > {
50- const completedTasks = await this . getCompletedTasksInternal ( { token, offset : 0 , projectId } )
56+ const completedTasks = await this . getCompletedTasksInternal ( { token, projectId } )
5157
5258 return completedTasks . map ( ( task ) => this . getTaskFromQuickAddResponse ( task ) )
5359 }
5460
5561 private async getCompletedTasksInternal ( {
56- offset ,
62+ cursor ,
5763 projectId,
5864 token,
5965 } : {
6066 token : string
61- offset : number
67+ cursor ?: string
6268 projectId : string
6369 } ) : Promise < SyncTask [ ] > {
6470 const response = await lastValueFrom (
65- this . httpService . get < SyncTask [ ] > (
66- // At time of writing (08/02/2023), this endpoint is undocumented and its stability is not guaranteed.
67- `https://api.todoist.com/sync/v9/items/get_completed?project_id=${ projectId } &offset=${ offset } &limit=${ LIMIT } ` ,
71+ this . httpService . get < CompletedTasksResponse > (
72+ 'https://api.todoist.com/sync/v9/tasks/archived' ,
6873 {
74+ params : {
75+ project_id : projectId ,
76+ cursor,
77+ limit : LIMIT ,
78+ } ,
6979 headers : {
7080 Authorization : `Bearer ${ token } ` ,
7181 } ,
7282 } ,
7383 ) ,
7484 )
7585
76- const { data : tasks } = response
86+ const { items : tasks , next_cursor } = response . data
7787
7888 if ( tasks . length === LIMIT ) {
7989 return tasks . concat (
80- await this . getCompletedTasksInternal ( { token, offset : offset + LIMIT , projectId } ) ,
90+ await this . getCompletedTasksInternal ( { token, cursor : next_cursor , projectId } ) ,
8191 )
8292 }
8393
0 commit comments