3 Commits
Author SHA1 Message Date
yuta 45657a171e fix lists queries and authenticated list reading
CI / test (push) Successful in 10m5s
Publish npm package / publish (push) Successful in 9m53s
2026-07-26 12:34:53 +09:00
yuta b8400469ca Release v0.10.0
CI / test (push) Successful in 9m59s
Publish npm package / publish (push) Successful in 9m53s
2026-07-14 02:35:26 +09:00
yuta 81921226f0 feat: support ranked tweet search
CI / test (push) Successful in 10m17s
2026-07-13 09:18:42 +09:00
26 changed files with 575 additions and 310 deletions
+17
View File
@@ -1,5 +1,22 @@
# Changelog
## 0.10.1 — 2026-07-26
### Added
- Add `getLists()` for the authenticated account's “Your Lists” timeline.
### Fixed
- Refresh Lists query IDs, variables, and feature flags.
- Keep usable list and tweet data when optional GraphQL fields return partial errors.
## 0.10.0 — 2026-07-14
### Added
- Add Top and Latest product selection to library and CLI search.
## 0.9.0 — 2026-06-26
### Added
+7 -2
View File
@@ -51,6 +51,8 @@ bird thread 1234567890123456789 --max-pages 3 --json
# Search + mentions
bird search "from:steipete" -n 5
bird search "AI lang:ja" --product Top -n 20
bird search "from:steipete" --product Latest -n 20
bird mentions -n 5
bird mentions --user @steipete -n 5
@@ -145,6 +147,9 @@ const client = new TwitterClient({
// Search for tweets
const searchResult = await client.search('from:steipete', 50);
const popular = await client.search('AI lang:ja', 20, {
product: 'Top',
});
// Fetch news and trending topics from all tabs (default: For You, News, Sports, Entertainment)
const newsResult = await client.getNews(10, { aiOnly: true });
@@ -184,7 +189,7 @@ Fields:
- `bird <tweet-id-or-url> [--json]` — shorthand for `read` when only a URL or ID is provided.
- `bird replies <tweet-id-or-url> [--all] [--max-pages n] [--cursor string] [--delay ms] [--json]` — list replies to a tweet.
- `bird thread <tweet-id-or-url> [--all] [--max-pages n] [--cursor string] [--delay ms] [--json]` — show the full conversation thread.
- `bird search "<query>" [-n count] [--all] [--max-pages n] [--cursor string] [--json]` — search for tweets matching a query; `--max-pages` requires `--all` or `--cursor`.
- `bird search "<query>" [-n count] [--all] [--max-pages n] [--cursor string] [--product Top|Latest] [--json]` — search for tweets matching a query; `Latest` is the default product, and `--max-pages` requires `--all` or `--cursor`.
- `bird mentions [-n count] [--user @handle] [--json]` — find tweets mentioning a user (defaults to the authenticated user).
- `bird user-tweets <@handle> [-n count] [--cursor string] [--max-pages n] [--delay ms] [--json]` — get tweets from a user's profile timeline.
- `bird bookmarks [-n count] [--folder-id id] [--all] [--max-pages n] [--cursor string] [--expand-root-only] [--author-chain] [--author-only] [--full-chain-only] [--include-ancestor-branches] [--include-parent] [--thread-meta] [--sort-chronological] [--json]` — list your bookmarked tweets (or a specific bookmark folder); expansion flags control thread context; `--max-pages` requires `--all` or `--cursor`.
@@ -192,7 +197,7 @@ Fields:
- `bird likes [-n count] [--all] [--max-pages n] [--cursor string] [--json] [--json-full]` — list your liked tweets; `--max-pages` requires `--all` or `--cursor`.
- `bird news [-n count] [--ai-only] [--with-tweets] [--tweets-per-item n] [--for-you] [--news-only] [--sports] [--entertainment] [--trending-only] [--json]` — fetch news and trending topics from X's Explore tabs.
- `bird trending` — alias for `news` command.
- `bird lists [--member-of] [-n count] [--json]` — list your lists (owned or memberships).
- `bird lists [--member-of] [-n count] [--json]` — list the authenticated account's “Your Lists”, or memberships with `--member-of`.
- `bird list-timeline <list-id-or-url> [-n count] [--all] [--max-pages n] [--cursor string] [--json]` — get tweets from a list timeline; `--max-pages` implies `--all`.
- `bird following [--user <userId>] [-n count] [--cursor string] [--all] [--max-pages n] [--json]` — list users that you (or another user) follow; `--max-pages` requires `--all`.
- `bird followers [--user <userId>] [-n count] [--cursor string] [--all] [--max-pages n] [--json]` — list users that follow you (or another user); `--max-pages` requires `--all`.
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@yuta/bird",
"version": "0.9.0",
"version": "0.10.1",
"description": "CLI tool for tweeting and replying via Twitter/X GraphQL API",
"type": "module",
"main": "dist/index.js",
+5
View File
@@ -29,6 +29,10 @@ const TARGET_OPERATIONS = [
'GenericTimelineById',
'TrendHistory',
'AboutAccountQuery',
'ListsManagementPageTimeline',
'ListOwnerships',
'ListMemberships',
'ListLatestTweetsTimeline',
] as const;
type OperationName = (typeof TARGET_OPERATIONS)[number];
@@ -36,6 +40,7 @@ type OperationName = (typeof TARGET_OPERATIONS)[number];
const DISCOVERY_PAGES = [
'https://x.com/?lang=en',
'https://x.com/explore',
'https://x.com/i/lists',
'https://x.com/notifications',
'https://x.com/settings/profile',
];
+2 -2
View File
@@ -47,13 +47,13 @@ export function registerListsCommand(program: Command, ctx: CliContext): void {
const client = new TwitterClient({ relayBaseUrl, profileName, timeoutMs });
const result = cmdOpts.memberOf ? await client.getListMemberships(count) : await client.getOwnedLists(count);
const result = cmdOpts.memberOf ? await client.getListMemberships(count) : await client.getLists(count);
if (result.success && result.lists) {
if (cmdOpts.json) {
console.log(JSON.stringify(result.lists, null, 2));
} else {
const emptyMessage = cmdOpts.memberOf ? 'You are not a member of any lists.' : 'You do not own any lists.';
const emptyMessage = cmdOpts.memberOf ? 'You are not a member of any lists.' : 'No lists found.';
if (result.lists.length === 0) {
console.log(emptyMessage);
} else {
+2 -14
View File
@@ -6,6 +6,7 @@ import {
refreshFeatureOverridesCache,
} from '../lib/runtime-features.js';
import { runtimeQueryIds } from '../lib/runtime-query-ids.js';
import { TARGET_QUERY_ID_OPERATIONS } from '../lib/twitter-client-constants.js';
function countFeatureOverrides(overrides: FeatureOverrides): number {
let count = 0;
@@ -27,22 +28,9 @@ export function registerQueryIdsCommand(program: Command, ctx: CliContext): void
.option('--json', 'Output as JSON')
.option('--fresh', 'Force refresh (downloads X client bundles)', false)
.action(async (cmdOpts: { json?: boolean; fresh?: boolean }) => {
const operations = [
'CreateTweet',
'CreateRetweet',
'FavoriteTweet',
'TweetDetail',
'SearchTimeline',
'UserArticlesTweets',
'Bookmarks',
'Following',
'Followers',
'Likes',
];
if (cmdOpts.fresh) {
console.error(`${ctx.p('info')}Refreshing GraphQL query IDs…`);
await runtimeQueryIds.refresh(operations, { force: true });
await runtimeQueryIds.refresh(TARGET_QUERY_ID_OPERATIONS, { force: true });
console.error(`${ctx.p('info')}Refreshing feature overrides…`);
await refreshFeatureOverridesCache();
}
+11 -3
View File
@@ -1,8 +1,9 @@
import type { Command } from 'commander';
import { type Command, Option } from 'commander';
import { parsePaginationFlags } from '../cli/pagination.js';
import type { CliContext } from '../cli/shared.js';
import { mentionsQueryFromUserOption, normalizeHandle } from '../lib/normalize-handle.js';
import { TwitterClient } from '../lib/twitter-client.js';
import type { SearchProduct } from '../lib/twitter-client-search.js';
export function registerSearchCommands(program: Command, ctx: CliContext): void {
program
@@ -13,6 +14,11 @@ export function registerSearchCommands(program: Command, ctx: CliContext): void
.option('--all', 'Fetch all search results (paged)')
.option('--max-pages <number>', 'Stop after N pages when using --all')
.option('--cursor <string>', 'Resume pagination from a cursor')
.addOption(
new Option('--product <product>', 'Search product')
.choices(['Top', 'Latest'] satisfies SearchProduct[])
.default('Latest'),
)
.option('--json', 'Output as JSON')
.option('--json-full', 'Output as JSON with full raw API response in _raw field')
.action(
@@ -23,6 +29,7 @@ export function registerSearchCommands(program: Command, ctx: CliContext): void
all?: boolean;
maxPages?: string;
cursor?: string;
product?: SearchProduct;
json?: boolean;
jsonFull?: boolean;
},
@@ -53,8 +60,9 @@ export function registerSearchCommands(program: Command, ctx: CliContext): void
const client = new TwitterClient({ relayBaseUrl, profileName, timeoutMs, quoteDepth });
const includeRaw = cmdOpts.jsonFull ?? false;
const searchOptions = { includeRaw };
const paginationOptions = { includeRaw, maxPages, cursor: pagination.cursor };
const product = cmdOpts.product ?? 'Latest';
const searchOptions = { includeRaw, product };
const paginationOptions = { includeRaw, maxPages, cursor: pagination.cursor, product };
const result = usePagination
? await client.getAllSearchResults(query, paginationOptions)
: await client.search(query, count, searchOptions);
+2 -12
View File
@@ -1,17 +1,7 @@
{
"global": {
"responsive_web_grok_annotations_enabled": false,
"post_ctas_fetch_enabled": true,
"responsive_web_graphql_exclude_directive_enabled": true
"post_ctas_fetch_enabled": true
},
"sets": {
"lists": {
"blue_business_profile_image_shape_enabled": true,
"tweetypie_unmention_optimization_enabled": true,
"responsive_web_text_conversations_enabled": false,
"interactive_text_enabled": true,
"vibe_api_enabled": true,
"responsive_web_twitter_blue_verified_badge_is_enabled": true
}
}
"sets": {}
}
+5 -1
View File
@@ -11,7 +11,11 @@ export {
} from './twitter-client.js';
export type { HomeTimelineFetchOptions } from './twitter-client-home.js';
export type { ExploreTab, NewsFetchOptions, NewsItem, NewsResult } from './twitter-client-news.js';
export type { SearchFetchOptions } from './twitter-client-search.js';
export type {
SearchFetchOptions,
SearchPaginationOptions,
SearchProduct,
} from './twitter-client-search.js';
export type { TimelineFetchOptions } from './twitter-client-timelines.js';
export type { TweetFetchOptions } from './twitter-client-tweet-detail.js';
export type {
+5 -1
View File
@@ -16,5 +16,9 @@
"ExplorePage": "kheAINB_4pzRDqkzG3K-ng",
"GenericTimelineById": "uGSr7alSjR9v6QJAIaqSKQ",
"TrendHistory": "Sj4T-jSB9pr0Mxtsc1UKZQ",
"AboutAccountQuery": "zs_jFPFT78rBpXv9Z3U2YQ"
"AboutAccountQuery": "zs_jFPFT78rBpXv9Z3U2YQ",
"ListsManagementPageTimeline": "I96vj5eyw1D9g_NA0wWj3Q",
"ListOwnerships": "N_9HejNT9Amo1tDF7Gb_TQ",
"ListMemberships": "EPnwxyczkTMNNqPjlCBP9Q",
"ListLatestTweetsTimeline": "K77PSxWq_St4HLusAV9nVg"
}
+1
View File
@@ -8,6 +8,7 @@ const DEFAULT_TTL_MS: number = 24 * 60 * 60 * 1000;
const DISCOVERY_PAGES: string[] = [
'https://x.com/?lang=en',
'https://x.com/explore',
'https://x.com/i/lists',
'https://x.com/notifications',
'https://x.com/settings/profile',
];
+4 -4
View File
@@ -28,10 +28,10 @@ export const FALLBACK_QUERY_IDS = {
Followers: 'kuFUYP9eV1FPoEy4N-pi7w',
Likes: 'JR2gceKucIKcVNB_9JkhsA',
BookmarkFolderTimeline: 'KJIQpsvxrTfRIlbaRIySHQ',
ListOwnerships: 'wQcOSjSQ8NtgxIwvYl1lMg',
ListMemberships: 'BlEXXdARdSeL_0KyKHHvvg',
ListLatestTweetsTimeline: '2TemLyqrMpTeAmysdbnVqw',
ListByRestId: 'wXzyA5vM_aVkBL9G8Vp3kw',
ListsManagementPageTimeline: 'I96vj5eyw1D9g_NA0wWj3Q',
ListOwnerships: 'N_9HejNT9Amo1tDF7Gb_TQ',
ListMemberships: 'EPnwxyczkTMNNqPjlCBP9Q',
ListLatestTweetsTimeline: 'K77PSxWq_St4HLusAV9nVg',
HomeTimeline: 'edseUwk9sP5Phz__9TIRnA',
HomeLatestTimeline: 'iOEZpOdfekFsxSlPQCQtPg',
ExploreSidebar: 'lpSN4M6qpimkF4nRFPE3nQ',
+16 -19
View File
@@ -180,50 +180,47 @@ export function buildLikesFeatures(): Record<string, boolean> {
}
export function buildListsFeatures(): Record<string, boolean> {
return applyFeatureOverrides('lists', {
rweb_video_screen_enabled: true,
return {
rweb_video_screen_enabled: false,
rweb_cashtags_enabled: true,
profile_label_improvements_pcf_label_in_post_enabled: true,
responsive_web_profile_redirect_enabled: true,
rweb_tipjar_consumption_enabled: true,
responsive_web_profile_redirect_enabled: false,
rweb_tipjar_consumption_enabled: false,
verified_phone_label_enabled: false,
creator_subscriptions_tweet_preview_api_enabled: true,
responsive_web_graphql_timeline_navigation_enabled: true,
responsive_web_graphql_exclude_directive_enabled: true,
responsive_web_graphql_skip_user_profile_image_extensions_enabled: false,
premium_content_api_read_enabled: false,
communities_web_enable_tweet_community_results_fetch: true,
c9s_tweet_anatomy_moderator_badge_enabled: true,
responsive_web_grok_analyze_button_fetch_trends_enabled: false,
responsive_web_grok_analyze_post_followups_enabled: false,
responsive_web_grok_annotations_enabled: false,
responsive_web_grok_analyze_post_followups_enabled: true,
rweb_cashtags_composer_attachment_enabled: true,
responsive_web_jetfuel_frame: true,
post_ctas_fetch_enabled: true,
responsive_web_grok_share_attachment_enabled: true,
responsive_web_grok_annotations_enabled: true,
articles_preview_enabled: true,
responsive_web_edit_tweet_api_enabled: true,
rweb_conversational_replies_downvote_enabled: false,
graphql_is_translatable_rweb_tweet_is_translatable_enabled: true,
view_counts_everywhere_api_enabled: true,
longform_notetweets_consumption_enabled: true,
responsive_web_twitter_article_tweet_consumption_enabled: true,
tweet_awards_web_tipping_enabled: false,
responsive_web_grok_show_grok_translated_post: false,
content_disclosure_indicator_enabled: true,
content_disclosure_ai_generated_indicator_enabled: true,
responsive_web_grok_show_grok_translated_post: true,
responsive_web_grok_analysis_button_from_backend: true,
creator_subscriptions_quote_tweet_preview_enabled: false,
post_ctas_fetch_enabled: true,
freedom_of_speech_not_reach_fetch_enabled: true,
standardized_nudges_misinfo: true,
tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled: true,
longform_notetweets_rich_text_read_enabled: true,
longform_notetweets_inline_media_enabled: true,
longform_notetweets_inline_media_enabled: false,
responsive_web_grok_image_annotation_enabled: true,
responsive_web_grok_imagine_annotation_enabled: true,
responsive_web_grok_community_note_auto_translation_is_enabled: false,
responsive_web_grok_community_note_auto_translation_is_enabled: true,
responsive_web_enhance_cards_enabled: false,
blue_business_profile_image_shape_enabled: false,
responsive_web_text_conversations_enabled: false,
tweetypie_unmention_optimization_enabled: true,
vibe_api_enabled: false,
interactive_text_enabled: false,
});
};
}
export function buildHomeTimelineFeatures(): Record<string, boolean> {
+197 -223
View File
@@ -9,6 +9,7 @@ import type { GraphqlTweetResult, ListsResult, SearchResult, TweetData, TwitterL
import { extractCursorFromInstructions, parseTweetsFromInstructions } from './twitter-client-utils.js';
export interface TwitterClientListMethods {
getLists(count?: number): Promise<ListsResult>;
getOwnedLists(count?: number): Promise<ListsResult>;
getListMemberships(count?: number): Promise<ListsResult>;
getListTimeline(listId: string, count?: number, options?: TimelineFetchOptions): Promise<SearchResult>;
@@ -22,7 +23,7 @@ interface GraphqlListResult {
member_count?: number;
subscriber_count?: number;
mode?: string;
created_at?: string;
created_at?: string | number;
user_results?: {
result?: {
rest_id?: string;
@@ -30,16 +31,73 @@ interface GraphqlListResult {
screen_name?: string;
name?: string;
};
core?: {
screen_name?: string;
name?: string;
};
};
};
}
interface GraphqlListItem {
itemContent?: {
list?: GraphqlListResult;
};
}
interface GraphqlListInstruction {
entries?: Array<{
content?: GraphqlListItem & {
clientEventInfo?: {
component?: string;
};
items?: Array<{
item?: GraphqlListItem;
}>;
};
}>;
}
interface GraphqlListsResponse {
data?: {
user?: {
result?: {
__typename?: string;
timeline?: {
timeline?: {
instructions?: GraphqlListInstruction[];
};
};
};
};
viewer?: {
list_management_timeline?: {
timeline?: {
instructions?: GraphqlListInstruction[];
};
};
};
};
errors?: Array<{ message: string }>;
}
function parseList(listResult: GraphqlListResult): TwitterList | null {
if (!listResult.id_str || !listResult.name) {
return null;
}
const owner = listResult.user_results?.result;
const ownerProfile = owner?.core ?? owner?.legacy;
const createdAtDate =
typeof listResult.created_at === 'number' && Number.isFinite(listResult.created_at)
? new Date(listResult.created_at)
: null;
const createdAt =
createdAtDate && !Number.isNaN(createdAtDate.getTime())
? createdAtDate.toISOString()
: typeof listResult.created_at === 'string'
? listResult.created_at
: undefined;
return {
id: listResult.id_str,
name: listResult.name,
@@ -47,51 +105,49 @@ function parseList(listResult: GraphqlListResult): TwitterList | null {
memberCount: listResult.member_count,
subscriberCount: listResult.subscriber_count,
isPrivate: listResult.mode?.toLowerCase() === 'private',
createdAt: listResult.created_at,
createdAt,
owner: owner
? {
id: owner.rest_id ?? '',
username: owner.legacy?.screen_name ?? '',
name: owner.legacy?.name ?? '',
username: ownerProfile?.screen_name ?? '',
name: ownerProfile?.name ?? '',
}
: undefined,
};
}
function parseListsFromInstructions(
instructions:
| Array<{
entries?: Array<{
content?: {
itemContent?: {
list?: GraphqlListResult;
};
};
}>;
}>
| undefined,
instructions: GraphqlListInstruction[] | undefined,
component?: string,
): TwitterList[] {
const lists: TwitterList[] = [];
const lists = new Map<string, TwitterList>();
if (!instructions) {
return lists;
return [];
}
for (const instruction of instructions) {
if (!instruction.entries) {
continue;
const addList = (listResult: GraphqlListResult | undefined) => {
if (!listResult) {
return;
}
for (const entry of instruction.entries) {
const listResult = entry.content?.itemContent?.list;
if (listResult) {
const parsed = parseList(listResult);
if (parsed) {
lists.push(parsed);
}
const parsed = parseList(listResult);
if (parsed) {
lists.set(parsed.id, parsed);
}
};
for (const instruction of instructions) {
for (const entry of instruction.entries ?? []) {
if (component && entry.content?.clientEventInfo?.component !== component) {
continue;
}
addList(entry.content?.itemContent?.list);
for (const item of entry.content?.items ?? []) {
addList(item.item?.itemContent?.list);
}
}
}
return lists;
return Array.from(lists.values());
}
export function withLists<TBase extends AbstractConstructor<TwitterClientBase>>(
@@ -103,19 +159,108 @@ export function withLists<TBase extends AbstractConstructor<TwitterClientBase>>(
super(...args);
}
private async getListsManagementQueryIds(): Promise<string[]> {
return [await this.getQueryId('ListsManagementPageTimeline')];
}
private async getListOwnershipsQueryIds(): Promise<string[]> {
const primary = await this.getQueryId('ListOwnerships');
return Array.from(new Set([primary, 'wQcOSjSQ8NtgxIwvYl1lMg']));
return [await this.getQueryId('ListOwnerships')];
}
private async getListMembershipsQueryIds(): Promise<string[]> {
const primary = await this.getQueryId('ListMemberships');
return Array.from(new Set([primary, 'BlEXXdARdSeL_0KyKHHvvg']));
return [await this.getQueryId('ListMemberships')];
}
private async getListTimelineQueryIds(): Promise<string[]> {
const primary = await this.getQueryId('ListLatestTweetsTimeline');
return Array.from(new Set([primary, '2TemLyqrMpTeAmysdbnVqw']));
return [await this.getQueryId('ListLatestTweetsTimeline')];
}
private async fetchListsOperation(
operationName: 'ListsManagementPageTimeline' | 'ListOwnerships' | 'ListMemberships',
variables: Record<string, unknown>,
getQueryIds: () => Promise<string[]>,
selectInstructions: (data: GraphqlListsResponse) => GraphqlListInstruction[] | undefined,
component?: string,
): Promise<ListsResult> {
const params = new URLSearchParams({
variables: JSON.stringify(variables),
features: JSON.stringify(buildListsFeatures()),
});
const tryOnce = async () => {
let lastError: string | undefined;
let had404 = false;
for (const queryId of await getQueryIds()) {
const url = `${TWITTER_API_BASE}/${queryId}/${operationName}?${params.toString()}`;
try {
const response = await this.fetchWithTimeout(url, {
method: 'GET',
headers: this.getHeaders(),
});
if (response.status === 404) {
had404 = true;
lastError = `HTTP ${response.status}`;
continue;
}
if (!response.ok) {
const text = await response.text();
return { success: false as const, error: `HTTP ${response.status}: ${text.slice(0, 200)}`, had404 };
}
const data = (await response.json()) as GraphqlListsResponse;
const instructions = selectInstructions(data);
if (instructions) {
return { success: true as const, lists: parseListsFromInstructions(instructions, component), had404 };
}
if (data.data?.user?.result?.__typename === 'UserUnavailable') {
return { success: false as const, error: 'User unavailable', had404 };
}
if (data.errors?.length) {
return { success: false as const, error: data.errors.map((error) => error.message).join(', '), had404 };
}
return { success: false as const, error: `Missing ${operationName} timeline`, had404 };
} catch (error) {
lastError = error instanceof Error ? error.message : String(error);
}
}
return { success: false as const, error: lastError ?? `Unknown error fetching ${operationName}`, had404 };
};
const firstAttempt = await tryOnce();
if (firstAttempt.success) {
return { success: true, lists: firstAttempt.lists };
}
if (!firstAttempt.had404) {
return { success: false, error: firstAttempt.error };
}
await this.refreshQueryIds();
const secondAttempt = await tryOnce();
return secondAttempt.success
? { success: true, lists: secondAttempt.lists }
: { success: false, error: secondAttempt.error };
}
/**
* Get the lists shown on the authenticated user's Lists page.
*/
async getLists(count = 100): Promise<ListsResult> {
return this.fetchListsOperation(
'ListsManagementPageTimeline',
{ count },
() => this.getListsManagementQueryIds(),
(data) => data.data?.viewer?.list_management_timeline?.timeline?.instructions,
'suggest_owned_subscribed_list',
);
}
/**
@@ -127,99 +272,16 @@ export function withLists<TBase extends AbstractConstructor<TwitterClientBase>>(
return { success: false, error: userResult.error ?? 'Could not determine current user' };
}
const variables = {
userId: userResult.user.id,
count,
isListMembershipShown: true,
isListMemberTargetUserId: userResult.user.id,
};
const features = buildListsFeatures();
const params = new URLSearchParams({
variables: JSON.stringify(variables),
features: JSON.stringify(features),
});
const tryOnce = async () => {
let lastError: string | undefined;
let had404 = false;
const queryIds = await this.getListOwnershipsQueryIds();
for (const queryId of queryIds) {
const url = `${TWITTER_API_BASE}/${queryId}/ListOwnerships?${params.toString()}`;
try {
const response = await this.fetchWithTimeout(url, {
method: 'GET',
headers: this.getHeaders(),
});
if (response.status === 404) {
had404 = true;
lastError = `HTTP ${response.status}`;
continue;
}
if (!response.ok) {
const text = await response.text();
return { success: false as const, error: `HTTP ${response.status}: ${text.slice(0, 200)}`, had404 };
}
const data = (await response.json()) as {
data?: {
user?: {
result?: {
timeline?: {
timeline?: {
instructions?: Array<{
entries?: Array<{
content?: {
itemContent?: {
list?: GraphqlListResult;
};
};
}>;
}>;
};
};
};
};
};
errors?: Array<{ message: string }>;
};
if (data.errors && data.errors.length > 0) {
return { success: false as const, error: data.errors.map((e) => e.message).join(', '), had404 };
}
const instructions = data.data?.user?.result?.timeline?.timeline?.instructions;
const lists = parseListsFromInstructions(instructions);
return { success: true as const, lists, had404 };
} catch (error) {
lastError = error instanceof Error ? error.message : String(error);
}
}
return { success: false as const, error: lastError ?? 'Unknown error fetching owned lists', had404 };
};
const firstAttempt = await tryOnce();
if (firstAttempt.success) {
return { success: true, lists: firstAttempt.lists };
}
if (firstAttempt.had404) {
await this.refreshQueryIds();
const secondAttempt = await tryOnce();
if (secondAttempt.success) {
return { success: true, lists: secondAttempt.lists };
}
return { success: false, error: secondAttempt.error };
}
return { success: false, error: firstAttempt.error };
return this.fetchListsOperation(
'ListOwnerships',
{
userId: userResult.user.id,
isListMemberTargetUserId: userResult.user.id,
count,
},
() => this.getListOwnershipsQueryIds(),
(data) => data.data?.user?.result?.timeline?.timeline?.instructions,
);
}
/**
@@ -231,99 +293,12 @@ export function withLists<TBase extends AbstractConstructor<TwitterClientBase>>(
return { success: false, error: userResult.error ?? 'Could not determine current user' };
}
const variables = {
userId: userResult.user.id,
count,
isListMembershipShown: true,
isListMemberTargetUserId: userResult.user.id,
};
const features = buildListsFeatures();
const params = new URLSearchParams({
variables: JSON.stringify(variables),
features: JSON.stringify(features),
});
const tryOnce = async () => {
let lastError: string | undefined;
let had404 = false;
const queryIds = await this.getListMembershipsQueryIds();
for (const queryId of queryIds) {
const url = `${TWITTER_API_BASE}/${queryId}/ListMemberships?${params.toString()}`;
try {
const response = await this.fetchWithTimeout(url, {
method: 'GET',
headers: this.getHeaders(),
});
if (response.status === 404) {
had404 = true;
lastError = `HTTP ${response.status}`;
continue;
}
if (!response.ok) {
const text = await response.text();
return { success: false as const, error: `HTTP ${response.status}: ${text.slice(0, 200)}`, had404 };
}
const data = (await response.json()) as {
data?: {
user?: {
result?: {
timeline?: {
timeline?: {
instructions?: Array<{
entries?: Array<{
content?: {
itemContent?: {
list?: GraphqlListResult;
};
};
}>;
}>;
};
};
};
};
};
errors?: Array<{ message: string }>;
};
if (data.errors && data.errors.length > 0) {
return { success: false as const, error: data.errors.map((e) => e.message).join(', '), had404 };
}
const instructions = data.data?.user?.result?.timeline?.timeline?.instructions;
const lists = parseListsFromInstructions(instructions);
return { success: true as const, lists, had404 };
} catch (error) {
lastError = error instanceof Error ? error.message : String(error);
}
}
return { success: false as const, error: lastError ?? 'Unknown error fetching list memberships', had404 };
};
const firstAttempt = await tryOnce();
if (firstAttempt.success) {
return { success: true, lists: firstAttempt.lists };
}
if (firstAttempt.had404) {
await this.refreshQueryIds();
const secondAttempt = await tryOnce();
if (secondAttempt.success) {
return { success: true, lists: secondAttempt.lists };
}
return { success: false, error: secondAttempt.error };
}
return { success: false, error: firstAttempt.error };
return this.fetchListsOperation(
'ListMemberships',
{ userId: userResult.user.id, count },
() => this.getListMembershipsQueryIds(),
(data) => data.data?.user?.result?.timeline?.timeline?.instructions,
);
}
/**
@@ -416,11 +391,10 @@ export function withLists<TBase extends AbstractConstructor<TwitterClientBase>>(
errors?: Array<{ message: string }>;
};
if (data.errors && data.errors.length > 0) {
const instructions = data.data?.list?.tweets_timeline?.timeline?.instructions;
if (!instructions && data.errors?.length) {
return { success: false as const, error: data.errors.map((e) => e.message).join(', '), had404 };
}
const instructions = data.data?.list?.tweets_timeline?.timeline?.instructions;
const pageTweets = parseTweetsFromInstructions(instructions, { quoteDepth: this.quoteDepth, includeRaw });
const nextCursor = extractCursorFromInstructions(instructions);
+6 -2
View File
@@ -6,10 +6,14 @@ import { extractCursorFromInstructions, parseTweetsFromInstructions } from './tw
const RAW_QUERY_MISSING_REGEX = /must be defined/i;
export type SearchProduct = 'Top' | 'Latest';
/** Options for search methods */
export interface SearchFetchOptions {
/** Include raw GraphQL response in `_raw` field */
includeRaw?: boolean;
/** Search result ranking (default: Latest) */
product?: SearchProduct;
}
/** Options for paged search methods */
@@ -80,7 +84,7 @@ export function withSearch<TBase extends AbstractConstructor<TwitterClientBase>>
let cursor: string | undefined = options.cursor;
let nextCursor: string | undefined;
let pagesFetched = 0;
const { includeRaw = false, maxPages } = options;
const { includeRaw = false, maxPages, product = 'Latest' } = options;
const fetchPage = async (pageCount: number, pageCursor?: string) => {
let lastError: string | undefined;
@@ -92,7 +96,7 @@ export function withSearch<TBase extends AbstractConstructor<TwitterClientBase>>
rawQuery: query,
count: pageCount,
querySource: 'typed_query',
product: 'Latest',
product,
...(pageCursor ? { cursor: pageCursor } : {}),
};
+35
View File
@@ -0,0 +1,35 @@
import { Command } from 'commander';
import { describe, expect, it, vi } from 'vitest';
import type { CliContext } from '../src/cli/shared.js';
import { registerListsCommand } from '../src/commands/lists.js';
import { TwitterClient } from '../src/lib/twitter-client.js';
describe('lists command', () => {
it('uses the authenticated Lists page by default', async () => {
const program = new Command();
const ctx = {
resolveTimeoutFromOptions: () => undefined,
resolveRelayBaseUrlFromOptions: () => 'https://relay.example',
resolveProfileNameFromOptions: () => undefined,
p: () => '',
colors: { muted: (value: string) => value, accent: (value: string) => value },
getOutput: () => 'plain',
} as unknown as CliContext;
const listsSpy = vi.spyOn(TwitterClient.prototype, 'getLists').mockResolvedValue({
success: true,
lists: [],
});
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => undefined);
try {
registerListsCommand(program, ctx);
await program.parseAsync(['node', 'bird', 'lists', '--count', '25']);
expect(listsSpy).toHaveBeenCalledWith(25);
expect(logSpy).toHaveBeenCalledWith('No lists found.');
} finally {
listsSpy.mockRestore();
logSpy.mockRestore();
}
});
});
+26 -2
View File
@@ -82,8 +82,8 @@ describe('search command', () => {
const searchSpy = vi.spyOn(TwitterClient.prototype, 'search').mockResolvedValue({ success: true, tweets: [] });
try {
await program.parseAsync(['node', 'bird', 'search', 'cats', '--all', '--json']);
expect(getAllSpy).toHaveBeenCalledWith('cats', expect.objectContaining({ includeRaw: false }));
await program.parseAsync(['node', 'bird', 'search', 'cats', '--all', '--json', '--product', 'Top']);
expect(getAllSpy).toHaveBeenCalledWith('cats', expect.objectContaining({ includeRaw: false, product: 'Top' }));
expect(searchSpy).not.toHaveBeenCalled();
expect(mockContext.printTweetsResult).toHaveBeenCalledWith(expect.objectContaining({ tweets: [] }), {
json: true,
@@ -96,6 +96,30 @@ describe('search command', () => {
}
});
it('passes --product to non-paged search', async () => {
registerSearchCommands(program, mockContext as CliContext);
const searchSpy = vi.spyOn(TwitterClient.prototype, 'search').mockResolvedValue({ success: true, tweets: [] });
try {
await program.parseAsync(['node', 'bird', 'search', 'cats', '--product', 'Top']);
expect(searchSpy).toHaveBeenCalledWith('cats', 10, {
includeRaw: false,
product: 'Top',
});
} finally {
searchSpy.mockRestore();
}
});
it('rejects unsupported search products', async () => {
program.exitOverride();
registerSearchCommands(program, mockContext as CliContext);
await expect(program.parseAsync(['node', 'bird', 'search', 'cats', '--product', 'Media'])).rejects.toMatchObject({
code: 'commander.invalidArgument',
});
});
it('uses cursor pagination when --cursor is set', async () => {
registerSearchCommands(program, mockContext as CliContext);
const getAllSpy = vi
+8 -1
View File
@@ -1,8 +1,15 @@
import { describe, expect, it } from 'vitest';
import { TwitterClient } from '../src/index.js';
import { type SearchFetchOptions, type SearchProduct, TwitterClient } from '../src/index.js';
describe('library exports', () => {
it('exposes primary library surface', () => {
expect(typeof TwitterClient).toBe('function');
});
it('exposes search product types', () => {
const product: SearchProduct = 'Top';
const options: SearchFetchOptions = { product };
expect(options.product).toBe('Top');
});
});
+23 -12
View File
@@ -29,9 +29,6 @@ describe('runtime-features', () => {
likes: {
likes_only: true,
},
lists: {
responsive_web_text_conversations_enabled: true,
},
},
});
@@ -44,9 +41,6 @@ describe('runtime-features', () => {
const likes = buildLikesFeatures();
expect(likes.global_flag).toBe(true);
expect(likes.likes_only).toBe(true);
const lists = buildListsFeatures();
expect(lists.responsive_web_text_conversations_enabled).toBe(true);
});
it('refresh merges defaults with existing cache', async () => {
@@ -119,11 +113,28 @@ describe('runtime-features', () => {
it('includes required lists API feature flags', () => {
const features = buildListsFeatures();
expect(features.responsive_web_graphql_exclude_directive_enabled).toBe(true);
expect(features.blue_business_profile_image_shape_enabled).toBe(true);
expect(features.responsive_web_text_conversations_enabled).toBe(false);
expect(features.tweetypie_unmention_optimization_enabled).toBe(true);
expect(features.vibe_api_enabled).toBe(true);
expect(features.interactive_text_enabled).toBe(true);
expect(features.responsive_web_graphql_exclude_directive_enabled).toBeUndefined();
expect(features.responsive_web_grok_annotations_enabled).toBe(true);
expect(features.rweb_cashtags_enabled).toBe(true);
expect(features.responsive_web_grok_show_grok_translated_post).toBe(true);
expect(features.longform_notetweets_inline_media_enabled).toBe(false);
});
it('keeps catalog-locked list features independent from runtime overrides', () => {
process.env.BIRD_FEATURES_JSON = JSON.stringify({
global: {
responsive_web_grok_annotations_enabled: false,
},
sets: {
lists: {
rweb_cashtags_enabled: false,
},
},
});
clearFeatureOverridesCache();
const features = buildListsFeatures();
expect(features.responsive_web_grok_annotations_enabled).toBe(true);
expect(features.rweb_cashtags_enabled).toBe(true);
});
});
+1
View File
@@ -9,6 +9,7 @@ export type TwitterClientPrivate = TwitterClient & {
error?: string;
}>;
getLikesQueryIds: () => Promise<string[]>;
getListsManagementQueryIds: () => Promise<string[]>;
getListTimelineQueryIds: () => Promise<string[]>;
getListOwnershipsQueryIds: () => Promise<string[]>;
getListMembershipsQueryIds: () => Promise<string[]>;
@@ -0,0 +1,171 @@
// ABOUTME: Tests for the authenticated user's Lists management timeline.
// ABOUTME: Covers nested list items, deduplication, and partial GraphQL errors.
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { TwitterClient } from '../src/lib/twitter-client.js';
import { type TwitterClientPrivate, testRelayBaseUrl } from './twitter-client-fixtures.js';
const originalFetch = global.fetch;
afterEach(() => {
global.fetch = originalFetch;
vi.restoreAllMocks();
});
describe('TwitterClient lists management', () => {
let mockFetch: ReturnType<typeof vi.fn>;
beforeEach(() => {
mockFetch = vi.fn();
global.fetch = mockFetch as unknown as typeof fetch;
});
it('gets and deduplicates the lists shown on the authenticated Lists page', async () => {
const list = {
id_str: '1234567890',
name: 'My List',
description: 'A useful list',
member_count: 10,
mode: 'Private',
created_at: 1_704_067_200_000,
user_results: {
result: {
rest_id: '12345',
core: { screen_name: 'testuser', name: 'Test User' },
},
},
};
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({
data: {
viewer: {
list_management_timeline: {
timeline: {
instructions: [
{
entries: [
{
content: {
clientEventInfo: { component: 'suggest_list_to_follow' },
items: [
{
item: {
itemContent: {
list: { id_str: '999', name: 'Suggested List' },
},
},
},
],
},
},
{
content: {
clientEventInfo: { component: 'suggest_owned_subscribed_list' },
items: [{ item: { itemContent: { list } } }, { item: { itemContent: { list } } }],
},
},
],
},
],
},
},
},
},
errors: [{ message: 'optional banner field failed' }],
}),
});
const client = new TwitterClient({ relayBaseUrl: testRelayBaseUrl });
const clientPrivate = client as unknown as TwitterClientPrivate;
clientPrivate.getListsManagementQueryIds = async () => ['test'];
const result = await client.getLists(100);
expect(result).toEqual({
success: true,
lists: [
{
id: '1234567890',
name: 'My List',
description: 'A useful list',
memberCount: 10,
subscriberCount: undefined,
isPrivate: true,
createdAt: '2024-01-01T00:00:00.000Z',
owner: {
id: '12345',
username: 'testuser',
name: 'Test User',
},
},
],
});
const [url] = mockFetch.mock.calls[0];
const requestUrl = new URL(url as string);
expect(requestUrl.pathname).toContain('/test/ListsManagementPageTimeline');
expect(JSON.parse(requestUrl.searchParams.get('variables') as string)).toEqual({ count: 100 });
});
it('returns an error when the management timeline is missing', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({ errors: [{ message: 'Not authorized' }] }),
});
const client = new TwitterClient({ relayBaseUrl: testRelayBaseUrl });
const clientPrivate = client as unknown as TwitterClientPrivate;
clientPrivate.getListsManagementQueryIds = async () => ['test'];
await expect(client.getLists()).resolves.toEqual({
success: false,
error: 'Not authorized',
});
});
it('ignores an invalid numeric creation date', async () => {
mockFetch.mockResolvedValueOnce({
ok: true,
status: 200,
json: async () => ({
data: {
viewer: {
list_management_timeline: {
timeline: {
instructions: [
{
entries: [
{
content: {
clientEventInfo: { component: 'suggest_owned_subscribed_list' },
items: [
{
item: {
itemContent: {
list: { id_str: '123', name: 'List', created_at: Number.NaN },
},
},
},
],
},
},
],
},
],
},
},
},
},
}),
});
const client = new TwitterClient({ relayBaseUrl: testRelayBaseUrl });
const clientPrivate = client as unknown as TwitterClientPrivate;
clientPrivate.getListsManagementQueryIds = async () => ['test'];
const result = await client.getLists();
expect(result.success && result.lists?.[0].createdAt).toBeUndefined();
});
});
@@ -75,6 +75,9 @@ describe('TwitterClient lists memberships', () => {
expect(result.lists?.[0].id).toBe('5555');
expect(result.lists?.[0].name).toBe('Member List');
expect(result.lists?.[0].owner?.username).toBe('otheruser');
const [url] = mockFetch.mock.calls[0];
const variables = JSON.parse(new URL(url as string).searchParams.get('variables') as string);
expect(variables).toEqual({ userId: '12345', count: 100 });
});
it('retries on 404 error after refreshing query IDs', async () => {
+13 -7
View File
@@ -116,13 +116,19 @@ describe('TwitterClient lists owned', () => {
await client.getOwnedLists(1);
const [url] = mockFetch.mock.calls[0];
const parsedFeatures = JSON.parse(new URL(url as string).searchParams.get('features') as string);
expect(parsedFeatures.responsive_web_graphql_exclude_directive_enabled).toBe(true);
expect(parsedFeatures.blue_business_profile_image_shape_enabled).toBe(true);
expect(parsedFeatures.responsive_web_text_conversations_enabled).toBe(false);
expect(parsedFeatures.tweetypie_unmention_optimization_enabled).toBe(true);
expect(parsedFeatures.vibe_api_enabled).toBe(true);
expect(parsedFeatures.interactive_text_enabled).toBe(true);
const requestUrl = new URL(url as string);
const parsedVariables = JSON.parse(requestUrl.searchParams.get('variables') as string);
const parsedFeatures = JSON.parse(requestUrl.searchParams.get('features') as string);
expect(parsedVariables).toEqual({
userId: '12345',
isListMemberTargetUserId: '12345',
count: 1,
});
expect(parsedFeatures.responsive_web_graphql_exclude_directive_enabled).toBeUndefined();
expect(parsedFeatures.responsive_web_grok_annotations_enabled).toBe(true);
expect(parsedFeatures.rweb_cashtags_enabled).toBe(true);
expect(parsedFeatures.responsive_web_grok_show_grok_translated_post).toBe(true);
expect(parsedFeatures.longform_notetweets_inline_media_enabled).toBe(false);
});
it('handles private lists correctly', async () => {
@@ -66,6 +66,7 @@ describe('TwitterClient lists timeline', () => {
},
},
},
errors: [{ message: 'optional media field failed' }],
}),
});
+10 -4
View File
@@ -83,8 +83,12 @@ describe('TwitterClient search', () => {
expect(body.queryId).toBeDefined();
const urlVars = new URL(url as string).searchParams.get('variables');
expect(urlVars).toBeTruthy();
const parsed = JSON.parse(urlVars as string) as { rawQuery?: string };
const parsed = JSON.parse(urlVars as string) as {
rawQuery?: string;
product?: string;
};
expect(parsed.rawQuery).toBe('needle');
expect(parsed.product).toBe('Latest');
});
it('refreshes query IDs when all search endpoints 404', async () => {
@@ -186,7 +190,7 @@ describe('TwitterClient search', () => {
});
const client = new TwitterClient({ relayBaseUrl: testRelayBaseUrl });
const result = await client.search('needle', 3);
const result = await client.search('needle', 3, { product: 'Top' });
expect(result.success).toBe(true);
expect(result.tweets?.map((tweet) => tweet.id)).toEqual(['1', '2', '3']);
@@ -194,13 +198,15 @@ describe('TwitterClient search', () => {
const firstVars = JSON.parse(
new URL(mockFetch.mock.calls[0][0] as string).searchParams.get('variables') as string,
) as { cursor?: string };
) as { cursor?: string; product?: string };
const secondVars = JSON.parse(
new URL(mockFetch.mock.calls[1][0] as string).searchParams.get('variables') as string,
) as { cursor?: string };
) as { cursor?: string; product?: string };
expect(firstVars.cursor).toBeUndefined();
expect(secondVars.cursor).toBe('cursor-1');
expect(firstVars.product).toBe('Top');
expect(secondVars.product).toBe('Top');
});
it('stops paginating when the cursor repeats', async () => {
+3
View File
@@ -6,3 +6,6 @@
- When adding package registry publishing for this repository, use the `@yuta` package scope unless the user explicitly asks for another namespace.
- Gitea Actions secret and variable names are case-insensitive, alphanumeric or underscore only, and cannot start with `GITEA_` or `GITHUB_`; use names like `PACKAGE_TOKEN`.
- When adapting upstream tests to this fork, run tests with `TWITTER_RELAY_BASE_URL` unset; local relay environment variables can hide missing explicit `relayBaseUrl` fixtures.
- For authenticated account pages, use the viewer-scoped catalog operation that
matches the UI. Do not substitute a similarly named user-scoped operation;
verify the exact request against the relay before changing Bird.