@matrix-widget-toolkit/api 3.0.1 → 3.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.
@@ -1047,8 +1047,7 @@ var WidgetApiImpl = /** @class */ (function () {
1047
1047
  this.matrixWidgetApi = matrixWidgetApi$1;
1048
1048
  this.widgetId = widgetId;
1049
1049
  this.widgetParameters = widgetParameters;
1050
- var eventName = "action:".concat(matrixWidgetApi.WidgetApiToWidgetAction.SendEvent);
1051
- this.events$ = rxjs.fromEvent(this.matrixWidgetApi, eventName, function (event) {
1050
+ this.events$ = rxjs.fromEvent(this.matrixWidgetApi, "action:".concat(matrixWidgetApi.WidgetApiToWidgetAction.SendEvent), function (event) {
1052
1051
  event.preventDefault();
1053
1052
  try {
1054
1053
  _this.matrixWidgetApi.transport.reply(event.detail, {});
@@ -1058,6 +1057,16 @@ var WidgetApiImpl = /** @class */ (function () {
1058
1057
  }
1059
1058
  return event;
1060
1059
  }).pipe(rxjs.share());
1060
+ this.toDeviceMessages$ = rxjs.fromEvent(this.matrixWidgetApi, 'action:send_to_device', function (event) {
1061
+ event.preventDefault();
1062
+ try {
1063
+ matrixWidgetApi$1.transport.reply(event.detail, {});
1064
+ }
1065
+ catch (_) {
1066
+ // Ignore errors while replying
1067
+ }
1068
+ return event;
1069
+ }).pipe(rxjs.share());
1061
1070
  this.initialCapabilities = __spreadArray(__spreadArray([], capabilities, true), (supportStandalone ? [] : [matrixWidgetApi.MatrixCapabilities.RequiresClient]), true);
1062
1071
  }
1063
1072
  /**
@@ -1432,6 +1441,23 @@ var WidgetApiImpl = /** @class */ (function () {
1432
1441
  });
1433
1442
  });
1434
1443
  };
1444
+ /** {@inheritDoc WidgetApi.sendToDeviceMessage} */
1445
+ WidgetApiImpl.prototype.sendToDeviceMessage = function (eventType, encrypted, content) {
1446
+ return __awaiter(this, void 0, void 0, function () {
1447
+ return __generator(this, function (_a) {
1448
+ switch (_a.label) {
1449
+ case 0: return [4 /*yield*/, this.matrixWidgetApi.sendToDevice(eventType, encrypted, content)];
1450
+ case 1:
1451
+ _a.sent();
1452
+ return [2 /*return*/];
1453
+ }
1454
+ });
1455
+ });
1456
+ };
1457
+ /** {@inheritDoc WidgetApi.observeToDeviceMessages} */
1458
+ WidgetApiImpl.prototype.observeToDeviceMessages = function (eventType) {
1459
+ return this.toDeviceMessages$.pipe(rxjs.map(function (e) { return e.detail.data; }), rxjs.filter(function (e) { return e.type === eventType; }));
1460
+ };
1435
1461
  /** {@inheritDoc WidgetApi.openModal} */
1436
1462
  WidgetApiImpl.prototype.openModal = function (pathName, name, options) {
1437
1463
  return __awaiter(this, void 0, void 0, function () {
@@ -1592,6 +1618,20 @@ var WidgetApiImpl = /** @class */ (function () {
1592
1618
  });
1593
1619
  });
1594
1620
  };
1621
+ /** {@inheritdoc WidgetApi.observeTurnServers} */
1622
+ WidgetApiImpl.prototype.observeTurnServers = function () {
1623
+ return rxjs.from(this.matrixWidgetApi.getTurnServers()).pipe(
1624
+ // For some reason a different naming was chosen for the API, but
1625
+ // we already convert them to the right type for WebRTC consumers.
1626
+ rxjs.map(function (_a) {
1627
+ var uris = _a.uris, username = _a.username, password = _a.password;
1628
+ return ({
1629
+ urls: uris,
1630
+ username: username,
1631
+ credential: password,
1632
+ });
1633
+ }));
1634
+ };
1595
1635
  return WidgetApiImpl;
1596
1636
  }());
1597
1637
 
