Skip to content

Commit b46aed4

Browse files
authored
Merge pull request #46 from fa0311/dev
v0.0.13
2 parents 456e430 + 180adf6 commit b46aed4

23 files changed

+864
-5015
lines changed

twitter-openapi-typescript/package-lock.json

Lines changed: 554 additions & 4756 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

twitter-openapi-typescript/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "twitter-openapi-typescript",
3-
"version": "0.0.12",
3+
"version": "0.0.13",
44
"description": "Implementation of Twitter internal API in TypeScript",
55
"scripts": {
66
"test": "jest",
@@ -22,10 +22,10 @@
2222
"main": "./dist/src/index.js",
2323
"typings": "./dist/src/index.d.ts",
2424
"dependencies": {
25-
"twitter-openapi-typescript-generated": "^0.0.8"
25+
"twitter-openapi-typescript-generated": "^0.0.9"
2626
},
2727
"devDependencies": {
28-
"@types/jest": "^28.0.0",
28+
"@types/jest": "^28.1.8",
2929
"@types/node": "^20.2.0",
3030
"@types/node-fetch": "^2.0.0",
3131
"@typescript-eslint/eslint-plugin": "^5.53.0",

twitter-openapi-typescript/src/api.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
V20GetApiUtils,
1212
} from '@/apis';
1313
import { DefaultFlag } from '@/models';
14+
import { UsersApiUtils } from './apis/usersApi';
1415

1516
export type TwitterOpenApiParams = {
1617
lang?: string;
@@ -27,7 +28,7 @@ export type TwitterOpenApiCookie = {
2728
};
2829

2930
export class TwitterOpenApi {
30-
static hash = '29e05d162f600933fdbf633e992d2d0a249c9413';
31+
static hash = '2d477a0fb84d249a30b5af535b467efc25b34923';
3132
static url = `https://raw.githubusercontent.com/fa0311/twitter-openapi/${this.hash}/src/config/placeholder.json`;
3233
static twitter = 'https://twitter.com/home';
3334

@@ -167,6 +168,10 @@ export class TwitterOpenApiClient {
167168
return new UserApiUtils(new i.UserApi(this.config), this.flag);
168169
}
169170

171+
getUsersApi(): UsersApiUtils {
172+
return new UsersApiUtils(new i.UsersApi(this.config), this.flag);
173+
}
174+
170175
getUserListApi(): UserListApiUtils {
171176
return new UserListApiUtils(new i.UserListApi(this.config), this.flag);
172177
}

twitter-openapi-typescript/src/apis/defaultApi.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as i from 'twitter-openapi-typescript-generated';
2-
import { buildHeader } from '@/utils';
2+
import { buildHeader, errorCheck, getKwargs } from '@/utils';
33
import { RequestParam, DefaultFlag, TwitterApiUtilsResponse } from '@/models';
44

55
export type ProfileSpotlightsQueryParam = {
@@ -18,18 +18,10 @@ export class DefaultApiUtils {
1818

1919
async request<T1, T2>(param: RequestParam<T1, T2>): Promise<TwitterApiUtilsResponse<T1>> {
2020
const apiFn: typeof param.apiFn = param.apiFn.bind(this.api);
21-
const fieldTogglesFn = () => {
22-
if (this.flag[param.key]['fieldToggles'] == null) return { fieldToggles: '' };
23-
return { fieldToggles: JSON.stringify(this.flag[param.key]['fieldToggles']) };
24-
};
25-
const response = await apiFn({
26-
pathQueryId: this.flag[param.key]['queryId'],
27-
queryId: this.flag[param.key]['queryId'],
28-
variables: JSON.stringify({ ...this.flag[param.key]['variables'], ...param.param }),
29-
features: JSON.stringify(this.flag[param.key]['features']),
30-
...fieldTogglesFn(),
31-
});
32-
const data = param.convertFn(await response.value());
21+
const args = getKwargs(this.flag[param.key], param.param);
22+
const response = await apiFn(args);
23+
const checked = errorCheck(await response.value());
24+
const data = param.convertFn(checked);
3325
return {
3426
raw: { response: response.raw },
3527
header: buildHeader(response.raw.headers),

twitter-openapi-typescript/src/apis/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export { DefaultApiUtils } from './defaultApi';
22
export { TweetApiUtils } from './tweetApi';
33
export { PostApiUtils } from './postApi';
44
export { UserApiUtils } from './userApi';
5+
export { UsersApiUtils } from './usersApi';
56
export { UserListApiUtils } from './userListApi';
67
export { V11GetApiUtils } from './v11GetApi';
78
export { V11PostApiUtils } from './v11PostApi';

twitter-openapi-typescript/src/apis/postApi.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as i from 'twitter-openapi-typescript-generated';
22
import { DefaultFlag } from '@/models';
3-
import { buildHeader } from '@/utils';
3+
import { buildHeader, errorCheck } from '@/utils';
44
import { TwitterApiUtilsResponse } from '@/models';
5+
import { error } from 'console';
56

67
type PostCreateTweetParam = {
78
tweetText: string;
@@ -58,10 +59,11 @@ export class PostApiUtils {
5859
variables: { ...variables, ...args },
5960
},
6061
});
62+
6163
return {
6264
raw: { response: response.raw },
6365
header: buildHeader(response.raw.headers),
64-
data: await response.value(),
66+
data: errorCheck(await response.value()),
6567
};
6668
}
6769

@@ -82,7 +84,7 @@ export class PostApiUtils {
8284
return {
8385
raw: { response: response.raw },
8486
header: buildHeader(response.raw.headers),
85-
data: await response.value(),
87+
data: errorCheck(await response.value()),
8688
};
8789
}
8890

@@ -103,7 +105,7 @@ export class PostApiUtils {
103105
return {
104106
raw: { response: response.raw },
105107
header: buildHeader(response.raw.headers),
106-
data: await response.value(),
108+
data: errorCheck(await response.value()),
107109
};
108110
}
109111
async postDeleteRetweet(param: PostDeleteRetweetParam): Promise<TwitterApiUtilsResponse<i.DeleteRetweetResponse>> {
@@ -123,7 +125,7 @@ export class PostApiUtils {
123125
return {
124126
raw: { response: response.raw },
125127
header: buildHeader(response.raw.headers),
126-
data: await response.value(),
128+
data: errorCheck(await response.value()),
127129
};
128130
}
129131

@@ -146,7 +148,7 @@ export class PostApiUtils {
146148
return {
147149
raw: { response: response.raw },
148150
header: buildHeader(response.raw.headers),
149-
data: await response.value(),
151+
data: errorCheck(await response.value()),
150152
};
151153
}
152154

@@ -169,7 +171,7 @@ export class PostApiUtils {
169171
return {
170172
raw: { response: response.raw },
171173
header: buildHeader(response.raw.headers),
172-
data: await response.value(),
174+
data: errorCheck(await response.value()),
173175
};
174176
}
175177
}

twitter-openapi-typescript/src/apis/tweetApi.ts

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import * as i from 'twitter-openapi-typescript-generated';
2-
import { DefaultFlag, TweetListApiUtilsResponse, ApiUtilsRaw, RequestParam } from '@/models';
3-
import { buildHeader, entriesCursor, instructionToEntry, tweetEntriesConverter } from '@/utils';
2+
import {
3+
DefaultFlag,
4+
RequestParam,
5+
TwitterApiUtilsResponse,
6+
TweetApiUtilsData,
7+
TimelineApiUtilsResponse,
8+
} from '@/models';
9+
import { buildHeader, entriesCursor, errorCheck, getKwargs, instructionToEntry, tweetEntriesConverter } from '@/utils';
410

511
type GetTweetDetailParam = {
612
focalTweetId: string;
@@ -69,6 +75,8 @@ type GetBookmarksParam = {
6975
extraParam?: { [key: string]: any };
7076
};
7177

78+
type ResponseType = TwitterApiUtilsResponse<TimelineApiUtilsResponse<TweetApiUtilsData>>;
79+
7280
export class TweetApiUtils {
7381
api: i.TweetApi;
7482
flag: DefaultFlag;
@@ -78,36 +86,31 @@ export class TweetApiUtils {
7886
this.flag = flag;
7987
}
8088

81-
async request<T>(param: RequestParam<i.InstructionUnion[], T>): Promise<TweetListApiUtilsResponse> {
89+
async request<T>(param: RequestParam<i.InstructionUnion[], T>): Promise<ResponseType> {
8290
const apiFn: typeof param.apiFn = param.apiFn.bind(this.api);
83-
const fieldTogglesFn = () => {
84-
if (this.flag[param.key]['fieldToggles'] == null) return { fieldToggles: '' };
85-
return { fieldToggles: JSON.stringify(this.flag[param.key]['fieldToggles']) };
86-
};
87-
const response = await apiFn({
88-
pathQueryId: this.flag[param.key]['queryId'],
89-
queryId: this.flag[param.key]['queryId'],
90-
variables: JSON.stringify({ ...this.flag[param.key]['variables'], ...param.param }),
91-
features: JSON.stringify(this.flag[param.key]['features']),
92-
...fieldTogglesFn(),
93-
});
94-
const instruction = param.convertFn(await response.value());
91+
const args = getKwargs(this.flag[param.key], param.param);
92+
const response = await apiFn(args);
93+
const checked = errorCheck(await response.value());
94+
const instruction = param.convertFn(checked);
9595
const entry = instructionToEntry(instruction);
9696
const data = tweetEntriesConverter(entry);
97-
const raw: ApiUtilsRaw = {
98-
response: response.raw,
99-
instruction: instruction,
100-
entry: entry,
101-
};
10297
return {
103-
raw: raw,
98+
raw: {
99+
response: response.raw,
100+
},
104101
header: buildHeader(response.raw.headers),
105-
cursor: entriesCursor(entry),
106-
data: data,
102+
data: {
103+
raw: {
104+
instruction,
105+
entry,
106+
},
107+
cursor: entriesCursor(entry),
108+
data: data,
109+
},
107110
};
108111
}
109112

110-
async getTweetDetail(param: GetTweetDetailParam): Promise<TweetListApiUtilsResponse> {
113+
async getTweetDetail(param: GetTweetDetailParam): Promise<ResponseType> {
111114
const args = {
112115
focalTweetId: param.focalTweetId,
113116
...(param.cursor == null ? {} : { cursor: param.cursor }),
@@ -123,7 +126,7 @@ export class TweetApiUtils {
123126
});
124127
return response;
125128
}
126-
async getSearchTimeline(param: GetSearchTimelineParam): Promise<TweetListApiUtilsResponse> {
129+
async getSearchTimeline(param: GetSearchTimelineParam): Promise<ResponseType> {
127130
const args = {
128131
rawQuery: param.rawQuery,
129132
...(param.product == null ? {} : { product: param.product }),
@@ -141,7 +144,7 @@ export class TweetApiUtils {
141144
return response;
142145
}
143146

144-
async getHomeTimeline(param: GetHomeTimelineParam = {}): Promise<TweetListApiUtilsResponse> {
147+
async getHomeTimeline(param: GetHomeTimelineParam = {}): Promise<ResponseType> {
145148
const args = {
146149
...(param.count == null ? {} : { count: param.count }),
147150
...(param.couser == null ? {} : { couser: param.couser }),
@@ -157,7 +160,7 @@ export class TweetApiUtils {
157160
return response;
158161
}
159162

160-
async getHomeLatestTimeline(param: GetHomeLatestTimelineParam = {}): Promise<TweetListApiUtilsResponse> {
163+
async getHomeLatestTimeline(param: GetHomeLatestTimelineParam = {}): Promise<ResponseType> {
161164
const args = {
162165
...(param.count == null ? {} : { count: param.count }),
163166
...(param.cursor == null ? {} : { cursor: param.cursor }),
@@ -173,7 +176,7 @@ export class TweetApiUtils {
173176
return response;
174177
}
175178

176-
async getListLatestTweetsTimeline(param: GetListLatestTweetsTimelineParam): Promise<TweetListApiUtilsResponse> {
179+
async getListLatestTweetsTimeline(param: GetListLatestTweetsTimelineParam): Promise<ResponseType> {
177180
const args = {
178181
listId: param.listId,
179182
...(param.count == null ? {} : { count: param.count }),
@@ -190,7 +193,7 @@ export class TweetApiUtils {
190193
return response;
191194
}
192195

193-
async getUserTweets(param: GetUserTweetsParam): Promise<TweetListApiUtilsResponse> {
196+
async getUserTweets(param: GetUserTweetsParam): Promise<ResponseType> {
194197
const args = {
195198
userId: param.userId,
196199
...(param.count == null ? {} : { count: param.count }),
@@ -207,7 +210,7 @@ export class TweetApiUtils {
207210
return response;
208211
}
209212

210-
async getUserTweetsAndReplies(param: GetUserTweetsAndRepliesParam): Promise<TweetListApiUtilsResponse> {
213+
async getUserTweetsAndReplies(param: GetUserTweetsAndRepliesParam): Promise<ResponseType> {
211214
const args = {
212215
userId: param.userId,
213216
...(param.count == null ? {} : { count: param.count }),
@@ -224,7 +227,7 @@ export class TweetApiUtils {
224227
return response;
225228
}
226229

227-
async getUserMedia(param: GetUserMediaParam): Promise<TweetListApiUtilsResponse> {
230+
async getUserMedia(param: GetUserMediaParam): Promise<ResponseType> {
228231
const args = {
229232
userId: param.userId,
230233
...(param.count == null ? {} : { count: param.count }),
@@ -240,7 +243,7 @@ export class TweetApiUtils {
240243
});
241244
return response;
242245
}
243-
async getLikes(param: GetLikesParam): Promise<TweetListApiUtilsResponse> {
246+
async getLikes(param: GetLikesParam): Promise<ResponseType> {
244247
const args = {
245248
userId: param.userId,
246249
...(param.count == null ? {} : { count: param.count }),
@@ -256,7 +259,7 @@ export class TweetApiUtils {
256259
});
257260
return response;
258261
}
259-
async getBookmarks(param: GetBookmarksParam = {}): Promise<TweetListApiUtilsResponse> {
262+
async getBookmarks(param: GetBookmarksParam = {}): Promise<ResponseType> {
260263
const args = {
261264
...(param.count == null ? {} : { count: param.count }),
262265
...(param.cursor == null ? {} : { cursor: param.cursor }),

0 commit comments

Comments
 (0)