@frak-labs/core-sdk 0.1.0-beta.afa252b0 → 0.1.0-beta.b0bd1f8a

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.
Files changed (139) hide show
  1. package/cdn/bundle.iife.js +14 -0
  2. package/dist/actions-CEEObPYc.js +1 -0
  3. package/dist/actions-DbQhWYx8.cjs +1 -0
  4. package/dist/actions.cjs +1 -1
  5. package/dist/actions.d.cts +3 -1481
  6. package/dist/actions.d.ts +3 -1481
  7. package/dist/actions.js +1 -1
  8. package/dist/bundle.cjs +1 -13
  9. package/dist/bundle.d.cts +6 -2087
  10. package/dist/bundle.d.ts +6 -2087
  11. package/dist/bundle.js +1 -13
  12. package/dist/index-7OZ39x1U.d.ts +195 -0
  13. package/dist/index-C6FxkWPC.d.cts +511 -0
  14. package/dist/index-UFX7xCg3.d.ts +351 -0
  15. package/dist/index-d8xS4ryI.d.ts +511 -0
  16. package/dist/index-p4FqSp8z.d.cts +351 -0
  17. package/dist/index-zDq-VlKx.d.cts +195 -0
  18. package/dist/index.cjs +1 -13
  19. package/dist/index.d.cts +4 -1387
  20. package/dist/index.d.ts +4 -1387
  21. package/dist/index.js +1 -13
  22. package/dist/interaction-DMJ3ZfaF.d.cts +45 -0
  23. package/dist/interaction-KX1h9a7V.d.ts +45 -0
  24. package/dist/interactions-DnfM3oe0.js +1 -0
  25. package/dist/interactions-EIXhNLf6.cjs +1 -0
  26. package/dist/interactions.cjs +1 -1
  27. package/dist/interactions.d.cts +2 -182
  28. package/dist/interactions.d.ts +2 -182
  29. package/dist/interactions.js +1 -1
  30. package/dist/openSso-D--Airj6.d.cts +1018 -0
  31. package/dist/openSso-DsKJ4y0j.d.ts +1018 -0
  32. package/dist/productTypes-BUkXJKZ7.cjs +1 -0
  33. package/dist/productTypes-CGb1MmBF.js +1 -0
  34. package/dist/src-B_xO0AR6.cjs +13 -0
  35. package/dist/src-D2d52OZa.js +13 -0
  36. package/dist/trackEvent-CHnYa85W.js +1 -0
  37. package/dist/trackEvent-GuQm_1Nm.cjs +1 -0
  38. package/package.json +23 -18
  39. package/src/actions/displayEmbeddedWallet.test.ts +194 -0
  40. package/src/actions/displayEmbeddedWallet.ts +20 -0
  41. package/src/actions/displayModal.test.ts +387 -0
  42. package/src/actions/displayModal.ts +131 -0
  43. package/src/actions/getProductInformation.test.ts +133 -0
  44. package/src/actions/getProductInformation.ts +14 -0
  45. package/src/actions/index.ts +29 -0
  46. package/src/actions/openSso.test.ts +407 -0
  47. package/src/actions/openSso.ts +116 -0
  48. package/src/actions/prepareSso.test.ts +223 -0
  49. package/src/actions/prepareSso.ts +48 -0
  50. package/src/actions/referral/processReferral.ts +230 -0
  51. package/src/actions/referral/referralInteraction.ts +57 -0
  52. package/src/actions/sendInteraction.test.ts +219 -0
  53. package/src/actions/sendInteraction.ts +32 -0
  54. package/src/actions/trackPurchaseStatus.test.ts +287 -0
  55. package/src/actions/trackPurchaseStatus.ts +53 -0
  56. package/src/actions/watchWalletStatus.test.ts +372 -0
  57. package/src/actions/watchWalletStatus.ts +94 -0
  58. package/src/actions/wrapper/modalBuilder.ts +212 -0
  59. package/src/actions/wrapper/sendTransaction.ts +62 -0
  60. package/src/actions/wrapper/siweAuthenticate.ts +94 -0
  61. package/src/bundle.ts +3 -0
  62. package/src/clients/DebugInfo.ts +182 -0
  63. package/src/clients/createIFrameFrakClient.ts +287 -0
  64. package/src/clients/index.ts +3 -0
  65. package/src/clients/setupClient.test.ts +343 -0
  66. package/src/clients/setupClient.ts +73 -0
  67. package/src/clients/transports/iframeLifecycleManager.test.ts +399 -0
  68. package/src/clients/transports/iframeLifecycleManager.ts +90 -0
  69. package/src/constants/interactionTypes.ts +44 -0
  70. package/src/constants/locales.ts +14 -0
  71. package/src/constants/productTypes.ts +33 -0
  72. package/src/index.ts +101 -0
  73. package/src/interactions/index.ts +5 -0
  74. package/src/interactions/pressEncoder.test.ts +215 -0
  75. package/src/interactions/pressEncoder.ts +53 -0
  76. package/src/interactions/purchaseEncoder.test.ts +291 -0
  77. package/src/interactions/purchaseEncoder.ts +99 -0
  78. package/src/interactions/referralEncoder.test.ts +170 -0
  79. package/src/interactions/referralEncoder.ts +47 -0
  80. package/src/interactions/retailEncoder.test.ts +107 -0
  81. package/src/interactions/retailEncoder.ts +37 -0
  82. package/src/interactions/webshopEncoder.test.ts +56 -0
  83. package/src/interactions/webshopEncoder.ts +30 -0
  84. package/src/types/client.ts +14 -0
  85. package/src/types/compression.ts +22 -0
  86. package/src/types/config.ts +111 -0
  87. package/src/types/context.ts +13 -0
  88. package/src/types/index.ts +71 -0
  89. package/src/types/lifecycle/client.ts +46 -0
  90. package/src/types/lifecycle/iframe.ts +35 -0
  91. package/src/types/lifecycle/index.ts +2 -0
  92. package/src/types/rpc/displayModal.ts +84 -0
  93. package/src/types/rpc/embedded/index.ts +68 -0
  94. package/src/types/rpc/embedded/loggedIn.ts +55 -0
  95. package/src/types/rpc/embedded/loggedOut.ts +28 -0
  96. package/src/types/rpc/interaction.ts +43 -0
  97. package/src/types/rpc/modal/final.ts +46 -0
  98. package/src/types/rpc/modal/generic.ts +46 -0
  99. package/src/types/rpc/modal/index.ts +20 -0
  100. package/src/types/rpc/modal/login.ts +32 -0
  101. package/src/types/rpc/modal/openSession.ts +25 -0
  102. package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
  103. package/src/types/rpc/modal/transaction.ts +33 -0
  104. package/src/types/rpc/productInformation.ts +59 -0
  105. package/src/types/rpc/sso.ts +80 -0
  106. package/src/types/rpc/walletStatus.ts +35 -0
  107. package/src/types/rpc.ts +158 -0
  108. package/src/types/transport.ts +34 -0
  109. package/src/utils/FrakContext.test.ts +407 -0
  110. package/src/utils/FrakContext.ts +158 -0
  111. package/src/utils/compression/b64.test.ts +181 -0
  112. package/src/utils/compression/b64.ts +29 -0
  113. package/src/utils/compression/compress.test.ts +123 -0
  114. package/src/utils/compression/compress.ts +11 -0
  115. package/src/utils/compression/decompress.test.ts +145 -0
  116. package/src/utils/compression/decompress.ts +11 -0
  117. package/src/utils/compression/index.ts +3 -0
  118. package/src/utils/computeProductId.test.ts +80 -0
  119. package/src/utils/computeProductId.ts +11 -0
  120. package/src/utils/constants.test.ts +23 -0
  121. package/src/utils/constants.ts +4 -0
  122. package/src/utils/formatAmount.test.ts +113 -0
  123. package/src/utils/formatAmount.ts +18 -0
  124. package/src/utils/getCurrencyAmountKey.test.ts +44 -0
  125. package/src/utils/getCurrencyAmountKey.ts +15 -0
  126. package/src/utils/getSupportedCurrency.test.ts +51 -0
  127. package/src/utils/getSupportedCurrency.ts +14 -0
  128. package/src/utils/getSupportedLocale.test.ts +64 -0
  129. package/src/utils/getSupportedLocale.ts +16 -0
  130. package/src/utils/iframeHelper.test.ts +450 -0
  131. package/src/utils/iframeHelper.ts +143 -0
  132. package/src/utils/index.ts +21 -0
  133. package/src/utils/sso.test.ts +361 -0
  134. package/src/utils/sso.ts +119 -0
  135. package/src/utils/ssoUrlListener.ts +60 -0
  136. package/src/utils/trackEvent.test.ts +162 -0
  137. package/src/utils/trackEvent.ts +26 -0
  138. package/cdn/bundle.js +0 -19
  139. package/cdn/bundle.js.LICENSE.txt +0 -10
