Skip to content

Commit 0d7825b

Browse files
authored
Merge pull request #12 from thecodrr/feature/allow-custom-flag
Allow overriding default options
2 parents 00fd846 + fba9eb8 commit 0d7825b

File tree

1 file changed

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

1 file changed

+16
-8
lines changed

twitter-openapi-typescript/src/api.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ 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 = {
@@ -97,17 +101,19 @@ export class TwitterOpenApi {
97101
middleware: [{ pre: async (context) => this.setCookies(context, cookies) }],
98102
apiKey: (key) => {
99103
return {
100-
'user-agent': TwitterOpenApi.userAgent,
104+
'user-agent': this.param.userAgent || TwitterOpenApi.userAgent,
101105
'x-twitter-client-language': this.param.lang ?? 'en',
102106
'x-twitter-active-user': 'yes',
103107
'x-csrf-token': cookies.find((cookie) => cookie.name === 'ct0')?.value,
104108
'x-guest-token': cookies.find((cookie) => cookie.name === 'gt')?.value,
105109
}[key]!;
106110
},
107-
accessToken: TwitterOpenApi.bearer,
111+
accessToken: this.param.accessToken || TwitterOpenApi.bearer,
108112
};
109-
const flag = this.fetchApi(TwitterOpenApi.url, { method: 'GET' }).then((res) => res.json()) as Promise<DefaultFlag>;
110-
return new TwitterOpenApiClient(new i.Configuration(config), await flag);
113+
const flag =
114+
this.param.flag ||
115+
((await this.fetchApi(TwitterOpenApi.url, { method: 'GET' }).then((res) => res.json())) as DefaultFlag);
116+
return new TwitterOpenApiClient(new i.Configuration(config), flag);
111117
}
112118

113119
async getClientFromCookies(ct0: string, authToken: string): Promise<TwitterOpenApiClient> {
@@ -124,17 +130,19 @@ export class TwitterOpenApi {
124130
middleware: [{ pre: async (context) => this.setCookies(context, cookies) }],
125131
apiKey: (key) => {
126132
return {
127-
'user-agent': TwitterOpenApi.userAgent,
133+
'user-agent': this.param.userAgent || TwitterOpenApi.userAgent,
128134
'x-twitter-client-language': this.param.lang ?? 'en',
129135
'x-twitter-active-user': 'yes',
130136
'x-twitter-auth-type': 'OAuth2Session',
131137
'x-csrf-token': cookies.find((cookie) => cookie.name === 'ct0')?.value,
132138
}[key]!;
133139
},
134-
accessToken: TwitterOpenApi.bearer,
140+
accessToken: this.param.accessToken || TwitterOpenApi.bearer,
135141
};
136-
const flag = this.fetchApi(TwitterOpenApi.url, { method: 'GET' }).then((res) => res.json()) as Promise<DefaultFlag>;
137-
return new TwitterOpenApiClient(new i.Configuration(config), await flag);
142+
const flag =
143+
this.param.flag ||
144+
((await this.fetchApi(TwitterOpenApi.url, { method: 'GET' }).then((res) => res.json())) as DefaultFlag);
145+
return new TwitterOpenApiClient(new i.Configuration(config), flag);
138146
}
139147
}
140148

0 commit comments

Comments
 (0)