Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
package-lock=true
package-manager-strict=false
@jsr:registry=https://npm.jsr.io
19 changes: 19 additions & 0 deletions lib/routes/twitter/api/web-api/client-transaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ClientTransaction, handleXMigration } from '@lami/x-client-transaction-id';

let clientTransactionPromise: Promise<ClientTransaction> | undefined;

const buildClientTransaction = async () => ClientTransaction.create(await handleXMigration());

const getClientTransaction = () => {
clientTransactionPromise ??= buildClientTransaction();
return clientTransactionPromise;
};

export const getClientTransactionId = async (method: string, path: string) => {
try {
const clientTransaction = await getClientTransaction();
return await clientTransaction.generateTransactionId(method, path);
} catch {
clientTransactionPromise = undefined;
}
};
12 changes: 11 additions & 1 deletion lib/routes/twitter/api/web-api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import logger from '@/utils/logger';
import ofetch from '@/utils/ofetch';
import proxy from '@/utils/proxy';

import { getClientTransactionId } from './client-transaction';
import { baseUrl, bearerToken, gqlFeatures, gqlMap, thirdPartySupportedAPI } from './constants';
import login from './login';

Expand Down Expand Up @@ -80,6 +81,7 @@ export const twitterGot = async (
params,
options?: {
allowNoAuth?: boolean;
headers?: Record<string, string>;
}
) => {
const auth = await getAuth(30);
Expand Down Expand Up @@ -171,6 +173,7 @@ export const twitterGot = async (
: {
'x-guest-token': jsonCookie.gt,
}),
...options?.headers,
},
dispatcher: dispatchers?.agent,
});
Expand Down Expand Up @@ -263,7 +266,14 @@ export const paginationTweets = async (endpoint: string, userId: number | undefi
});
return data;
}
const { data } = await twitterGot(baseUrl + gqlMap[endpoint], params);
const transactionId = endpoint === 'UserTweetsAndReplies' ? await getClientTransactionId('GET', new URL(baseUrl + gqlMap[endpoint]).pathname) : undefined;
const { data } = await twitterGot(baseUrl + gqlMap[endpoint], params, {
headers: transactionId
? {
'x-client-transaction-id': transactionId,
}
: undefined,
});
return data;
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@hono/node-server": "1.19.14",
"@hono/zod-openapi": "1.3.0",
"@jocmp/mercury-parser": "3.0.7",
"@lami/x-client-transaction-id": "npm:@jsr/lami__x-client-transaction-id@^0.2.0",
"@notionhq/client": "5.18.0",
"@opentelemetry/api": "1.9.1",
"@opentelemetry/exporter-prometheus": "0.214.0",
Expand Down
50 changes: 50 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.