Skip to content

Commit

Permalink
fix: pocket API is actively requesting pagination (and state) now (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
e-krebs authored Sep 25, 2024
1 parent 306f5b6 commit 98e242d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "pile",
"description": "pile",
"version": "1.9.6",
"version": "1.9.7",
"icons": {
"16": "src/content/icons/icon-16.png",
"48": "src/content/icons/icon-48.png",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pile",
"version": "1.9.6",
"version": "1.9.7",
"author": "Emmanuel Krebs <e-krebs@users.noreply.github.com>",
"license": "MIT",
"type": "module",
Expand Down
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 98e242d

Please sign in to comment.