@chat-leo/chatleolib 2.0.2 → 2.1.1

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.js CHANGED
@@ -6996,7 +6996,7 @@ class PushService {
6996
6996
  }
6997
6997
  _PushService_options = new WeakMap(), _PushService_deviceId = new WeakMap(), _PushService_client = new WeakMap();
6998
6998
 
6999
- var _ChatLeoLib_instances, _a, _ChatLeoLib_applySession, _ChatLeoLib_setCallRole, _ChatLeoLib_requireCalls, _ChatLeoLib_callRequest, _ChatLeoLib_scanComponents, _ChatLeoLib_observeDomChanges, _ChatLeoLib_buildServerUrl, _ChatLeoLib_loginWithCredentials, _ChatLeoLib_loginWithTicket, _ChatLeoLib_loginRequest, _ChatLeoLib_selectTransport, _ChatLeoLib_connectChat, _ChatLeoLib_detachTransport, _ChatLeoLib_onTransportEvent, _ChatLeoLib_request, _ChatLeoLib_onMessageSent, _ChatLeoLib_getRooms, _ChatLeoLib_getUsers, _ChatLeoLib_getRoomMessages, _ChatLeoLib_fetchMessagesPage, _ChatLeoLib_completeLogin, _ChatLeoLib_runSync, _ChatLeoLib_runLegacyCatchUp, _ChatLeoLib_detectGap, _ChatLeoLib_repairGap, _ChatLeoLib_loadOlderMessages, _ChatLeoLib_canReportRead, _ChatLeoLib_clearReadMarkerTimers, _ChatLeoLib_scheduleReadMarker, _ChatLeoLib_sendReadMarker, _ChatLeoLib_scheduleTokenRefresh, _ChatLeoLib_clearRefreshTimer, _ChatLeoLib_refreshAccessToken, _ChatLeoLib_performTokenRefresh, _ChatLeoLib_handleSessionExpired;
6999
+ var _ChatLeoLib_instances, _a, _ChatLeoLib_applySession, _ChatLeoLib_setCallRole, _ChatLeoLib_requireCalls, _ChatLeoLib_callRequest, _ChatLeoLib_scanComponents, _ChatLeoLib_adoptComponentServerUrl, _ChatLeoLib_observeDomChanges, _ChatLeoLib_buildServerUrl, _ChatLeoLib_loginWithCredentials, _ChatLeoLib_loginWithTicket, _ChatLeoLib_loginRequest, _ChatLeoLib_selectTransport, _ChatLeoLib_connectChat, _ChatLeoLib_detachTransport, _ChatLeoLib_onTransportEvent, _ChatLeoLib_request, _ChatLeoLib_onMessageSent, _ChatLeoLib_getRooms, _ChatLeoLib_getUsers, _ChatLeoLib_getRoomMessages, _ChatLeoLib_fetchMessagesPage, _ChatLeoLib_completeLogin, _ChatLeoLib_runSync, _ChatLeoLib_runLegacyCatchUp, _ChatLeoLib_detectGap, _ChatLeoLib_repairGap, _ChatLeoLib_loadOlderMessages, _ChatLeoLib_canReportRead, _ChatLeoLib_clearReadMarkerTimers, _ChatLeoLib_scheduleReadMarker, _ChatLeoLib_sendReadMarker, _ChatLeoLib_scheduleTokenRefresh, _ChatLeoLib_clearRefreshTimer, _ChatLeoLib_refreshAccessToken, _ChatLeoLib_performTokenRefresh, _ChatLeoLib_handleSessionExpired;
7000
7000
  /** A sign-in failure with a machine-readable reason. */
