Skip to content

Commit 6c98542

Browse files
committed
feat: Support declaration merging in streaming APIs
1 parent ee44b97 commit 6c98542

File tree

5 files changed

+93
-68
lines changed

5 files changed

+93
-68
lines changed

src/adapters/ws/web-socket-subscription.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ import { type mastodon } from "../../mastodon/index.js";
1010
import { MastoUnexpectedError } from "../errors/index.js";
1111
import { toAsyncIterable } from "./async-iterable.js";
1212

13+
interface RawEventOk {
14+
stream: string[];
15+
event: string;
16+
payload?: string;
17+
}
18+
19+
interface RawEventError {
20+
error: string;
21+
}
22+
23+
type RawEvent = RawEventOk | RawEventError;
24+
1325
export class WebSocketSubscription implements mastodon.streaming.Subscription {
1426
private connection?: WebSocket;
1527

@@ -94,10 +106,7 @@ export class WebSocketSubscription implements mastodon.streaming.Subscription {
94106
}
95107

96108
private parseMessage(rawEvent: string): mastodon.streaming.Event {
97-
const data = this.serializer.deserialize<mastodon.streaming.RawEvent>(
98-
"json",
99-
rawEvent,
100-
);
109+
const data = this.serializer.deserialize<RawEvent>("json", rawEvent);
101110

102111
if ("error" in data) {
103112
throw new MastoUnexpectedError(data.error);

src/mastodon/streaming/client.ts

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Event } from "./event.js";
1+
import { type Subscription } from "./subscription.js";
22

33
export interface SubscribeListParams {
44
readonly list: string;
@@ -8,48 +8,67 @@ export interface SubscribeHashtagParams {
88
readonly tag: string;
99
}
1010

11-
export interface Subscription extends AsyncIterable<Event>, Disposable {
12-
values(): AsyncIterableIterator<Event>;
13-
unsubscribe(): void;
11+
export interface PublicMediaResource {
12+
subscribe(): Subscription;
13+
}
14+
15+
export interface PublicLocalMediaResource {
16+
subscribe(): Subscription;
17+
}
18+
19+
export interface PublicLocalResource {
20+
media: PublicLocalMediaResource;
21+
subscribe(): Subscription;
22+
}
23+
24+
export interface PublicRemoteMediaResource {
25+
subscribe(): Subscription;
26+
}
27+
28+
export interface PublicRemoteResource {
29+
media: PublicRemoteMediaResource;
30+
subscribe(): Subscription;
31+
}
32+
33+
export interface PublicResource {
34+
media: PublicMediaResource;
35+
local: PublicLocalResource;
36+
remote: PublicRemoteResource;
37+
subscribe(): Subscription;
38+
}
39+
40+
export interface HashtagLocalResource {
41+
subscribe(params: SubscribeHashtagParams): Subscription;
42+
}
43+
44+
export interface HashtagResource {
45+
local: HashtagLocalResource;
46+
subscribe(params: SubscribeHashtagParams): Subscription;
47+
}
48+
49+
export interface ListResource {
50+
subscribe(params: SubscribeListParams): Subscription;
51+
}
52+
53+
export interface DirectResource {
54+
subscribe(): Subscription;
55+
}
56+
57+
export interface UserNotificationResource {
58+
subscribe(): Subscription;
59+
}
60+
61+
export interface UserResource {
62+
notification: UserNotificationResource;
63+
subscribe(): Subscription;
1464
}
1565

1666
export interface Client extends Disposable {
17-
public: {
18-
subscribe(): Subscription;
19-
media: {
20-
subscribe(): Subscription;
21-
};
22-
local: {
23-
subscribe(): Subscription;
24-
media: {
25-
subscribe(): Subscription;
26-
};
27-
};
28-
remote: {
29-
subscribe(): Subscription;
30-
media: {
31-
subscribe(): Subscription;
32-
};
33-
};
34-
};
35-
hashtag: {
36-
subscribe(params: SubscribeHashtagParams): Subscription;
37-
local: {
38-
subscribe(params: SubscribeHashtagParams): Subscription;
39-
};
40-
};
41-
list: {
42-
subscribe(params: SubscribeListParams): Subscription;
43-
};
44-
direct: {
45-
subscribe(): Subscription;
46-
};
47-
user: {
48-
subscribe(): Subscription;
49-
notification: {
50-
subscribe(): Subscription;
51-
};
52-
};
67+
public: PublicResource;
68+
hashtag: HashtagResource;
69+
list: ListResource;
70+
direct: DirectResource;
71+
user: UserNotificationResource;
5372

5473
close(): void;
5574

src/mastodon/streaming/event.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,6 @@ import {
55
type Reaction,
66
type Status,
77
} from "../entities/v1/index.js";
8-
9-
export interface RawEventOk {
10-
stream: string[];
11-
event: string;
12-
payload?: string;
13-
}
14-
15-
export interface RawEventError {
16-
error: string;
17-
}
18-
19-
export type RawEvent = RawEventOk | RawEventError;
20-
218
interface BaseEvent<T, U> {
229
stream: string[];
2310
event: T;
@@ -51,14 +38,17 @@ export type NotificationsMergedEvent = BaseEvent<
5138
undefined
5239
>;
5340

54-
export type Event =
55-
| UpdateEvent
56-
| DeleteEvent
57-
| NotificationEvent
58-
| FiltersChangedEvent
59-
| ConversationEvent
60-
| AnnouncementEvent
61-
| AnnouncementReactionEvent
62-
| AnnouncementDeleteEvent
63-
| StatusUpdateEvent
64-
| NotificationsMergedEvent;
41+
export interface EventRegistry {
42+
update: UpdateEvent;
43+
delete: DeleteEvent;
44+
notification: NotificationEvent;
45+
filters_changed: FiltersChangedEvent;
46+
conversation: ConversationEvent;
47+
announcement: AnnouncementEvent;
48+
"announcement.reaction": AnnouncementReactionEvent;
49+
"announcement.delete": AnnouncementDeleteEvent;
50+
"status.update": StatusUpdateEvent;
51+
notifications_merged: NotificationsMergedEvent;
52+
}
53+
54+
export type Event = EventRegistry[keyof EventRegistry];

src/mastodon/streaming/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export type * from "./client.js";
22
export type * from "./event.js";
3+
export type * from "./subscription.js";
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { type Event } from "./event.js";
2+
3+
export interface Subscription extends AsyncIterable<Event>, Disposable {
4+
values(): AsyncIterableIterator<Event>;
5+
unsubscribe(): void;
6+
}

0 commit comments

Comments
 (0)