@frak-labs/core-sdk 0.1.0 → 0.1.1-beta.4dfea079

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 (130) hide show
  1. package/README.md +58 -0
  2. package/cdn/bundle.js +3 -8
  3. package/dist/actions.cjs +1 -1
  4. package/dist/actions.d.cts +3 -1400
  5. package/dist/actions.d.ts +3 -1400
  6. package/dist/actions.js +1 -1
  7. package/dist/bundle.cjs +1 -13
  8. package/dist/bundle.d.cts +4 -1927
  9. package/dist/bundle.d.ts +4 -1927
  10. package/dist/bundle.js +1 -13
  11. package/dist/computeLegacyProductId-CscYhyUi.d.cts +525 -0
  12. package/dist/computeLegacyProductId-WbD1gXV9.d.ts +525 -0
  13. package/dist/index.cjs +1 -13
  14. package/dist/index.d.cts +3 -1269
  15. package/dist/index.d.ts +3 -1269
  16. package/dist/index.js +1 -13
  17. package/dist/openSso-CC1-loUk.d.cts +1019 -0
  18. package/dist/openSso-tkqaDQLV.d.ts +1019 -0
  19. package/dist/setupClient-BjIbK6XJ.cjs +13 -0
  20. package/dist/setupClient-D_HId3e2.js +13 -0
  21. package/dist/siweAuthenticate-B_Z2OZmj.cjs +1 -0
  22. package/dist/siweAuthenticate-CQ4OfPuA.js +1 -0
  23. package/dist/siweAuthenticate-CR4Dpji6.d.cts +467 -0
  24. package/dist/siweAuthenticate-udoruuy9.d.ts +467 -0
  25. package/dist/trackEvent-CGIryq5h.cjs +1 -0
  26. package/dist/trackEvent-YfUh4jrx.js +1 -0
  27. package/package.json +24 -30
  28. package/src/actions/displayEmbeddedWallet.test.ts +194 -0
  29. package/src/actions/displayEmbeddedWallet.ts +20 -0
  30. package/src/actions/displayModal.test.ts +388 -0
  31. package/src/actions/displayModal.ts +120 -0
  32. package/src/actions/getMerchantInformation.test.ts +116 -0
  33. package/src/actions/getMerchantInformation.ts +9 -0
  34. package/src/actions/index.ts +29 -0
  35. package/src/actions/openSso.ts +116 -0
  36. package/src/actions/prepareSso.test.ts +223 -0
  37. package/src/actions/prepareSso.ts +48 -0
  38. package/src/actions/referral/processReferral.test.ts +248 -0
  39. package/src/actions/referral/processReferral.ts +232 -0
  40. package/src/actions/referral/referralInteraction.test.ts +147 -0
  41. package/src/actions/referral/referralInteraction.ts +52 -0
  42. package/src/actions/sendInteraction.ts +24 -0
  43. package/src/actions/trackPurchaseStatus.test.ts +287 -0
  44. package/src/actions/trackPurchaseStatus.ts +56 -0
  45. package/src/actions/watchWalletStatus.test.ts +372 -0
  46. package/src/actions/watchWalletStatus.ts +93 -0
  47. package/src/actions/wrapper/modalBuilder.test.ts +239 -0
  48. package/src/actions/wrapper/modalBuilder.ts +203 -0
  49. package/src/actions/wrapper/sendTransaction.test.ts +164 -0
  50. package/src/actions/wrapper/sendTransaction.ts +62 -0
  51. package/src/actions/wrapper/siweAuthenticate.test.ts +290 -0
  52. package/src/actions/wrapper/siweAuthenticate.ts +94 -0
  53. package/src/bundle.ts +2 -0
  54. package/src/clients/DebugInfo.test.ts +418 -0
  55. package/src/clients/DebugInfo.ts +182 -0
  56. package/src/clients/createIFrameFrakClient.ts +289 -0
  57. package/src/clients/index.ts +3 -0
  58. package/src/clients/setupClient.test.ts +343 -0
  59. package/src/clients/setupClient.ts +73 -0
  60. package/src/clients/transports/iframeLifecycleManager.test.ts +558 -0
  61. package/src/clients/transports/iframeLifecycleManager.ts +174 -0
  62. package/src/constants/interactionTypes.ts +15 -0
  63. package/src/constants/locales.ts +14 -0
  64. package/src/index.ts +110 -0
  65. package/src/types/client.ts +14 -0
  66. package/src/types/compression.ts +22 -0
  67. package/src/types/config.ts +117 -0
  68. package/src/types/context.ts +13 -0
  69. package/src/types/index.ts +75 -0
  70. package/src/types/lifecycle/client.ts +69 -0
  71. package/src/types/lifecycle/iframe.ts +41 -0
  72. package/src/types/lifecycle/index.ts +2 -0
  73. package/src/types/rpc/displayModal.ts +82 -0
  74. package/src/types/rpc/embedded/index.ts +68 -0
  75. package/src/types/rpc/embedded/loggedIn.ts +55 -0
  76. package/src/types/rpc/embedded/loggedOut.ts +28 -0
  77. package/src/types/rpc/interaction.ts +30 -0
  78. package/src/types/rpc/merchantInformation.ts +77 -0
  79. package/src/types/rpc/modal/final.ts +46 -0
  80. package/src/types/rpc/modal/generic.ts +46 -0
  81. package/src/types/rpc/modal/index.ts +16 -0
  82. package/src/types/rpc/modal/login.ts +36 -0
  83. package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
  84. package/src/types/rpc/modal/transaction.ts +33 -0
  85. package/src/types/rpc/sso.ts +80 -0
  86. package/src/types/rpc/walletStatus.ts +29 -0
  87. package/src/types/rpc.ts +146 -0
  88. package/src/types/tracking.ts +60 -0
  89. package/src/types/transport.ts +34 -0
  90. package/src/utils/FrakContext.test.ts +407 -0
  91. package/src/utils/FrakContext.ts +158 -0
  92. package/src/utils/backendUrl.test.ts +83 -0
  93. package/src/utils/backendUrl.ts +62 -0
  94. package/src/utils/clientId.test.ts +41 -0
  95. package/src/utils/clientId.ts +40 -0
  96. package/src/utils/compression/b64.test.ts +181 -0
  97. package/src/utils/compression/b64.ts +29 -0
  98. package/src/utils/compression/compress.test.ts +123 -0
  99. package/src/utils/compression/compress.ts +11 -0
  100. package/src/utils/compression/decompress.test.ts +149 -0
  101. package/src/utils/compression/decompress.ts +11 -0
  102. package/src/utils/compression/index.ts +3 -0
  103. package/src/utils/computeLegacyProductId.ts +11 -0
  104. package/src/utils/constants.test.ts +23 -0
  105. package/src/utils/constants.ts +14 -0
  106. package/src/utils/deepLinkWithFallback.test.ts +243 -0
  107. package/src/utils/deepLinkWithFallback.ts +97 -0
  108. package/src/utils/formatAmount.test.ts +113 -0
  109. package/src/utils/formatAmount.ts +18 -0
  110. package/src/utils/getCurrencyAmountKey.test.ts +44 -0
  111. package/src/utils/getCurrencyAmountKey.ts +15 -0
  112. package/src/utils/getSupportedCurrency.test.ts +51 -0
  113. package/src/utils/getSupportedCurrency.ts +14 -0
  114. package/src/utils/getSupportedLocale.test.ts +64 -0
  115. package/src/utils/getSupportedLocale.ts +16 -0
  116. package/src/utils/iframeHelper.test.ts +450 -0
  117. package/src/utils/iframeHelper.ts +147 -0
  118. package/src/utils/index.ts +36 -0
  119. package/src/utils/merchantId.test.ts +564 -0
  120. package/src/utils/merchantId.ts +122 -0
  121. package/src/utils/sso.ts +126 -0
  122. package/src/utils/ssoUrlListener.test.ts +252 -0
  123. package/src/utils/ssoUrlListener.ts +60 -0
  124. package/src/utils/trackEvent.test.ts +180 -0
  125. package/src/utils/trackEvent.ts +31 -0
  126. package/cdn/bundle.js.LICENSE.txt +0 -10
  127. package/dist/interactions.cjs +0 -1
  128. package/dist/interactions.d.cts +0 -182
  129. package/dist/interactions.d.ts +0 -182
  130. package/dist/interactions.js +0 -1
