twitter-openapi-typescript 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +37 -17
  2. package/dist/src/api.d.ts +41 -33
  3. package/dist/src/api.d.ts.map +1 -1
  4. package/dist/src/api.js +239 -151
  5. package/dist/src/api.js.map +1 -1
  6. package/dist/src/apis/defaultApi.d.ts +17 -17
  7. package/dist/src/apis/defaultApi.js +106 -106
  8. package/dist/src/apis/index.d.ts +9 -9
  9. package/dist/src/apis/index.js +21 -21
  10. package/dist/src/apis/initialStateApi.d.ts +2 -2
  11. package/dist/src/apis/initialStateApi.js +9 -9
  12. package/dist/src/apis/postApi.d.ts +51 -51
  13. package/dist/src/apis/postApi.js +236 -236
  14. package/dist/src/apis/tweetApi.d.ts +88 -88
  15. package/dist/src/apis/tweetApi.js +277 -277
  16. package/dist/src/apis/userApi.d.ts +17 -17
  17. package/dist/src/apis/userApi.js +101 -101
  18. package/dist/src/apis/userListApi.d.ts +28 -28
  19. package/dist/src/apis/userListApi.js +135 -135
  20. package/dist/src/apis/v11GetApi.d.ts +7 -7
  21. package/dist/src/apis/v11GetApi.js +11 -11
  22. package/dist/src/apis/v11PostApi.d.ts +22 -22
  23. package/dist/src/apis/v11PostApi.js +93 -93
  24. package/dist/src/apis/v20GetApi.d.ts +7 -7
  25. package/dist/src/apis/v20GetApi.js +11 -11
  26. package/dist/src/index.d.ts +4 -4
  27. package/dist/src/index.js +20 -20
  28. package/dist/src/models/api.d.ts +17 -17
  29. package/dist/src/models/api.js +130 -130
  30. package/dist/src/models/index.d.ts +2 -2
  31. package/dist/src/models/index.js +18 -18
  32. package/dist/src/models/type.d.ts +15 -15
  33. package/dist/src/models/type.js +2 -2
  34. package/dist/src/types/flag.d.ts +5 -5
  35. package/dist/src/types/flag.js +2 -2
  36. package/dist/src/types/header.d.ts +14 -14
  37. package/dist/src/types/header.js +2 -2
  38. package/dist/src/types/index.d.ts +7 -7
  39. package/dist/src/types/index.js +23 -23
  40. package/dist/src/types/response.d.ts +9 -9
  41. package/dist/src/types/response.js +2 -2
  42. package/dist/src/types/timeline.d.ts +10 -10
  43. package/dist/src/types/timeline.js +2 -2
  44. package/dist/src/types/tweet.d.ts +17 -17
  45. package/dist/src/types/tweet.js +2 -2
  46. package/dist/src/types/user.d.ts +10 -10
  47. package/dist/src/types/user.js +2 -2
  48. package/dist/src/types/user_list.d.ts +12 -12
  49. package/dist/src/types/user_list.js +2 -2
  50. package/package.json +4 -4
  51. package/src/api.ts +91 -35
  52. package/test/api/defaultApi.test.ts +0 -1
  53. package/test/init.ts +2 -2
