@ermis-network/ermis-chat-sdk 1.0.3 → 1.0.5
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/index.browser.cjs +123 -4
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.full-bundle.min.js +7 -7
- package/dist/index.browser.full-bundle.min.js.map +1 -1
- package/dist/index.browser.mjs +123 -3
- package/dist/index.browser.mjs.map +1 -1
- package/dist/index.cjs +123 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +173 -0
- package/dist/index.d.ts +173 -0
- package/dist/index.mjs +123 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/channel.ts +42 -0
- package/src/channel_state.ts +27 -0
- package/src/client.ts +104 -0
- package/src/wasm/ermis_call_node_wasm.js +1 -1
package/dist/index.browser.mjs
CHANGED
|
@@ -356,15 +356,25 @@ var createForwardMessagePayload = (message, targetCid, activeCid) => {
|
|
|
356
356
|
// src/channel_state.ts
|
|
357
357
|
var ChannelState = class {
|
|
358
358
|
_channel;
|
|
359
|
+
/** The current count of users actively watching (having an open WebSocket) this channel. */
|
|
359
360
|
watcher_count;
|
|
361
|
+
/** A dictionary of active typing events gracefully keyed by the user's ID. */
|
|
360
362
|
typing;
|
|
363
|
+
/** A dictionary of read states mapped per user's ID detailing the last viewed message. */
|
|
361
364
|
read;
|
|
365
|
+
/** The locally cached array of pinned messages across the channel. */
|
|
362
366
|
pinnedMessages;
|
|
367
|
+
/** A directory of users actively watching the channel, keyed by User ID. */
|
|
363
368
|
watchers;
|
|
369
|
+
/** A comprehensive directory mapping user IDs to their member status in the channel. */
|
|
364
370
|
members;
|
|
371
|
+
/** The count of messages not yet read by the currently authenticated user. */
|
|
365
372
|
unreadCount;
|
|
373
|
+
/** Information detailing the authenticated user's own membership relation to this channel. */
|
|
366
374
|
membership;
|
|
375
|
+
/** Timestamp indicating when the very last message was created in this chat. */
|
|
367
376
|
last_message_at;
|
|
377
|
+
/** Designates if the local channel state is entirely synchronized with the backend history. */
|
|
368
378
|
isUpToDate;
|
|
369
379
|
messageSets = [];
|
|
370
380
|
topics = [];
|
|
@@ -396,6 +406,15 @@ var ChannelState = class {
|
|
|
396
406
|
const index = this.messageSets.findIndex((s) => s.isLatest);
|
|
397
407
|
this.messageSets[index].messages = messages;
|
|
398
408
|
}
|
|
409
|
+
/**
|
|
410
|
+
* Pushes a new message directly into the sorted array of the local tracking state.
|
|
411
|
+
* Useful to achieve optimistic UI updates locally.
|
|
412
|
+
*
|
|
413
|
+
* @param newMessage - The message context payload to insert.
|
|
414
|
+
* @param timestampChanged - Specifies if the underlying `created_at` timestamp mutated.
|
|
415
|
+
* @param addIfDoesNotExist - Append it strictly if its ID doesn't already exist.
|
|
416
|
+
* @param messageSetToAddToIfDoesNotExist - Specifies which message set scope to manipulate.
|
|
417
|
+
*/
|
|
399
418
|
addMessageSorted(newMessage, timestampChanged = false, addIfDoesNotExist = true, messageSetToAddToIfDoesNotExist = "latest") {
|
|
400
419
|
return this.addMessagesSorted(
|
|
401
420
|
[newMessage],
|
|
@@ -583,6 +602,12 @@ var ChannelState = class {
|
|
|
583
602
|
const result = msgArray.filter((message) => !(!!message.id && !!msg.id && message.id === msg.id));
|
|
584
603
|
return { removed: result.length < msgArray.length, result };
|
|
585
604
|
};
|
|
605
|
+
/**
|
|
606
|
+
* Refreshes internal user references cascading across all presently active messages.
|
|
607
|
+
* Invoked instantly whenever an underlying user's profile metadata updates (e.g. name or avatar changes).
|
|
608
|
+
*
|
|
609
|
+
* @param user - The newly formatted and populated User details object.
|
|
610
|
+
*/
|
|
586
611
|
updateUserMessages = (user) => {
|
|
587
612
|
const _updateUserMessages = (messages, user2) => {
|
|
588
613
|
for (let i = 0; i < messages.length; i++) {
|
|
@@ -854,6 +879,15 @@ var Channel = class {
|
|
|
854
879
|
lastTypingEvent;
|
|
855
880
|
isTyping;
|
|
856
881
|
disconnected;
|
|
882
|
+
/**
|
|
883
|
+
* Initializes a new Channel class instance.
|
|
884
|
+
* Normally you should not call this directly; use `client.channel(type, id)` instead.
|
|
885
|
+
*
|
|
886
|
+
* @param client - The shared ErmisChat client instance initializing this channel.
|
|
887
|
+
* @param type - The type of channel (`messaging`, `team`, `livestream`, etc.).
|
|
888
|
+
* @param id - The unique ID of the channel.
|
|
889
|
+
* @param data - Initial arbitrary metadata stored within this channel.
|
|
890
|
+
*/
|
|
857
891
|
constructor(client, type, id, data) {
|
|
858
892
|
const validTypeRe = /^[\w_-]+$/;
|
|
859
893
|
const validIDRe = /^[\w!:_-]+$/;
|
|
@@ -880,6 +914,13 @@ var Channel = class {
|
|
|
880
914
|
getClient() {
|
|
881
915
|
return this._client;
|
|
882
916
|
}
|
|
917
|
+
/**
|
|
918
|
+
* Sends a message to this channel.
|
|
919
|
+
* By default, it pushes the message eagerly (optimistically) to the local UI state before the server replies.
|
|
920
|
+
*
|
|
921
|
+
* @param message - The constructed text/attachment object payload representing the message.
|
|
922
|
+
* @returns A Promise resolving to the exact API response encompassing message details.
|
|
923
|
+
*/
|
|
883
924
|
async sendMessage(message) {
|
|
884
925
|
if (!message.id) {
|
|
885
926
|
message = { ...message, id: randomId() };
|
|
@@ -1109,6 +1150,11 @@ var Channel = class {
|
|
|
1109
1150
|
const url = this.getClient().baseURL + `/invites/${this.type}/${this.id}/skip`;
|
|
1110
1151
|
return this.getClient().post(url);
|
|
1111
1152
|
}
|
|
1153
|
+
/**
|
|
1154
|
+
* Directly invites or adds registered users into this channel.
|
|
1155
|
+
*
|
|
1156
|
+
* @param members - Array of user IDs explicitly selected to be added.
|
|
1157
|
+
*/
|
|
1112
1158
|
async addMembers(members) {
|
|
1113
1159
|
return await this._update({ add_members: members });
|
|
1114
1160
|
}
|
|
@@ -1174,6 +1220,11 @@ var Channel = class {
|
|
|
1174
1220
|
})
|
|
1175
1221
|
};
|
|
1176
1222
|
}
|
|
1223
|
+
/**
|
|
1224
|
+
* Expels specified currently participating users out of the channel.
|
|
1225
|
+
*
|
|
1226
|
+
* @param members - Array of user IDs to strictly remove from this chat.
|
|
1227
|
+
*/
|
|
1177
1228
|
async removeMembers(members) {
|
|
1178
1229
|
return await this._update({ remove_members: members });
|
|
1179
1230
|
}
|
|
@@ -1253,6 +1304,10 @@ var Channel = class {
|
|
|
1253
1304
|
messageSlice.sort((a, b) => b.created_at.getTime() - a.created_at.getTime());
|
|
1254
1305
|
return messageSlice[0];
|
|
1255
1306
|
}
|
|
1307
|
+
/**
|
|
1308
|
+
* Emits a mark-read event, updating the backend that the authenticated user has viewed up to the latest known message.
|
|
1309
|
+
* @returns Successful acknowledgement from the server.
|
|
1310
|
+
*/
|
|
1256
1311
|
async markRead() {
|
|
1257
1312
|
return await this.getClient().post(this._channelURL() + "/read");
|
|
1258
1313
|
}
|
|
@@ -1266,6 +1321,13 @@ var Channel = class {
|
|
|
1266
1321
|
}
|
|
1267
1322
|
this.state.clean();
|
|
1268
1323
|
}
|
|
1324
|
+
/**
|
|
1325
|
+
* Subscribes to realtime events (WebSocket) for this channel, grabs the latest available metadata,
|
|
1326
|
+
* loads the most recent messages, and initializes the local state.
|
|
1327
|
+
*
|
|
1328
|
+
* @param options - Pagination limits like `{ watch: true, presence: true, state: true }`.
|
|
1329
|
+
* @returns The synchronized comprehensive channel state.
|
|
1330
|
+
*/
|
|
1269
1331
|
async watch(options) {
|
|
1270
1332
|
await this.getClient().wsPromise;
|
|
1271
1333
|
const combined = { ...options };
|
|
@@ -2703,32 +2765,55 @@ function isString(x) {
|
|
|
2703
2765
|
var ErmisChat = class _ErmisChat {
|
|
2704
2766
|
static _instance;
|
|
2705
2767
|
// type is undefined|ErmisChat, unknown is due to TS limitations with statics
|
|
2768
|
+
/** A map of active channels currently tracked by the client, keyed by Channel ID. */
|
|
2706
2769
|
activeChannels;
|
|
2770
|
+
/** The internal configured Axios instance used for REST API requests. */
|
|
2707
2771
|
axiosInstance;
|
|
2772
|
+
/** The primary base URL for REST API communication. */
|
|
2708
2773
|
baseURL;
|
|
2774
|
+
/** The specific base URL for standard user-focused REST calls. */
|
|
2709
2775
|
userBaseURL;
|
|
2776
|
+
/** True when the SDK is executed inside a browser environment. */
|
|
2710
2777
|
browser;
|
|
2711
2778
|
cleaningIntervalRef;
|
|
2712
2779
|
clientID;
|
|
2713
2780
|
apiKey;
|
|
2714
2781
|
projectId;
|
|
2782
|
+
/** Internal mapped registry of event listeners. */
|
|
2715
2783
|
listeners;
|
|
2716
2784
|
logger;
|
|
2785
|
+
/** Whether the client should automatically fetch missing messages upon unexpected disconnects. */
|
|
2717
2786
|
recoverStateOnReconnect;
|
|
2787
|
+
/** True when the SDK is executed in a NodeJS environment constraint. */
|
|
2718
2788
|
node;
|
|
2789
|
+
/** Custom options passed during client initialization. */
|
|
2719
2790
|
options;
|
|
2720
2791
|
setUserPromise;
|
|
2792
|
+
/** Centralized global state orchestrating user and client metadata. */
|
|
2721
2793
|
state;
|
|
2722
2794
|
tokenManager;
|
|
2795
|
+
/** The globally authenticated current user object. */
|
|
2723
2796
|
user;
|
|
2724
2797
|
userAgent;
|
|
2798
|
+
/** The unique ID of the current authenticated user. */
|
|
2725
2799
|
userID;
|
|
2800
|
+
/** The configured WebSocket endpoint base URL for realtime subscriptions. */
|
|
2726
2801
|
wsBaseURL;
|
|
2802
|
+
/** The active WebSocket connection controller. */
|
|
2727
2803
|
wsConnection;
|
|
2728
2804
|
wsPromise;
|
|
2805
|
+
/** Tracks consecutive REST API failures for exponential backoff purposes. */
|
|
2729
2806
|
consecutiveFailures;
|
|
2730
2807
|
defaultWSTimeout;
|
|
2731
2808
|
eventSource = null;
|
|
2809
|
+
/**
|
|
2810
|
+
* Initializes a new Ermis Chat Client instance.
|
|
2811
|
+
*
|
|
2812
|
+
* @param apiKey - Your public Ermis Network API Key.
|
|
2813
|
+
* @param projectId - Your specific Project UUID pointing to the app config.
|
|
2814
|
+
* @param baseURL - The API base endpoint assigned to your project.
|
|
2815
|
+
* @param options - Additional connection rules and configuration options.
|
|
2816
|
+
*/
|
|
2732
2817
|
constructor(apiKey, projectId, baseURL, options) {
|
|
2733
2818
|
this.apiKey = apiKey;
|
|
2734
2819
|
this.projectId = projectId;
|
|
@@ -2762,6 +2847,16 @@ var ErmisChat = class _ErmisChat {
|
|
|
2762
2847
|
this.logger = isFunction(inputOptions.logger) ? inputOptions.logger : () => null;
|
|
2763
2848
|
this.recoverStateOnReconnect = this.options.recoverStateOnReconnect;
|
|
2764
2849
|
}
|
|
2850
|
+
/**
|
|
2851
|
+
* Retrieves the globally registered Singleton instance of the ErmisChat client.
|
|
2852
|
+
* If the instance lacks existence, it initializes a new one.
|
|
2853
|
+
*
|
|
2854
|
+
* @param key - Your public Ermis Network API Key.
|
|
2855
|
+
* @param projectId - Your specific Project UUID.
|
|
2856
|
+
* @param baseURL - The API base endpoint.
|
|
2857
|
+
* @param options - Connection options.
|
|
2858
|
+
* @returns The shared ErmisChat client instance.
|
|
2859
|
+
*/
|
|
2765
2860
|
static getInstance(key, projectId, baseURL, options) {
|
|
2766
2861
|
if (!_ErmisChat._instance) {
|
|
2767
2862
|
_ErmisChat._instance = new _ErmisChat(key, projectId, baseURL, options);
|
|
@@ -2809,6 +2904,15 @@ var ErmisChat = class _ErmisChat {
|
|
|
2809
2904
|
}
|
|
2810
2905
|
return await response.json();
|
|
2811
2906
|
}
|
|
2907
|
+
/**
|
|
2908
|
+
* Connects a user to the Ermis network and establishes the WebSocket connection.
|
|
2909
|
+
* This is the primary method to authenticate your client application.
|
|
2910
|
+
*
|
|
2911
|
+
* @param user - The User object containing `id`, `name`, and optional `avatar`.
|
|
2912
|
+
* @param userTokenOrProvider - The JWT token or an async token provider function.
|
|
2913
|
+
* @param extenal_auth - Set to `true` to use your custom backend external authentication flow.
|
|
2914
|
+
* @returns A promise resolving to the API connection response once authenticated.
|
|
2915
|
+
*/
|
|
2812
2916
|
connectUser = async (user, userTokenOrProvider, extenal_auth) => {
|
|
2813
2917
|
this.logger("info", "client:connectUser() - started", {
|
|
2814
2918
|
tags: ["connection", "client"]
|
|
@@ -2891,6 +2995,12 @@ var ErmisChat = class _ErmisChat {
|
|
|
2891
2995
|
return this.wsPromise;
|
|
2892
2996
|
};
|
|
2893
2997
|
_setupConnection = this.openConnection;
|
|
2998
|
+
/**
|
|
2999
|
+
* Gracefully disconnects the current user, terminates the WebSocket connection,
|
|
3000
|
+
* cleans up listeners, and resets the client's internal references.
|
|
3001
|
+
*
|
|
3002
|
+
* @param timeout - Optional timeout in milliseconds before forcing the disconnect.
|
|
3003
|
+
*/
|
|
2894
3004
|
disconnectUser = async (timeout) => {
|
|
2895
3005
|
this.logger("info", "client:disconnect() - Disconnecting the client", {
|
|
2896
3006
|
tags: ["connection", "client"]
|
|
@@ -3456,6 +3566,16 @@ var ErmisChat = class _ErmisChat {
|
|
|
3456
3566
|
this.state.updateUser(response);
|
|
3457
3567
|
return response;
|
|
3458
3568
|
}
|
|
3569
|
+
/**
|
|
3570
|
+
* Queries the API for a list of channels based on provided search filters and sort conditions.
|
|
3571
|
+
* Also hydrates these channels into the local SDK state memory.
|
|
3572
|
+
*
|
|
3573
|
+
* @param filterConditions - Specific criteria to filter channels (e.g. `{ type: 'messaging', members: { $in: ['user1'] } }`).
|
|
3574
|
+
* @param sort - The sorting hierarchy applied to the channel results.
|
|
3575
|
+
* @param options - Pagination and message limit parameters.
|
|
3576
|
+
* @param stateOptions - Defines whether to skip state initialization or offline usage.
|
|
3577
|
+
* @returns An array of hydrated and locally manageable `Channel` objects.
|
|
3578
|
+
*/
|
|
3459
3579
|
async queryChannels(filterConditions, sort = [], options = {}, stateOptions = {}) {
|
|
3460
3580
|
await this.wsPromise;
|
|
3461
3581
|
let project_id = this.projectId;
|
|
@@ -3607,7 +3727,7 @@ var ErmisChat = class _ErmisChat {
|
|
|
3607
3727
|
return pinExpires;
|
|
3608
3728
|
}
|
|
3609
3729
|
getUserAgent() {
|
|
3610
|
-
return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.
|
|
3730
|
+
return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.5"}`;
|
|
3611
3731
|
}
|
|
3612
3732
|
setUserAgent(userAgent) {
|
|
3613
3733
|
this.userAgent = userAgent;
|
|
@@ -5130,7 +5250,7 @@ async function __wbg_init(module_or_path) {
|
|
|
5130
5250
|
}
|
|
5131
5251
|
}
|
|
5132
5252
|
if (typeof module_or_path === "undefined") {
|
|
5133
|
-
module_or_path =
|
|
5253
|
+
module_or_path = "/ermis_call_node_wasm_bg.wasm";
|
|
5134
5254
|
}
|
|
5135
5255
|
const imports = __wbg_get_imports();
|
|
5136
5256
|
if (typeof module_or_path === "string" || typeof Request === "function" && module_or_path instanceof Request || typeof URL === "function" && module_or_path instanceof URL) {
|
|
@@ -6965,7 +7085,7 @@ var ErmisAuthProvider = class {
|
|
|
6965
7085
|
return data;
|
|
6966
7086
|
}
|
|
6967
7087
|
getUserAgent() {
|
|
6968
|
-
return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.
|
|
7088
|
+
return this.userAgent || `ermis-chat-sdk-javascript-client-${this.node ? "node" : "browser"}-${"1.0.5"}`;
|
|
6969
7089
|
}
|
|
6970
7090
|
setUserAgent(userAgent) {
|
|
6971
7091
|
this.userAgent = userAgent;
|