@@ -0,0 +1,287 @@
1
+ /**
2
+ * Tests for trackPurchaseStatus action
3
+ * Tests webhook registration for purchase tracking
4
+ */
5
+
6
+ import {
7
+ afterEach,
8
+ beforeEach,
9
+ describe,
10
+ expect,
11
+ it,
12
+ vi,
13
+ } from "../../tests/vitest-fixtures";
14
+ import { trackPurchaseStatus } from "./trackPurchaseStatus";
15
+
16
+ describe("trackPurchaseStatus", () => {
17
+ let mockSessionStorage: {
18
+ getItem: ReturnType<typeof vi.fn>;
19
+ setItem: ReturnType<typeof vi.fn>;
20
+ removeItem: ReturnType<typeof vi.fn>;
21
+ };
22
+ let fetchSpy: any;
23
+ let consoleWarnSpy: any;
24
+
25
+ beforeEach(() => {
26
+ // Mock sessionStorage
27
+ mockSessionStorage = {
28
+ getItem: vi.fn(),
29
+ setItem: vi.fn(),
30
+ removeItem: vi.fn(),
31
+ };
32
+ Object.defineProperty(window, "sessionStorage", {
33
+ value: mockSessionStorage,
34
+ writable: true,
35
+ configurable: true,
36
+ });
37
+
38
+ // Mock fetch
39
+ fetchSpy = vi.fn().mockResolvedValue({
40
+ ok: true,
41
+ status: 200,
42
+ });
43
+ global.fetch = fetchSpy;
44
+
45
+ // Mock console.warn
46
+ consoleWarnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
47
+ });
48
+
49
+ afterEach(() => {
50
+ vi.clearAllMocks();
51
+ consoleWarnSpy.mockRestore();
52
+ });
53
+
54
+ describe("successful tracking", () => {
55
+ it("should send POST request with correct parameters", async () => {
56
+ mockSessionStorage.getItem.mockReturnValue("token-123");
57
+
58
+ await trackPurchaseStatus({
59
+ customerId: "cust-456",
60
+ orderId: "order-789",
61
+ token: "purchase-token",
62
+ });
63
+
64
+ expect(fetchSpy).toHaveBeenCalledWith(
65
+ "https://backend.frak.id/interactions/listenForPurchase",
66
+ {
67
+ method: "POST",
68
+ headers: {
69
+ Accept: "application/json",
70
+ "Content-Type": "application/json",
71
+ "x-wallet-sdk-auth": "token-123",
72
+ },
73
+ body: JSON.stringify({
74
+ customerId: "cust-456",
75
+ orderId: "order-789",
76
+ token: "purchase-token",
77
+ }),
78
+ }
79
+ );
80
+ });
81
+
82
+ it("should read interaction token from sessionStorage", async () => {
83
+ mockSessionStorage.getItem.mockReturnValue("my-token");
84
+
85
+ await trackPurchaseStatus({
86
+ customerId: "cust-1",
87
+ orderId: "order-1",
88
+ token: "token-1",
89
+ });
90
+
91
+ expect(mockSessionStorage.getItem).toHaveBeenCalledWith(
92
+ "frak-wallet-interaction-token"
93
+ );
94
+ expect(fetchSpy).toHaveBeenCalled();
95
+ });
96
+
97
+ it("should handle numeric customerId", async () => {
98
+ mockSessionStorage.getItem.mockReturnValue("token-123");
99
+
100
+ await trackPurchaseStatus({
101
+ customerId: 12345,
102
+ orderId: "order-789",
103
+ token: "purchase-token",
104
+ });
105
+
106
+ expect(fetchSpy).toHaveBeenCalledWith(
107
+ expect.any(String),
108
+ expect.objectContaining({
109
+ body: JSON.stringify({
110
+ customerId: 12345,
111
+ orderId: "order-789",
112
+ token: "purchase-token",
113
+ }),
114
+ })
115
+ );
116
+ });
117
+
118
+ it("should handle numeric orderId", async () => {
119
+ mockSessionStorage.getItem.mockReturnValue("token-123");
120
+
121
+ await trackPurchaseStatus({
122
+ customerId: "cust-456",
123
+ orderId: 67890,
124
+ token: "purchase-token",
125
+ });
126
+
127
+ expect(fetchSpy).toHaveBeenCalledWith(
128
+ expect.any(String),
129
+ expect.objectContaining({
130
+ body: JSON.stringify({
131
+ customerId: "cust-456",
132
+ orderId: 67890,
133
+ token: "purchase-token",
134
+ }),
135
+ })
136
+ );
137
+ });
138
+
139
+ it("should handle both numeric customerId and orderId", async () => {
140
+ mockSessionStorage.getItem.mockReturnValue("token-123");
141
+
142
+ await trackPurchaseStatus({
143
+ customerId: 12345,
144
+ orderId: 67890,
145
+ token: "purchase-token",
146
+ });
147
+
148
+ expect(fetchSpy).toHaveBeenCalledWith(
149
+ expect.any(String),
150
+ expect.objectContaining({
151
+ body: JSON.stringify({
152
+ customerId: 12345,
153
+ orderId: 67890,
154
+ token: "purchase-token",
155
+ }),
156
+ })
157
+ );
158
+ });
159
+ });
160
+
161
+ describe("missing interaction token", () => {
162
+ it("should warn when no interaction token found", async () => {
163
+ mockSessionStorage.getItem.mockReturnValue(null);
164
+
165
+ await trackPurchaseStatus({
166
+ customerId: "cust-456",
167
+ orderId: "order-789",
168
+ token: "purchase-token",
169
+ });
170
+
171
+ expect(consoleWarnSpy).toHaveBeenCalledWith(
172
+ "[Frak] No frak session found, skipping purchase check"
173
+ );
174
+ });
175
+
176
+ it("should not send request when no interaction token", async () => {
177
+ mockSessionStorage.getItem.mockReturnValue(null);
178
+
179
+ await trackPurchaseStatus({
180
+ customerId: "cust-456",
181
+ orderId: "order-789",
182
+ token: "purchase-token",
183
+ });
184
+
185
+ expect(fetchSpy).not.toHaveBeenCalled();
186
+ });
187
+
188
+ it("should not send request when interaction token is empty string", async () => {
189
+ mockSessionStorage.getItem.mockReturnValue("");
190
+
191
+ await trackPurchaseStatus({
192
+ customerId: "cust-456",
193
+ orderId: "order-789",
194
+ token: "purchase-token",
195
+ });
196
+
197
+ expect(fetchSpy).not.toHaveBeenCalled();
198
+ });
199
+ });
200
+
201
+ describe("network errors", () => {
202
+ it("should handle fetch rejection", async () => {
203
+ mockSessionStorage.getItem.mockReturnValue("token-123");
204
+ fetchSpy.mockRejectedValue(new Error("Network error"));
205
+
206
+ await expect(
207
+ trackPurchaseStatus({
208
+ customerId: "cust-456",
209
+ orderId: "order-789",
210
+ token: "purchase-token",
211
+ })
212
+ ).rejects.toThrow("Network error");
213
+ });
214
+
215
+ it("should handle fetch with error response", async () => {
216
+ mockSessionStorage.getItem.mockReturnValue("token-123");
217
+ fetchSpy.mockResolvedValue({
218
+ ok: false,
219
+ status: 500,
220
+ });
221
+
222
+ // Function doesn't check response, so it should complete
223
+ await trackPurchaseStatus({
224
+ customerId: "cust-456",
225
+ orderId: "order-789",
226
+ token: "purchase-token",
227
+ });
228
+
229
+ expect(fetchSpy).toHaveBeenCalled();
230
+ });
231
+ });
232
+
233
+ describe("request format", () => {
234
+ it("should include correct headers", async () => {
235
+ mockSessionStorage.getItem.mockReturnValue("my-auth-token");
236
+
237
+ await trackPurchaseStatus({
238
+ customerId: "cust-1",
239
+ orderId: "order-1",
240
+ token: "token-1",
241
+ });
242
+
243
+ expect(fetchSpy).toHaveBeenCalledWith(
244
+ expect.any(String),
245
+ expect.objectContaining({
246
+ headers: {
247
+ Accept: "application/json",
248
+ "Content-Type": "application/json",
249
+ "x-wallet-sdk-auth": "my-auth-token",
250
+ },
251
+ })
252
+ );
253
+ });
254
+
255
+ it("should use POST method", async () => {
256
+ mockSessionStorage.getItem.mockReturnValue("token-123");
257
+
258
+ await trackPurchaseStatus({
259
+ customerId: "cust-1",
260
+ orderId: "order-1",
261
+ token: "token-1",
262
+ });
263
+
264
+ expect(fetchSpy).toHaveBeenCalledWith(
265
+ expect.any(String),
266
+ expect.objectContaining({
267
+ method: "POST",
268
+ })
269
+ );
270
+ });
271
+
272
+ it("should target correct backend URL", async () => {
273
+ mockSessionStorage.getItem.mockReturnValue("token-123");
274
+
275
+ await trackPurchaseStatus({
276
+ customerId: "cust-1",
277
+ orderId: "order-1",
278
+ token: "token-1",
279
+ });
280
+
281
+ expect(fetchSpy).toHaveBeenCalledWith(
282
+ "https://backend.frak.id/interactions/listenForPurchase",
283
+ expect.any(Object)
284
+ );
285
+ });
286
+ });
287
+ });
@@ -0,0 +1,56 @@
1
+ import { getBackendUrl } from "../utils/backendUrl";
2
+
3
+ /**
4
+ * Function used to track the status of a purchase
5
+ * when a purchase is tracked, the `purchaseCompleted` interactions will be automatically send for the user when we receive the purchase confirmation via webhook.
6
+ *
7
+ * @param args.customerId - The customer id that made the purchase (on your side)
8
+ * @param args.orderId - The order id of the purchase (on your side)
9
+ * @param args.token - The token of the purchase
10
+ *
11
+ * @description This function will send a request to the backend to listen for the purchase status.
12
+ *
13
+ * @example
14
+ * async function trackPurchase(checkout) {
15
+ * const payload = {
16
+ * customerId: checkout.order.customer.id,
17
+ * orderId: checkout.order.id,
18
+ * token: checkout.token,
19
+ * };
20
+ *
21
+ * await trackPurchaseStatus(payload);
22
+ * }
23
+ *
24
+ * @remarks
25
+ * - The `trackPurchaseStatus` function requires the `frak-wallet-interaction-token` stored in the session storage to authenticate the request.
26
+ * - This function will print a warning if used in a non-browser environment or if the wallet interaction token is not available.
27
+ */
28
+ export async function trackPurchaseStatus(args: {
29
+ customerId: string | number;
30
+ orderId: string | number;
31
+ token: string;
32
+ }) {
33
+ if (typeof window === "undefined") {
34
+ console.warn("[Frak] No window found, can't track purchase");
35
+ return;
36
+ }
37
+ const interactionToken = window.sessionStorage.getItem(
38
+ "frak-wallet-interaction-token"
39
+ );
40
+ if (!interactionToken) {
41
+ console.warn("[Frak] No frak session found, skipping purchase check");
42
+ return;
43
+ }
44
+
45
+ // Submit the listening request
46
+ const backendUrl = getBackendUrl();
47
+ await fetch(`${backendUrl}/interactions/listenForPurchase`, {
48
+ method: "POST",
49
+ headers: {
50
+ Accept: "application/json",
51
+ "Content-Type": "application/json",
52
+ "x-wallet-sdk-auth": interactionToken,
53
+ },
54
+ body: JSON.stringify(args),
55
+ });
56
+ }