fix: stabilize bookmark pagination cursors

This commit is contained in:
Peter Steinberger
2026-01-04 23:42:34 +00:00
parent 3442ba84c6
commit f765955116
5 changed files with 82 additions and 16 deletions
+5 -6
View File
@@ -40,11 +40,11 @@ export function registerBookmarksCommand(program: Command, ctx: CliContext): voi
process.exit(1);
}
if (maxPages !== undefined && !cmdOpts.all) {
console.error(`${ctx.p('err')}--max-pages requires --all.`);
const usePagination = cmdOpts.all || cmdOpts.cursor;
if (maxPages !== undefined && !usePagination) {
console.error(`${ctx.p('err')}--max-pages requires --all or --cursor.`);
process.exit(1);
}
const usePagination = cmdOpts.all || cmdOpts.cursor;
if (!usePagination && (!Number.isFinite(count) || count <= 0)) {
console.error(`${ctx.p('err')}Invalid --count. Expected a positive integer.`);
process.exit(1);
@@ -74,9 +74,8 @@ export function registerBookmarksCommand(program: Command, ctx: CliContext): voi
if (result.success && result.tweets) {
const emptyMessage = folderId ? 'No bookmarks found in folder.' : 'No bookmarks found.';
const isJson = cmdOpts.json || cmdOpts.jsonFull;
if (isJson && result.nextCursor) {
// Output with nextCursor for pagination
console.log(JSON.stringify({ tweets: result.tweets, nextCursor: result.nextCursor }, null, 2));
if (isJson && usePagination) {
console.log(JSON.stringify({ tweets: result.tweets, nextCursor: result.nextCursor ?? null }, null, 2));
} else {
ctx.printTweets(result.tweets, { json: isJson, emptyMessage });
}
+16 -8
View File
@@ -201,6 +201,7 @@ export function withTimelines<TBase extends AbstractConstructor<TwitterClientBas
const seen = new Set<string>();
const tweets: TweetData[] = [];
let cursor: string | undefined = options.cursor;
let nextCursor: string | undefined;
let pagesFetched = 0;
const { includeRaw = false, maxPages } = options;
@@ -339,17 +340,20 @@ export function withTimelines<TBase extends AbstractConstructor<TwitterClientBas
}
}
if (!page.cursor || page.cursor === cursor || page.tweets.length === 0) {
const pageCursor = page.cursor;
if (!pageCursor || pageCursor === cursor || page.tweets.length === 0) {
nextCursor = undefined;
break;
}
if (maxPages && pagesFetched >= maxPages) {
cursor = page.cursor;
nextCursor = pageCursor;
break;
}
cursor = page.cursor;
cursor = pageCursor;
nextCursor = pageCursor;
}
return { success: true, tweets, nextCursor: cursor };
return { success: true, tweets, nextCursor };
}
private async getBookmarkFolderTimelinePaged(
@@ -362,6 +366,7 @@ export function withTimelines<TBase extends AbstractConstructor<TwitterClientBas
const seen = new Set<string>();
const tweets: TweetData[] = [];
let cursor: string | undefined = options.cursor;
let nextCursor: string | undefined;
let pagesFetched = 0;
const { includeRaw = false, maxPages } = options;
@@ -516,17 +521,20 @@ export function withTimelines<TBase extends AbstractConstructor<TwitterClientBas
}
}
if (!page.cursor || page.cursor === cursor || page.tweets.length === 0) {
const pageCursor = page.cursor;
if (!pageCursor || pageCursor === cursor || page.tweets.length === 0) {
nextCursor = undefined;
break;
}
if (maxPages && pagesFetched >= maxPages) {
cursor = page.cursor;
nextCursor = pageCursor;
break;
}
cursor = page.cursor;
cursor = pageCursor;
nextCursor = pageCursor;
}
return { success: true, tweets, nextCursor: cursor };
return { success: true, tweets, nextCursor };
}
private async fetchWithRetry(url: string, init: RequestInit): Promise<Response> {