@@ -1045,8 +1045,7 @@ var WidgetApiImpl = /** @class */ (function () {
1045
1045
  this.matrixWidgetApi = matrixWidgetApi;
1046
1046
  this.widgetId = widgetId;
1047
1047
  this.widgetParameters = widgetParameters;
1048
- var eventName = "action:".concat(WidgetApiToWidgetAction.SendEvent);
1049
- this.events$ = fromEvent(this.matrixWidgetApi, eventName, function (event) {
1048
+ this.events$ = fromEvent(this.matrixWidgetApi, "action:".concat(WidgetApiToWidgetAction.SendEvent), function (event) {
1050
1049
  event.preventDefault();
1051
1050
  try {
1052
1051
  _this.matrixWidgetApi.transport.reply(event.detail, {});
@@ -1056,6 +1055,16 @@ var WidgetApiImpl = /** @class */ (function () {
1056
1055
  }
1057
1056
  return event;
1058
1057
  }).pipe(share());
1058
+ this.toDeviceMessages$ = fromEvent(this.matrixWidgetApi, 'action:send_to_device', function (event) {
1059
+ event.preventDefault();
1060
+ try {
1061
+ matrixWidgetApi.transport.reply(event.detail, {});
1062
+ }
1063
+ catch (_) {
1064
+ // Ignore errors while replying
1065
+ }
1066
+ return event;
1067
+ }).pipe(share());
1059
1068
  this.initialCapabilities = __spreadArray(__spreadArray([], capabilities, true), (supportStandalone ? [] : [MatrixCapabilities.RequiresClient]), true);
1060
1069
  }
1061
1070
  /**
@@ -1430,6 +1439,23 @@ var WidgetApiImpl = /** @class */ (function () {
1430
1439
  });
1431
1440
  });
1432
1441
  };
1442
+ /** {@inheritDoc WidgetApi.sendToDeviceMessage} */
1443
+ WidgetApiImpl.prototype.sendToDeviceMessage = function (eventType, encrypted, content) {
1444
+ return __awaiter(this, void 0, void 0, function () {
1445
+ return __generator(this, function (_a) {
1446
+ switch (_a.label) {
1447
+ case 0: return [4 /*yield*/, this.matrixWidgetApi.sendToDevice(eventType, encrypted, content)];
1448
+ case 1:
1449
+ _a.sent();
1450
+ return [2 /*return*/];
1451
+ }
1452
+ });
1453
+ });
1454
+ };
1455
+ /** {@inheritDoc WidgetApi.observeToDeviceMessages} */
1456
+ WidgetApiImpl.prototype.observeToDeviceMessages = function (eventType) {
1457
+ return this.toDeviceMessages$.pipe(map(function (e) { return e.detail.data; }), filter(function (e) { return e.type === eventType; }));
1458
+ };
1433
1459
  /** {@inheritDoc WidgetApi.openModal} */
1434
1460
  WidgetApiImpl.prototype.openModal = function (pathName, name, options) {
1435
1461
  return __awaiter(this, void 0, void 0, function () {
@@ -1590,6 +1616,20 @@ var WidgetApiImpl = /** @class */ (function () {
1590
1616
  });
1591
1617
  });
1592
1618
  };
1619
+ /** {@inheritdoc WidgetApi.observeTurnServers} */
1620
+ WidgetApiImpl.prototype.observeTurnServers = function () {
1621
+ return from(this.matrixWidgetApi.getTurnServers()).pipe(
1622
+ // For some reason a different naming was chosen for the API, but
1623
+ // we already convert them to the right type for WebRTC consumers.
1624
+ map(function (_a) {
1625
+ var uris = _a.uris, username = _a.username, password = _a.password;
1626
+ return ({
1627
+ urls: uris,
1628
+ username: username,
1629
+ credential: password,
1630
+ });
1631
+ }));
1632
+ };
1593
1633
  return WidgetApiImpl;
1594
1634
  }());
1595
1635
 
package/build/index.d.ts CHANGED
@@ -395,6 +395,29 @@ export declare type StateEvent<T = unknown> = Omit<IRoomEvent, 'content' | 'unsi
395
395
  content: T;
396
396
  };
397
397
 
398
+ /**
399
+ * Generic type for to device message events.
400
+ */
401
+ export declare type ToDeviceMessageEvent<T = unknown> = {
402
+ type: string;
403
+ sender: string;
404
+ encrypted: boolean;
405
+ content: T;
406
+ };
407
+
408
+ /**
409
+ * WebRTC Ice server credentials like turn servers, type is compatible to
410
+ * `RTCIceServer` from WebRTC.
411
+ */
412
+ export declare type TurnServer = {
413
+ /** One or more URLs for this turn server. */
414
+ urls: string[];
415
+ /** Username for this turn server. */
416
+ username: string;
417
+ /** Credentials for this turn server. */
418
+ credential: string;
419
+ };
420
+
398
421
  /**
399
422
  * The capability that needs to be requested in order to navigate to another room.
400
423
  */
