twitter-openapi-typescript 0.0.1

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 (85) hide show
  1. package/.prettierrc +8 -0
  2. package/.vscode/extensions.json +3 -0
  3. package/.vscode/settings.json +5 -0
  4. package/dist/src/api.d.ts +30 -0
  5. package/dist/src/api.js +145 -0
  6. package/dist/src/apis/defaultApi.d.ts +17 -0
  7. package/dist/src/apis/defaultApi.js +106 -0
  8. package/dist/src/apis/index.d.ts +9 -0
  9. package/dist/src/apis/index.js +21 -0
  10. package/dist/src/apis/initialStateApi.d.ts +2 -0
  11. package/dist/src/apis/initialStateApi.js +9 -0
  12. package/dist/src/apis/postApi.d.ts +51 -0
  13. package/dist/src/apis/postApi.js +236 -0
  14. package/dist/src/apis/tweetApi.d.ts +88 -0
  15. package/dist/src/apis/tweetApi.js +277 -0
  16. package/dist/src/apis/userApi.d.ts +17 -0
  17. package/dist/src/apis/userApi.js +101 -0
  18. package/dist/src/apis/userListApi.d.ts +28 -0
  19. package/dist/src/apis/userListApi.js +135 -0
  20. package/dist/src/apis/v11GetApi.d.ts +7 -0
  21. package/dist/src/apis/v11GetApi.js +11 -0
  22. package/dist/src/apis/v11PostApi.d.ts +22 -0
  23. package/dist/src/apis/v11PostApi.js +93 -0
  24. package/dist/src/apis/v20GetApi.d.ts +7 -0
  25. package/dist/src/apis/v20GetApi.js +11 -0
  26. package/dist/src/index.d.ts +4 -0
  27. package/dist/src/index.js +20 -0
  28. package/dist/src/models/api.d.ts +17 -0
  29. package/dist/src/models/api.js +130 -0
  30. package/dist/src/models/index.d.ts +2 -0
  31. package/dist/src/models/index.js +18 -0
  32. package/dist/src/models/type.d.ts +15 -0
  33. package/dist/src/models/type.js +2 -0
  34. package/dist/src/types/flag.d.ts +5 -0
  35. package/dist/src/types/flag.js +2 -0
  36. package/dist/src/types/header.d.ts +14 -0
  37. package/dist/src/types/header.js +2 -0
  38. package/dist/src/types/index.d.ts +7 -0
  39. package/dist/src/types/index.js +23 -0
  40. package/dist/src/types/response.d.ts +9 -0
  41. package/dist/src/types/response.js +2 -0
  42. package/dist/src/types/timeline.d.ts +10 -0
  43. package/dist/src/types/timeline.js +2 -0
  44. package/dist/src/types/tweet.d.ts +17 -0
  45. package/dist/src/types/tweet.js +2 -0
  46. package/dist/src/types/user.d.ts +10 -0
  47. package/dist/src/types/user.js +2 -0
  48. package/dist/src/types/user_list.d.ts +12 -0
  49. package/dist/src/types/user_list.js +2 -0
  50. package/jest.config.js +14 -0
  51. package/package.json +44 -0
  52. package/src/api.ts +118 -0
  53. package/src/apis/defaultApi.ts +51 -0
  54. package/src/apis/index.ts +9 -0
  55. package/src/apis/initialStateApi.ts +3 -0
  56. package/src/apis/postApi.ts +169 -0
  57. package/src/apis/tweetApi.ts +244 -0
  58. package/src/apis/userApi.ts +47 -0
  59. package/src/apis/userListApi.ts +90 -0
  60. package/src/apis/v11GetApi.ts +12 -0
  61. package/src/apis/v11PostApi.ts +73 -0
  62. package/src/apis/v20GetApi.ts +12 -0
  63. package/src/index.ts +4 -0
  64. package/src/models/api.ts +129 -0
  65. package/src/models/index.ts +2 -0
  66. package/src/models/type.ts +16 -0
  67. package/src/types/flag.ts +5 -0
  68. package/src/types/header.ts +14 -0
  69. package/src/types/index.ts +7 -0
  70. package/src/types/response.ts +11 -0
  71. package/src/types/timeline.ts +12 -0
  72. package/src/types/tweet.ts +19 -0
  73. package/src/types/user.ts +12 -0
  74. package/src/types/user_list.ts +14 -0
  75. package/test/api/defaultApi.test.ts +13 -0
  76. package/test/api/postApi.test.ts +50 -0
  77. package/test/api/tweetApi.test.ts +74 -0
  78. package/test/api/userApi.test.ts +13 -0
  79. package/test/api/userListApi.test.ts +18 -0
  80. package/test/api/v11PostApi.test.ts +13 -0
  81. package/test/init.ts +39 -0
  82. package/test/raw/tweet_api.test.ts +12 -0
  83. package/test/util.ts +18 -0
  84. package/tsconfig.build.json +22 -0
  85. package/tsconfig.json +22 -0
