@trycourier/courier-js 2.0.12 → 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.mjs CHANGED
@@ -89,6 +89,10 @@ const _UUID = class _UUID {
89
89
  };
90
90
  __publicField(_UUID, "ALPHABET", "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict");
91
91
  let UUID = _UUID;
92
+ const HTTP_HEADER_KEY = "x-courier-ua";
93
+ const SDK_KEY = "sdk";
94
+ const SDK_VERSION_KEY = "sdkv";
95
+ const CLIENT_ID_KEY = "cid";
92
96
  class CourierRequestError extends Error {
93
97
  constructor(code, message, type) {
94
98
  super(message);
@@ -118,10 +122,12 @@ Response JSON: ${JSON.stringify(data.response, null, 2)}
118
122
  async function http(props) {
119
123
  const validCodes = props.validCodes ?? [200];
120
124
  const uid = props.options.showLogs ? UUID.nanoid() : void 0;
125
+ const courierUserAgentHeader = props.options.courierUserAgent.toHttpHeaderValue();
121
126
  const request = new Request(props.url, {
122
127
  method: props.method,
123
128
  headers: {
124
129
  "Content-Type": "application/json",
130
+ [HTTP_HEADER_KEY]: courierUserAgentHeader,
125
131
  ...props.headers
126
132
  },
127
133
  body: props.body ? JSON.stringify(props.body) : void 0
@@ -168,6 +174,7 @@ async function http(props) {
168
174
  }
169
175
  async function graphql(props) {
170
176
  const uid = props.options.showLogs ? UUID.nanoid() : void 0;
177
+ const courierUserAgentHeader = props.options.courierUserAgent.toHttpHeaderValue();
171
178
  if (uid) {
172
179
  logRequest(props.options.logger, uid, "GraphQL", {
173
180
  url: props.url,
@@ -180,6 +187,7 @@ async function graphql(props) {
180
187
  method: "POST",
181
188
  headers: {
182
189
  "Content-Type": "application/json",
190
+ [HTTP_HEADER_KEY]: courierUserAgentHeader,
183
191
  ...props.headers
184
192
  },
185
193
  body: JSON.stringify({
@@ -376,6 +384,9 @@ const _CourierSocket = class _CourierSocket {
376
384
  get logger() {
377
385
  return this.options.logger;
378
386
  }
387
+ get courierUserAgent() {
388
+ return this.options.courierUserAgent;
389
+ }
379
390
  /**
380
391
  * Whether the WebSocket connection is currently being established.
381
392
  */
@@ -398,7 +409,9 @@ const _CourierSocket = class _CourierSocket {
398
409
  const accessToken = this.options.accessToken;
399
410
  const connectionId = this.options.connectionId;
400
411
  const userId = this.userId;
401
- return `${this.url}?auth=${accessToken}&cid=${connectionId}&iwpv=${INBOX_WIRE_PROTOCOL_VERSION}&userId=${userId}`;
412
+ const sdkName = this.courierUserAgent.getUserAgentInfo()[SDK_KEY];
413
+ const sdkVersion = this.courierUserAgent.getUserAgentInfo()[SDK_VERSION_KEY];
414
+ return `${this.url}?auth=${accessToken}&cid=${connectionId}&iwpv=${INBOX_WIRE_PROTOCOL_VERSION}&userId=${userId}&${SDK_KEY}=${sdkName}&${SDK_VERSION_KEY}=${sdkVersion}`;
402
415
  }
403
416
  /**
404
417
  * Parses the Retry-After time from the WebSocket close event reason,
@@ -711,9 +724,13 @@ const _CourierInboxSocket = class _CourierInboxSocket extends CourierSocket {
711
724
  * from the Courier WebSocket server.
712
725
  *
713
726
  * @param listener The listener function
727
+ * @returns A function that can be called to remove this specific listener
714
728
  */
715
729
  addMessageEventListener(listener) {
716
730
  this.messageEventListeners.push(listener);
731
+ return () => {
732
+ this.removeMessageEventListener(listener);
733
+ };
717
734
  }
718
735
  /**
719
736
  * Send a ping message to the server.
@@ -792,7 +809,20 @@ const _CourierInboxSocket = class _CourierInboxSocket extends CourierSocket {
792
809
  * Removes all message event listeners.
793
810
  */
794
811
  clearMessageEventListeners() {
795
- this.messageEventListeners = [];
812
+ while (this.messageEventListeners.length > 0) {
813
+ this.messageEventListeners.pop();
814
+ }
815
+ }
816
+ /**
817
+ * Remove the message event listener specified.
818
+ *
819
+ * This is the same listener function passed to {@link addMessageEventListener}.
820
+ */
821
+ removeMessageEventListener(listener) {
822
+ const index = this.messageEventListeners.indexOf(listener);
823
+ if (index > -1) {
824
+ this.messageEventListeners.splice(index, 1);
825
+ }
796
826
  }
797
827
  static isInboxMessageEvent(event) {
798
828
  return Object.values(InboxMessageEvent).includes(event);
@@ -1425,19 +1455,52 @@ class TrackingClient extends Client {
1425
1455
  });
1426
1456
  }
1427
1457
  }
1428
- class CourierClient extends Client {
1458
+ class CourierUserAgent {
1459
+ /**
1460
+ * Create User Agent info
1461
+ * @param clientId client ID for this session
1462
+ * @param sdkName identifier of the SDK making requests to the Courier backend
1463
+ * @param sdkVersion version of the SDK making requests to the Courier backend
1464
+ */
1465
+ constructor(clientId, sdkName, sdkVersion) {
1466
+ this.clientId = clientId;
1467
+ this.sdkName = sdkName;
1468
+ this.sdkVersion = sdkVersion;
1469
+ }
1470
+ /** Get the telemetry payload as a JSON-serializable object. */
1471
+ getUserAgentInfo() {
1472
+ return {
1473
+ [SDK_KEY]: this.sdkName,
1474
+ [SDK_VERSION_KEY]: this.sdkVersion,
1475
+ [CLIENT_ID_KEY]: this.clientId
1476
+ };
1477
+ }
1478
+ /** Get the telemetry payload as a comma-separated string, where keys and values are joined by `=`. */
1479
+ toHttpHeaderValue() {
1480
+ return Object.entries(this.getUserAgentInfo()).map(([key, value]) => `${key}=${value}`).join(",");
1481
+ }
1482
+ }
1483
+ const _CourierClient = class _CourierClient extends Client {
1429
1484
  constructor(props) {
1430
1485
  var _a, _b;
1431
1486
  const showLogs = props.showLogs !== void 0 ? props.showLogs : false;
1487
+ const connectionId = UUID.nanoid();
1432
1488
  const baseOptions = {
1433
1489
  ...props,
1434
1490
  showLogs,
1491
+ connectionId,
1435
1492
  apiUrls: props.apiUrls || getCourierApiUrls(),
1436
1493
  accessToken: props.jwt ?? props.publicApiKey
1437
1494
  };
1495
+ const courierUserAgent = new CourierUserAgent(
1496
+ connectionId,
1497
+ props.courierUserAgentName || _CourierClient.COURIER_JS_NAME,
1498
+ props.courierUserAgentVersion || _CourierClient.COURIER_JS_VERSION
1499
+ );
1438
1500
  super({
1439
1501
  ...baseOptions,
1440
1502
  logger: new Logger(baseOptions.showLogs),
1503
+ courierUserAgent,
1441
1504
  apiUrls: getCourierApiUrls(baseOptions.apiUrls)
1442
1505
  });
1443
1506
  __publicField(this, "tokens");
@@ -1466,7 +1529,15 @@ class CourierClient extends Client {
1466
1529
  );
1467
1530
  }
1468
1531
  }
1469
- }
1532
+ };
1533
+ /** User-agent reporting name of the courier-js package. */
1534
+ __publicField(_CourierClient, "COURIER_JS_NAME", "courier-js");
1535
+ /**
1536
+ * User agent reporting version of the courier-js package.
1537
+ * Inlined from package.json at build time.
1538
+ */
1539
+ __publicField(_CourierClient, "COURIER_JS_VERSION", "2.1.1");
1540
+ let CourierClient = _CourierClient;
1470
1541
  class AuthenticationListener {
1471
1542
  constructor(callback) {
1472
1543
  __publicField(this, "callback");
@@ -1486,6 +1557,18 @@ const _Courier = class _Courier {
1486
1557
  * The Courier client instance
1487
1558
  */
1488
1559
  __publicField(this, "instanceClient");
1560
+ /**
1561
+ * Client's name reported in the user agent to the Courier backend.
1562
+ *
1563
+ * Other Courier SDKs calling APIs though the courier-js should set this property.
1564
+ */
1565
+ __publicField(this, "courierUserAgentName");
1566
+ /**
1567
+ * Client's version reported in the user agent to the Courier backend.
1568
+ *
1569
+ * Other Courier SDKs calling APIs though the courier-js should set this property.
1570
+ */
1571
+ __publicField(this, "courierUserAgentVersion");
1489
1572
  /**
1490
1573
  * The pagination limit (min: 1, max: 100)
1491
1574
  */
@@ -1527,8 +1610,11 @@ const _Courier = class _Courier {
1527
1610
  this.instanceClient.options.logger.warn("Sign in called but there is already a user signed in. Signing out the current user.");
1528
1611
  this.signOut();
1529
1612
  }
1530
- const connectionId = props.connectionId ?? UUID.nanoid();
1531
- this.instanceClient = new CourierClient({ ...props, connectionId });
1613
+ this.instanceClient = new CourierClient({
1614
+ ...props,
1615
+ courierUserAgentName: this.courierUserAgentName,
1616
+ courierUserAgentVersion: this.courierUserAgentVersion
1617
+ });
1532
1618
  this.notifyAuthenticationListeners({ userId: props.userId });
1533
1619
  }
1534
1620
  /**