diff --git a/packages/pl-api/lib/client-base.ts b/packages/pl-api/lib/client-base.ts index 983ed59d3..f588c9473 100644 --- a/packages/pl-api/lib/client-base.ts +++ b/packages/pl-api/lib/client-base.ts @@ -6,6 +6,8 @@ import { type Features, getFeatures } from './features'; import request, { getNextLink, getPrevLink, type RequestBody } from './request'; import type { Instance } from './entities/instance'; +import type { StreamingEvent } from './entities/streaming-event'; +import type { StreamingParams } from './params/streaming'; import type { Response as PlApiResponse } from './request'; import type { PaginatedResponse } from './responses'; @@ -31,10 +33,10 @@ class PlApiBaseClient { public features: Features = getFeatures(this.#instance); /** @internal */ socket?: { - listen: (listener: any, stream?: string) => number; - unlisten: (listener: any) => void; - subscribe: (stream: string, params?: { list?: string; tag?: string }) => void; - unsubscribe: (stream: string, params?: { list?: string; tag?: string }) => void; + listen: (listener: (event: StreamingEvent) => void, stream?: string) => number; + unlisten: (listener: (event: StreamingEvent) => void) => void; + subscribe: (stream: string, params?: StreamingParams) => void; + unsubscribe: (stream: string, params?: StreamingParams) => void; close: () => void; }; /** @internal */ diff --git a/packages/pl-api/lib/client/streaming.ts b/packages/pl-api/lib/client/streaming.ts index 458240e4e..a35451fc5 100644 --- a/packages/pl-api/lib/client/streaming.ts +++ b/packages/pl-api/lib/client/streaming.ts @@ -55,13 +55,12 @@ const streaming = (client: PlApiBaseClient) => ({ }; client.socket = { - listen: (listener: (event: StreamingEvent) => any, stream?: string) => - listeners.push({ listener, stream }), - unlisten: (listener: (event: StreamingEvent) => any) => + listen: (listener, stream) => listeners.push({ listener, stream }), + unlisten: (listener) => (listeners = listeners.filter((value) => value.listener !== listener)), - subscribe: (stream: string, { list, tag }: { list?: string; tag?: string } = {}) => + subscribe: (stream, { list, tag } = {}) => enqueue(() => ws.send(JSON.stringify({ type: 'subscribe', stream, list, tag }))), - unsubscribe: (stream: string, { list, tag }: { list?: string; tag?: string } = {}) => + unsubscribe: (stream, { list, tag } = {}) => enqueue(() => ws.send(JSON.stringify({ type: 'unsubscribe', stream, list, tag }))), close: () => { ws.close(); diff --git a/packages/pl-api/lib/params/index.ts b/packages/pl-api/lib/params/index.ts index 8516faf41..7bb9e34bc 100644 --- a/packages/pl-api/lib/params/index.ts +++ b/packages/pl-api/lib/params/index.ts @@ -22,5 +22,6 @@ export * from './scheduled-statuses'; export * from './search'; export * from './settings'; export * from './statuses'; +export * from './streaming'; export * from './timelines'; export * from './trends'; diff --git a/packages/pl-api/lib/params/streaming.ts b/packages/pl-api/lib/params/streaming.ts new file mode 100644 index 000000000..3e272b486 --- /dev/null +++ b/packages/pl-api/lib/params/streaming.ts @@ -0,0 +1,14 @@ +/** + * @category Request params + */ +interface StreamingParams { + /** When stream is set to `list`, use this parameter to specify the list ID. */ + list?: string; + /** When stream is set to `hashtag` or `hashtag:local`, use this parameter to specify the tag name. */ + tag?: string; + group?: string; + /** Domain name of the instance. Required when `stream` is `public:remote` or `public:remote:media`. */ + instance?: string; +} + +export { type StreamingParams };