@matrix-widget-toolkit/api 1.0.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/README.md +148 -0
- package/build/cjs/index.js +1651 -0
- package/build/esm/index.js +1620 -0
- package/build/index.d.ts +959 -0
- package/package.json +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# `@matrix-widget-toolkit/api`
|
|
2
|
+
|
|
3
|
+
This is package that wraps `matrix-widget-api` to provide a more convenient API.
|
|
4
|
+
For now, the API includes:
|
|
5
|
+
|
|
6
|
+
- A `WidgetApi` interface with more advanced features.
|
|
7
|
+
- Helper functions for registering the Widget in rooms.
|
|
8
|
+
- Types and helper functions for common events, like power levels, room members, redactions, and relations.
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
Install it with:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
yarn add @matrix-widget-toolkit/api
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
### Creating a `WidgetApi` instance
|
|
19
|
+
|
|
20
|
+
Creating the `WidgetApi` instance should be done as early as possible in your code because otherwise the widget misses the connection estabilshment by the widget host (especially on Safari).
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { WidgetApiImpl } from '@matrix-widget-toolkit/api';
|
|
24
|
+
|
|
25
|
+
const widgetApi = await WidgetApiImpl.create();
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Request capabilities
|
|
29
|
+
|
|
30
|
+
You can request additional capabilities at any time using `requestCapabilities()`.
|
|
31
|
+
A call returns a promise that resolves once the user approved the capabilities.
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { WidgetEventCapability } from 'matrix-widget-api';
|
|
35
|
+
|
|
36
|
+
await widgetApi.requestCapabilities([
|
|
37
|
+
WidgetEventCapability.forStateEvent(
|
|
38
|
+
EventDirection.Send,
|
|
39
|
+
STATE_EVENT_POWER_LEVELS
|
|
40
|
+
),
|
|
41
|
+
]);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Receiving state events
|
|
45
|
+
|
|
46
|
+
You can read state events once from the current room using `receiveStateEvents()`:
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
const events = await widgetApi.receiveStateEvents(STATE_EVENT_ROOM_NAME);
|
|
50
|
+
|
|
51
|
+
// events is an array of state events
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
> **Warning** Don't assume the type of the returned events, always validate that they have the expected schema.
|
|
55
|
+
|
|
56
|
+
### Observing state event changes
|
|
57
|
+
|
|
58
|
+
You can listen to state event changes using `observeStateEvents()`.
|
|
59
|
+
Note that the initial state is also returned.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
const subscription = widgetApi
|
|
63
|
+
.observeStateEvents(STATE_EVENT_POWER_LEVELS)
|
|
64
|
+
.subscribe(async (event) => {
|
|
65
|
+
// The callback is always called if a state event changes
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// Don't forget to unsubscribe later
|
|
69
|
+
subscription.unsubscribe();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Sending state events
|
|
73
|
+
|
|
74
|
+
You can send state events using `sendStateEvent()`.
|
|
75
|
+
It returns a promise that resolves once the event has been sent.
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
await widgetApi.sendStateEvent('m.room.topic', {
|
|
79
|
+
topic: 'A brief description',
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
> **Warning** Avoid sending event content that has no changes, as the returned promise doesn't resolve in this case.
|
|
84
|
+
|
|
85
|
+
### Receiving room events
|
|
86
|
+
|
|
87
|
+
You can read the current room events using `receiveRoomEvents()`.
|
|
88
|
+
Note that this API only returns the events that are currently in the view of the client.
|
|
89
|
+
There is no guarantees to read all events from the room.
|
|
90
|
+
Therefore this function is of limited use.
|
|
91
|
+
For more details and solutions, see [MSC3869](https://github.com/nordeck/matrix-spec-proposals/blob/nic/feat/widgetapi-read-relations/proposals/3869-widgetapi-read-event-relations.md).
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
const events = await widgetApi.receiveRoomEvents('com.example.test');
|
|
95
|
+
|
|
96
|
+
// events is an array of room events
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Observing room events
|
|
100
|
+
|
|
101
|
+
You can listen to room events using `observeRoomEvents()`.
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
const subscription = widgetApi
|
|
105
|
+
.observeRoomEvents(ROOM_EVENT_REACTION)
|
|
106
|
+
.subscribe(async (event) => {
|
|
107
|
+
// Callback is called every time a room event is received
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
// Don't forget to unsubscribe later
|
|
111
|
+
subscription.unsubscribe();
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Sending room events
|
|
115
|
+
|
|
116
|
+
You can send room events using `sendRoomEvent()`.
|
|
117
|
+
It returns a promise that resolves once the event has been sent.
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
await widgetApi.sendRoomEvent('m.room.redaction', {
|
|
121
|
+
redacts: '$event-id',
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Read related events
|
|
126
|
+
|
|
127
|
+
You can use `readEventRelations()` to read events releated to an event via `m.relates_to`.
|
|
128
|
+
It provides an API with cursors that you can use to page through the results.
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
const result = await widgetApi.readEventRelations(eventId, {
|
|
132
|
+
limit: 50,
|
|
133
|
+
from,
|
|
134
|
+
relationType: 'm.annotation',
|
|
135
|
+
eventType: 'm.reaction',
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// result contains the events and cursor for paging further though the events
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Opening Modal Widgets
|
|
142
|
+
|
|
143
|
+
You can open modal widgets using `openModal()`.
|
|
144
|
+
It returns a promise that resolves with the result of the modal.
|
|
145
|
+
|
|
146
|
+
Inside the modal widget, you can use `observeModalButtons()` to listen to clicks on the bottom buttons of the modal.
|
|
147
|
+
You can use `setModalButtonEnabled()` to disable buttons from within the widget.
|
|
148
|
+
Once you are done, you can call `closeModal()` to close the modal and pass the results back to the main widget.
|