@fabriktor/client 0.0.31 → 0.0.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/chat/chat.d.ts +7 -13
- package/dist/src/chat/chat.js +1 -44
- package/dist/src/core/ws.d.ts +16 -0
- package/dist/src/core/ws.js +51 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/package.json +1 -1
package/dist/src/chat/chat.d.ts
CHANGED
|
@@ -2,15 +2,9 @@ import { type Chat, type ChatRoom, type ChatRoomMember, type ID, type InChat, ty
|
|
|
2
2
|
import type { TokenProvider } from "../core/auth.js";
|
|
3
3
|
import type { DeleteManyOptionsCRUD, DeleteOneOptionsCRUD, FindOneOptionsCRUD, InsertOneOptionsCRUD, OperationResultOf, QueryOptionsCRUD, ReplaceOneOptionsCRUD, SearchManyOptionsCRUD, SearchResultOf } from "../core/crud.js";
|
|
4
4
|
import type { HTTPDoer } from "../core/http.js";
|
|
5
|
+
import { type WSCloseHandler, type WSConnection, type WSErrorHandler, type WSMessageHandler } from "../core/ws.js";
|
|
5
6
|
export type ReactChatOptions = PLChatReact;
|
|
6
7
|
export type EnsureChatRoomOptions = PLChatEnsure;
|
|
7
|
-
export type WSMessageHandler<T> = (v: T, ev: MessageEvent<string>) => void;
|
|
8
|
-
export type WSErrorHandler = (ev: Event) => void;
|
|
9
|
-
export type WSCloseHandler = (ev: CloseEvent) => void;
|
|
10
|
-
export type WSConnection<T> = {
|
|
11
|
-
socket: WebSocket;
|
|
12
|
-
close(code?: number, reason?: string): void;
|
|
13
|
-
};
|
|
14
8
|
export type WSChatsOptions = {
|
|
15
9
|
owner_id: string;
|
|
16
10
|
chat_room_id: string;
|
|
@@ -57,9 +51,9 @@ export interface ChatOperator {
|
|
|
57
51
|
searchManyChatRoomMembers(opts?: SearchManyOptionsCRUD): Promise<OperationResultOf<SearchResultOf<ChatRoomMember>>>;
|
|
58
52
|
reactChat(opts: ReactChatOptions): Promise<void>;
|
|
59
53
|
ensureChatRoom(opts: EnsureChatRoomOptions): Promise<ID>;
|
|
60
|
-
wsChats(opts: WSChatsOptions): Promise<WSConnection
|
|
61
|
-
wsChatRooms(opts: WSChatRoomsOptions): Promise<WSConnection
|
|
62
|
-
wsChatRoom(opts: WSChatRoomOptions): Promise<WSConnection
|
|
54
|
+
wsChats(opts: WSChatsOptions): Promise<WSConnection>;
|
|
55
|
+
wsChatRooms(opts: WSChatRoomsOptions): Promise<WSConnection>;
|
|
56
|
+
wsChatRoom(opts: WSChatRoomOptions): Promise<WSConnection>;
|
|
63
57
|
}
|
|
64
58
|
export declare class ChatClient implements ChatOperator {
|
|
65
59
|
private chats;
|
|
@@ -90,8 +84,8 @@ export declare class ChatClient implements ChatOperator {
|
|
|
90
84
|
searchManyChatRoomMembers(opts?: SearchManyOptionsCRUD): Promise<OperationResultOf<SearchResultOf<ChatRoomMember>>>;
|
|
91
85
|
reactChat(opts: ReactChatOptions): Promise<void>;
|
|
92
86
|
ensureChatRoom(opts: EnsureChatRoomOptions): Promise<ID>;
|
|
93
|
-
wsChats(opts: WSChatsOptions): Promise<WSConnection
|
|
94
|
-
wsChatRooms(opts: WSChatRoomsOptions): Promise<WSConnection
|
|
95
|
-
wsChatRoom(opts: WSChatRoomOptions): Promise<WSConnection
|
|
87
|
+
wsChats(opts: WSChatsOptions): Promise<WSConnection>;
|
|
88
|
+
wsChatRooms(opts: WSChatRoomsOptions): Promise<WSConnection>;
|
|
89
|
+
wsChatRoom(opts: WSChatRoomOptions): Promise<WSConnection>;
|
|
96
90
|
}
|
|
97
91
|
export declare function newChatClient(opts: ChatClientOptions): ChatClient;
|
package/dist/src/chat/chat.js
CHANGED
|
@@ -8,11 +8,7 @@ import { requiredToken } from "../core/auth.js";
|
|
|
8
8
|
import { queryParam, queryParams, withQuery } from "../core/col.js";
|
|
9
9
|
import { newCRUDClient } from "../core/crud.js";
|
|
10
10
|
import { buildPath } from "../core/path.js";
|
|
11
|
-
|
|
12
|
-
const schemeHTTP = "http:";
|
|
13
|
-
const schemeHTTPS = "https:";
|
|
14
|
-
const schemeWS = "ws:";
|
|
15
|
-
const schemeWSS = "wss:";
|
|
11
|
+
import { openWS, } from "../core/ws.js";
|
|
16
12
|
export class ChatClient {
|
|
17
13
|
chats;
|
|
18
14
|
chat_rooms;
|
|
@@ -183,42 +179,3 @@ function chatRoomPath(action) {
|
|
|
183
179
|
action,
|
|
184
180
|
});
|
|
185
181
|
}
|
|
186
|
-
function openWS(opts) {
|
|
187
|
-
const socket = new WebSocket(wsURL(opts.base_url, opts.path), [
|
|
188
|
-
protocolFirebase,
|
|
189
|
-
opts.token,
|
|
190
|
-
]);
|
|
191
|
-
socket.addEventListener("message", (ev) => {
|
|
192
|
-
if (typeof ev.data !== "string") {
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
opts.on_message(JSON.parse(ev.data), ev);
|
|
196
|
-
});
|
|
197
|
-
if (opts.on_error !== undefined) {
|
|
198
|
-
socket.addEventListener("error", opts.on_error);
|
|
199
|
-
}
|
|
200
|
-
if (opts.on_close !== undefined) {
|
|
201
|
-
socket.addEventListener("close", opts.on_close);
|
|
202
|
-
}
|
|
203
|
-
return {
|
|
204
|
-
socket,
|
|
205
|
-
close(code, reason) {
|
|
206
|
-
socket.close(code, reason);
|
|
207
|
-
},
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
function wsURL(base, path) {
|
|
211
|
-
const u = new URL(path.replace(/^\/+/g, ""), ensureSlash(base));
|
|
212
|
-
if (u.protocol === schemeHTTPS) {
|
|
213
|
-
u.protocol = schemeWSS;
|
|
214
|
-
return u.toString();
|
|
215
|
-
}
|
|
216
|
-
if (u.protocol === schemeHTTP) {
|
|
217
|
-
u.protocol = schemeWS;
|
|
218
|
-
return u.toString();
|
|
219
|
-
}
|
|
220
|
-
return u.toString();
|
|
221
|
-
}
|
|
222
|
-
function ensureSlash(x) {
|
|
223
|
-
return x.endsWith("/") ? x : `${x}/`;
|
|
224
|
-
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type WSMessageHandler<T> = (v: T, ev: MessageEvent<string>) => void;
|
|
2
|
+
export type WSErrorHandler = (ev: Event) => void;
|
|
3
|
+
export type WSCloseHandler = (ev: CloseEvent) => void;
|
|
4
|
+
export type WSConnection = {
|
|
5
|
+
socket: WebSocket;
|
|
6
|
+
close(code?: number, reason?: string): void;
|
|
7
|
+
};
|
|
8
|
+
export type OpenWSOptions<T> = {
|
|
9
|
+
base_url: string;
|
|
10
|
+
path: string;
|
|
11
|
+
token: string;
|
|
12
|
+
on_message: WSMessageHandler<T>;
|
|
13
|
+
on_error?: WSErrorHandler;
|
|
14
|
+
on_close?: WSCloseHandler;
|
|
15
|
+
};
|
|
16
|
+
export declare function openWS<T>(opts: OpenWSOptions<T>): WSConnection;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Copyright (C) Fabriktor, Inc. 2025-present.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
4
|
+
// not use this file except in compliance with the License. You may obtain
|
|
5
|
+
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
import { HTTPScheme, HTTPSScheme } from "@fabriktor/schema";
|
|
7
|
+
const protocolFirebase = "firebase";
|
|
8
|
+
const schemeWS = "ws:";
|
|
9
|
+
const schemeWSS = "wss:";
|
|
10
|
+
export function openWS(opts) {
|
|
11
|
+
const socket = new WebSocket(wsURL(opts.base_url, opts.path), [
|
|
12
|
+
protocolFirebase,
|
|
13
|
+
opts.token,
|
|
14
|
+
]);
|
|
15
|
+
socket.addEventListener("message", (ev) => {
|
|
16
|
+
if (typeof ev.data !== "string") {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
opts.on_message(JSON.parse(ev.data), ev);
|
|
20
|
+
});
|
|
21
|
+
if (opts.on_error !== undefined) {
|
|
22
|
+
socket.addEventListener("error", opts.on_error);
|
|
23
|
+
}
|
|
24
|
+
if (opts.on_close !== undefined) {
|
|
25
|
+
socket.addEventListener("close", opts.on_close);
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
socket,
|
|
29
|
+
close(code, reason) {
|
|
30
|
+
socket.close(code, reason);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function wsURL(base, path) {
|
|
35
|
+
const u = new URL(path.replace(/^\/+/g, ""), ensureSlash(base));
|
|
36
|
+
if (u.protocol === protocolOf(HTTPSScheme)) {
|
|
37
|
+
u.protocol = schemeWSS;
|
|
38
|
+
return u.toString();
|
|
39
|
+
}
|
|
40
|
+
if (u.protocol === protocolOf(HTTPScheme)) {
|
|
41
|
+
u.protocol = schemeWS;
|
|
42
|
+
return u.toString();
|
|
43
|
+
}
|
|
44
|
+
return u.toString();
|
|
45
|
+
}
|
|
46
|
+
function protocolOf(scheme) {
|
|
47
|
+
return `${scheme}:`;
|
|
48
|
+
}
|
|
49
|
+
function ensureSlash(x) {
|
|
50
|
+
return x.endsWith("/") ? x : `${x}/`;
|
|
51
|
+
}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ export * from "./core/http.js";
|
|
|
11
11
|
export * from "./core/idempotency.js";
|
|
12
12
|
export * from "./core/multipart.js";
|
|
13
13
|
export * from "./core/path.js";
|
|
14
|
+
export * from "./core/ws.js";
|
|
14
15
|
export * from "./feed/feed.js";
|
|
15
16
|
export * from "./fulfillments/fulfillments.js";
|
|
16
17
|
export * from "./jobs/jobs.js";
|
package/dist/src/index.js
CHANGED
|
@@ -11,6 +11,7 @@ export * from "./core/http.js";
|
|
|
11
11
|
export * from "./core/idempotency.js";
|
|
12
12
|
export * from "./core/multipart.js";
|
|
13
13
|
export * from "./core/path.js";
|
|
14
|
+
export * from "./core/ws.js";
|
|
14
15
|
export * from "./feed/feed.js";
|
|
15
16
|
export * from "./fulfillments/fulfillments.js";
|
|
16
17
|
export * from "./jobs/jobs.js";
|