@@ -8,25 +8,46 @@ import { itemToListItem, PocketItem } from './item';
8
8
9
9
interface PocketList {
10
10
list : Record < number , PocketItem > ;
11
+ total : string ;
11
12
}
12
13
14
+ // limit from the doc
15
+ const count = 30 ;
16
+
13
17
export const get = async ( param : GetParams ) : Promise < ListItem [ ] > => {
14
- const response = await post < PocketList > ( {
15
- url : 'https://getpocket.com/v3/get' ,
16
- headers,
17
- params : {
18
- consumer_key : getPocketKey ( ) ,
19
- access_token : await getPocketToken ( ) ,
20
- sort : 'newest' ,
21
- search : param . type === 'search' ? param . search : undefined ,
22
- tag : param . type === 'tag' ? param . tag ?? '_untagged_' : undefined ,
23
- detailType : 'complete' ,
24
- } ,
25
- } ) ;
26
-
27
- if ( ! response . ok ) throw Error ( "couldn't get pocket list" ) ;
28
-
29
- return Object . values ( response . result . list )
30
- . sort ( ( a , b ) => ( a . time_added < b . time_added ? 1 : - 1 ) )
31
- . map ( itemToListItem ) ;
18
+ const listItems : ListItem [ ] = [ ] ;
19
+ let offset = 0 ;
20
+ let total = 0 ;
21
+
22
+ do {
23
+ const response = await post < PocketList > ( {
24
+ url : 'https://getpocket.com/v3/get' ,
25
+ headers,
26
+ params : {
27
+ consumer_key : getPocketKey ( ) ,
28
+ access_token : await getPocketToken ( ) ,
29
+ sort : 'newest' ,
30
+ search : param . type === 'search' ? param . search : undefined ,
31
+ tag : param . type === 'tag' ? param . tag ?? '_untagged_' : undefined ,
32
+ detailType : 'complete' ,
33
+ state : 'unread' ,
34
+ count,
35
+ total : '1' ,
36
+ offset,
37
+ } ,
38
+ } ) ;
39
+
40
+ if ( ! response . ok ) throw Error ( "couldn't get pocket list" ) ;
41
+
42
+ listItems . push (
43
+ ...Object . values ( response . result . list )
44
+ . sort ( ( a , b ) => ( a . time_added < b . time_added ? 1 : - 1 ) )
45
+ . map ( itemToListItem )
46
+ ) ;
47
+
48
+ total = parseInt ( response . result . total ) ;
49
+ offset += count ;
50
+ } while ( listItems . length < total ) ;
51
+
52
+ return listItems ;
32
53
} ;
0 commit comments