SDK Fixes websocket subscription output typing (#19791)

Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>
This commit is contained in:
Brainslug
2023-09-25 13:24:06 +02:00
committed by GitHub
parent 8374a9a8b6
commit b33fbe2002
3 changed files with 13 additions and 5 deletions

View File

@@ -0,0 +1,5 @@
---
"@directus/sdk": patch
---
Fixed WebSocket subscription output typing in the SDK

View File

@@ -226,7 +226,7 @@ export function realtime(config: WebSocketConfig = {}) {
socket?.send(JSON.stringify(message));
},
async subscribe<Collection extends keyof Schema, Options extends SubscribeOptions<Schema, Collection>>(
async subscribe<Collection extends keyof Schema, const Options extends SubscribeOptions<Schema, Collection>>(
collection: Collection,
options = {} as Options
) {

View File

@@ -34,7 +34,7 @@ export interface WebSocketClient<Schema extends object> {
onWebSocket(event: 'message', callback: (this: WebSocket, ev: any) => any): RemoveEventHandler;
onWebSocket(event: WebSocketEvents, callback: WebSocketEventHandler): RemoveEventHandler;
sendMessage(message: string | Record<string, any>): void;
subscribe<Collection extends keyof Schema, Options extends SubscribeOptions<Schema, Collection>>(
subscribe<Collection extends keyof Schema, const Options extends SubscribeOptions<Schema, Collection>>(
collection: Collection,
options?: Options
): Promise<{
@@ -64,9 +64,12 @@ export type SubscriptionOutput<
TItem = TQuery extends Query<Schema, Schema[Collection]>
? ApplyQueryFields<Schema, CollectionType<Schema, Collection>, TQuery['fields']>
: Partial<Schema[Collection]>
> = { type: 'subscription' } & {
[Event in Events]: SubscriptionPayload<TItem>[Event];
}[Events];
> = { type: 'subscription'; uid?: string } & (
| {
[Event in Events]: { event: Event; data: SubscriptionPayload<TItem>[Event] };
}[Events]
| { event: 'error'; error: { code: string; message: string } }
);
export type SubscriptionPayload<Item> = {
init: Item[];