Skip to content

Commit

Permalink
fix: pocket API is actively requesting pagination (and state) now
Browse files Browse the repository at this point in the history
  • Loading branch information
e-krebs committed Sep 25, 2024
1 parent 306f5b6 commit e6cb23e
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions src/services/pocket/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,46 @@ import { itemToListItem, PocketItem } from './item';

interface PocketList {
list: Record<number, PocketItem>;
total: string;
}

// limit from the doc
const count = 30;

export const get = async (param: GetParams): Promise<ListItem[]> => {
const response = await post<PocketList>({
url: 'https://getpocket.com/v3/get',
headers,
params: {
consumer_key: getPocketKey(),
access_token: await getPocketToken(),
sort: 'newest',
search: param.type === 'search' ? param.search : undefined,
tag: param.type === 'tag' ? param.tag ?? '_untagged_' : undefined,
detailType: 'complete',
},
});

if (!response.ok) throw Error("couldn't get pocket list");

return Object.values(response.result.list)
.sort((a, b) => (a.time_added < b.time_added ? 1 : -1))
.map(itemToListItem);
const listItems: ListItem[] = [];
let offset = 0;
let total = 0;

do {
const response = await post<PocketList>({
url: 'https://getpocket.com/v3/get',
headers,
params: {
consumer_key: getPocketKey(),
access_token: await getPocketToken(),
sort: 'newest',
search: param.type === 'search' ? param.search : undefined,
tag: param.type === 'tag' ? param.tag ?? '_untagged_' : undefined,
detailType: 'complete',
state: 'unread',
count,
total: '1',
offset,
},
});

if (!response.ok) throw Error("couldn't get pocket list");

listItems.push(
...Object.values(response.result.list)
.sort((a, b) => (a.time_added < b.time_added ? 1 : -1))
.map(itemToListItem)
);

total = parseInt(response.result.total);
offset += count;
} while (listItems.length < total);

return listItems;
};

0 comments on commit e6cb23e

Please sign in to comment.