7001
7001
  class ChatLeoLoginError extends Error {
7002
7002
  constructor(reason, status, message) {
@@ -7017,10 +7017,23 @@ class ChatCallError extends Error {
7017
7017
  }
7018
7018
  }
7019
7019
  class ChatLeoLib {
7020
+ /**
7021
+ * The one `ChatLeoLib` bound to this page.
7022
+ *
7023
+ * A host that owns the session itself — an application shell with no embedded
7024
+ * `<chatleo-component>` on the current page — uses this to point the library at a server
7025
+ * (`setServerUrl`) and sign in. Components discovered by the DOM scan are bound to the same
7026
+ * instance, so they share the session and state.
7027
+ */
7028
+ static getInstance() {
7029
+ return chatLeoLibSingleton;
7030
+ }
7020
7031
  constructor() {
7021
7032
  // this.serverUrlName = new URL(import.meta.url).searchParams.get("server"); // 5
7022
7033
  _ChatLeoLib_instances.add(this);
7023
7034
  this.serverUrlName = null;
7035
+ /** `true` once the host set the server URL itself (programmatic config wins over the DOM scan). */
7036
+ this.serverUrlExplicit = false;
7024
7037
  this.accessToken = null;
7025
7038
  this.refreshToken = null;
7026
7039
  this.userProfile = null;
@@ -7137,6 +7150,45 @@ class ChatLeoLib {
7137
7150
  });
7138
7151
  }
7139
7152
  // external methods called from components
7153
+ /**
7154
+ * Points the library at a ChatLeo server. Required before the first sign-in when the host
7155
+ * has no `<chatleo-component>` on the page; components configure it through their
7156
+ * `server-url` attribute otherwise.
7157
+ *
7158
+ * Accepts an origin URL (`https://chat.example.com`); REST and discovery endpoints are
7159
+ * resolved from it. Re-setting the current URL is a no-op. Changing it while signed in
7160
+ * throws, because the session's tokens, transport and rooms belong to the previous server —
7161
+ * call `logout()` first.
7162
+ */
7163
+ setServerUrl(serverUrl) {
7164
+ const raw = typeof serverUrl === 'string' ? serverUrl.trim() : '';
7165
+ let parsed;
7166
+ try {
7167
+ parsed = new URL(raw);
7168
+ }
7169
+ catch (_b) {
7170
+ throw new Error(`setServerUrl requires an absolute http(s) URL, received "${String(serverUrl)}".`);
7171
+ }
7172
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
7173
+ throw new Error(`setServerUrl requires an http(s) URL, received "${raw}".`);
7174
+ }
7175
+ if (parsed.pathname !== '/' || parsed.search || parsed.hash) {
7176
+ throw new Error(`setServerUrl requires an origin URL with no path, query or hash (received "${raw}"); REST endpoints are resolved from the origin.`);
7177
+ }
7178
+ const origin = parsed.origin;
7179
+ if (this.serverUrlName === origin) {
7180
+ // The host has confirmed this server; keep it sticky against the DOM scan.
7181
+ this.serverUrlExplicit = true;
7182
+ return;
7183
+ }
7184
+ if (this.isLoggedIn) {
7185
+ throw new Error(`Cannot change the server URL while signed in (currently ${this.serverUrlName}); call logout() first.`);
7186
+ }
7187
+ this.serverUrlName = origin;
7188
+ this.serverUrlExplicit = true;
7189
+ // A different server may advertise a different transport in its discovery document.
7190
+ clearDiscovery(origin);
7191
+ }
7140
7192
  /**
7141
7193
  * Signs in with credentials and completes the session (transport, rooms, users).
7142
7194
  *
@@ -7185,7 +7237,12 @@ class ChatLeoLib {
7185
7237
  return this.userProfile;
7186
7238
  if (!(session === null || session === void 0 ? void 0 : session.refreshToken) || !session.profile)
7187
7239
  return null;
7188
- this.serverUrlName = session.serverUrl || this.serverUrlName;
7240
+ if (session.serverUrl) {
7241
+ // Normalise the stored base so a later setServerUrl() for the same server is a no-op.
7242
+ this.serverUrlName = session.serverUrl.replace(/\/+$/, '');
7243
+ // A restored session's server is host intent; keep it sticky against the DOM scan.
7244
+ this.serverUrlExplicit = true;
7245
+ }
7189
7246
  this.accessToken = session.accessToken || null;
7190
7247
  this.refreshToken = session.refreshToken;
7191
7248
  this.userProfile = session.profile;
@@ -7499,7 +7556,7 @@ class ChatLeoLib {
7499
7556
  this.clients.delete(client);
7500
7557
  }
7501
7558
  async requestConnection(client, options = {}) {
7502
- var _b, _c, _d, _e, _f, _g;
7559
+ var _b, _c, _d, _e, _f, _g, _h;
7503
7560
  console.debug(`requestConnection`, client, options);
7504
7561
  client.chatStore = this.chatStore;
7505
7562
  this.registerClient(client);
@@ -7516,18 +7573,21 @@ class ChatLeoLib {
7516
7573
  console.debug('Already logged in, hydrate client');
7517
7574
  (_b = client.setLoading) === null || _b === void 0 ? void 0 : _b.call(client, false);
7518
7575
  (_c = client.setLoggedIn) === null || _c === void 0 ? void 0 : _c.call(client, true, this.userProfile);
7576
+ // A client that joins an already-connected session must learn the current
7577
+ // status, or it keeps its initial "disconnected" state (stale Offline banner).
7578
+ (_d = client.setConnectionStatus) === null || _d === void 0 ? void 0 : _d.call(client, this.chatStore.connectionStatus);
7519
7579
  return { status: 'connected' };
7520
7580
  }
7521
7581
  // No active session: allow client to wait without triggering login if no credentials
7522
7582
  const hasCreds = Boolean(options.ticket || (options.username && options.password));
7523
7583
  if (!this.loginPromise && !hasCreds) {
7524
7584
  console.debug('No active session, allow client to wait');
7525
- (_d = client.setLoading) === null || _d === void 0 ? void 0 : _d.call(client, false);
7585
+ (_e = client.setLoading) === null || _e === void 0 ? void 0 : _e.call(client, false);
7526
7586
  return { status: 'waiting' };
7527
7587
  }
7528
7588
  console.debug('Requesting connection');
7529
7589
  // Gate concurrent logins
7530
- (_e = client.setLoading) === null || _e === void 0 ? void 0 : _e.call(client, true);
7590
+ (_f = client.setLoading) === null || _f === void 0 ? void 0 : _f.call(client, true);
7531
7591
  if (!this.loginPromise && hasCreds) {
7532
7592
  // Start a new login process
7533
7593
  this.loginPromise = this.performLogin(options)
@@ -7547,8 +7607,8 @@ class ChatLeoLib {
7547
7607
  catch (err) {
7548
7608
  // notify this client of error
7549
7609
  console.error('Login failed', err);
7550
- (_f = client.setLoading) === null || _f === void 0 ? void 0 : _f.call(client, false);
7551
- (_g = client.setLoggedIn) === null || _g === void 0 ? void 0 : _g.call(client, false, null);
7610
+ (_g = client.setLoading) === null || _g === void 0 ? void 0 : _g.call(client, false);
7611
+ (_h = client.setLoggedIn) === null || _h === void 0 ? void 0 : _h.call(client, false, null);
7552
7612
  return { status: 'error' };
7553
7613
  }
7554
7614
  }
@@ -7914,14 +7974,16 @@ async function _ChatLeoLib_scanComponents() {
7914
7974
  const chatComponentElements = document.querySelectorAll('chatleo-component');
7915
7975
  for (let i = 0; i < chatComponentElements.length; i++) {
7916
7976
  const component = chatComponentElements[i];
7917
- if (component.serverUrl) {
7918
- this.serverUrlName = component.serverUrl;
7919
- }
7977
+ __classPrivateFieldGet(this, _ChatLeoLib_instances, "m", _ChatLeoLib_adoptComponentServerUrl).call(this, component.serverUrl);
7920
7978
  if (!component.chatLeoLib) {
7921
7979
  console.debug('chatComponentElement attached', component, component.serverUrl);
7922
7980
  component.chatLeoLib = this;
7923
7981
  }
7924
7982
  }
7983
+ }, _ChatLeoLib_adoptComponentServerUrl = function _ChatLeoLib_adoptComponentServerUrl(serverUrl) {
7984
+ if (!serverUrl || this.serverUrlExplicit)
7985
+ return;
7986
+ this.serverUrlName = serverUrl;
7925
7987
  }, _ChatLeoLib_observeDomChanges = function _ChatLeoLib_observeDomChanges() {
7926
7988
  if (!document.body)
7927
7989
  return;
@@ -7957,6 +8019,9 @@ async function _ChatLeoLib_scanComponents() {
7957
8019
  }, _ChatLeoLib_loginRequest =
7958
8020
  /** POSTs to the login endpoint and turns HTTP/network failures into a typed reason. */