@@ -0,0 +1,372 @@
1
+ /**
2
+ * Tests for watchWalletStatus action
3
+ * Tests wallet status watching and side effects
4
+ */
5
+
6
+ import { vi } from "vitest";
7
+
8
+ // Mock Deferred before imports
9
+ vi.mock("@frak-labs/frame-connector", () => ({
10
+ Deferred: class {
11
+ promise: Promise<any>;
12
+ resolve: (value: any) => void;
13
+
14
+ constructor() {
15
+ let resolveFunc: (value: any) => void;
16
+ this.promise = new Promise((resolve) => {
17
+ resolveFunc = resolve;
18
+ });
19
+ this.resolve = resolveFunc!;
20
+ }
21
+ },
22
+ }));
23
+
24
+ import type { Address } from "viem";
25
+ import {
26
+ afterEach,
27
+ beforeEach,
28
+ describe,
29
+ expect,
30
+ it,
31
+ } from "../../tests/vitest-fixtures";
32
+ import type { FrakClient, WalletStatusReturnType } from "../types";
33
+ import { watchWalletStatus } from "./watchWalletStatus";
34
+
35
+ describe("watchWalletStatus", () => {
36
+ let mockSessionStorage: {
37
+ getItem: ReturnType<typeof vi.fn>;
38
+ setItem: ReturnType<typeof vi.fn>;
39
+ removeItem: ReturnType<typeof vi.fn>;
40
+ };
41
+
42
+ beforeEach(() => {
43
+ // Mock sessionStorage
44
+ mockSessionStorage = {
45
+ getItem: vi.fn(),
46
+ setItem: vi.fn(),
47
+ removeItem: vi.fn(),
48
+ };
49
+ Object.defineProperty(window, "sessionStorage", {
50
+ value: mockSessionStorage,
51
+ writable: true,
52
+ configurable: true,
53
+ });
54
+ });
55
+
56
+ afterEach(() => {
57
+ vi.clearAllMocks();
58
+ });
59
+
60
+ describe("without callback", () => {
61
+ it("should make one-shot request", async () => {
62
+ const mockStatus: WalletStatusReturnType = {
63
+ key: "connected",
64
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
65
+ interactionToken: "token-123",
66
+ };
67
+
68
+ const mockClient = {
69
+ request: vi.fn().mockResolvedValue(mockStatus),
70
+ openPanel: {
71
+ setGlobalProperties: vi.fn(),
72
+ },
73
+ } as unknown as FrakClient;
74
+
75
+ const result = await watchWalletStatus(mockClient);
76
+
77
+ expect(mockClient.request).toHaveBeenCalledWith({
78
+ method: "frak_listenToWalletStatus",
79
+ });
80
+ expect(result).toEqual(mockStatus);
81
+ });
82
+
83
+ it("should save interaction token to sessionStorage", async () => {
84
+ const mockStatus: WalletStatusReturnType = {
85
+ key: "connected",
86
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
87
+ interactionToken: "token-abc",
88
+ };
89
+
90
+ const mockClient = {
91
+ request: vi.fn().mockResolvedValue(mockStatus),
92
+ openPanel: {
93
+ setGlobalProperties: vi.fn(),
94
+ },
95
+ } as unknown as FrakClient;
96
+
97
+ await watchWalletStatus(mockClient);
98
+
99
+ expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
100
+ "frak-wallet-interaction-token",
101
+ "token-abc"
102
+ );
103
+ });
104
+
105
+ it("should remove interaction token when not present", async () => {
106
+ const mockStatus: WalletStatusReturnType = {
107
+ key: "not-connected",
108
+ };
109
+
110
+ const mockClient = {
111
+ request: vi.fn().mockResolvedValue(mockStatus),
112
+ openPanel: {
113
+ setGlobalProperties: vi.fn(),
114
+ },
115
+ } as unknown as FrakClient;
116
+
117
+ await watchWalletStatus(mockClient);
118
+
119
+ expect(mockSessionStorage.removeItem).toHaveBeenCalledWith(
120
+ "frak-wallet-interaction-token"
121
+ );
122
+ });
123
+
124
+ it("should update OpenPanel global properties", async () => {
125
+ const mockStatus: WalletStatusReturnType = {
126
+ key: "connected",
127
+ wallet: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd" as Address,
128
+ };
129
+
130
+ const mockClient = {
131
+ request: vi.fn().mockResolvedValue(mockStatus),
132
+ openPanel: {
133
+ setGlobalProperties: vi.fn(),
134
+ },
135
+ } as unknown as FrakClient;
136
+
137
+ await watchWalletStatus(mockClient);
138
+
139
+ expect(
140
+ mockClient.openPanel?.setGlobalProperties
141
+ ).toHaveBeenCalledWith({
142
+ wallet: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd",
143
+ });
144
+ });
145
+
146
+ it("should set wallet to null when not connected", async () => {
147
+ const mockStatus: WalletStatusReturnType = {
148
+ key: "not-connected",
149
+ };
150
+
151
+ const mockClient = {
152
+ request: vi.fn().mockResolvedValue(mockStatus),
153
+ openPanel: {
154
+ setGlobalProperties: vi.fn(),
155
+ },
156
+ } as unknown as FrakClient;
157
+
158
+ await watchWalletStatus(mockClient);
159
+
160
+ expect(
161
+ mockClient.openPanel?.setGlobalProperties
162
+ ).toHaveBeenCalledWith({
163
+ wallet: null,
164
+ });
165
+ });
166
+
167
+ it("should work without openPanel", async () => {
168
+ const mockStatus: WalletStatusReturnType = {
169
+ key: "connected",
170
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
171
+ };
172
+
173
+ const mockClient = {
174
+ request: vi.fn().mockResolvedValue(mockStatus),
175
+ } as unknown as FrakClient;
176
+
177
+ const result = await watchWalletStatus(mockClient);
178
+
179
+ expect(result).toEqual(mockStatus);
180
+ });
181
+ });
182
+
183
+ describe("with callback", () => {
184
+ it("should setup listener request", async () => {
185
+ const mockStatus: WalletStatusReturnType = {
186
+ key: "connected",
187
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
188
+ };
189
+
190
+ const mockCallback = vi.fn();
191
+ const mockClient = {
192
+ listenerRequest: vi.fn((_params, callback) => {
193
+ // Simulate status update
194
+ setTimeout(() => callback(mockStatus), 0);
195
+ }),
196
+ openPanel: {
197
+ setGlobalProperties: vi.fn(),
198
+ },
199
+ } as unknown as FrakClient;
200
+
201
+ const resultPromise = watchWalletStatus(mockClient, mockCallback);
202
+
203
+ expect(mockClient.listenerRequest).toHaveBeenCalled();
204
+
205
+ await resultPromise;
206
+ });
207
+
208
+ it("should call callback with status updates", async () => {
209
+ const mockStatus: WalletStatusReturnType = {
210
+ key: "connected",
211
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
212
+ interactionToken: "token-123",
213
+ };
214
+
215
+ const mockCallback = vi.fn();
216
+ const mockClient = {
217
+ listenerRequest: vi.fn((_params, callback) => {
218
+ setTimeout(() => callback(mockStatus), 0);
219
+ }),
220
+ openPanel: {
221
+ setGlobalProperties: vi.fn(),
222
+ },
223
+ } as unknown as FrakClient;
224
+
225
+ await watchWalletStatus(mockClient, mockCallback);
226
+
227
+ expect(mockCallback).toHaveBeenCalledWith(mockStatus);
228
+ });
229
+
230
+ it("should resolve promise with first status", async () => {
231
+ const firstStatus: WalletStatusReturnType = {
232
+ key: "connected",
233
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
234
+ };
235
+
236
+ const secondStatus: WalletStatusReturnType = {
237
+ key: "connected",
238
+ wallet: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd" as Address,
239
+ };
240
+
241
+ const mockCallback = vi.fn();
242
+ const mockClient = {
243
+ listenerRequest: vi.fn((_params, callback) => {
244
+ setTimeout(() => {
245
+ callback(firstStatus);
246
+ callback(secondStatus);
247
+ }, 0);
248
+ }),
249
+ openPanel: {
250
+ setGlobalProperties: vi.fn(),
251
+ },
252
+ } as unknown as FrakClient;
253
+
254
+ const result = await watchWalletStatus(mockClient, mockCallback);
255
+
256
+ expect(result).toEqual(firstStatus);
257
+ expect(mockCallback).toHaveBeenCalledTimes(2);
258
+ });
259
+
260
+ it("should handle side effects for each status update", async () => {
261
+ const firstStatus: WalletStatusReturnType = {
262
+ key: "connected",
263
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
264
+ interactionToken: "token-1",
265
+ };
266
+
267
+ const secondStatus: WalletStatusReturnType = {
268
+ key: "connected",
269
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
270
+ interactionToken: "token-2",
271
+ };
272
+
273
+ const mockCallback = vi.fn();
274
+ const mockClient = {
275
+ listenerRequest: vi.fn((_params, callback) => {
276
+ setTimeout(() => {
277
+ callback(firstStatus);
278
+ callback(secondStatus);
279
+ }, 0);
280
+ }),
281
+ openPanel: {
282
+ setGlobalProperties: vi.fn(),
283
+ },
284
+ } as unknown as FrakClient;
285
+
286
+ await watchWalletStatus(mockClient, mockCallback);
287
+
288
+ // Both tokens should be saved
289
+ expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
290
+ "frak-wallet-interaction-token",
291
+ "token-1"
292
+ );
293
+ expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
294
+ "frak-wallet-interaction-token",
295
+ "token-2"
296
+ );
297
+ });
298
+
299
+ it("should save and remove interaction token based on status", async () => {
300
+ const connectedStatus: WalletStatusReturnType = {
301
+ key: "connected",
302
+ wallet: "0x1234567890123456789012345678901234567890" as Address,
303
+ interactionToken: "token-123",
304
+ };
305
+
306
+ const disconnectedStatus: WalletStatusReturnType = {
307
+ key: "not-connected",
308
+ };
309
+
310
+ const mockCallback = vi.fn();
311
+ const mockClient = {
312
+ listenerRequest: vi.fn((_params, callback) => {
313
+ setTimeout(() => {
314
+ callback(connectedStatus);
315
+ callback(disconnectedStatus);
316
+ }, 0);
317
+ }),
318
+ openPanel: {
319
+ setGlobalProperties: vi.fn(),
320
+ },
321
+ } as unknown as FrakClient;
322
+
323
+ await watchWalletStatus(mockClient, mockCallback);
324
+
325
+ expect(mockSessionStorage.setItem).toHaveBeenCalledWith(
326
+ "frak-wallet-interaction-token",
327
+ "token-123"
328
+ );
329
+ expect(mockSessionStorage.removeItem).toHaveBeenCalledWith(
330
+ "frak-wallet-interaction-token"
331
+ );
332
+ });
333
+
334
+ it("should update OpenPanel properties with each status", async () => {
335
+ const firstStatus: WalletStatusReturnType = {
336
+ key: "connected",
337
+ wallet: "0x1111111111111111111111111111111111111111" as Address,
338
+ };
339
+
340
+ const secondStatus: WalletStatusReturnType = {
341
+ key: "connected",
342
+ wallet: "0x2222222222222222222222222222222222222222" as Address,
343
+ };
344
+
345
+ const mockCallback = vi.fn();
346
+ const mockClient = {
347
+ listenerRequest: vi.fn((_params, callback) => {
348
+ setTimeout(() => {
349
+ callback(firstStatus);
350
+ callback(secondStatus);
351
+ }, 0);
352
+ }),
353
+ openPanel: {
354
+ setGlobalProperties: vi.fn(),
355
+ },
356
+ } as unknown as FrakClient;
357
+
358
+ await watchWalletStatus(mockClient, mockCallback);
359
+
360
+ expect(
361
+ mockClient.openPanel?.setGlobalProperties
362
+ ).toHaveBeenCalledWith({
363
+ wallet: "0x1111111111111111111111111111111111111111",
364
+ });
365
+ expect(
366
+ mockClient.openPanel?.setGlobalProperties
367
+ ).toHaveBeenCalledWith({
368
+ wallet: "0x2222222222222222222222222222222222222222",
369
+ });
370
+ });
371
+ });
372
+ });
@@ -0,0 +1,94 @@
1
+ import { Deferred } from "@frak-labs/frame-connector";
2
+ import type { FrakClient } from "../types/client";
3
+ import type { WalletStatusReturnType } from "../types/rpc/walletStatus";
4
+
5
+ /**
6
+ * Function used to watch the current frak wallet status
7
+ * @param client - The current Frak Client
8
+ * @param callback - The callback that will receive any wallet status change
9
+ * @returns A promise resolving with the initial wallet status
10
+ *
11
+ * @description This function will return the current wallet status, and will listen to any change in the wallet status.
12
+ *
13
+ * @example
14
+ * await watchWalletStatus(frakConfig, (status: WalletStatusReturnType) => {
15
+ * if (status.key === "connected") {
16
+ * console.log("Wallet connected:", status.wallet);
17
+ * console.log("Current interaction session:", status.interactionSession);
18
+ * } else {
19
+ * console.log("Wallet not connected");
20
+ * }
21
+ * });
22
+ */
23
+ export function watchWalletStatus(
24
+ client: FrakClient,
25
+ callback?: (status: WalletStatusReturnType) => void
26
+ ): Promise<WalletStatusReturnType> {
27
+ // If no callback is provided, just do a request with deferred result
28
+ if (!callback) {
29
+ return client
30
+ .request({ method: "frak_listenToWalletStatus" })
31
+ .then((result) => {
32
+ // Handle side effects of this request
33
+ walletStatusSideEffect(client, result);
34
+
35
+ // Return the result
36
+ return result;
37
+ });
38
+ }
39
+
40
+ // Otherwise, listen to the wallet status and return the first one received
41
+ const firstResult = new Deferred<WalletStatusReturnType>();
42
+ let hasResolved = false;
43
+
44
+ // Start the listening request, and return the first result
45
+ client.listenerRequest(
46
+ {
47
+ method: "frak_listenToWalletStatus",
48
+ },
49
+ (status) => {
50
+ // Handle side effects of this request
51
+ walletStatusSideEffect(client, status);
52
+
53
+ // Transmit the status to the callback
54
+ callback(status);
55
+
56
+ // If the promise hasn't resolved yet, resolve it
57
+ if (!hasResolved) {
58
+ firstResult.resolve(status);
59
+ hasResolved = true;
60
+ }
61
+ }
62
+ );
63
+
64
+ return firstResult.promise;
65
+ }
66
+
67
+ /**
68
+ * Helper to save a potential interaction token
69
+ * @param interactionToken
70
+ */
71
+ function walletStatusSideEffect(
72
+ client: FrakClient,
73
+ status: WalletStatusReturnType
74
+ ) {
75
+ if (typeof window === "undefined") {
76
+ return;
77
+ }
78
+
79
+ // Update the global properties
80
+ client.openPanel?.setGlobalProperties({
81
+ wallet: status.wallet ?? null,
82
+ });
83
+
84
+ if (status.interactionToken) {
85
+ // If we got an interaction token, save it
86
+ window.sessionStorage.setItem(
87
+ "frak-wallet-interaction-token",
88
+ status.interactionToken
89
+ );
90
+ } else {
91
+ // Otherwise, remove it
92
+ window.sessionStorage.removeItem("frak-wallet-interaction-token");
93
+ }
94
+ }
@@ -0,0 +1,212 @@
1
+ import type {
2
+ DisplayModalParamsType,
3
+ FinalActionType,
4
+ FinalModalStepType,
5
+ FrakClient,
6
+ LoginModalStepType,
7
+ ModalRpcMetadata,
8
+ ModalRpcStepsResultType,
9
+ ModalStepTypes,
10
+ OpenInteractionSessionModalStepType,
11
+ SendTransactionModalStepType,
12
+ } from "../../types";
13
+ import { displayModal } from "../displayModal";
14
+
15
+ /**
16
+ * Represent the type of the modal step builder
17
+ */
18
+ export type ModalStepBuilder<
19
+ Steps extends ModalStepTypes[] = ModalStepTypes[],
20
+ > = {
21
+ /**
22
+ * The current modal params
23
+ */
24
+ params: DisplayModalParamsType<Steps>;
25
+ /**
26
+ * Add a send transaction step to the modal
27
+ */
28
+ sendTx: (
29
+ options: SendTransactionModalStepType["params"]
30
+ ) => ModalStepBuilder<[...Steps, SendTransactionModalStepType]>;
31
+ /**
32
+ * Add a final step of type reward to the modal
33
+ */
34
+ reward: (
35
+ options?: Omit<FinalModalStepType["params"], "action">
36
+ ) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
37
+ /**
38
+ * Add a final step of type sharing to the modal
39
+ */
40
+ sharing: (
41
+ sharingOptions?: Extract<
42
+ FinalActionType,
43
+ { key: "sharing" }
44
+ >["options"],
45
+ options?: Omit<FinalModalStepType["params"], "action">
46
+ ) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
47
+ /**
48
+ * Display the modal
49
+ * @param metadataOverride - Function returning optional metadata to override the current modal metadata
50
+ */
51
+ display: (
52
+ metadataOverride?: (
53
+ current?: ModalRpcMetadata
54
+ ) => ModalRpcMetadata | undefined
55
+ ) => Promise<ModalRpcStepsResultType<Steps>>;
56
+ };
57
+
58
+ /**
59
+ * Represent the output type of the modal builder
60
+ */
61
+ export type ModalBuilder = ModalStepBuilder<
62
+ [LoginModalStepType, OpenInteractionSessionModalStepType]
63
+ >;
64
+
65
+ /**
66
+ * Helper to craft Frak modal, and share a base initial config
67
+ * @param client - The current Frak Client
68
+ * @param args
69
+ * @param args.metadata - Common modal metadata (customisation, language etc)
70
+ * @param args.login - Login step parameters
71
+ * @param args.openSession - Open session step parameters
72
+ *
73
+ * @description This function will create a modal builder with the provided metadata, login and open session parameters.
74
+ *
75
+ * @example
76
+ * Here is an example of how to use the `modalBuilder` to create and display a sharing modal:
77
+ *
78
+ * ```js
79
+ * // Create the modal builder
80
+ * const modalBuilder = window.FrakSDK.modalBuilder(frakClient, baseModalConfig);
81
+ *
82
+ * // Configure the information to be shared via the sharing link
83
+ * const sharingConfig = {
84
+ * popupTitle: "Share this with your friends",
85
+ * text: "Discover our product!",
86
+ * link: window.location.href,
87
+ * };
88
+ *
89
+ * // Display the sharing modal
90
+ * function modalShare() {
91
+ * modalBuilder.sharing(sharingConfig).display();
92
+ * }
93
+ * ```
94
+ *
95
+ * @see {@link ModalStepTypes} for more info about each modal step types and their parameters
96
+ * @see {@link ModalRpcMetadata} for more info about the metadata that can be passed to the modal
97
+ * @see {@link ModalRpcStepsResultType} for more info about the result of each modal steps
98
+ * @see {@link displayModal} for more info about how the modal is displayed
99
+ */
100
+ export function modalBuilder(
101
+ client: FrakClient,
102
+ {
103
+ metadata,
104
+ login,
105
+ openSession,
106
+ }: {
107
+ metadata?: ModalRpcMetadata;
108
+ login?: LoginModalStepType["params"];
109
+ openSession?: OpenInteractionSessionModalStepType["params"];
110
+ }
111
+ ): ModalBuilder {
112
+ // Build the initial modal params
113
+ const baseParams: DisplayModalParamsType<
114
+ [LoginModalStepType, OpenInteractionSessionModalStepType]
115
+ > = {
116
+ steps: {
117
+ login: login ?? {},
118
+ openSession: openSession ?? {},
119
+ },
120
+ metadata,
121
+ };
122
+
123
+ // Return the step builder
124
+ return modalStepsBuilder(client, baseParams);
125
+ }
126
+
127
+ /**
128
+ * Modal step builder, allowing to add new steps to the modal, and to build and display it
129
+ */
130
+ function modalStepsBuilder<CurrentSteps extends ModalStepTypes[]>(
131
+ client: FrakClient,
132
+ params: DisplayModalParamsType<CurrentSteps>
133
+ ): ModalStepBuilder<CurrentSteps> {
134
+ // Function add the send tx step
135
+ function sendTx(options: SendTransactionModalStepType["params"]) {
136
+ return modalStepsBuilder<
137
+ [...CurrentSteps, SendTransactionModalStepType]
138
+ >(client, {
139
+ ...params,
140
+ steps: {
141
+ ...params.steps,
142
+ sendTransaction: options,
143
+ },
144
+ } as DisplayModalParamsType<
145
+ [...CurrentSteps, SendTransactionModalStepType]
146
+ >);
147
+ }
148
+
149
+ // Function to add a reward step at the end
150
+ function reward(options?: Omit<FinalModalStepType["params"], "action">) {
151
+ return modalStepsBuilder<[...CurrentSteps, FinalModalStepType]>(
152
+ client,
153
+ {
154
+ ...params,
155
+ steps: {
156
+ ...params.steps,
157
+ final: {
158
+ ...options,
159
+ action: { key: "reward" },
160
+ },
161
+ },
162
+ } as DisplayModalParamsType<[...CurrentSteps, FinalModalStepType]>
163
+ );
164
+ }
165
+
166
+ // Function to add sharing step at the end
167
+ function sharing(
168
+ sharingOptions?: Extract<
169
+ FinalActionType,
170
+ { key: "sharing" }
171
+ >["options"],
172
+ options?: Omit<FinalModalStepType["params"], "action">
173
+ ) {
174
+ return modalStepsBuilder<[...CurrentSteps, FinalModalStepType]>(
175
+ client,
176
+ {
177
+ ...params,
178
+ steps: {
179
+ ...params.steps,
180
+ final: {
181
+ ...options,
182
+ action: { key: "sharing", options: sharingOptions },
183
+ },
184
+ },
185
+ } as DisplayModalParamsType<[...CurrentSteps, FinalModalStepType]>
186
+ );
187
+ }
188
+
189
+ // Function to display it
190
+ async function display(
191
+ metadataOverride?: (
192
+ current?: ModalRpcMetadata
193
+ ) => ModalRpcMetadata | undefined
194
+ ) {
195
+ // If we have a metadata override, apply it
196
+ if (metadataOverride) {
197
+ params.metadata = metadataOverride(params.metadata ?? {});
198
+ }
199
+ return await displayModal(client, params);
200
+ }
201
+
202
+ return {
203
+ // Access current modal params
204
+ params,
205
+ // Function to add new steps
206
+ sendTx,
207
+ reward,
208
+ sharing,
209
+ // Display the modal
210
+ display,
211
+ };
212
+ }