Skip to content

Commit edf581c

Browse files
authored
refactor: derive DM event field type from base event keys (#611)
1 parent 94ba02d commit edf581c

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

src/types/v2/dm.v2.types.ts

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,57 @@
11
import { TypeOrArrayOf } from '../shared.types';
2+
import { CashtagEntity, HashtagEntity, MentionEntity, UrlEntity } from '../entities.types';
23
import { TTweetv2MediaField, TTweetv2TweetField, TTweetv2UserField } from './tweet.v2.types';
34
import { ApiV2Includes, ReferencedTweetV2 } from './tweet.definition.v2';
45
import { DataMetaAndIncludeV2, PaginableCountMetaV2 } from './shared.v2.types';
56

6-
export type TDMEventV2Field = 'id' | 'text' | 'event_type' | 'created_at' | 'dm_conversation_id' | 'sender_id' | 'participant_ids' | 'referenced_tweets' | 'attachments';
7-
export type TDMEventV2Expansion = 'attachments.media_keys' | 'referenced_tweets.id' | 'sender_id' | 'participant_ids';
87
export type TDMEventV2EventType = 'MessageCreate' | 'ParticipantsJoin' | 'ParticipantsLeave';
98

9+
// Types
10+
11+
export interface DMEventAttachmentV2 {
12+
media_keys?: string[];
13+
card_ids?: string[];
14+
}
15+
16+
export interface BaseDMEventV2 {
17+
id: string;
18+
created_at?: string;
19+
sender_id?: string;
20+
dm_conversation_id?: string;
21+
attachments?: DMEventAttachmentV2;
22+
referenced_tweets?: ReferencedTweetV2[];
23+
participant_ids?: string[];
24+
entities?: {
25+
urls?: UrlEntity[];
26+
hashtags?: HashtagEntity[];
27+
cashtags?: CashtagEntity[];
28+
mentions?: MentionEntity[];
29+
};
30+
}
31+
32+
export type DMEventV2 =
33+
| ({
34+
event_type: 'MessageCreate';
35+
text: string;
36+
} & BaseDMEventV2)
37+
| ({
38+
event_type: Extract<TDMEventV2EventType, 'ParticipantsJoin' | 'ParticipantsLeave'>;
39+
} & BaseDMEventV2);
40+
41+
export type TDMEventV2Field = keyof BaseDMEventV2 | 'event_type' | 'text';
42+
export type TDMEventV2Expansion = 'attachments.media_keys' | 'referenced_tweets.id' | 'sender_id' | 'participant_ids';
43+
1044
// GET /2/dm_events
1145

1246
export interface GetDMEventV2Params {
13-
'dm_event.fields': TypeOrArrayOf<TDMEventV2Field> | string;
14-
event_types: TypeOrArrayOf<TDMEventV2EventType> | string;
15-
expansions: TypeOrArrayOf<TDMEventV2Expansion> | string;
16-
max_results: number;
17-
'media.fields': TypeOrArrayOf<TTweetv2MediaField> | string;
18-
pagination_token: string;
19-
'tweet.fields': TypeOrArrayOf<TTweetv2TweetField> | string;
20-
'user.fields': TypeOrArrayOf<TTweetv2UserField> | string;
47+
'dm_event.fields'?: TypeOrArrayOf<TDMEventV2Field> | string;
48+
event_types?: TypeOrArrayOf<TDMEventV2EventType> | string;
49+
expansions?: TypeOrArrayOf<TDMEventV2Expansion> | string;
50+
max_results?: number;
51+
'media.fields'?: TypeOrArrayOf<TTweetv2MediaField> | string;
52+
pagination_token?: string;
53+
'tweet.fields'?: TypeOrArrayOf<TTweetv2TweetField> | string;
54+
'user.fields'?: TypeOrArrayOf<TTweetv2UserField> | string;
2155
}
2256

2357
export type GetDMEventV2Result = DataMetaAndIncludeV2<DMEventV2[], PaginableCountMetaV2, ApiV2Includes>;
@@ -41,26 +75,3 @@ export interface PostDMInConversationResult {
4175
dm_conversation_id: string;
4276
dm_event_id: string;
4377
}
44-
45-
// Types
46-
47-
export interface BaseDMEventV2 {
48-
id: string;
49-
created_at?: string;
50-
sender_id?: string;
51-
dm_conversation_id?: string;
52-
attachments?: DMEventAttachmentV2;
53-
referenced_tweets?: ReferencedTweetV2[];
54-
participant_ids?: string[];
55-
}
56-
57-
export interface DMEventAttachmentV2 {
58-
media_keys: string[];
59-
}
60-
61-
export type DMEventV2 = ({
62-
event_type: 'MessageCreate',
63-
text: string;
64-
} & BaseDMEventV2) | ({
65-
event_type: Extract<TDMEventV2EventType, 'ParticipantsJoin' | 'ParticipantsLeave'>
66-
} & BaseDMEventV2);

0 commit comments

Comments
 (0)