refactor: unify pagination helpers

This commit is contained in:
Peter Steinberger
2026-01-12 02:58:00 +00:00
parent e4d2773d46
commit d5a527d5fb
15 changed files with 307 additions and 182 deletions
+8 -6
View File
@@ -1,4 +1,5 @@
import type { Command } from 'commander';
import { parsePositiveIntFlag } from '../cli/pagination.js';
import type { CliContext } from '../cli/shared.js';
import { extractBookmarkFolderId } from '../lib/extract-bookmark-folder-id.js';
import { TwitterClient } from '../lib/twitter-client.js';
@@ -27,7 +28,12 @@ export function registerBookmarksCommand(program: Command, ctx: CliContext): voi
const opts = program.opts();
const timeoutMs = ctx.resolveTimeoutFromOptions(opts);
const count = Number.parseInt(cmdOpts.count || '20', 10);
const maxPages = cmdOpts.maxPages ? Number.parseInt(cmdOpts.maxPages, 10) : undefined;
const maxPagesParsed = parsePositiveIntFlag(cmdOpts.maxPages, '--max-pages');
if (!maxPagesParsed.ok) {
console.error(`${ctx.p('err')}${maxPagesParsed.error}`);
process.exit(1);
}
const maxPages = maxPagesParsed.value;
const { cookies, warnings } = await ctx.resolveCredentialsFromOptions(opts);
@@ -49,10 +55,6 @@ export function registerBookmarksCommand(program: Command, ctx: CliContext): voi
console.error(`${ctx.p('err')}Invalid --count. Expected a positive integer.`);
process.exit(1);
}
if (maxPages !== undefined && (!Number.isFinite(maxPages) || maxPages <= 0)) {
console.error(`${ctx.p('err')}Invalid --max-pages. Expected a positive integer.`);
process.exit(1);
}
const client = new TwitterClient({ cookies, timeoutMs });
const folderId = cmdOpts.folderId ? extractBookmarkFolderId(cmdOpts.folderId) : null;
@@ -71,7 +73,7 @@ export function registerBookmarksCommand(program: Command, ctx: CliContext): voi
? await client.getAllBookmarks(paginationOptions)
: await client.getBookmarks(count, timelineOptions);
if (result.success && result.tweets) {
if (result.success) {
const emptyMessage = folderId ? 'No bookmarks found in folder.' : 'No bookmarks found.';
const isJson = Boolean(cmdOpts.json || cmdOpts.jsonFull);
ctx.printTweetsResult(result, { json: isJson, usePagination, emptyMessage });
+2 -2
View File
@@ -38,10 +38,10 @@ export function registerHomeCommand(program: Command, ctx: CliContext): void {
? await client.getHomeLatestTimeline(count, { includeRaw })
: await client.getHomeTimeline(count, { includeRaw });
if (result.success && result.tweets) {
if (result.success) {
const feedType = cmdOpts.following ? 'Following' : 'For You';
const emptyMessage = `No tweets found in ${feedType} timeline.`;
const isJson = cmdOpts.json || cmdOpts.jsonFull;
const isJson = Boolean(cmdOpts.json || cmdOpts.jsonFull);
ctx.printTweets(result.tweets, { json: isJson, emptyMessage });
} else {
console.error(`${ctx.p('err')}Failed to fetch home timeline: ${result.error}`);
+10 -8
View File
@@ -2,6 +2,7 @@
// ABOUTME: Supports listing owned lists, memberships, and list timelines.
import type { Command } from 'commander';
import { parsePaginationFlags } from '../cli/pagination.js';
import type { CliContext } from '../cli/shared.js';
import { extractListId } from '../lib/extract-list-id.js';
import { hyperlink } from '../lib/output.js';
@@ -99,7 +100,12 @@ export function registerListsCommand(program: Command, ctx: CliContext): void {
const timeoutMs = ctx.resolveTimeoutFromOptions(opts);
const quoteDepth = ctx.resolveQuoteDepthFromOptions(opts);
const count = Number.parseInt(cmdOpts.count || '20', 10);
const maxPages = cmdOpts.maxPages ? Number.parseInt(cmdOpts.maxPages, 10) : undefined;
const pagination = parsePaginationFlags(cmdOpts, { maxPagesImpliesPagination: true });
if (!pagination.ok) {
console.error(`${ctx.p('err')}${pagination.error}`);
process.exit(1);
}
const listId = extractListId(listIdOrUrl);
if (!listId) {
@@ -107,15 +113,11 @@ export function registerListsCommand(program: Command, ctx: CliContext): void {
process.exit(2);
}
const usePagination = Boolean(cmdOpts.all || cmdOpts.cursor || maxPages !== undefined);
const usePagination = pagination.usePagination;
if (!usePagination && (!Number.isFinite(count) || count <= 0)) {
console.error(`${ctx.p('err')}Invalid --count. Expected a positive integer.`);
process.exit(1);
}
if (maxPages !== undefined && (!Number.isFinite(maxPages) || maxPages <= 0)) {
console.error(`${ctx.p('err')}Invalid --max-pages. Expected a positive integer.`);
process.exit(1);
}
const { cookies, warnings } = await ctx.resolveCredentialsFromOptions(opts);
@@ -131,13 +133,13 @@ export function registerListsCommand(program: Command, ctx: CliContext): void {
const client = new TwitterClient({ cookies, timeoutMs, quoteDepth });
const includeRaw = cmdOpts.jsonFull ?? false;
const timelineOptions = { includeRaw };
const paginationOptions = { includeRaw, maxPages, cursor: cmdOpts.cursor };
const paginationOptions = { includeRaw, maxPages: pagination.maxPages, cursor: pagination.cursor };
const result = usePagination
? await client.getAllListTimeline(listId, paginationOptions)
: await client.getListTimeline(listId, count, timelineOptions);
if (result.success && result.tweets) {
if (result.success) {
const isJson = Boolean(cmdOpts.json || cmdOpts.jsonFull);
ctx.printTweetsResult(result, {
json: isJson,
+31 -40
View File
@@ -1,4 +1,5 @@
import type { Command } from 'commander';
import { parsePaginationFlags } from '../cli/pagination.js';
import type { CliContext } from '../cli/shared.js';
import { formatStatsLine } from '../lib/output.js';
import { TwitterClient } from '../lib/twitter-client.js';
@@ -71,16 +72,10 @@ export function registerReadCommands(program: Command, ctx: CliContext): void {
const timeoutMs = ctx.resolveTimeoutFromOptions(opts);
const quoteDepth = ctx.resolveQuoteDepthFromOptions(opts);
const tweetId = ctx.extractTweetId(tweetIdOrUrl);
const maxPages = cmdOpts.maxPages ? Number.parseInt(cmdOpts.maxPages, 10) : undefined;
const pageDelayMs = Number.parseInt(cmdOpts.delay || '1000', 10);
const usePagination = Boolean(cmdOpts.all || cmdOpts.cursor || maxPages !== undefined);
if (maxPages !== undefined && (!Number.isFinite(maxPages) || maxPages <= 0)) {
console.error(`${ctx.p('err')}Invalid --max-pages. Expected a positive integer.`);
process.exit(1);
}
if (!Number.isFinite(pageDelayMs) || pageDelayMs < 0) {
console.error(`${ctx.p('err')}Invalid --delay. Expected a non-negative integer.`);
const pagination = parsePaginationFlags(cmdOpts, { maxPagesImpliesPagination: true, includeDelay: true });
if (!pagination.ok) {
console.error(`${ctx.p('err')}${pagination.error}`);
process.exit(1);
}
@@ -98,29 +93,30 @@ export function registerReadCommands(program: Command, ctx: CliContext): void {
const client = new TwitterClient({ cookies, timeoutMs, quoteDepth });
const includeRaw = cmdOpts.jsonFull ?? false;
const result = usePagination
const result = pagination.usePagination
? await client.getRepliesPaged(tweetId, {
includeRaw,
maxPages,
cursor: cmdOpts.cursor,
pageDelayMs,
maxPages: pagination.maxPages,
cursor: pagination.cursor,
pageDelayMs: pagination.pageDelayMs,
})
: await client.getReplies(tweetId, { includeRaw });
const isJson = Boolean(cmdOpts.json || cmdOpts.jsonFull);
if (result.tweets) {
const isJson = Boolean(cmdOpts.json || cmdOpts.jsonFull);
ctx.printTweetsResult(result, { json: isJson, usePagination, emptyMessage: 'No replies found.' });
ctx.printTweetsResult(result, {
json: isJson,
usePagination: pagination.usePagination,
emptyMessage: 'No replies found.',
});
// Show pagination hint if there's more
if (result.nextCursor && !isJson) {
console.error(`${ctx.p('info')}More replies available. Use --cursor "${result.nextCursor}" to continue.`);
}
}
if (!result.success) {
console.error(`${ctx.p('err')}Failed to fetch replies: ${result.error}`);
process.exit(1);
}
} else {
if (!result.success) {
console.error(`${ctx.p('err')}Failed to fetch replies: ${result.error}`);
process.exit(1);
}
@@ -153,16 +149,10 @@ export function registerReadCommands(program: Command, ctx: CliContext): void {
const timeoutMs = ctx.resolveTimeoutFromOptions(opts);
const quoteDepth = ctx.resolveQuoteDepthFromOptions(opts);
const tweetId = ctx.extractTweetId(tweetIdOrUrl);
const maxPages = cmdOpts.maxPages ? Number.parseInt(cmdOpts.maxPages, 10) : undefined;
const pageDelayMs = Number.parseInt(cmdOpts.delay || '1000', 10);
const usePagination = Boolean(cmdOpts.all || cmdOpts.cursor || maxPages !== undefined);
if (maxPages !== undefined && (!Number.isFinite(maxPages) || maxPages <= 0)) {
console.error(`${ctx.p('err')}Invalid --max-pages. Expected a positive integer.`);
process.exit(1);
}
if (!Number.isFinite(pageDelayMs) || pageDelayMs < 0) {
console.error(`${ctx.p('err')}Invalid --delay. Expected a non-negative integer.`);
const pagination = parsePaginationFlags(cmdOpts, { maxPagesImpliesPagination: true, includeDelay: true });
if (!pagination.ok) {
console.error(`${ctx.p('err')}${pagination.error}`);
process.exit(1);
}
@@ -180,18 +170,22 @@ export function registerReadCommands(program: Command, ctx: CliContext): void {
const client = new TwitterClient({ cookies, timeoutMs, quoteDepth });
const includeRaw = cmdOpts.jsonFull ?? false;
const result = usePagination
const result = pagination.usePagination
? await client.getThreadPaged(tweetId, {
includeRaw,
maxPages,
cursor: cmdOpts.cursor,
pageDelayMs,
maxPages: pagination.maxPages,
cursor: pagination.cursor,
pageDelayMs: pagination.pageDelayMs,
})
: await client.getThread(tweetId, { includeRaw });
const isJson = Boolean(cmdOpts.json || cmdOpts.jsonFull);
if (result.tweets) {
const isJson = Boolean(cmdOpts.json || cmdOpts.jsonFull);
ctx.printTweetsResult(result, { json: isJson, usePagination, emptyMessage: 'No thread tweets found.' });
ctx.printTweetsResult(result, {
json: isJson,
usePagination: pagination.usePagination,
emptyMessage: 'No thread tweets found.',
});
// Show pagination hint if there's more
if (result.nextCursor && !isJson) {
@@ -199,12 +193,9 @@ export function registerReadCommands(program: Command, ctx: CliContext): void {
`${ctx.p('info')}More thread tweets available. Use --cursor "${result.nextCursor}" to continue.`,
);
}
}
if (!result.success) {
console.error(`${ctx.p('err')}Failed to fetch thread: ${result.error}`);
process.exit(1);
}
} else {
if (!result.success) {
console.error(`${ctx.p('err')}Failed to fetch thread: ${result.error}`);
process.exit(1);
}
+10 -4
View File
@@ -32,8 +32,11 @@ export function registerSearchCommands(program: Command, ctx: CliContext): void
const includeRaw = cmdOpts.jsonFull ?? false;
const result = await client.search(query, count, { includeRaw });
if (result.success && result.tweets) {
ctx.printTweets(result.tweets, { json: cmdOpts.json || cmdOpts.jsonFull, emptyMessage: 'No tweets found.' });
if (result.success) {
ctx.printTweets(result.tweets, {
json: cmdOpts.json || cmdOpts.jsonFull,
emptyMessage: 'No tweets found.',
});
} else {
console.error(`${ctx.p('err')}Search failed: ${result.error}`);
process.exit(1);
@@ -90,8 +93,11 @@ export function registerSearchCommands(program: Command, ctx: CliContext): void
const includeRaw = cmdOpts.jsonFull ?? false;
const result = await client.search(query, count, { includeRaw });
if (result.success && result.tweets) {
ctx.printTweets(result.tweets, { json: cmdOpts.json || cmdOpts.jsonFull, emptyMessage: 'No mentions found.' });
if (result.success) {
ctx.printTweets(result.tweets, {
json: cmdOpts.json || cmdOpts.jsonFull,
emptyMessage: 'No mentions found.',
});
} else {
console.error(`${ctx.p('err')}Failed to fetch mentions: ${result.error}`);
process.exit(1);
+17 -8
View File
@@ -1,4 +1,5 @@
import type { Command } from 'commander';
import { parseNonNegativeIntFlag, parsePositiveIntFlag } from '../cli/pagination.js';
import type { CliContext } from '../cli/shared.js';
import { normalizeHandle } from '../lib/normalize-handle.js';
import { TwitterClient } from '../lib/twitter-client.js';
@@ -45,8 +46,20 @@ export function registerUserTweetsCommand(program: Command, ctx: CliContext): vo
const timeoutMs = ctx.resolveTimeoutFromOptions(opts);
const quoteDepth = ctx.resolveQuoteDepthFromOptions(opts);
const count = Number.parseInt(cmdOpts.count || '20', 10);
const maxPages = cmdOpts.maxPages ? Number.parseInt(cmdOpts.maxPages, 10) : undefined;
const pageDelayMs = Number.parseInt(cmdOpts.delay || '1000', 10);
const maxPagesParsed = parsePositiveIntFlag(cmdOpts.maxPages, '--max-pages');
if (!maxPagesParsed.ok) {
console.error(`${ctx.p('err')}${maxPagesParsed.error}`);
process.exit(2);
}
const maxPages = maxPagesParsed.value;
const delayParsed = parseNonNegativeIntFlag(cmdOpts.delay, '--delay', 1000);
if (!delayParsed.ok) {
console.error(`${ctx.p('err')}${delayParsed.error}`);
process.exit(2);
}
const pageDelayMs = delayParsed.value;
// Validate inputs
if (!Number.isFinite(count) || count <= 0) {
@@ -62,14 +75,10 @@ export function registerUserTweetsCommand(program: Command, ctx: CliContext): vo
);
process.exit(2);
}
if (maxPages !== undefined && (!Number.isFinite(maxPages) || maxPages <= 0 || maxPages > hardMaxPages)) {
if (maxPages !== undefined && maxPages > hardMaxPages) {
console.error(`${ctx.p('err')}Invalid --max-pages. Expected a positive integer (max: ${hardMaxPages}).`);
process.exit(2);
}
if (!Number.isFinite(pageDelayMs) || pageDelayMs < 0) {
console.error(`${ctx.p('err')}Invalid --delay. Expected a non-negative integer.`);
process.exit(2);
}
// Normalize handle (strip @ if present)
const username = normalizeHandle(handle);
@@ -113,7 +122,7 @@ export function registerUserTweetsCommand(program: Command, ctx: CliContext): vo
pageDelayMs,
});
if (result.success && result.tweets) {
if (result.success) {
const isJson = Boolean(cmdOpts.json || cmdOpts.jsonFull);
ctx.printTweetsResult(result, {
json: isJson,
+1 -1
View File
@@ -351,7 +351,7 @@ export function registerUserCommands(program: Command, ctx: CliContext): void {
const includeRaw = cmdOpts.jsonFull ?? false;
const result = await client.getLikes(count, { includeRaw });
if (result.success && result.tweets) {
if (result.success) {
ctx.printTweets(result.tweets, {
json: cmdOpts.json || cmdOpts.jsonFull,
emptyMessage: 'No liked tweets found.',