@chat-leo/chatleolib 2.0.2 → 2.1.0

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;
@@ -7914,14 +7971,16 @@ async function _ChatLeoLib_scanComponents() {
7914
7971
  const chatComponentElements = document.querySelectorAll('chatleo-component');
7915
7972
  for (let i = 0; i < chatComponentElements.length; i++) {
7916
7973
  const component = chatComponentElements[i];
7917
- if (component.serverUrl) {
7918
- this.serverUrlName = component.serverUrl;
7919
- }
7974
+ __classPrivateFieldGet(this, _ChatLeoLib_instances, "m", _ChatLeoLib_adoptComponentServerUrl).call(this, component.serverUrl);
7920
7975
  if (!component.chatLeoLib) {
7921
7976
  console.debug('chatComponentElement attached', component, component.serverUrl);
7922
7977
  component.chatLeoLib = this;
7923
7978
  }
7924
7979
  }
7980
+ }, _ChatLeoLib_adoptComponentServerUrl = function _ChatLeoLib_adoptComponentServerUrl(serverUrl) {
7981
+ if (!serverUrl || this.serverUrlExplicit)
7982
+ return;
7983
+ this.serverUrlName = serverUrl;
7925
7984
  }, _ChatLeoLib_observeDomChanges = function _ChatLeoLib_observeDomChanges() {
7926
7985
  if (!document.body)
7927
7986
  return;
@@ -7957,6 +8016,9 @@ async function _ChatLeoLib_scanComponents() {
7957
8016
  }, _ChatLeoLib_loginRequest =
7958
8017
  /** POSTs to the login endpoint and turns HTTP/network failures into a typed reason. */
7959
8018
  async function _ChatLeoLib_loginRequest(body, invalidReason) {
8019
+ if (!this.serverUrlName) {
8020
+ throw new Error('No server URL configured. Provide <chatleo-component server-url="…"> or call ChatLeoLib.getInstance().setServerUrl("…") before signing in.');
8021
+ }
7960
8022
  let response;
7961
8023
  try {
7962
8024
  response = await fetch(__classPrivateFieldGet(this, _ChatLeoLib_instances, "m", _ChatLeoLib_buildServerUrl).call(this, '/api/v1/auth/login'), {
@@ -8522,7 +8584,9 @@ ChatLeoLib.MAX_REFRESH_RETRIES = 5;
8522
8584
  ChatLeoLib.RESUME_SYNC_COOLDOWN_MS = 10000;
8523
8585
  ChatLeoLib.READ_MARKER_DEBOUNCE_MS = 2000;
8524
8586
  ChatLeoLib.MESSAGE_PAGE_SIZE = 50;
8525
- new ChatLeoLib();
8587
+ // Created on load so the DOM scan and window listeners start without a caller; application code
8588
+ // reaches it through `ChatLeoLib.getInstance()`.
8589
+ const chatLeoLibSingleton = new ChatLeoLib();
8526
8590
 
8527
8591
  exports.ChatCallError = ChatCallError;
8528
8592
  exports.ChatLeoLib = ChatLeoLib;