@chat-leo/chatleolib 2.0.1 → 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.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;
@@ -7918,14 +7975,16 @@
7918
7975
  const chatComponentElements = document.querySelectorAll('chatleo-component');
7919
7976
  for (let i = 0; i < chatComponentElements.length; i++) {
7920
7977
  const component = chatComponentElements[i];
7921
- if (component.serverUrl) {
7922
- this.serverUrlName = component.serverUrl;
7923
- }
7978
+ __classPrivateFieldGet(this, _ChatLeoLib_instances, "m", _ChatLeoLib_adoptComponentServerUrl).call(this, component.serverUrl);
7924
7979
  if (!component.chatLeoLib) {
7925
7980
  console.debug('chatComponentElement attached', component, component.serverUrl);
7926
7981
  component.chatLeoLib = this;
7927
7982
  }
7928
7983
  }
7984
+ }, _ChatLeoLib_adoptComponentServerUrl = function _ChatLeoLib_adoptComponentServerUrl(serverUrl) {
7985
+ if (!serverUrl || this.serverUrlExplicit)
7986
+ return;
7987
+ this.serverUrlName = serverUrl;
7929
7988
  }, _ChatLeoLib_observeDomChanges = function _ChatLeoLib_observeDomChanges() {
7930
7989
  if (!document.body)
7931
7990
  return;
@@ -7961,6 +8020,9 @@
7961
8020
  }, _ChatLeoLib_loginRequest =
7962
8021
  /** POSTs to the login endpoint and turns HTTP/network failures into a typed reason. */
7963
8022
  async function _ChatLeoLib_loginRequest(body, invalidReason) {
8023
+ if (!this.serverUrlName) {
8024
+ throw new Error('No server URL configured. Provide <chatleo-component server-url="…"> or call ChatLeoLib.getInstance().setServerUrl("…") before signing in.');
8025
+ }
7964
8026
  let response;
7965
8027
  try {
7966
8028
  response = await fetch(__classPrivateFieldGet(this, _ChatLeoLib_instances, "m", _ChatLeoLib_buildServerUrl).call(this, '/api/v1/auth/login'), {
@@ -8526,7 +8588,9 @@
8526
8588
  ChatLeoLib.RESUME_SYNC_COOLDOWN_MS = 10000;
8527
8589
  ChatLeoLib.READ_MARKER_DEBOUNCE_MS = 2000;
8528
8590
  ChatLeoLib.MESSAGE_PAGE_SIZE = 50;
8529
- new ChatLeoLib();
8591
+ // Created on load so the DOM scan and window listeners start without a caller; application code
8592
+ // reaches it through `ChatLeoLib.getInstance()`.
8593
+ const chatLeoLibSingleton = new ChatLeoLib();
8530
8594
 
8531
8595
  exports.ChatCallError = ChatCallError;
8532
8596
  exports.ChatLeoLib = ChatLeoLib;