@@ -1,16 +1,16 @@
1
- import * as i from 'twitter-openapi-typescript-generated';
2
- export type ApiFunction<T> = (requestParameters: {
3
- queryId: string;
4
- variables: string;
5
- features: string;
6
- }) => Promise<i.ApiResponse<T>>;
7
- export type RequestParam<T1, T2> = {
8
- apiFn: ApiFunction<T2>;
9
- convertFn: ConvertFunction<T1, T2>;
10
- key: string;
11
- param: {
12
- [key: string]: any;
13
- };
14
- };
15
- export type ConvertFunction<T1, T2> = (arg0: T2) => T1;
1
+ import * as i from 'twitter-openapi-typescript-generated';
2
+ export type ApiFunction<T> = (requestParameters: {
3
+ queryId: string;
4
+ variables: string;
5
+ features: string;
6
+ }) => Promise<i.ApiResponse<T>>;
7
+ export type RequestParam<T1, T2> = {
8
+ apiFn: ApiFunction<T2>;
9
+ convertFn: ConvertFunction<T1, T2>;
10
+ key: string;
11
+ param: {
12
+ [key: string]: any;
13
+ };
14
+ };
15
+ export type ConvertFunction<T1, T2> = (arg0: T2) => T1;
16
16
  //# sourceMappingURL=type.d.ts.map
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=type.js.map
@@ -1,6 +1,6 @@
1
- export type DefaultFlag = {
2
- [key: string]: {
3
- [key: string]: any;
4
- };
5
- };
1
+ export type DefaultFlag = {
2
+ [key: string]: {
3
+ [key: string]: any;
4
+ };
5
+ };
6
6
  //# sourceMappingURL=flag.d.ts.map
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=flag.js.map
@@ -1,15 +1,15 @@
1
- export type ApiUtilsHeader = {
2
- raw: Headers;
3
- connectionHash: string | null;
4
- contentTypeOptions: string | null;
5
- frameOptions: string | null;
6
- rateLimitLimit: number;
7
- rateLimitRemaining: number;
8
- rateLimitReset: number;
9
- responseTime: number;
10
- tfePreserveBody: boolean;
11
- transactionId: string | null;
12
- twitterResponseTags: string | null;
13
- xssProtection: number;
14
- };
1
+ export type ApiUtilsHeader = {
2
+ raw: Headers;
3
+ connectionHash: string | null;
4
+ contentTypeOptions: string | null;
5
+ frameOptions: string | null;
6
+ rateLimitLimit: number;
7
+ rateLimitRemaining: number;
8
+ rateLimitReset: number;
9
+ responseTime: number;
10
+ tfePreserveBody: boolean;
11
+ transactionId: string | null;
12
+ twitterResponseTags: string | null;
13
+ xssProtection: number;
14
+ };
15
15
  //# sourceMappingURL=header.d.ts.map
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=header.js.map
@@ -1,8 +1,8 @@
1
- export * from './flag';
2
- export * from './header';
3
- export * from './timeline';
4
- export * from './tweet';
5
- export * from './user_list';
6
- export * from './user';
7
- export * from './response';
1
+ export * from './flag';
2
+ export * from './header';
3
+ export * from './timeline';
4
+ export * from './tweet';
5
+ export * from './user_list';
6
+ export * from './user';
7
+ export * from './response';
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -1,24 +1,24 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./flag"), exports);
18
- __exportStar(require("./header"), exports);
19
- __exportStar(require("./timeline"), exports);
20
- __exportStar(require("./tweet"), exports);
21
- __exportStar(require("./user_list"), exports);
22
- __exportStar(require("./user"), exports);
23
- __exportStar(require("./response"), exports);
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./flag"), exports);
18
+ __exportStar(require("./header"), exports);
19
+ __exportStar(require("./timeline"), exports);
20
+ __exportStar(require("./tweet"), exports);
21
+ __exportStar(require("./user_list"), exports);
22
+ __exportStar(require("./user"), exports);
23
+ __exportStar(require("./response"), exports);
24
24
  //# sourceMappingURL=index.js.map
