@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.umd.js CHANGED
@@ -7000,7 +7000,7 @@
7000
7000
  }
7001
7001
  _PushService_options = new WeakMap(), _PushService_deviceId = new WeakMap(), _PushService_client = new WeakMap();
7002
7002
 
7003
- 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;
7003
+ 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;
7004
7004
  /** A sign-in failure with a machine-readable reason. */
7005
7005
  class ChatLeoLoginError extends Error {
7006
7006
  constructor(reason, status, message) {
@@ -7021,10 +7021,23 @@
7021
7021
  }
7022
7022
  }
7023
7023
  class ChatLeoLib {
7024
+ /**
7025
+ * The one `ChatLeoLib` bound to this page.
7026
+ *
7027
+ * A host that owns the session itself — an application shell with no embedded
7028
+ * `<chatleo-component>` on the current page — uses this to point the library at a server
7029
+ * (`setServerUrl`) and sign in. Components discovered by the DOM scan are bound to the same
7030
+ * instance, so they share the session and state.
7031
+ */
7032
+ static getInstance() {
7033
+ return chatLeoLibSingleton;
7034
+ }
7024
7035
  constructor() {
7025
7036
  // this.serverUrlName = new URL(import.meta.url).searchParams.get("server"); // 5
7026
7037
  _ChatLeoLib_instances.add(this);
7027
7038
  this.serverUrlName = null;
7039
+ /** `true` once the host set the server URL itself (programmatic config wins over the DOM scan). */
7040
+ this.serverUrlExplicit = false;
7028
7041
  this.accessToken = null;
7029
7042
  this.refreshToken = null;
7030
7043
  this.userProfile = null;
@@ -7141,6 +7154,45 @@
7141
7154
  });
7142
7155
  }
7143
7156
  // external methods called from components