7959
8021
  async function _ChatLeoLib_loginRequest(body, invalidReason) {
8022
+ if (!this.serverUrlName) {
8023
+ throw new Error('No server URL configured. Provide <chatleo-component server-url="…"> or call ChatLeoLib.getInstance().setServerUrl("…") before signing in.');
8024
+ }
7960
8025
  let response;
7961
8026
  try {
7962
8027
  response = await fetch(__classPrivateFieldGet(this, _ChatLeoLib_instances, "m", _ChatLeoLib_buildServerUrl).call(this, '/api/v1/auth/login'), {
@@ -8522,7 +8587,9 @@ ChatLeoLib.MAX_REFRESH_RETRIES = 5;
8522
8587
  ChatLeoLib.RESUME_SYNC_COOLDOWN_MS = 10000;
8523
8588
  ChatLeoLib.READ_MARKER_DEBOUNCE_MS = 2000;
8524
8589
  ChatLeoLib.MESSAGE_PAGE_SIZE = 50;
8525
- new ChatLeoLib();
8590
+ // Created on load so the DOM scan and window listeners start without a caller; application code
8591
+ // reaches it through `ChatLeoLib.getInstance()`.
8592
+ const chatLeoLibSingleton = new ChatLeoLib();
8526
8593
 
8527
8594
  exports.ChatCallError = ChatCallError;
8528
8595
  exports.ChatLeoLib = ChatLeoLib;