@@ -1,10 +1,10 @@
1
- import { ApiUtilsHeader } from './';
2
- export type TwitterApiUtilsResponse<T> = {
3
- raw: TwitterApiUtilsRaw;
4
- header: ApiUtilsHeader;
5
- data: T;
6
- };
7
- export type TwitterApiUtilsRaw = {
8
- response: Response;
9
- };
1
+ import { ApiUtilsHeader } from './';
2
+ export type TwitterApiUtilsResponse<T> = {
3
+ raw: TwitterApiUtilsRaw;
4
+ header: ApiUtilsHeader;
5
+ data: T;
6
+ };
7
+ export type TwitterApiUtilsRaw = {
8
+ response: Response;
9
+ };
10
10
  //# sourceMappingURL=response.d.ts.map
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=response.js.map
@@ -1,11 +1,11 @@
1
- import * as i from 'twitter-openapi-typescript-generated';
2
- export type CursorApiUtilsResponse = {
3
- bottom?: i.TimelineTimelineCursor;
4
- top?: i.TimelineTimelineCursor;
5
- };
6
- export type ApiUtilsRaw = {
7
- response: Response;
8
- instruction: i.InstructionUnion[];
9
- entry: i.TimelineAddEntry[];
10
- };
1
+ import * as i from 'twitter-openapi-typescript-generated';
2
+ export type CursorApiUtilsResponse = {
3
+ bottom?: i.TimelineTimelineCursor;
4
+ top?: i.TimelineTimelineCursor;
5
+ };
6
+ export type ApiUtilsRaw = {
7
+ response: Response;
8
+ instruction: i.InstructionUnion[];
9
+ entry: i.TimelineAddEntry[];
10
+ };
11
11
  //# sourceMappingURL=timeline.d.ts.map
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=timeline.js.map
@@ -1,18 +1,18 @@
1
- import * as i from 'twitter-openapi-typescript-generated';
2
- import { CursorApiUtilsResponse, ApiUtilsRaw, ApiUtilsHeader } from './';
3
- export type TweetListApiUtilsResponse = {
4
- raw: ApiUtilsRaw;
5
- header: ApiUtilsHeader;
6
- cursor: CursorApiUtilsResponse;
7
- data: TweetApiUtilsData[];
8
- };
9
- export type TweetApiUtilsData = {
10
- raw: i.ItemResult;
11
- tweet: i.Tweet;
12
- user: i.User;
13
- reply: TweetApiUtilsData[];
14
- quoted?: TweetApiUtilsData;
15
- retweeted?: TweetApiUtilsData;
16
- promotedMetadata?: any;
17
- };
1
+ import * as i from 'twitter-openapi-typescript-generated';
2
+ import { CursorApiUtilsResponse, ApiUtilsRaw, ApiUtilsHeader } from './';
3
+ export type TweetListApiUtilsResponse = {
4
+ raw: ApiUtilsRaw;
5
+ header: ApiUtilsHeader;
6
+ cursor: CursorApiUtilsResponse;
7
+ data: TweetApiUtilsData[];
8
+ };
9
+ export type TweetApiUtilsData = {
10
+ raw: i.ItemResult;
11
+ tweet: i.Tweet;
12
+ user: i.User;
13
+ reply: TweetApiUtilsData[];
14
+ quoted?: TweetApiUtilsData;
15
+ retweeted?: TweetApiUtilsData;
16
+ promotedMetadata?: any;
17
+ };
18
18
  //# sourceMappingURL=tweet.d.ts.map
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=tweet.js.map
@@ -1,11 +1,11 @@
1
- import * as i from 'twitter-openapi-typescript-generated';
2
- import { ApiUtilsHeader } from './';
3
- export type UserApiUtilsResponse = {
4
- raw: UserApiUtilsRaw;
5
- header: ApiUtilsHeader;
6
- data: i.User;
7
- };
8
- export type UserApiUtilsRaw = {
9
- response: Response;
10
- };
1
+ import * as i from 'twitter-openapi-typescript-generated';
2
+ import { ApiUtilsHeader } from './';
3
+ export type UserApiUtilsResponse = {
4
+ raw: UserApiUtilsRaw;
5
+ header: ApiUtilsHeader;
6
+ data: i.User;
7
+ };
8
+ export type UserApiUtilsRaw = {
9
+ response: Response;
10
+ };
11
11
  //# sourceMappingURL=user.d.ts.map
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=user.js.map
@@ -1,13 +1,13 @@
1
- import * as i from 'twitter-openapi-typescript-generated';
2
- import { ApiUtilsRaw, CursorApiUtilsResponse, ApiUtilsHeader } from './';
3
- export type UserListApiUtilsResponse = {
4
- raw: ApiUtilsRaw;
5
- header: ApiUtilsHeader;
6
- cursor: CursorApiUtilsResponse;
7
- data: UserApiUtilsData[];
8
- };
9
- export type UserApiUtilsData = {
10
- raw: i.TimelineUser;
11
- user: i.User;
12
- };
1
+ import * as i from 'twitter-openapi-typescript-generated';
2
+ import { ApiUtilsRaw, CursorApiUtilsResponse, ApiUtilsHeader } from './';
3
+ export type UserListApiUtilsResponse = {
4
+ raw: ApiUtilsRaw;
5
+ header: ApiUtilsHeader;
6
+ cursor: CursorApiUtilsResponse;
7
+ data: UserApiUtilsData[];
8
+ };
9
+ export type UserApiUtilsData = {
10
+ raw: i.TimelineUser;
11
+ user: i.User;
12
+ };
13
13
  //# sourceMappingURL=user_list.d.ts.map
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=user_list.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twitter-openapi-typescript",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "Implementation of Twitter internal API in TypeScript",
5
5
  "scripts": {
6
6
  "test": "jest",
@@ -21,7 +21,7 @@
21
21
  "main": "./dist/src/index.js",
22
22
  "typings": "./dist/src/index.d.ts",
23
23
  "dependencies": {
24
- "twitter-openapi-typescript-generated": "^0.0.3"
24
+ "twitter-openapi-typescript-generated": "^0.0.5"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/jest": "^28.0.0",
@@ -31,7 +31,7 @@
31
31
  "@typescript-eslint/parser": "^5.53.0",
32
32
  "dotenv": "^16.0.0",
33
33
  "eslint": "^8.0.0",
34
- "eslint-config-standard-with-typescript": "^34.0.0",
34
+ "eslint-config-standard-with-typescript": "^35.0.0",
35
35
  "eslint-plugin-import": "^2.25.0",
36
36
  "eslint-plugin-n": "^15.0.0",
37
37
  "eslint-plugin-promise": "^6.0.0",
@@ -39,6 +39,6 @@
39
39
  "log4js": "^6.9.1",
40
40
  "node-fetch": "^2.0.0",
41
41
  "ts-jest": "^28.0.0",
42
- "typescript": "^4.0"
42
+ "typescript": "^5.1"
43
43
  }
44
44
  }
