Skip to content

Commit dec45fe

Browse files
committed
refactor
Signed-off-by: ふぁ <yuki@yuki0311.com>
1 parent 88d6c28 commit dec45fe

File tree

11 files changed

+44
-44
lines changed

11 files changed

+44
-44
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as i from 'twitter-openapi-typescript-generated';
22
import { DefaultFlag } from '@/types';
3-
import { RequestParamDefault } from '@/models';
3+
import { RequestParam, buildHeader } from '@/models';
4+
import { TwitterApiUtilsResponse } from '@/types/response';
45

56
export type ProfileSpotlightsQueryParam = {
67
screenName: string;
@@ -16,26 +17,35 @@ export class DefaultApiUtils {
1617
this.flag = flag;
1718
}
1819

19-
async request<T>(param: RequestParamDefault<T>): Promise<T> {
20+
async request<T1, T2>(param: RequestParam<T1, T2>): Promise<TwitterApiUtilsResponse<T1>> {
2021
const apiFn: typeof param.apiFn = param.apiFn.bind(this.api);
2122
const response = await apiFn({
2223
queryId: this.flag[param.key]['queryId'],
2324
variables: JSON.stringify({ ...this.flag[param.key]['variables'], ...param }),
2425
features: JSON.stringify(this.flag[param.key]['features']),
2526
});
26-
return response.value() as T;
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+
};
2733
}
2834

29-
async gettProfileSpotlightsQuery(param: ProfileSpotlightsQueryParam): Promise<i.UserResultByScreenName> {
35+
async getProfileSpotlightsQuery(
36+
param: ProfileSpotlightsQueryParam,
37+
): Promise<TwitterApiUtilsResponse<i.UserResultByScreenName>> {
3038
const args = {
3139
screenName: param.screenName,
3240
...param.extraParam,
3341
};
34-
const response = this.request({
42+
const response = await this.request({
3543
key: 'ProfileSpotlightsQuery',
3644
apiFn: this.api.getProfileSpotlightsQueryRaw,
45+
convertFn: (value) => value.data.userResultByScreenName,
3746
param: args,
3847
});
39-
return (await response).data.userResultByScreenName;
48+
49+
return response;
4050
}
4151
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ export class TweetApiUtils {
7373

7474
async request<T>(param: RequestParam<i.InstructionUnion[], T>): Promise<TweetListApiUtilsResponse> {
7575
const apiFn: typeof param.apiFn = param.apiFn.bind(this.api);
76-
const response = await apiFn.bind(this.api)({
76+
const response = await apiFn({
7777
queryId: this.flag[param.key]['queryId'],
7878
variables: JSON.stringify({ ...this.flag[param.key]['variables'], ...param }),
7979
features: JSON.stringify(this.flag[param.key]['features']),
8080
});
81-
const instruction = param.convertFn((await response.value()) as T);
81+
const instruction = param.convertFn(await response.value());
8282
const entry = instructionToEntry(instruction);
8383
const data = tweetEntriesConverter(entry);
8484
const raw: ApiUtilsRaw = {

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@ export class UserApiUtils {
1818

1919
async request<T>(param: RequestParam<i.UserResults, T>): Promise<UserApiUtilsResponse> {
2020
const apiFn: typeof param.apiFn = param.apiFn.bind(this.api);
21-
const response = await apiFn.bind(this.api)({
21+
const response = await apiFn({
2222
queryId: this.flag[param.key]['queryId'],
2323
variables: JSON.stringify({ ...this.flag[param.key]['variables'], ...param }),
2424
features: JSON.stringify(this.flag[param.key]['features']),
2525
});
26-
const user = param.convertFn((await response.value()) as T);
27-
const raw: UserApiUtilsRaw = {
28-
response: response.raw,
29-
};
26+
const user = param.convertFn(await response.value());
3027
return {
31-
raw: raw,
28+
raw: { response: response.raw },
3229
header: buildHeader(response.raw.headers),
3330
data: user.result,
3431
};

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ export class UserListApiUtils {
3434

3535
async request<T>(param: RequestParam<i.InstructionUnion[], T>): Promise<UserListApiUtilsResponse> {
3636
const apiFn: typeof param.apiFn = param.apiFn.bind(this.api);
37-
const response = await apiFn.bind(this.api)({
37+
const response = await apiFn({
3838
queryId: this.flag[param.key]['queryId'],
3939
variables: JSON.stringify({ ...this.flag[param.key]['variables'], ...param }),
4040
features: JSON.stringify(this.flag[param.key]['features']),
4141
});
42-
const instruction = param.convertFn((await response.value()) as T);
42+
const instruction = param.convertFn(await response.value());
4343
const entry = instructionToEntry(instruction);
4444
const userList = userEntriesConverter(entry);
4545
const data = userList.map((user) => buildUserResponse(user));

twitter-openapi-typescript/src/models/type.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ export type ApiFunction<T> = (requestParameters: {
66
features: string;
77
}) => Promise<i.ApiResponse<T>>;
88

9-
export type RequestParamDefault<T> = {
10-
apiFn: ApiFunction<T>;
11-
key: string;
12-
param: { [key: string]: any };
13-
};
14-
159
export type RequestParam<T1, T2> = {
1610
apiFn: ApiFunction<T2>;
1711
convertFn: ConvertFunction<T1, T2>;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './timeline';
44
export * from './tweet';
55
export * from './user_list';
66
export * from './user';
7+
export * from './response';
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { getClient, logger } from '@test/init';
22
import { printTweet } from '@test/util';
33

4-
test('gettProfileSpotlightsQuery', async () => {
5-
logger.log('gettProfileSpotlightsQuery');
4+
test('getProfileSpotlightsQuery', async () => {
5+
logger.log('getProfileSpotlightsQuery');
66
const client = await getClient();
7-
const response = await client.getDefaultApi().gettProfileSpotlightsQuery({ screenName: 'elonmusk' });
8-
const legacy = response.result.legacy;
7+
const response = await client.getDefaultApi().getProfileSpotlightsQuery({ screenName: 'elonmusk' });
8+
const legacy = response.data.result.legacy;
99
logger.log(legacy.screenName);
1010
logger.log(`followedBy: ${legacy.followedBy} following: ${legacy.following}`);
1111
logger.log('┄'.repeat(50));
12-
expect(0).toBe(0);
12+
expect(response.raw.response.ok).toBe(true);
1313
});

twitter-openapi-typescript/test/api/tweetApi.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,69 @@ test('getTweetDetail', async () => {
66
const client = await getClient();
77
const response = await client.getTweetApi().getTweetDetail({ focalTweetId: '1349129669258448897' });
88
response.data.filter((e) => !e.promotedMetadata).forEach((e) => printTweet(e));
9-
expect(0).toBe(0);
9+
expect(response.raw.response.ok).toBe(true);
1010
});
1111

1212
test('getHomeTimeline', async () => {
1313
logger.log('getHomeTimeline');
1414
const client = await getClient();
1515
const response = await client.getTweetApi().getHomeTimeline();
1616
response.data.filter((e) => !e.promotedMetadata).forEach((e) => printTweet(e));
17-
expect(0).toBe(0);
17+
expect(response.raw.response.ok).toBe(true);
1818
});
1919

2020
test('getHomeLatestTimeline', async () => {
2121
logger.log('getHomeLatestTimeline');
2222
const client = await getClient();
2323
const response = await client.getTweetApi().getHomeLatestTimeline();
2424
response.data.filter((e) => !e.promotedMetadata).forEach((e) => printTweet(e));
25-
expect(0).toBe(0);
25+
expect(response.raw.response.ok).toBe(true);
2626
});
2727

2828
test('getListLatestTweetsTimeline', async () => {
2929
logger.log('getListLatestTweetsTimeline');
3030
const client = await getClient();
3131
const response = await client.getTweetApi().getListLatestTweetsTimeline({ listId: '1141162794290520064' });
3232
response.data.filter((e) => !e.promotedMetadata).forEach((e) => printTweet(e));
33-
expect(0).toBe(0);
33+
expect(response.raw.response.ok).toBe(true);
3434
});
3535

3636
test('getUserTweets', async () => {
3737
logger.log('getUserTweets');
3838
const client = await getClient();
3939
const response = await client.getTweetApi().getUserTweets({ userId: '44196397' });
4040
response.data.filter((e) => !e.promotedMetadata).forEach((e) => printTweet(e));
41-
expect(0).toBe(0);
41+
expect(response.raw.response.ok).toBe(true);
4242
});
4343

4444
test('getUserTweetsAndReplies', async () => {
4545
logger.log('getUserTweetsAndReplies');
4646
const client = await getClient();
4747
const response = await client.getTweetApi().getUserTweetsAndReplies({ userId: '44196397' });
4848
response.data.filter((e) => !e.promotedMetadata).forEach((e) => printTweet(e));
49-
expect(0).toBe(0);
49+
expect(response.raw.response.ok).toBe(true);
5050
});
5151

5252
test('getUserMedia', async () => {
5353
logger.log('getUserMedia');
5454
const client = await getClient();
5555
const response = await client.getTweetApi().getUserMedia({ userId: '44196397' });
5656
response.data.filter((e) => !e.promotedMetadata).forEach((e) => printTweet(e));
57-
expect(0).toBe(0);
57+
expect(response.raw.response.ok).toBe(true);
5858
});
5959

6060
test('getLikes', async () => {
6161
logger.log('getLikes');
6262
const client = await getClient();
6363
const response = await client.getTweetApi().getLikes({ userId: '44196397' });
6464
response.data.filter((e) => !e.promotedMetadata).forEach((e) => printTweet(e));
65-
expect(0).toBe(0);
65+
expect(response.raw.response.ok).toBe(true);
6666
});
6767

6868
test('getBookmarks', async () => {
6969
logger.log('getBookmarks');
7070
const client = await getClient();
7171
const response = await client.getTweetApi().getBookmarks();
7272
response.data.filter((e) => !e.promotedMetadata).forEach((e) => printTweet(e));
73-
expect(0).toBe(0);
73+
expect(response.raw.response.ok).toBe(true);
7474
});

twitter-openapi-typescript/test/api/userApi.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ test('getUserByScreenName', async () => {
99
logger.log(`followedBy: ${legacy.followedBy} following: ${legacy.following}`);
1010
logger.log(`friendsCount: ${legacy.friendsCount} followersCount: ${legacy.followersCount}`);
1111
logger.log('┄'.repeat(50));
12-
expect(0).toBe(0);
12+
expect(response.raw.response.ok).toBe(true);
1313
});

twitter-openapi-typescript/test/api/userListApi.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ test('getFollowers', async () => {
66
const client = await getClient();
77
const response = await client.getUserListApi().getFollowers({ userId: '44196397' });
88
response.data.forEach((e) => printUser(e));
9-
expect(0).toBe(0);
9+
expect(response.raw.response.ok).toBe(true);
1010
});
1111

1212
test('getFollowing', async () => {
1313
logger.log('getFollowing');
1414
const client = await getClient();
1515
const response = await client.getUserListApi().getFollowing({ userId: '44196397' });
1616
response.data.forEach((e) => printUser(e));
17-
expect(0).toBe(0);
17+
expect(response.raw.response.ok).toBe(true);
1818
});

0 commit comments

Comments
 (0)