Skip to content

Commit fba9eb8

Browse files
committed
allow overriding accessToken and userAgent
1 parent e1e08eb commit fba9eb8

File tree

1 file changed

+12
-16
lines changed
  • twitter-openapi-typescript/src

1 file changed

+12
-16
lines changed

twitter-openapi-typescript/src/api.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ import { DefaultFlag } from '@/models';
1515
export type TwitterOpenApiParams = {
1616
lang?: string;
1717
fetchApi?: i.FetchAPI;
18+
19+
flag?: DefaultFlag;
20+
accessToken?: string;
21+
userAgent?: string;
1822
};
1923

2024
export type TwitterOpenApiCookie = {
2125
name: string;
2226
value: string;
2327
};
2428

25-
export type TwitterOpenApiClientOptions = {
26-
flag?: DefaultFlag;
27-
};
28-
2929
export class TwitterOpenApi {
3030
static hash = 'd5ccc25869b68cbb39c68fa81a1fa77967a667da';
3131
static url = `https://raw.githubusercontent.com/fa0311/twitter-openapi/${this.hash}/src/config/placeholder.json`;
@@ -94,33 +94,29 @@ export class TwitterOpenApi {
9494
return context;
9595
}
9696

97-
async getClient(options?: TwitterOpenApiClientOptions): Promise<TwitterOpenApiClient> {
97+
async getClient(): Promise<TwitterOpenApiClient> {
9898
const cookies: TwitterOpenApiCookie[] = await this.getGuestSession();
9999
const config: i.ConfigurationParameters = {
100100
fetchApi: this.fetchApi,
101101
middleware: [{ pre: async (context) => this.setCookies(context, cookies) }],
102102
apiKey: (key) => {
103103
return {
104-
'user-agent': TwitterOpenApi.userAgent,
104+
'user-agent': this.param.userAgent || TwitterOpenApi.userAgent,
105105
'x-twitter-client-language': this.param.lang ?? 'en',
106106
'x-twitter-active-user': 'yes',
107107
'x-csrf-token': cookies.find((cookie) => cookie.name === 'ct0')?.value,
108108
'x-guest-token': cookies.find((cookie) => cookie.name === 'gt')?.value,
109109
}[key]!;
110110
},
111-
accessToken: TwitterOpenApi.bearer,
111+
accessToken: this.param.accessToken || TwitterOpenApi.bearer,
112112
};
113113
const flag =
114-
options?.flag ||
114+
this.param.flag ||
115115
((await this.fetchApi(TwitterOpenApi.url, { method: 'GET' }).then((res) => res.json())) as DefaultFlag);
116116
return new TwitterOpenApiClient(new i.Configuration(config), flag);
117117
}
118118

119-
async getClientFromCookies(
120-
ct0: string,
121-
authToken: string,
122-
options?: TwitterOpenApiClientOptions,
123-
): Promise<TwitterOpenApiClient> {
119+
async getClientFromCookies(ct0: string, authToken: string): Promise<TwitterOpenApiClient> {
124120
const cookies: TwitterOpenApiCookie[] = await this.getGuestSession();
125121
[
126122
{ name: 'auth_token', value: authToken },
@@ -134,17 +130,17 @@ export class TwitterOpenApi {
134130
middleware: [{ pre: async (context) => this.setCookies(context, cookies) }],
135131
apiKey: (key) => {
136132
return {
137-
'user-agent': TwitterOpenApi.userAgent,
133+
'user-agent': this.param.userAgent || TwitterOpenApi.userAgent,
138134
'x-twitter-client-language': this.param.lang ?? 'en',
139135
'x-twitter-active-user': 'yes',
140136
'x-twitter-auth-type': 'OAuth2Session',
141137
'x-csrf-token': cookies.find((cookie) => cookie.name === 'ct0')?.value,
142138
}[key]!;
143139
},
144-
accessToken: TwitterOpenApi.bearer,
140+
accessToken: this.param.accessToken || TwitterOpenApi.bearer,
145141
};
146142
const flag =
147-
options?.flag ||
143+
this.param.flag ||
148144
((await this.fetchApi(TwitterOpenApi.url, { method: 'GET' }).then((res) => res.json())) as DefaultFlag);
149145
return new TwitterOpenApiClient(new i.Configuration(config), flag);
150146
}

0 commit comments

Comments
 (0)