Skip to content

Commit abbd77f

Browse files
committed
feat: attempt to parse text as json, log failures
1 parent 102bf40 commit abbd77f

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scraper",
88
"crawler"
99
],
10-
"version": "0.19.0",
10+
"version": "0.19.1",
1111
"main": "dist/default/cjs/index.js",
1212
"types": "./dist/types/index.d.ts",
1313
"exports": {

src/api.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export async function requestApi<T>(
105105
};
106106
}
107107

108-
const value: T = await res.json();
108+
const value: T = await flexParseJson(res);
109109
if (res.headers.get('x-rate-limit-incoming') == '0') {
110110
auth.deleteToken();
111111
return { success: true, value };
@@ -114,6 +114,17 @@ export async function requestApi<T>(
114114
}
115115
}
116116

117+
export async function flexParseJson<T>(res: Response): Promise<T> {
118+
try {
119+
return await res.json();
120+
} catch {
121+
log('Failed to parse response as JSON, trying text parse...');
122+
const text = await res.text();
123+
log('Response text:', text);
124+
return JSON.parse(text);
125+
}
126+
}
127+
117128
/** @internal */
118129
export function addApiFeatures(o: object) {
119130
return {

src/auth-user.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TwitterAuthOptions, TwitterGuestAuth } from './auth';
2-
import { requestApi } from './api';
2+
import { flexParseJson, requestApi } from './api';
33
import { CookieJar } from 'tough-cookie';
44
import { updateCookieJar } from './requests';
55
import { Headers } from 'headers-polyfill';
@@ -325,6 +325,10 @@ export class TwitterUserAuth extends TwitterGuestAuth {
325325
if (this.guestToken) {
326326
headers.set('x-guest-token', this.guestToken);
327327
}
328+
headers.set(
329+
'user-agent',
330+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
331+
);
328332
await this.installCsrfToken(headers);
329333
}
330334

@@ -659,7 +663,7 @@ export class TwitterUserAuth extends TwitterGuestAuth {
659663
return { status: 'error', err: await ApiError.fromResponse(res) };
660664
}
661665

662-
const flow: TwitterUserAuthFlowResponse = await res.json();
666+
const flow: TwitterUserAuthFlowResponse = await flexParseJson(res);
663667
if (flow?.flow_token == null) {
664668
return {
665669
status: 'error',

src/auth.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Cookie, CookieJar, MemoryCookieStore } from 'tough-cookie';
22
import { updateCookieJar } from './requests';
33
import { Headers } from 'headers-polyfill';
44
import fetch from 'cross-fetch';
5-
import { FetchTransformOptions } from './api';
5+
import { FetchTransformOptions, flexParseJson } from './api';
66
import {
77
RateLimitEvent,
88
RateLimitStrategy,
@@ -182,6 +182,10 @@ export class TwitterGuestAuth implements TwitterAuth {
182182

183183
headers.set('authorization', `Bearer ${this.bearerToken}`);
184184
headers.set('x-guest-token', token);
185+
headers.set(
186+
'user-agent',
187+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36',
188+
);
185189

186190
const cookies = await this.getCookies();
187191
const xCsrfToken = cookies.find((cookie) => cookie.key === 'ct0');
@@ -259,7 +263,7 @@ export class TwitterGuestAuth implements TwitterAuth {
259263
throw new AuthenticationError(await res.text());
260264
}
261265

262-
const o = await res.json();
266+
const o = await flexParseJson<any>(res);
263267
if (o == null || o['guest_token'] == null) {
264268
throw new AuthenticationError('guest_token not found.');
265269
}

0 commit comments

Comments
 (0)