package/src/api.ts ADDED
@@ -0,0 +1,118 @@
1
+ import * as i from 'twitter-openapi-typescript-generated';
2
+ import {
3
+ DefaultApiUtils,
4
+ InitialStateApiUtils,
5
+ PostApiUtils,
6
+ TweetApiUtils,
7
+ UserApiUtils,
8
+ UserListApiUtils,
9
+ V11GetApiUtils,
10
+ V11PostApiUtils,
11
+ V20GetApiUtils,
12
+ } from '@/apis';
13
+ import { DefaultFlag } from '@/types/flag';
14
+
15
+ type TwitterOpenApiParams = {
16
+ lang?: string;
17
+ };
18
+
19
+ export class TwitterOpenApi {
20
+ static hash = 'd5ccc25869b68cbb39c68fa81a1fa77967a667da';
21
+ static url = `https://raw.githubusercontent.com/fa0311/twitter-openapi/${this.hash}/src/config/placeholder.json`;
22
+ static userAgent =
23
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36';
24
+ static bearer =
25
+ 'AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA';
26
+ param: TwitterOpenApiParams;
27
+
28
+ constructor(param: TwitterOpenApiParams = {}) {
29
+ this.param = param;
30
+ }
31
+
32
+ async getClientFromCookies(ct0: string, authToken: string): Promise<TwitterOpenApiClient> {
33
+ const config: i.ConfigurationParameters = {
34
+ middleware: [
35
+ {
36
+ pre: async (context) => {
37
+ const cookie = [
38
+ {
39
+ name: 'auth_token',
40
+ value: authToken,
41
+ },
42
+ {
43
+ name: 'ct0',
44
+ value: ct0,
45
+ },
46
+ ]
47
+ .map((cookie) => `${cookie.name}=${cookie.value}`)
48
+ .join('; ');
49
+
50
+ if (context.init.headers) {
51
+ const headers = context.init.headers as i.HTTPHeaders;
52
+ headers['cookie'] = cookie;
53
+ }
54
+ return context;
55
+ },
56
+ },
57
+ ],
58
+ apiKey: (key) => {
59
+ return {
60
+ 'user-agent': TwitterOpenApi.userAgent,
61
+ 'x-twitter-client-language': this.param.lang ?? 'en',
62
+ 'x-twitter-active-user': 'yes',
63
+ 'x-twitter-auth-type': 'OAuth2Session',
64
+ 'x-csrf-token': ct0,
65
+ }[key]!;
66
+ },
67
+ accessToken: TwitterOpenApi.bearer,
68
+ };
69
+ const flag = fetch(TwitterOpenApi.url, { method: 'GET' }).then((res) => res.json()) as Promise<DefaultFlag>;
70
+ return new TwitterOpenApiClient(new i.Configuration(config), await flag);
71
+ }
72
+ }
73
+
74
+ export class TwitterOpenApiClient {
75
+ private config: i.Configuration;
76
+ public flag: DefaultFlag;
77
+
78
+ constructor(config: i.Configuration, flag: DefaultFlag) {
79
+ this.config = config;
80
+ this.flag = flag;
81
+ }
82
+
83
+ getDefaultApi(): DefaultApiUtils {
84
+ return new DefaultApiUtils(new i.DefaultApi(this.config), this.flag);
85
+ }
86
+
87
+ getTweetApi(): TweetApiUtils {
88
+ return new TweetApiUtils(new i.TweetApi(this.config), this.flag);
89
+ }
90
+
91
+ getUserApi(): UserApiUtils {
92
+ return new UserApiUtils(new i.UserApi(this.config), this.flag);
93
+ }
94
+
95
+ getUserListApi(): UserListApiUtils {
96
+ return new UserListApiUtils(new i.UserListApi(this.config), this.flag);
97
+ }
98
+
99
+ getPostApi(): PostApiUtils {
100
+ return new PostApiUtils(new i.PostApi(this.config), this.flag);
101
+ }
102
+
103
+ getV11GetApi(): V11GetApiUtils {
104
+ return new V11GetApiUtils(new i.V11GetApi(this.config), this.flag);
105
+ }
106
+
107
+ getV11PostApi(): V11PostApiUtils {
108
+ return new V11PostApiUtils(new i.V11PostApi(this.config), this.flag);
109
+ }
110
+
111
+ getV20GetApi(): V20GetApiUtils {
112
+ return new V20GetApiUtils(new i.V20GetApi(this.config), this.flag);
113
+ }
114
+
115
+ getInitialStateApi(): InitialStateApiUtils {
116
+ return new InitialStateApiUtils();
117
+ }
118
+ }
@@ -0,0 +1,51 @@
1
+ import * as i from 'twitter-openapi-typescript-generated';
2
+ import { DefaultFlag } from '@/types';
3
+ import { RequestParam, buildHeader } from '@/models';
4
+ import { TwitterApiUtilsResponse } from '@/types/response';
5
+
6
+ export type ProfileSpotlightsQueryParam = {
7
+ screenName: string;
8
+ extraParam?: { [key: string]: any };
9
+ };
10
+
11
+ export class DefaultApiUtils {
12
+ api: i.DefaultApi;
13
+ flag: DefaultFlag;
14
+
15
+ constructor(api: i.DefaultApi, flag: DefaultFlag) {
16
+ this.api = api;
17
+ this.flag = flag;
18
+ }
19
+
20
+ async request<T1, T2>(param: RequestParam<T1, T2>): Promise<TwitterApiUtilsResponse<T1>> {
21
+ const apiFn: typeof param.apiFn = param.apiFn.bind(this.api);
22
+ const response = await apiFn({
23
+ queryId: this.flag[param.key]['queryId'],
24
+ variables: JSON.stringify({ ...this.flag[param.key]['variables'], ...param }),
25
+ features: JSON.stringify(this.flag[param.key]['features']),
26
+ });
27
+ const data = param.convertFn(await response.value());
28
+ return {
29
+ raw: { response: response.raw },
30
+ header: buildHeader(response.raw.headers),
31
+ data: data,
32
+ };
33
+ }
34
+
35
+ async getProfileSpotlightsQuery(
36
+ param: ProfileSpotlightsQueryParam,
37
+ ): Promise<TwitterApiUtilsResponse<i.UserResultByScreenName>> {
38
+ const args = {
39
+ screenName: param.screenName,
40
+ ...param.extraParam,
41
+ };
42
+ const response = await this.request({
43
+ key: 'ProfileSpotlightsQuery',
44
+ apiFn: this.api.getProfileSpotlightsQueryRaw,
45
+ convertFn: (value) => value.data.userResultByScreenName,
46
+ param: args,
47
+ });
48
+
49
+ return response;
50
+ }
51
+ }
@@ -0,0 +1,9 @@
1
+ export { DefaultApiUtils } from './defaultApi';
2
+ export { TweetApiUtils } from './tweetApi';
3
+ export { PostApiUtils } from './postApi';
4
+ export { UserApiUtils } from './userApi';
5
+ export { UserListApiUtils } from './userListApi';
6
+ export { V11GetApiUtils } from './v11GetApi';
7
+ export { V11PostApiUtils } from './v11PostApi';
8
+ export { V20GetApiUtils } from './v20GetApi';
9
+ export { InitialStateApiUtils } from './initialStateApi';
@@ -0,0 +1,3 @@
1
+ export class InitialStateApiUtils {
2
+ // todo
3
+ }
@@ -0,0 +1,169 @@
1
+ import * as i from 'twitter-openapi-typescript-generated';
2
+ import { DefaultFlag } from '@/types';
3
+ import { buildHeader, entriesCursor } from '@/models';
4
+ import { TwitterApiUtilsResponse } from '@/types';
5
+
6
+ type PostCreateTweetParam = {
7
+ tweetText: string;
8
+ extraParam?: { [key: string]: any };
9
+ };
10
+
11
+ type PostDeleteTweetParam = {
12
+ tweetId: string;
13
+ extraParam?: { [key: string]: any };
14
+ };
15
+
16
+ type PostCreateRetweetParam = {
17
+ tweetId: string;
18
+ extraParam?: { [key: string]: any };
19
+ };
20
+
21
+ type PostDeleteRetweetParam = {
22
+ sourceTweetId: string;
23
+ extraParam?: { [key: string]: any };
24
+ };
25
+
26
+ type PostFavoriteTweetParam = {
27
+ tweetId: string;
28
+ extraParam?: { [key: string]: any };
29
+ };
30
+
31
+ type PostUnfavoriteTweetParam = {
32
+ tweetId: string;
33
+ extraParam?: { [key: string]: any };
34
+ };
35
+
36
+ export class PostApiUtils {
37
+ api: i.PostApi;
38
+ flag: DefaultFlag;
39
+
40
+ constructor(api: i.PostApi, flag: DefaultFlag) {
41
+ this.api = api;
42
+ this.flag = flag;
43
+ }
44
+
45
+ async postCreateTweet(param: PostCreateTweetParam): Promise<TwitterApiUtilsResponse<i.CreateTweetResponse>> {
46
+ const args = {
47
+ tweetText: param.tweetText,
48
+ ...param.extraParam,
49
+ };
50
+ const queryId = 'CreateTweet';
51
+ const features = i.PostCreateTweetRequestFeaturesFromJSON(this.flag[queryId]['features']);
52
+ const variables = i.PostCreateTweetRequestVariablesFromJSON(this.flag[queryId]['variables']);
53
+ const response = await this.api.postCreateTweetRaw({
54
+ postCreateTweetRequest: {
55
+ queryId: queryId,
56
+ features: features,
57
+ variables: { ...variables, ...args },
58
+ },
59
+ });
60
+ return {
61
+ raw: { response: response.raw },
62
+ header: buildHeader(response.raw.headers),
63
+ data: await response.value(),
64
+ };
65
+ }
66
+
67
+ async postDeleteTweet(param: PostDeleteTweetParam): Promise<TwitterApiUtilsResponse<i.DeleteTweetResponse>> {
68
+ const args = {
69
+ tweetId: param.tweetId,
70
+ ...param.extraParam,
71
+ };
72
+ const queryId = 'DeleteTweet';
73
+ const variables = i.PostDeleteTweetRequestVariablesFromJSON(this.flag[queryId]['variables']);
74
+ const response = await this.api.postDeleteTweetRaw({
75
+ postDeleteTweetRequest: {
76
+ queryId: queryId,
77
+ variables: { ...variables, ...args },
78
+ },
79
+ });
80
+ return {
81
+ raw: { response: response.raw },
82
+ header: buildHeader(response.raw.headers),
83
+ data: await response.value(),
84
+ };
85
+ }
86
+
87
+ async postCreateRetweet(param: PostCreateRetweetParam): Promise<TwitterApiUtilsResponse<i.CreateRetweetResponse>> {
88
+ const args = {
89
+ tweetId: param.tweetId,
90
+ ...param.extraParam,
91
+ };
92
+ const queryId = 'CreateRetweet';
93
+ const variables = i.PostCreateTweetRequestVariablesFromJSON(this.flag[queryId]['variables']);
94
+ const response = await this.api.postCreateRetweetRaw({
95
+ postCreateRetweetRequest: {
96
+ queryId: queryId,
97
+ variables: { ...variables, ...args },
98
+ },
99
+ });
100
+ return {
101
+ raw: { response: response.raw },
102
+ header: buildHeader(response.raw.headers),
103
+ data: await response.value(),
104
+ };
105
+ }
106
+ async postDeleteRetweet(param: PostDeleteRetweetParam): Promise<TwitterApiUtilsResponse<i.DeleteRetweetResponse>> {
107
+ const args = {
108
+ sourceTweetId: param.sourceTweetId,
109
+ ...param.extraParam,
110
+ };
111
+ const queryId = 'DeleteRetweet';
112
+ const variables = i.PostDeleteRetweetRequestVariablesFromJSON(this.flag[queryId]['variables']);
113
+ const response = await this.api.postDeleteRetweetRaw({
114
+ postDeleteRetweetRequest: {
115
+ queryId: queryId,
116
+ variables: { ...variables, ...args },
117
+ },
118
+ });
119
+ return {
120
+ raw: { response: response.raw },
121
+ header: buildHeader(response.raw.headers),
122
+ data: await response.value(),
123
+ };
124
+ }
125
+
126
+ async postFavoriteTweet(
127
+ param: PostFavoriteTweetParam,
128
+ ): Promise<TwitterApiUtilsResponse<i.FavoriteTweetResponseData>> {
129
+ const args = {
130
+ tweetId: param.tweetId,
131
+ ...param.extraParam,
132
+ };
133
+ const queryId = 'FavoriteTweet';
134
+ const variables = i.PostDeleteTweetRequestVariablesFromJSON(this.flag[queryId]['variables']);
135
+ const response = await this.api.postFavoriteTweetRaw({
136
+ postFavoriteTweetRequest: {
137
+ queryId: queryId,
138
+ variables: { ...variables, ...args },
139
+ },
140
+ });
141
+ return {
142
+ raw: { response: response.raw },
143
+ header: buildHeader(response.raw.headers),
144
+ data: await response.value(),
145
+ };
146
+ }
147
+
148
+ async postUnfavoriteTweet(
149
+ param: PostUnfavoriteTweetParam,
150
+ ): Promise<TwitterApiUtilsResponse<i.UnfavoriteTweetResponseData>> {
151
+ const args = {
152
+ tweetId: param.tweetId,
153
+ ...param.extraParam,
154
+ };
155
+ const queryId = 'UnfavoriteTweet';
156
+ const variables = i.PostDeleteTweetRequestVariablesFromJSON(this.flag[queryId]['variables']);
157
+ const response = await this.api.postUnfavoriteTweetRaw({
158
+ postUnfavoriteTweetRequest: {
159
+ queryId: queryId,
160
+ variables: { ...variables, ...args },
161
+ },
162
+ });
163
+ return {
164
+ raw: { response: response.raw },
165
+ header: buildHeader(response.raw.headers),
166
+ data: await response.value(),
167
+ };
168
+ }
169
+ }
@@ -0,0 +1,244 @@
1
+ import * as i from 'twitter-openapi-typescript-generated';
2
+ import { DefaultFlag, TweetListApiUtilsResponse, ApiUtilsRaw } from '@/types';
3
+ import { RequestParam, buildHeader, entriesCursor, instructionToEntry, tweetEntriesConverter } from '@/models';
4
+
5
+ type GetTweetDetailParam = {
6
+ focalTweetId: string;
7
+ cursor?: string;
8
+ controllerData?: string;
9
+ extraParam?: { [key: string]: any };
10
+ };
11
+
12
+ type GetHomeTimelineParam = {
13
+ couser?: string;
14
+ count?: number;
15
+ extraParam?: { [key: string]: any };
16
+ };
17
+
18
+ type GetHomeLatestTimelineParam = {
19
+ cursor?: string;
20
+ count?: number;
21
+ extraParam?: { [key: string]: any };
22
+ };
23
+
24
+ type GetListLatestTweetsTimelineParam = {
25
+ listId: string;
26
+ cursor?: string;
27
+ count?: number;
28
+ extraParam?: { [key: string]: any };
29
+ };
30
+
31
+ type GetUserTweetsParam = {
32
+ userId: string;
33
+ cursor?: string;
34
+ count?: number;
35
+ extraParam?: { [key: string]: any };
36
+ };
37
+
38
+ type GetUserTweetsAndRepliesParam = {
39
+ userId: string;
40
+ cursor?: string;
41
+ count?: number;
42
+ extraParam?: { [key: string]: any };
43
+ };
44
+
45
+ type GetUserMediaParam = {
46
+ userId: string;
47
+ cursor?: string;
48
+ count?: number;
49
+ extraParam?: { [key: string]: any };
50
+ };
51
+
52
+ type GetLikesParam = {
53
+ userId: string;
54
+ cursor?: string;
55
+ count?: number;
56
+ extraParam?: { [key: string]: any };
57
+ };
58
+
59
+ type GetBookmarksParam = {
60
+ cursor?: string;
61
+ count?: number;
62
+ extraParam?: { [key: string]: any };
63
+ };
64
+
65
+ export class TweetApiUtils {
66
+ api: i.TweetApi;
67
+ flag: DefaultFlag;
68
+
69
+ constructor(api: i.TweetApi, flag: DefaultFlag) {
70
+ this.api = api;
71
+ this.flag = flag;
72
+ }
73
+
74
+ async request<T>(param: RequestParam<i.InstructionUnion[], T>): Promise<TweetListApiUtilsResponse> {
75
+ const apiFn: typeof param.apiFn = param.apiFn.bind(this.api);
76
+ const response = await apiFn({
77
+ queryId: this.flag[param.key]['queryId'],
78
+ variables: JSON.stringify({ ...this.flag[param.key]['variables'], ...param }),
79
+ features: JSON.stringify(this.flag[param.key]['features']),
80
+ });
81
+ const instruction = param.convertFn(await response.value());
82
+ const entry = instructionToEntry(instruction);
83
+ const data = tweetEntriesConverter(entry);
84
+ const raw: ApiUtilsRaw = {
85
+ response: response.raw,
86
+ instruction: instruction,
87
+ entry: entry,
88
+ };
89
+ return {
90
+ raw: raw,
91
+ header: buildHeader(response.raw.headers),
92
+ cursor: entriesCursor(entry),
93
+ data: data,
94
+ };
95
+ }
96
+
97
+ async getTweetDetail(param: GetTweetDetailParam): Promise<TweetListApiUtilsResponse> {
98
+ const args = {
99
+ focalTweetId: param.focalTweetId,
100
+ ...(param.cursor == null ? {} : { cursor: param.cursor }),
101
+ ...(param.controllerData == null ? {} : { controller_data: param.controllerData }),
102
+ ...param.extraParam,
103
+ };
104
+
105
+ const response = await this.request({
106
+ apiFn: this.api.getTweetDetailRaw,
107
+ convertFn: (e) => e.data.threadedConversationWithInjectionsV2.instructions,
108
+ key: 'TweetDetail',
109
+ param: args,
110
+ });
111
+ return response;
112
+ }
113
+
114
+ async getHomeTimeline(param: GetHomeTimelineParam = {}): Promise<TweetListApiUtilsResponse> {
115
+ const args = {
116
+ ...(param.count == null ? {} : { count: param.count }),
117
+ ...(param.couser == null ? {} : { couser: param.couser }),
118
+ ...param.extraParam,
119
+ };
120
+
121
+ const response = await this.request({
122
+ apiFn: this.api.getHomeTimelineRaw,
123
+ convertFn: (e) => e.data.home.homeTimelineUrt.instructions,
124
+ key: 'HomeTimeline',
125
+ param: args,
126
+ });
127
+ return response;
128
+ }
129
+
130
+ async getHomeLatestTimeline(param: GetHomeLatestTimelineParam = {}): Promise<TweetListApiUtilsResponse> {
131
+ const args = {
132
+ ...(param.count == null ? {} : { count: param.count }),
133
+ ...(param.cursor == null ? {} : { cursor: param.cursor }),
134
+ ...param.extraParam,
135
+ };
136
+
137
+ const response = await this.request({
138
+ apiFn: this.api.getHomeLatestTimelineRaw,
139
+ convertFn: (e) => e.data.home.homeTimelineUrt.instructions,
140
+ key: 'HomeLatestTimeline',
141
+ param: args,
142
+ });
143
+ return response;
144
+ }
145
+
146
+ async getListLatestTweetsTimeline(param: GetListLatestTweetsTimelineParam): Promise<TweetListApiUtilsResponse> {
147
+ const args = {
148
+ listId: param.listId,
149
+ ...(param.count == null ? {} : { count: param.count }),
150
+ ...(param.cursor == null ? {} : { cursor: param.cursor }),
151
+ ...param.extraParam,
152
+ };
153
+
154
+ const response = await this.request({
155
+ apiFn: this.api.getListLatestTweetsTimelineRaw,
156
+ convertFn: (e) => e.data.list.tweetsTimeline.timeline.instructions,
157
+ key: 'ListLatestTweetsTimeline',
158
+ param: args,
159
+ });
160
+ return response;
161
+ }
162
+
163
+ async getUserTweets(param: GetUserTweetsParam): Promise<TweetListApiUtilsResponse> {
164
+ const args = {
165
+ userId: param.userId,
166
+ ...(param.count == null ? {} : { count: param.count }),
167
+ ...(param.cursor == null ? {} : { cursor: param.cursor }),
168
+ ...param.extraParam,
169
+ };
170
+
171
+ const response = await this.request({
172
+ apiFn: this.api.getUserTweetsRaw,
173
+ convertFn: (e) => e.data.user.result.timelineV2.timeline.instructions,
174
+ key: 'UserTweets',
175
+ param: args,
176
+ });
177
+ return response;
178
+ }
179
+
180
+ async getUserTweetsAndReplies(param: GetUserTweetsAndRepliesParam): Promise<TweetListApiUtilsResponse> {
181
+ const args = {
182
+ userId: param.userId,
183
+ ...(param.count == null ? {} : { count: param.count }),
184
+ ...(param.cursor == null ? {} : { cursor: param.cursor }),
185
+ ...param.extraParam,
186
+ };
187
+
188
+ const response = await this.request({
189
+ apiFn: this.api.getUserTweetsAndRepliesRaw,
190
+ convertFn: (e) => e.data.user.result.timelineV2.timeline.instructions,
191
+ key: 'UserTweetsAndReplies',
192
+ param: args,
193
+ });
194
+ return response;
195
+ }
196
+
197
+ async getUserMedia(param: GetUserMediaParam): Promise<TweetListApiUtilsResponse> {
198
+ const args = {
199
+ userId: param.userId,
200
+ ...(param.count == null ? {} : { count: param.count }),
201
+ ...(param.cursor == null ? {} : { cursor: param.cursor }),
202
+ ...param.extraParam,
203
+ };
204
+
205
+ const response = await this.request({
206
+ apiFn: this.api.getUserMediaRaw,
207
+ convertFn: (e) => e.data.user.result.timelineV2.timeline.instructions,
208
+ key: 'UserMedia',
209
+ param: args,
210
+ });
211
+ return response;
212
+ }
213
+ async getLikes(param: GetLikesParam): Promise<TweetListApiUtilsResponse> {
214
+ const args = {
215
+ userId: param.userId,
216
+ ...(param.count == null ? {} : { count: param.count }),
217
+ ...(param.cursor == null ? {} : { cursor: param.cursor }),
218
+ ...param.extraParam,
219
+ };
220
+
221
+ const response = await this.request({
222
+ apiFn: this.api.getLikesRaw,
223
+ convertFn: (e) => e.data.user.result.timelineV2.timeline.instructions,
224
+ key: 'Likes',
225
+ param: args,
226
+ });
227
+ return response;
228
+ }
229
+ async getBookmarks(param: GetBookmarksParam = {}): Promise<TweetListApiUtilsResponse> {
230
+ const args = {
231
+ ...(param.count == null ? {} : { count: param.count }),
232
+ ...(param.cursor == null ? {} : { cursor: param.cursor }),
233
+ ...param.extraParam,
234
+ };
235
+
236
+ const response = await this.request({
237
+ apiFn: this.api.getBookmarksRaw,
238
+ convertFn: (e) => e.data.bookmarkTimelineV2.timeline.instructions,
239
+ key: 'Bookmarks',
240
+ param: args,
241
+ });
242
+ return response;
243
+ }
244
+ }
@@ -0,0 +1,47 @@
1
+ import * as i from 'twitter-openapi-typescript-generated';
2
+ import { UserApiUtilsRaw, DefaultFlag, UserApiUtilsResponse } from '@/types';
3
+ import { RequestParam, buildHeader } from '@/models';
4
+
5
+ type getUserByScreenNameParam = {
6
+ screenName: string;
7
+ extraParam?: { [key: string]: any };
8
+ };
9
+
10
+ export class UserApiUtils {
11
+ api: i.UserApi;
12
+ flag: DefaultFlag;
13
+
14
+ constructor(api: i.UserApi, flag: DefaultFlag) {
15
+ this.api = api;
16
+ this.flag = flag;
17
+ }
18
+
19
+ async request<T>(param: RequestParam<i.UserResults, T>): Promise<UserApiUtilsResponse> {
20
+ const apiFn: typeof param.apiFn = param.apiFn.bind(this.api);
21
+ const response = await apiFn({
22
+ queryId: this.flag[param.key]['queryId'],
23
+ variables: JSON.stringify({ ...this.flag[param.key]['variables'], ...param }),
24
+ features: JSON.stringify(this.flag[param.key]['features']),
25
+ });
26
+ const user = param.convertFn(await response.value());
27
+ return {
28
+ raw: { response: response.raw },
29
+ header: buildHeader(response.raw.headers),
30
+ data: user.result,
31
+ };
32
+ }
33
+
34
+ async getUserByScreenName(param: getUserByScreenNameParam): Promise<UserApiUtilsResponse> {
35
+ const args = {
36
+ screenName: param.screenName,
37
+ ...param.extraParam,
38
+ };
39
+ const response = this.request({
40
+ key: 'UserByScreenName',
41
+ apiFn: this.api.getUserByScreenNameRaw,
42
+ convertFn: (e) => e.data.user,
43
+ param: args,
44
+ });
45
+ return response;
46
+ }
47
+ }