package/src/api.ts CHANGED
@@ -12,72 +12,128 @@ import {
12
12
  } from '@/apis';
13
13
  import { DefaultFlag } from '@/types/flag';
14
14
 
15
- type TwitterOpenApiParams = {
15
+ export type TwitterOpenApiParams = {
16
16
  lang?: string;
17
+ fetchApi?: i.FetchAPI;
17
18
  };
18
19
 
19
- export type FetchApi = i.FetchAPI;
20
+ export type TwitterOpenApiCookie = {
21
+ name: string;
22
+ value: string;
23
+ };
20
24
 
21
25
  export class TwitterOpenApi {
22
26
  static hash = 'd5ccc25869b68cbb39c68fa81a1fa77967a667da';
23
27
  static url = `https://raw.githubusercontent.com/fa0311/twitter-openapi/${this.hash}/src/config/placeholder.json`;
28
+ static twitter = 'https://twitter.com/home';
29
+
24
30
  static userAgent =
25
31
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36';
26
32
  static bearer =
27
33
  'AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA';
28
34
  param: TwitterOpenApiParams;
29
35
 
30
- fetchApi: FetchApi | undefined;
31
-
32
36
  constructor(param: TwitterOpenApiParams = {}) {
33
37
  this.param = param;
34
38
  }
35
39
 
36
- setFetchApi(fetchApi: FetchApi) {
37
- this.fetchApi = fetchApi;
40
+ get fetchApi(): i.FetchAPI {
41
+ return this.param.fetchApi || fetch;
42
+ }
43
+
44
+ cookieDecode(cookie: string): TwitterOpenApiCookie[] {
45
+ return cookie
46
+ .split(', ')
47
+ .map((cookie) => cookie.split(';')[0])
48
+ .filter((cookie) => cookie.indexOf('=') != -1)
49
+ .map((cookie) => cookie.split('='))
50
+ .map(([name, value]) => ({
51
+ name: name,
52
+ value: value,
53
+ }));
54
+ }
55
+ cookieEncode(cookie: TwitterOpenApiCookie[]): string {
56
+ return cookie.map((cookie) => `${cookie.name}=${cookie.value}`).join('; ');
57
+ }
58
+
59
+ async getGuestSession(): Promise<TwitterOpenApiCookie[]> {
60
+ const response = await this.fetchApi(TwitterOpenApi.twitter, { method: 'GET', redirect: 'manual' });
61
+ const chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
62
+ const csrfToken = [...Array(32)].reduce((a) => a + chars[Math.floor(Math.random() * chars.length)], '');
63
+ const cookies = this.cookieDecode(response.headers.get('set-cookie')!);
64
+ cookies.push({ name: 'ct0', value: csrfToken });
65
+
66
+ const html = await this.fetchApi(TwitterOpenApi.twitter, {
67
+ method: 'GET',
68
+ headers: { Cookie: this.cookieEncode(cookies) },
69
+ }).then((response) => response.text());
70
+
71
+ const re = new RegExp('<script nonce="([a-zA-Z0-9]{48})">(document.cookie="(.*?)";)+<\\/script>');
72
+
73
+ const script = html.match(re)[0];
74
+ script
75
+ .split('document.cookie="')
76
+ .slice(1)
77
+ .map((e) => e.replace('</script>', '').replace('";', ''))
78
+ .map((e) => this.cookieDecode(e)[0])
79
+ .filter((e) => cookies.findIndex((c) => c.name == e.name) == -1)
80
+ .forEach((e) => cookies.push(e));
81
+
82
+ return cookies;
83
+ }
84
+
85
+ setCookies(context: i.RequestContext, cookies: TwitterOpenApiCookie[]): i.RequestContext {
86
+ if (context.init.headers) {
87
+ const headers = context.init.headers as i.HTTPHeaders;
88
+ headers['cookie'] = this.cookieEncode(cookies);
89
+ }
90
+ return context;
91
+ }
92
+
93
+ async getClient(): Promise<TwitterOpenApiClient> {
94
+ const cookies: TwitterOpenApiCookie[] = await this.getGuestSession();
95
+ const config: i.ConfigurationParameters = {
96
+ fetchApi: this.fetchApi,
97
+ middleware: [{ pre: async (context) => this.setCookies(context, cookies) }],
98
+ apiKey: (key) => {
99
+ return {
100
+ 'user-agent': TwitterOpenApi.userAgent,
101
+ 'x-twitter-client-language': this.param.lang ?? 'en',
102
+ 'x-twitter-active-user': 'yes',
103
+ 'x-csrf-token': cookies.find((cookie) => cookie.name === 'ct0')?.value,
104
+ 'x-guest-token': cookies.find((cookie) => cookie.name === 'gt')?.value,
105
+ }[key]!;
106
+ },
107
+ accessToken: TwitterOpenApi.bearer,
108
+ };
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);
38
111
  }
39
112
 
40
113
  async getClientFromCookies(ct0: string, authToken: string): Promise<TwitterOpenApiClient> {
114
+ const cookies: TwitterOpenApiCookie[] = await this.getGuestSession();
115
+ [
116
+ { name: 'auth_token', value: authToken },
117
+ { name: 'ct0', value: ct0 },
118
+ ]
119
+ .filter((e) => cookies.findIndex((c) => c.name == e.name) == -1)
120
+ .forEach((e) => cookies.push(e));
121
+
41
122
  const config: i.ConfigurationParameters = {
42
- fetchApi: this.fetchApi || fetch,
43
- middleware: [
44
- {
45
- pre: async (context) => {
46
- const cookie = [
47
- {
48
- name: 'auth_token',
49
- value: authToken,
50
- },
51
- {
52
- name: 'ct0',
53
- value: ct0,
54
- },
55
- ]
56
- .map((cookie) => `${cookie.name}=${cookie.value}`)
57
- .join('; ');
58
-
59
- if (context.init.headers) {
60
- const headers = context.init.headers as i.HTTPHeaders;
61
- headers['cookie'] = cookie;
62
- }
63
- return context;
64
- },
65
- },
66
- ],
123
+ fetchApi: this.param.fetchApi || fetch,
124
+ middleware: [{ pre: async (context) => this.setCookies(context, cookies) }],
67
125
  apiKey: (key) => {
68
126
  return {
69
127
  'user-agent': TwitterOpenApi.userAgent,
70
128
  'x-twitter-client-language': this.param.lang ?? 'en',
71
129
  'x-twitter-active-user': 'yes',
72
130
  'x-twitter-auth-type': 'OAuth2Session',
73
- 'x-csrf-token': ct0,
131
+ 'x-csrf-token': cookies.find((cookie) => cookie.name === 'ct0')?.value,
74
132
  }[key]!;
75
133
  },
76
134
  accessToken: TwitterOpenApi.bearer,
77
135
  };
78
- const flag = config
79
- .fetchApi(TwitterOpenApi.url, { method: 'GET' })
80
- .then((res) => res.json()) as Promise<DefaultFlag>;
136
+ const flag = this.fetchApi(TwitterOpenApi.url, { method: 'GET' }).then((res) => res.json()) as Promise<DefaultFlag>;
81
137
  return new TwitterOpenApiClient(new i.Configuration(config), await flag);
82
138
  }
83
139
  }
@@ -1,5 +1,4 @@
1
1
  import { getClient, logger } from '@test/init';
2
- import { printTweet } from '@test/util';
3
2
 
4
3
  test('getProfileSpotlightsQuery', async () => {
5
4
  logger.log('getProfileSpotlightsQuery');
package/test/init.ts CHANGED
@@ -27,8 +27,8 @@ export const logger = log4js
27
27
  .getLogger('Test');
28
28
 
29
29
  export const getClient = async () => {
30
- const api = new TwitterOpenApi();
31
- api.setFetchApi(fetch as any);
30
+ const api = new TwitterOpenApi({ fetchApi: fetch as any });
32
31
  const client = await api.getClientFromCookies(ct0, authToken);
32
+ // const client = await api.getClient();
33
33
  return client;
34
34
  };