7157
+ /**
7158
+ * Points the library at a ChatLeo server. Required before the first sign-in when the host
7159
+ * has no `<chatleo-component>` on the page; components configure it through their
7160
+ * `server-url` attribute otherwise.
7161
+ *
7162
+ * Accepts an origin URL (`https://chat.example.com`); REST and discovery endpoints are
7163
+ * resolved from it. Re-setting the current URL is a no-op. Changing it while signed in
7164
+ * throws, because the session's tokens, transport and rooms belong to the previous server —
7165
+ * call `logout()` first.
7166
+ */
7167
+ setServerUrl(serverUrl) {
7168
+ const raw = typeof serverUrl === 'string' ? serverUrl.trim() : '';
7169
+ let parsed;
7170
+ try {
7171
+ parsed = new URL(raw);
7172
+ }
7173
+ catch (_b) {
7174
+ throw new Error(`setServerUrl requires an absolute http(s) URL, received "${String(serverUrl)}".`);
7175
+ }
7176
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
7177
+ throw new Error(`setServerUrl requires an http(s) URL, received "${raw}".`);
7178
+ }
7179
+ if (parsed.pathname !== '/' || parsed.search || parsed.hash) {
7180
+ throw new Error(`setServerUrl requires an origin URL with no path, query or hash (received "${raw}"); REST endpoints are resolved from the origin.`);
7181
+ }
7182
+ const origin = parsed.origin;
7183
+ if (this.serverUrlName === origin) {
7184
+ // The host has confirmed this server; keep it sticky against the DOM scan.
7185
+ this.serverUrlExplicit = true;
7186
+ return;
7187
+ }
7188
+ if (this.isLoggedIn) {
7189
+ throw new Error(`Cannot change the server URL while signed in (currently ${this.serverUrlName}); call logout() first.`);
7190
+ }
7191
+ this.serverUrlName = origin;
7192
+ this.serverUrlExplicit = true;
7193
+ // A different server may advertise a different transport in its discovery document.
7194
+ clearDiscovery(origin);
7195
+ }
7144
7196
  /**
7145
7197
  * Signs in with credentials and completes the session (transport, rooms, users).
7146
7198
  *
@@ -7189,7 +7241,12 @@
7189
7241
  return this.userProfile;
7190
7242
  if (!(session === null || session === void 0 ? void 0 : session.refreshToken) || !session.profile)
7191
7243
  return null;
7192
- this.serverUrlName = session.serverUrl || this.serverUrlName;
7244
+ if (session.serverUrl) {
7245
+ // Normalise the stored base so a later setServerUrl() for the same server is a no-op.
7246
+ this.serverUrlName = session.serverUrl.replace(/\/+$/, '');
7247
+ // A restored session's server is host intent; keep it sticky against the DOM scan.
7248
+ this.serverUrlExplicit = true;
7249
+ }
7193
7250
  this.accessToken = session.accessToken || null;
7194
7251
  this.refreshToken = session.refreshToken;
7195
7252
  this.userProfile = session.profile;
@@ -7503,7 +7560,7 @@
7503
7560
  this.clients.delete(client);
7504
7561
  }
7505
7562
  async requestConnection(client, options = {}) {
7506
- var _b, _c, _d, _e, _f, _g;
7563
+ var _b, _c, _d, _e, _f, _g, _h;
7507
7564
  console.debug(`requestConnection`, client, options);
7508
7565
  client.chatStore = this.chatStore;
7509
7566
  this.registerClient(client);
@@ -7520,18 +7577,21 @@
7520
7577
  console.debug('Already logged in, hydrate client');
7521
7578
  (_b = client.setLoading) === null || _b === void 0 ? void 0 : _b.call(client, false);
7522
7579
  (_c = client.setLoggedIn) === null || _c === void 0 ? void 0 : _c.call(client, true, this.userProfile);
7580
+ // A client that joins an already-connected session must learn the current
7581
+ // status, or it keeps its initial "disconnected" state (stale Offline banner).
7582
+ (_d = client.setConnectionStatus) === null || _d === void 0 ? void 0 : _d.call(client, this.chatStore.connectionStatus);
7523
7583
  return { status: 'connected' };
7524
7584
  }
7525
7585
  // No active session: allow client to wait without triggering login if no credentials
7526
7586
  const hasCreds = Boolean(options.ticket || (options.username && options.password));
7527
7587
  if (!this.loginPromise && !hasCreds) {
7528
7588
  console.debug('No active session, allow client to wait');
7529
- (_d = client.setLoading) === null || _d === void 0 ? void 0 : _d.call(client, false);
7589
+ (_e = client.setLoading) === null || _e === void 0 ? void 0 : _e.call(client, false);
7530
7590
  return { status: 'waiting' };
7531
7591
  }
7532
7592
  console.debug('Requesting connection');
7533
7593
  // Gate concurrent logins
7534
- (_e = client.setLoading) === null || _e === void 0 ? void 0 : _e.call(client, true);
7594
+ (_f = client.setLoading) === null || _f === void 0 ? void 0 : _f.call(client, true);
7535
7595
  if (!this.loginPromise && hasCreds) {
7536
7596
  // Start a new login process
7537
7597
  this.loginPromise = this.performLogin(options)
@@ -7551,8 +7611,8 @@
7551
7611
  catch (err) {
7552
7612
  // notify this client of error
7553
7613
  console.error('Login failed', err);
7554
- (_f = client.setLoading) === null || _f === void 0 ? void 0 : _f.call(client, false);
7555
- (_g = client.setLoggedIn) === null || _g === void 0 ? void 0 : _g.call(client, false, null);
7614
+ (_g = client.setLoading) === null || _g === void 0 ? void 0 : _g.call(client, false);
7615
+ (_h = client.setLoggedIn) === null || _h === void 0 ? void 0 : _h.call(client, false, null);
7556
7616
  return { status: 'error' };
7557
7617
  }
7558
7618
  }
@@ -7918,14 +7978,16 @@
7918
7978
  const chatComponentElements = document.querySelectorAll('chatleo-component');
7919
7979
  for (let i = 0; i < chatComponentElements.length; i++) {
7920
7980
  const component = chatComponentElements[i];
7921
- if (component.serverUrl) {
7922
- this.serverUrlName = component.serverUrl;
7923
- }
7981
+ __classPrivateFieldGet(this, _ChatLeoLib_instances, "m", _ChatLeoLib_adoptComponentServerUrl).call(this, component.serverUrl);
7924
7982
  if (!component.chatLeoLib) {
7925
7983
  console.debug('chatComponentElement attached', component, component.serverUrl);
7926
7984
  component.chatLeoLib = this;
7927
7985
  }
7928
7986
  }
7987
+ }, _ChatLeoLib_adoptComponentServerUrl = function _ChatLeoLib_adoptComponentServerUrl(serverUrl) {
7988
+ if (!serverUrl || this.serverUrlExplicit)
7989
+ return;
7990
+ this.serverUrlName = serverUrl;
7929
7991
  }, _ChatLeoLib_observeDomChanges = function _ChatLeoLib_observeDomChanges() {
7930
7992
  if (!document.body)
7931
7993
  return;
@@ -7961,6 +8023,9 @@
7961
8023
  }, _ChatLeoLib_loginRequest =
7962
8024
  /** POSTs to the login endpoint and turns HTTP/network failures into a typed reason. */
7963
8025
  async function _ChatLeoLib_loginRequest(body, invalidReason) {
8026
+ if (!this.serverUrlName) {
8027
+ throw new Error('No server URL configured. Provide <chatleo-component server-url="…"> or call ChatLeoLib.getInstance().setServerUrl("…") before signing in.');
8028
+ }
7964
8029
  let response;
7965
8030
  try {
7966
8031
  response = await fetch(__classPrivateFieldGet(this, _ChatLeoLib_instances, "m", _ChatLeoLib_buildServerUrl).call(this, '/api/v1/auth/login'), {
@@ -8526,7 +8591,9 @@
8526
8591
  ChatLeoLib.RESUME_SYNC_COOLDOWN_MS = 10000;
8527
8592
  ChatLeoLib.READ_MARKER_DEBOUNCE_MS = 2000;
8528
8593
  ChatLeoLib.MESSAGE_PAGE_SIZE = 50;
8529
- new ChatLeoLib();
8594
+ // Created on load so the DOM scan and window listeners start without a caller; application code
8595
+ // reaches it through `ChatLeoLib.getInstance()`.
8596
+ const chatLeoLibSingleton = new ChatLeoLib();
8530
8597
 
8531
8598
  exports.ChatCallError = ChatCallError;
8532
8599
  exports.ChatLeoLib = ChatLeoLib;