@@ -420,13 +443,13 @@ export declare type WidgetApi = {
420
443
  */
421
444
  getWidgetConfig<T extends IWidgetApiRequestData>(): Readonly<WidgetConfig<T> | undefined>;
422
445
  /**
423
- * Rerequests the initialy in the constructor passed capabilities.
446
+ * Rerequests capabilities initially passed in the constructor.
424
447
  *
425
448
  * This is useful in case the user denied one or all of them.
426
449
  */
427
450
  rerequestInitialCapabilities(): Promise<void>;
428
451
  /**
429
- * True, if the initial capabilties passed via the constructor were granted.
452
+ * True, if the initial capabilities passed via the constructor were granted.
430
453
  */
431
454
  hasInitialCapabilities(): boolean;
432
455
  /**
@@ -526,7 +549,7 @@ export declare type WidgetApi = {
526
549
  *
527
550
  * @param eventType - The type of the event to receive.
528
551
  * @param options - Options for receiving the room event.
529
- * Use `messageType` to receive events with a specifc
552
+ * Use `messageType` to receive events with a specific
530
553
  * message type.
531
554
  * Use `roomIds` to receive the state events from other
532
555
  * rooms.
@@ -548,7 +571,7 @@ export declare type WidgetApi = {
548
571
  *
549
572
  * @param eventType - The type of the event to receive.
550
573
  * @param options - Options for receiving the room event.
551
- * Use `messageType` to receive events with a specifc
574
+ * Use `messageType` to receive events with a specific
552
575
  * message type.
553
576
  * Use `roomIds` to receive the state events from other
554
577
  * rooms.
@@ -605,6 +628,27 @@ export declare type WidgetApi = {
605
628
  chunk: Array<RoomEvent | StateEvent>;
606
629
  nextToken?: string;
607
630
  }>;
631
+ /**
632
+ * Send a message to a device of a user (or multiple users / devices).
633
+ *
634
+ * @param eventType - The type of the event.
635
+ * @param encrypted - Whether the event should be encrypted.
636
+ * @param content - The content to send. This is a map of user ids, to device
637
+ * ids, to the content that should be send. It is possible to
638
+ * specify a `'*'` device, to send the content to all devices
639
+ * of a user.
640
+ */
641
+ sendToDeviceMessage<T>(eventType: string, encrypted: boolean, content: {
642
+ [userId: string]: {
643
+ [deviceId: string | '*']: T;
644
+ };
645
+ }): Promise<void>;
646
+ /**
647
+ * Observes all to device messages send to the current device.
648
+ *
649
+ * @param eventType - The type of the event.
650
+ */
651
+ observeToDeviceMessages<T>(eventType: string): Observable<ToDeviceMessageEvent<T>>;
608
652
  /**
609
653
  * Open a new modal, wait until the modal closes, and return the result.
610
654
  *
@@ -678,6 +722,11 @@ export declare type WidgetApi = {
678
722
  * old one expired.
679
723
  */
680
724
  requestOpenIDConnectToken(): Promise<IOpenIDCredentials>;
725
+ /**
726
+ * Returns an observable containing WebRTC Ice server credentials, like turn
727
+ * servers, if available.
728
+ */
729
+ observeTurnServers(): Observable<TurnServer>;
681
730
  };
682
731
 
683
732
  /**
@@ -717,6 +766,7 @@ export declare class WidgetApiImpl implements WidgetApi {
717
766
  private outstandingOpenIDConnectTokenRequest;
718
767
  private cachedOpenIdToken;
719
768
  private readonly events$;
769
+ private readonly toDeviceMessages$;
720
770
  private readonly initialCapabilities;
721
771
  constructor(
722
772
  /**
@@ -797,6 +847,14 @@ export declare class WidgetApiImpl implements WidgetApi {
797
847
  chunk: Array<RoomEvent | StateEvent>;
798
848
  nextToken?: string;
799
849
  }>;
850
+ /** {@inheritDoc WidgetApi.sendToDeviceMessage} */
851
+ sendToDeviceMessage<T>(eventType: string, encrypted: boolean, content: {
852
+ [userId: string]: {
853
+ [deviceId: string | '*']: T;
854
+ };
855
+ }): Promise<void>;
856
+ /** {@inheritDoc WidgetApi.observeToDeviceMessages} */
857
+ observeToDeviceMessages<T>(eventType: string): Observable<ToDeviceMessageEvent<T>>;
800
858
  /** {@inheritDoc WidgetApi.openModal} */
801
859
  openModal<T extends Record<string, unknown> = Record<string, unknown>, U extends IModalWidgetCreateData = IModalWidgetCreateData>(pathName: string, name: string, options?: {
802
860
  buttons?: IModalWidgetOpenRequestDataButton[];
@@ -813,6 +871,8 @@ export declare class WidgetApiImpl implements WidgetApi {
813
871
  /** {@inheritdoc WidgetApi.requestOpenIDConnectToken} */
814
872
  requestOpenIDConnectToken(): Promise<IOpenIDCredentials>;
815
873
  private requestOpenIDConnectTokenInternal;
874
+ /** {@inheritdoc WidgetApi.observeTurnServers} */
875
+ observeTurnServers(): Observable<TurnServer>;
816
876
  }
817
877
 
818
878
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matrix-widget-toolkit/api",
3
- "version": "3.0.1",
3
+ "version": "3.1.0",
4
4
  "description": "A simplified layer on top of matrix-widget-api to use build widgets.",
5
5
  "author": "Nordeck IT + Consulting GmbH",
6
6
  "license": "Apache-2.0",