@dora-cell/sdk 2.0.0 → 3.0.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.mjs CHANGED
@@ -23011,7 +23011,6 @@ var ApiTokenAuthProvider = class {
23011
23011
  }
23012
23012
  const baseUrl = apiBaseUrl?.replace(/\/$/, "") || "https://api.cell.usedora.com/api";
23013
23013
  try {
23014
- console.log("SDK: Verifying keys at:", `${baseUrl}/sdk/v1/auth/session`);
23015
23014
  const authResponse = await fetch(`${baseUrl}/sdk/v1/auth/session`, {
23016
23015
  method: "POST",
23017
23016
  headers: {
@@ -23033,7 +23032,6 @@ var ApiTokenAuthProvider = class {
23033
23032
  if (!this.sessionToken) {
23034
23033
  throw new AuthenticationError("No session token returned after key verification");
23035
23034
  }
23036
- console.log("SDK: Performing secondary validation...");
23037
23035
  const origin = typeof window !== "undefined" ? window.location.origin : "";
23038
23036
  const validateResponse = await fetch(`${baseUrl}/sdk/v1/auth/validate`, {
23039
23037
  method: "GET",
@@ -23051,9 +23049,9 @@ var ApiTokenAuthProvider = class {
23051
23049
  );
23052
23050
  }
23053
23051
  const validateData = await validateResponse.json();
23054
- console.log(`SDK: Secondary validation successful for "${validateData.app_name}"`);
23052
+ console.log(`Dora Cell SDK: Authenticated successfully`);
23055
23053
  if (validateData.features && !validateData.features.includes("voice")) {
23056
- console.warn('SDK: App token does not have the "voice" feature enabled.');
23054
+ console.warn('Dora Cell SDK: App token does not have the "voice" feature enabled.');
23057
23055
  }
23058
23056
  const actualResponseData = authData.data && typeof authData.data === "object" ? authData.data : authData;
23059
23057
  this.credentials = this.parseCredentials(actualResponseData);
@@ -23094,7 +23092,6 @@ var ApiTokenAuthProvider = class {
23094
23092
  if (!sipUri && extensions.length > 0) {
23095
23093
  const ext = extensions[0].extension;
23096
23094
  sipUri = `sip:${ext}@${sipDomain}`;
23097
- console.log(`SDK: Constructed SIP URI from extension: ${sipUri}`);
23098
23095
  } else if (!sipUri) {
23099
23096
  sipUri = "";
23100
23097
  }
@@ -23246,11 +23243,11 @@ var CallSession = class {
23246
23243
  };
23247
23244
  });
23248
23245
  this.session.on("ended", (evt) => {
23249
- console.log(`SDK: Call ended. Cause: ${evt?.cause || "Normal"}`);
23246
+ console.log(`Dora Cell SDK: Call ended (${evt?.cause || "Normal"})`);
23250
23247
  this.handleCallEnd(evt?.cause);
23251
23248
  });
23252
23249
  this.session.on("failed", (evt) => {
23253
- console.warn(`SDK: Call failed. Cause: ${evt?.cause || "Unknown failure"}`);
23250
+ console.warn(`Dora Cell SDK: Call failed (${evt?.cause || "Internal Error"})`);
23254
23251
  this.handleCallEnd(evt?.cause || "Call failed");
23255
23252
  });
23256
23253
  this.session.on("rejected", (evt) => {
@@ -23270,7 +23267,6 @@ var CallSession = class {
23270
23267
  const ssrc = report.ssrc;
23271
23268
  if (ssrc && !this.lastKnownSSRCs.has(ssrc)) {
23272
23269
  this.lastKnownSSRCs.add(ssrc);
23273
- console.log(`SDK: SSRC Switch Detected: ${ssrc}. Re-binding audio...`);
23274
23270
  setTimeout(() => this.reattachFromReceivers(pc), 200);
23275
23271
  }
23276
23272
  }
@@ -23289,7 +23285,6 @@ var CallSession = class {
23289
23285
  if (!pc) return;
23290
23286
  const liveAudioTracks = pc.getReceivers().map((r) => r.track).filter((t) => t && t.kind === "audio" && t.readyState === "live");
23291
23287
  if (liveAudioTracks.length > 0) {
23292
- console.log(`SDK: Audio Fix: Rebuilding stream from ${liveAudioTracks.length} receivers`);
23293
23288
  const newStream = new MediaStream(liveAudioTracks);
23294
23289
  this.remoteStreamValue = newStream;
23295
23290
  this.events.emit("call:stream", this, newStream);
@@ -23380,7 +23375,7 @@ var CallManager = class {
23380
23375
  const extension = options?.extension || this.getDefaultExtension();
23381
23376
  const sipDomain = this.extractDomain(this.credentials.sipUri);
23382
23377
  const sipTarget = formatPhoneToSIP(targetNumber, sipDomain);
23383
- console.log(`SDK: Calling ${sipTarget} using extension ${extension}...`);
23378
+ console.log(`Dora Cell SDK: Initiating call to ${targetNumber}...`);
23384
23379
  const session = this.ua.call(sipTarget, {
23385
23380
  mediaConstraints: options?.mediaConstraints || { audio: true },
23386
23381
  pcConfig: this.callConfig.pcConfig
@@ -23458,10 +23453,13 @@ var CallManager = class {
23458
23453
  this.pendingSession = null;
23459
23454
  }
23460
23455
  getDefaultExtension() {
23456
+ if (this.credentials.sipUri) {
23457
+ return this.extractExtension(this.credentials.sipUri);
23458
+ }
23461
23459
  if (this.credentials.extensions && this.credentials.extensions.length > 0) {
23462
23460
  return this.credentials.extensions[0].extension;
23463
23461
  }
23464
- return this.extractExtension(this.credentials.sipUri);
23462
+ return "unknown";
23465
23463
  }
23466
23464
  extractExtension(sipUri) {
23467
23465
  const match = sipUri.match(/sip:([^@]+)@/);
@@ -23571,30 +23569,23 @@ var DoraCell = class {
23571
23569
  if (this.authProvider instanceof ApiTokenAuthProvider) {
23572
23570
  const token = this.authProvider.getSessionToken();
23573
23571
  if (token) {
23574
- console.log("SDK: Session token acquired and set in API client");
23575
23572
  this.apiClient.setSessionToken(token);
23576
- } else {
23577
- console.warn("SDK: No session token found in auth provider");
23578
23573
  }
23579
23574
  }
23580
23575
  await this.getWallet().catch(() => {
23581
23576
  });
23582
23577
  if (!this.credentials?.extensions || this.credentials.extensions.length === 0) {
23583
- console.log("SDK: No extensions in auth response, fetching from API...");
23584
23578
  await this.fetchExtensions();
23585
23579
  }
23586
23580
  if (this.config.autoSelectExtension && this.credentials?.extensions && this.credentials.extensions.length > 0) {
23587
23581
  const primary = this.credentials.extensions.find((e) => e.isPrimary) || this.credentials.extensions[0];
23588
23582
  const domain = this.credentials.sipDomain || "64.227.10.164";
23589
23583
  this.credentials.sipUri = `sip:${primary.extension}@${domain}`;
23590
- console.log(`SDK: Auto-selected extension ${primary.extension}`);
23591
23584
  }
23592
23585
  if (this.credentials?.sipUri) {
23593
23586
  await this.initializeUserAgent();
23594
23587
  this.initializeCallManager();
23595
23588
  await this.waitForRegistration();
23596
- } else {
23597
- console.warn("SDK: No SIP URI available yet. UA initialization deferred.");
23598
23589
  }
23599
23590
  } catch (error) {
23600
23591
  const errorMessage = error instanceof Error ? error.message : "Unknown error";
@@ -23629,10 +23620,15 @@ var DoraCell = class {
23629
23620
  if (!this.credentials) {
23630
23621
  throw new ConnectionError("No credentials available");
23631
23622
  }
23623
+ this.connectionStatus = "connecting";
23624
+ this.emitConnectionStatus();
23632
23625
  if (this.ua) {
23633
- console.log("SDK: Stopping existing User Agent...");
23634
- this.ua.stop();
23626
+ try {
23627
+ this.ua.stop();
23628
+ } catch (e) {
23629
+ }
23635
23630
  this.ua = null;
23631
+ await new Promise((resolve) => setTimeout(resolve, 500));
23636
23632
  }
23637
23633
  try {
23638
23634
  const socket = new import_jssip.default.WebSocketInterface(this.credentials.wsUrl);
@@ -23651,10 +23647,8 @@ var DoraCell = class {
23651
23647
  pcConfig,
23652
23648
  instance_id: this.generateInstanceId()
23653
23649
  };
23654
- console.log("SDK: Initializing UA with config:", { ...uaConfig, password: "***" });
23655
23650
  this.ua = new import_jssip.default.UA(uaConfig);
23656
23651
  this.setupUserAgentHandlers();
23657
- console.log("SDK: Starting UA...");
23658
23652
  this.ua.start();
23659
23653
  if (this.callManager) {
23660
23654
  this.callManager.setUserAgent(this.ua);
@@ -23678,6 +23672,7 @@ var DoraCell = class {
23678
23672
  this.ua.on("registered", () => {
23679
23673
  this.connectionStatus = "registered";
23680
23674
  this.retryCount = 0;
23675
+ console.log(`Dora Cell SDK: Connected (${this.getDisplayName()})`);
23681
23676
  this.emitConnectionStatus();
23682
23677
  });
23683
23678
  this.ua.on("registrationFailed", (e) => {
@@ -23692,9 +23687,7 @@ var DoraCell = class {
23692
23687
  });
23693
23688
  this.ua.on("newRTCSession", (e) => {
23694
23689
  const session = e.session;
23695
- console.log(`SDK: New session detected (${session.direction}):`, session.remote_identity?.uri?.toString());
23696
23690
  if (session.direction === "incoming") {
23697
- console.log("SDK: Handling incoming call event");
23698
23691
  this.callManager?.handleIncomingCall(session);
23699
23692
  }
23700
23693
  });
@@ -23764,7 +23757,6 @@ var DoraCell = class {
23764
23757
  return { balance: 0, currency: "NGN" };
23765
23758
  }
23766
23759
  try {
23767
- console.log("SDK: Fetching wallet balance...");
23768
23760
  const response = await this.apiClient.get("/wallets");
23769
23761
  const wallets = Array.isArray(response) ? response : response.data || [];
23770
23762
  if (wallets.length === 0) {
@@ -23777,7 +23769,6 @@ var DoraCell = class {
23777
23769
  balance: parseFloat(primary.balance || primary.amount || "0"),
23778
23770
  currency: primary.currency || "NGN"
23779
23771
  };
23780
- console.log("SDK: Wallet balance fetched successfully, userId:", this.userId);
23781
23772
  return result;
23782
23773
  } catch (error) {
23783
23774
  console.error("SDK: Failed to fetch wallet:", error);
@@ -23799,7 +23790,6 @@ var DoraCell = class {
23799
23790
  }
23800
23791
  try {
23801
23792
  const path = this.userId ? `/user/${this.userId}/extensions` : "/extensions";
23802
- console.log(`SDK: Fetching extensions from: ${path}`);
23803
23793
  const response = await this.apiClient.get(path);
23804
23794
  const extensions = response.data || response.extensions || response;
23805
23795
  if (this.credentials && Array.isArray(extensions)) {
@@ -23807,7 +23797,6 @@ var DoraCell = class {
23807
23797
  }
23808
23798
  return Array.isArray(extensions) ? extensions : [];
23809
23799
  } catch (error) {
23810
- console.error("SDK: Failed to fetch extensions:", error);
23811
23800
  return this.credentials?.extensions || [];
23812
23801
  }
23813
23802
  }
@@ -23821,11 +23810,11 @@ var DoraCell = class {
23821
23810
  * Update active extension and re-initialize SIP connection
23822
23811
  */
23823
23812
  async setExtension(extension) {
23824
- console.log(`SDK: Switching to extension ${extension}...`);
23825
23813
  if (this.credentials) {
23826
23814
  const domain = this.credentials.sipDomain || "64.227.10.164";
23827
23815
  this.credentials.sipUri = `sip:${extension}@${domain}`;
23828
23816
  await this.initializeUserAgent();
23817
+ await this.waitForRegistration();
23829
23818
  this.emitConnectionStatus(this.connectionStatus);
23830
23819
  }
23831
23820
  }
@@ -23859,10 +23848,14 @@ var DoraCell = class {
23859
23848
  }
23860
23849
  // Helper methods
23861
23850
  emitConnectionStatus(error) {
23862
- const extension = this.credentials?.extensions?.[0]?.extension || (this.credentials?.sipUri ? extractNumberFromSipUri(this.credentials.sipUri) : void 0);
23851
+ let activeExt = this.credentials?.sipUri ? extractNumberFromSipUri(this.credentials.sipUri) : void 0;
23852
+ if (!activeExt && this.credentials?.extensions && this.credentials.extensions.length > 0) {
23853
+ const primary = this.credentials.extensions.find((e) => e.isPrimary) || this.credentials.extensions[0];
23854
+ activeExt = primary.extension;
23855
+ }
23863
23856
  const state = {
23864
23857
  status: this.connectionStatus,
23865
- extension,
23858
+ extension: activeExt,
23866
23859
  error
23867
23860
  };
23868
23861
  this.events.emit("connection:status", state);
@@ -23888,6 +23881,12 @@ var DoraCell = class {
23888
23881
  ];
23889
23882
  }
23890
23883
  getDisplayName() {
23884
+ const currentExt = this.credentials?.sipUri ? extractNumberFromSipUri(this.credentials.sipUri) : null;
23885
+ if (currentExt && this.credentials?.extensions) {
23886
+ const found = this.credentials.extensions.find((e) => e.extension === currentExt);
23887
+ if (found?.displayName) return found.displayName;
23888
+ if (found?.extension) return `Ext ${found.extension}`;
23889
+ }
23891
23890
  if (this.credentials?.extensions?.[0]?.displayName) {
23892
23891
  return this.credentials.extensions[0].displayName;
23893
23892
  }