@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,72 @@
1
+ import init, {
2
+ generateInboxId as generate_inbox_id,
3
+ getInboxIdForAddress as get_inbox_id_for_address,
4
+ } from "@xmtp/wasm-bindings";
5
+ import { ApiUrls } from "@/constants";
6
+ import type {
7
+ UtilsEventsActions,
8
+ UtilsEventsClientMessageData,
9
+ UtilsEventsErrorData,
10
+ UtilsEventsWorkerPostMessageData,
11
+ XmtpEnv,
12
+ } from "@/types";
13
+
14
+ /**
15
+ * Type-safe postMessage
16
+ */
17
+ const postMessage = <A extends UtilsEventsActions>(
18
+ data: UtilsEventsWorkerPostMessageData<A>,
19
+ ) => {
20
+ self.postMessage(data);
21
+ };
22
+
23
+ /**
24
+ * Type-safe postMessage for errors
25
+ */
26
+ const postMessageError = (data: UtilsEventsErrorData) => {
27
+ self.postMessage(data);
28
+ };
29
+
30
+ export const generateInboxId = async (address: string) => {
31
+ await init();
32
+ return generate_inbox_id(address);
33
+ };
34
+
35
+ export const getInboxIdForAddress = async (address: string, env?: XmtpEnv) => {
36
+ await init();
37
+ const host = env ? ApiUrls[env] : ApiUrls.dev;
38
+ return get_inbox_id_for_address(host, address);
39
+ };
40
+
41
+ self.onmessage = async (event: MessageEvent<UtilsEventsClientMessageData>) => {
42
+ const { action, id, data } = event.data;
43
+ if (data.enableLogging) {
44
+ console.log("utils worker received event data", event.data);
45
+ }
46
+
47
+ try {
48
+ switch (action) {
49
+ case "generateInboxId":
50
+ postMessage({
51
+ id,
52
+ action,
53
+ result: await generateInboxId(data.address),
54
+ });
55
+ break;
56
+ case "getInboxIdForAddress":
57
+ postMessage({
58
+ id,
59
+ action,
60
+ result: await getInboxIdForAddress(data.address, data.env),
61
+ });
62
+ break;
63
+ // no default
64
+ }
65
+ } catch (e) {
66
+ postMessageError({
67
+ id,
68
+ action,
69
+ error: (e as Error).message,
70
+ });
71
+ }
72
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "tsconfig/react-sdk.json",
3
+ "include": ["src", "test", "vite.config.ts", "rollup.config.js"],
4
+ "compilerOptions": {
5
+ "paths": {
6
+ "@/*": ["./src/*"],
7
+ "@test/*": ["./test/*"]
8
+ },
9
+ "types": ["@vitest/browser/providers/playwright"]
10
+ }
11
+ }