Skip to content

Commit dc61895

Browse files
committed
fix(twitter): add X-Client-Transaction-Id
1 parent 6a664a8 commit dc61895

5 files changed

Lines changed: 82 additions & 1 deletion

File tree

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
package-lock=true
22
package-manager-strict=false
3+
@jsr:registry=https://npm.jsr.io
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ClientTransaction, handleXMigration } from '@lami/x-client-transaction-id';
2+
3+
let clientTransactionPromise: Promise<ClientTransaction> | undefined;
4+
5+
const buildClientTransaction = async () => ClientTransaction.create(await handleXMigration());
6+
7+
const getClientTransaction = () => {
8+
clientTransactionPromise ??= buildClientTransaction();
9+
return clientTransactionPromise;
10+
};
11+
12+
export const getClientTransactionId = async (method: string, path: string) => {
13+
try {
14+
const clientTransaction = await getClientTransaction();
15+
return await clientTransaction.generateTransactionId(method, path);
16+
} catch {
17+
clientTransactionPromise = undefined;
18+
}
19+
};

lib/routes/twitter/api/web-api/utils.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import logger from '@/utils/logger';
1010
import ofetch from '@/utils/ofetch';
1111
import proxy from '@/utils/proxy';
1212

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

@@ -80,6 +81,7 @@ export const twitterGot = async (
8081
params,
8182
options?: {
8283
allowNoAuth?: boolean;
84+
headers?: Record<string, string>;
8385
}
8486
) => {
8587
const auth = await getAuth(30);
@@ -171,6 +173,7 @@ export const twitterGot = async (
171173
: {
172174
'x-guest-token': jsonCookie.gt,
173175
}),
176+
...options?.headers,
174177
},
175178
dispatcher: dispatchers?.agent,
176179
});
@@ -263,7 +266,14 @@ export const paginationTweets = async (endpoint: string, userId: number | undefi
263266
});
264267
return data;
265268
}
266-
const { data } = await twitterGot(baseUrl + gqlMap[endpoint], params);
269+
const transactionId = endpoint === 'UserTweetsAndReplies' ? await getClientTransactionId('GET', new URL(baseUrl + gqlMap[endpoint]).pathname) : undefined;
270+
const { data } = await twitterGot(baseUrl + gqlMap[endpoint], params, {
271+
headers: transactionId
272+
? {
273+
'x-client-transaction-id': transactionId,
274+
}
275+
: undefined,
276+
});
267277
return data;
268278
};
269279

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@hono/node-server": "1.19.14",
6464
"@hono/zod-openapi": "1.3.0",
6565
"@jocmp/mercury-parser": "3.0.7",
66+
"@lami/x-client-transaction-id": "npm:@jsr/lami__x-client-transaction-id@^0.2.0",
6667
"@notionhq/client": "5.18.0",
6768
"@opentelemetry/api": "1.9.1",
6869
"@opentelemetry/exporter-prometheus": "0.214.0",

pnpm-lock.yaml

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)