@xmtp/browser-sdk 0.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.
@@ -0,0 +1,169 @@
1
+ import type {
2
+ WasmConsentState,
3
+ WasmEncodedContent,
4
+ WasmGroup,
5
+ WasmGroupMember,
6
+ } from "@xmtp/wasm-bindings";
7
+ import {
8
+ fromSafeListMessagesOptions,
9
+ type SafeListMessagesOptions,
10
+ } from "@/utils/conversions";
11
+ import type { WorkerClient } from "@/WorkerClient";
12
+
13
+ export class WorkerConversation {
14
+ // eslint-disable-next-line no-unused-private-class-members
15
+ #client: WorkerClient;
16
+
17
+ #group: WasmGroup;
18
+
19
+ constructor(client: WorkerClient, group: WasmGroup) {
20
+ this.#client = client;
21
+ this.#group = group;
22
+ }
23
+
24
+ get id() {
25
+ return this.#group.id();
26
+ }
27
+
28
+ get name() {
29
+ return this.#group.group_name();
30
+ }
31
+
32
+ async updateName(name: string) {
33
+ return this.#group.update_group_name(name);
34
+ }
35
+
36
+ get imageUrl() {
37
+ return this.#group.group_image_url_square();
38
+ }
39
+
40
+ async updateImageUrl(imageUrl: string) {
41
+ return this.#group.update_group_image_url_square(imageUrl);
42
+ }
43
+
44
+ get description() {
45
+ return this.#group.group_description();
46
+ }
47
+
48
+ async updateDescription(description: string) {
49
+ return this.#group.update_group_description(description);
50
+ }
51
+
52
+ get pinnedFrameUrl() {
53
+ return this.#group.group_pinned_frame_url();
54
+ }
55
+
56
+ async updatePinnedFrameUrl(pinnedFrameUrl: string) {
57
+ return this.#group.update_group_pinned_frame_url(pinnedFrameUrl);
58
+ }
59
+
60
+ get isActive() {
61
+ return this.#group.is_active();
62
+ }
63
+
64
+ get addedByInboxId() {
65
+ return this.#group.added_by_inbox_id();
66
+ }
67
+
68
+ get createdAtNs() {
69
+ return this.#group.created_at_ns();
70
+ }
71
+
72
+ get metadata() {
73
+ const metadata = this.#group.group_metadata();
74
+ return {
75
+ creatorInboxId: metadata.creator_inbox_id(),
76
+ conversationType: metadata.conversation_type(),
77
+ };
78
+ }
79
+
80
+ async members() {
81
+ return this.#group.list_members() as Promise<WasmGroupMember[]>;
82
+ }
83
+
84
+ get admins() {
85
+ return this.#group.admin_list();
86
+ }
87
+
88
+ get superAdmins() {
89
+ return this.#group.super_admin_list();
90
+ }
91
+
92
+ get permissions() {
93
+ const permissions = this.#group.group_permissions();
94
+ return {
95
+ policyType: permissions.policy_type(),
96
+ policySet: permissions.policy_set(),
97
+ };
98
+ }
99
+
100
+ isAdmin(inboxId: string) {
101
+ return this.#group.is_admin(inboxId);
102
+ }
103
+
104
+ isSuperAdmin(inboxId: string) {
105
+ return this.#group.is_super_admin(inboxId);
106
+ }
107
+
108
+ async sync() {
109
+ return this.#group.sync();
110
+ }
111
+
112
+ async addMembers(accountAddresses: string[]) {
113
+ return this.#group.add_members(accountAddresses);
114
+ }
115
+
116
+ async addMembersByInboxId(inboxIds: string[]) {
117
+ return this.#group.add_members_by_inbox_id(inboxIds);
118
+ }
119
+
120
+ async removeMembers(accountAddresses: string[]) {
121
+ return this.#group.remove_members(accountAddresses);
122
+ }
123
+
124
+ async removeMembersByInboxId(inboxIds: string[]) {
125
+ return this.#group.remove_members_by_inbox_id(inboxIds);
126
+ }
127
+
128
+ async addAdmin(inboxId: string) {
129
+ return this.#group.add_admin(inboxId);
130
+ }
131
+
132
+ async removeAdmin(inboxId: string) {
133
+ return this.#group.remove_admin(inboxId);
134
+ }
135
+
136
+ async addSuperAdmin(inboxId: string) {
137
+ return this.#group.add_super_admin(inboxId);
138
+ }
139
+
140
+ async removeSuperAdmin(inboxId: string) {
141
+ return this.#group.remove_super_admin(inboxId);
142
+ }
143
+
144
+ async publishMessages() {
145
+ return this.#group.publish_messages();
146
+ }
147
+
148
+ sendOptimistic(encodedContent: WasmEncodedContent) {
149
+ return this.#group.send_optimistic(encodedContent);
150
+ }
151
+
152
+ async send(encodedContent: WasmEncodedContent) {
153
+ return this.#group.send(encodedContent);
154
+ }
155
+
156
+ messages(options?: SafeListMessagesOptions) {
157
+ return this.#group.find_messages(
158
+ options ? fromSafeListMessagesOptions(options) : undefined,
159
+ );
160
+ }
161
+
162
+ get consentState() {
163
+ return this.#group.consent_state();
164
+ }
165
+
166
+ updateConsentState(state: WasmConsentState) {
167
+ this.#group.update_consent_state(state);
168
+ }
169
+ }
@@ -0,0 +1,60 @@
1
+ import type { WasmConversations, WasmGroup } from "@xmtp/wasm-bindings";
2
+ import {
3
+ fromSafeCreateGroupOptions,
4
+ fromSafeListConversationsOptions,
5
+ toSafeMessage,
6
+ type SafeCreateGroupOptions,
7
+ type SafeListConversationsOptions,
8
+ } from "@/utils/conversions";
9
+ import type { WorkerClient } from "@/WorkerClient";
10
+ import { WorkerConversation } from "@/WorkerConversation";
11
+
12
+ export class WorkerConversations {
13
+ #client: WorkerClient;
14
+
15
+ #conversations: WasmConversations;
16
+
17
+ constructor(client: WorkerClient, conversations: WasmConversations) {
18
+ this.#client = client;
19
+ this.#conversations = conversations;
20
+ }
21
+
22
+ async sync() {
23
+ return this.#conversations.sync();
24
+ }
25
+
26
+ getConversationById(id: string) {
27
+ try {
28
+ const group = this.#conversations.find_group_by_id(id);
29
+ // findGroupById will throw if group is not found
30
+ return new WorkerConversation(this.#client, group);
31
+ } catch {
32
+ return undefined;
33
+ }
34
+ }
35
+
36
+ getMessageById(id: string) {
37
+ try {
38
+ // findMessageById will throw if message is not found
39
+ const message = this.#conversations.find_message_by_id(id);
40
+ return toSafeMessage(message);
41
+ } catch {
42
+ return undefined;
43
+ }
44
+ }
45
+
46
+ async list(options?: SafeListConversationsOptions) {
47
+ const groups = (await this.#conversations.list(
48
+ options ? fromSafeListConversationsOptions(options) : undefined,
49
+ )) as WasmGroup[];
50
+ return groups.map((group) => new WorkerConversation(this.#client, group));
51
+ }
52
+
53
+ async newGroup(accountAddresses: string[], options?: SafeCreateGroupOptions) {
54
+ const group = await this.#conversations.create_group(
55
+ accountAddresses,
56
+ options ? fromSafeCreateGroupOptions(options) : undefined,
57
+ );
58
+ return new WorkerConversation(this.#client, group);
59
+ }
60
+ }
@@ -0,0 +1,5 @@
1
+ export const ApiUrls = {
2
+ local: "http://localhost:5555",
3
+ dev: "https://dev.xmtp.network",
4
+ production: "https://production.xmtp.network",
5
+ } as const;
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export { Client } from "./Client";
2
+ export { Conversations } from "./Conversations";
3
+ export { Conversation } from "./Conversation";
4
+ export * from "./DecodedMessage";
5
+ export { Utils } from "./Utils";
6
+ export { ApiUrls } from "./constants";
7
+ export type * from "./types";
8
+ export * from "./utils/conversions";
9
+ export type * from "@xmtp/wasm-bindings";
@@ -0,0 +1,426 @@
1
+ import type {
2
+ WasmConsentEntityType,
3
+ WasmConsentState,
4
+ WasmSignatureRequestType,
5
+ } from "@xmtp/wasm-bindings";
6
+ import type {
7
+ ClientOptions,
8
+ EventsClientMessageData,
9
+ EventsClientPostMessageData,
10
+ EventsErrorData,
11
+ EventsResult,
12
+ EventsWorkerMessageData,
13
+ EventsWorkerPostMessageData,
14
+ SendMessageData,
15
+ } from "@/types";
16
+ import type {
17
+ SafeConsent,
18
+ SafeConversation,
19
+ SafeCreateGroupOptions,
20
+ SafeEncodedContent,
21
+ SafeGroupMember,
22
+ SafeInboxState,
23
+ SafeListConversationsOptions,
24
+ SafeListMessagesOptions,
25
+ SafeMessage,
26
+ } from "@/utils/conversions";
27
+
28
+ export type ClientEvents =
29
+ /**
30
+ * Client actions
31
+ */
32
+ | {
33
+ action: "init";
34
+ id: string;
35
+ result: {
36
+ inboxId: string;
37
+ installationId: string;
38
+ };
39
+ data: {
40
+ address: string;
41
+ options?: ClientOptions;
42
+ };
43
+ }
44
+ | {
45
+ action: "getCreateInboxSignatureText";
46
+ id: string;
47
+ result: string | undefined;
48
+ data: undefined;
49
+ }
50
+ | {
51
+ action: "getAddWalletSignatureText";
52
+ id: string;
53
+ result: string | undefined;
54
+ data: {
55
+ accountAddress: string;
56
+ };
57
+ }
58
+ | {
59
+ action: "getRevokeWalletSignatureText";
60
+ id: string;
61
+ result: string | undefined;
62
+ data: {
63
+ accountAddress: string;
64
+ };
65
+ }
66
+ | {
67
+ action: "getRevokeInstallationsSignatureText";
68
+ id: string;
69
+ result: string | undefined;
70
+ data: undefined;
71
+ }
72
+ | {
73
+ action: "addSignature";
74
+ id: string;
75
+ result: undefined;
76
+ data: {
77
+ type: WasmSignatureRequestType;
78
+ bytes: Uint8Array;
79
+ };
80
+ }
81
+ | {
82
+ action: "applySignaturesRequests";
83
+ id: string;
84
+ result: undefined;
85
+ data: undefined;
86
+ }
87
+ | {
88
+ action: "registerIdentity";
89
+ id: string;
90
+ result: undefined;
91
+ data: undefined;
92
+ }
93
+ | {
94
+ action: "isRegistered";
95
+ id: string;
96
+ result: boolean;
97
+ data: undefined;
98
+ }
99
+ | {
100
+ action: "canMessage";
101
+ id: string;
102
+ result: Map<string, boolean>;
103
+ data: {
104
+ accountAddresses: string[];
105
+ };
106
+ }
107
+ | {
108
+ action: "inboxState";
109
+ id: string;
110
+ result: SafeInboxState;
111
+ data: {
112
+ refreshFromNetwork: boolean;
113
+ };
114
+ }
115
+ | {
116
+ action: "getLatestInboxState";
117
+ id: string;
118
+ result: SafeInboxState;
119
+ data: {
120
+ inboxId: string;
121
+ };
122
+ }
123
+ | {
124
+ action: "setConsentStates";
125
+ id: string;
126
+ result: undefined;
127
+ data: {
128
+ records: SafeConsent[];
129
+ };
130
+ }
131
+ | {
132
+ action: "getConsentState";
133
+ id: string;
134
+ result: WasmConsentState;
135
+ data: {
136
+ entityType: WasmConsentEntityType;
137
+ entity: string;
138
+ };
139
+ }
140
+ | {
141
+ action: "findInboxIdByAddress";
142
+ id: string;
143
+ result: string | undefined;
144
+ data: {
145
+ address: string;
146
+ };
147
+ }
148
+ /**
149
+ * Conversations actions
150
+ */
151
+ | {
152
+ action: "getConversationById";
153
+ id: string;
154
+ result: SafeConversation | undefined;
155
+ data: {
156
+ id: string;
157
+ };
158
+ }
159
+ | {
160
+ action: "getMessageById";
161
+ id: string;
162
+ result: SafeMessage | undefined;
163
+ data: {
164
+ id: string;
165
+ };
166
+ }
167
+ | {
168
+ action: "getConversations";
169
+ id: string;
170
+ result: SafeConversation[];
171
+ data: {
172
+ options?: SafeListConversationsOptions;
173
+ };
174
+ }
175
+ | {
176
+ action: "newGroup";
177
+ id: string;
178
+ result: SafeConversation;
179
+ data: {
180
+ accountAddresses: string[];
181
+ options?: SafeCreateGroupOptions;
182
+ };
183
+ }
184
+ | {
185
+ action: "syncConversations";
186
+ id: string;
187
+ result: undefined;
188
+ data: undefined;
189
+ }
190
+ /**
191
+ * Group actions
192
+ */
193
+ | {
194
+ action: "syncGroup";
195
+ id: string;
196
+ result: SafeConversation;
197
+ data: {
198
+ id: string;
199
+ };
200
+ }
201
+ | {
202
+ action: "sendGroupMessage";
203
+ id: string;
204
+ result: string;
205
+ data: {
206
+ id: string;
207
+ content: SafeEncodedContent;
208
+ };
209
+ }
210
+ | {
211
+ action: "sendOptimisticGroupMessage";
212
+ id: string;
213
+ result: string;
214
+ data: {
215
+ id: string;
216
+ content: SafeEncodedContent;
217
+ };
218
+ }
219
+ | {
220
+ action: "publishGroupMessages";
221
+ id: string;
222
+ result: undefined;
223
+ data: {
224
+ id: string;
225
+ };
226
+ }
227
+ | {
228
+ action: "getGroupMessages";
229
+ id: string;
230
+ result: SafeMessage[];
231
+ data: {
232
+ id: string;
233
+ options?: SafeListMessagesOptions;
234
+ };
235
+ }
236
+ | {
237
+ action: "getGroupMembers";
238
+ id: string;
239
+ result: SafeGroupMember[];
240
+ data: {
241
+ id: string;
242
+ };
243
+ }
244
+ | {
245
+ action: "getGroupAdmins";
246
+ id: string;
247
+ result: string[];
248
+ data: {
249
+ id: string;
250
+ };
251
+ }
252
+ | {
253
+ action: "getGroupSuperAdmins";
254
+ id: string;
255
+ result: string[];
256
+ data: {
257
+ id: string;
258
+ };
259
+ }
260
+ | {
261
+ action: "isGroupAdmin";
262
+ id: string;
263
+ result: boolean;
264
+ data: {
265
+ id: string;
266
+ inboxId: string;
267
+ };
268
+ }
269
+ | {
270
+ action: "isGroupSuperAdmin";
271
+ id: string;
272
+ result: boolean;
273
+ data: {
274
+ id: string;
275
+ inboxId: string;
276
+ };
277
+ }
278
+ | {
279
+ action: "addGroupMembers";
280
+ id: string;
281
+ result: undefined;
282
+ data: {
283
+ id: string;
284
+ accountAddresses: string[];
285
+ };
286
+ }
287
+ | {
288
+ action: "removeGroupMembers";
289
+ id: string;
290
+ result: undefined;
291
+ data: {
292
+ id: string;
293
+ accountAddresses: string[];
294
+ };
295
+ }
296
+ | {
297
+ action: "addGroupMembersByInboxId";
298
+ id: string;
299
+ result: undefined;
300
+ data: {
301
+ id: string;
302
+ inboxIds: string[];
303
+ };
304
+ }
305
+ | {
306
+ action: "removeGroupMembersByInboxId";
307
+ id: string;
308
+ result: undefined;
309
+ data: {
310
+ id: string;
311
+ inboxIds: string[];
312
+ };
313
+ }
314
+ | {
315
+ action: "addGroupAdmin";
316
+ id: string;
317
+ result: undefined;
318
+ data: {
319
+ id: string;
320
+ inboxId: string;
321
+ };
322
+ }
323
+ | {
324
+ action: "removeGroupAdmin";
325
+ id: string;
326
+ result: undefined;
327
+ data: {
328
+ id: string;
329
+ inboxId: string;
330
+ };
331
+ }
332
+ | {
333
+ action: "addGroupSuperAdmin";
334
+ id: string;
335
+ result: undefined;
336
+ data: {
337
+ id: string;
338
+ inboxId: string;
339
+ };
340
+ }
341
+ | {
342
+ action: "removeGroupSuperAdmin";
343
+ id: string;
344
+ result: undefined;
345
+ data: {
346
+ id: string;
347
+ inboxId: string;
348
+ };
349
+ }
350
+ | {
351
+ action: "updateGroupName";
352
+ id: string;
353
+ result: undefined;
354
+ data: {
355
+ id: string;
356
+ name: string;
357
+ };
358
+ }
359
+ | {
360
+ action: "updateGroupDescription";
361
+ id: string;
362
+ result: undefined;
363
+ data: {
364
+ id: string;
365
+ description: string;
366
+ };
367
+ }
368
+ | {
369
+ action: "updateGroupImageUrlSquare";
370
+ id: string;
371
+ result: undefined;
372
+ data: {
373
+ id: string;
374
+ imageUrl: string;
375
+ };
376
+ }
377
+ | {
378
+ action: "updateGroupPinnedFrameUrl";
379
+ id: string;
380
+ result: undefined;
381
+ data: {
382
+ id: string;
383
+ pinnedFrameUrl: string;
384
+ };
385
+ }
386
+ | {
387
+ action: "getGroupConsentState";
388
+ id: string;
389
+ result: WasmConsentState;
390
+ data: {
391
+ id: string;
392
+ };
393
+ }
394
+ | {
395
+ action: "updateGroupConsentState";
396
+ id: string;
397
+ result: undefined;
398
+ data: {
399
+ id: string;
400
+ state: WasmConsentState;
401
+ };
402
+ };
403
+
404
+ export type ClientEventsActions = ClientEvents["action"];
405
+
406
+ export type ClientEventsClientMessageData =
407
+ EventsClientMessageData<ClientEvents>;
408
+
409
+ export type ClientEventsWorkerMessageData =
410
+ EventsWorkerMessageData<ClientEvents>;
411
+
412
+ export type ClientEventsResult<A extends ClientEventsActions> = EventsResult<
413
+ ClientEvents,
414
+ A
415
+ >;
416
+
417
+ export type ClientSendMessageData<A extends ClientEventsActions> =
418
+ SendMessageData<ClientEvents, A>;
419
+
420
+ export type ClientEventsWorkerPostMessageData<A extends ClientEventsActions> =
421
+ EventsWorkerPostMessageData<ClientEvents, A>;
422
+
423
+ export type ClientEventsClientPostMessageData<A extends ClientEventsActions> =
424
+ EventsClientPostMessageData<ClientEvents, A>;
425
+
426
+ export type ClientEventsErrorData = EventsErrorData<ClientEvents>;
@@ -0,0 +1,4 @@
1
+ export type * from "./options";
2
+ export type * from "./clientEvents";
3
+ export type * from "./utilsEvents";
4
+ export type * from "./utils";