@frak-labs/core-sdk 0.2.1 → 1.0.0

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 (77) hide show
  1. package/README.md +1 -2
  2. package/cdn/bundle.js +3 -3
  3. package/dist/actions-D4aBXbdp.cjs +1 -0
  4. package/dist/actions-Dq_uN-wn.js +1 -0
  5. package/dist/actions.cjs +1 -1
  6. package/dist/actions.d.cts +3 -3
  7. package/dist/actions.d.ts +3 -3
  8. package/dist/actions.js +1 -1
  9. package/dist/bundle.cjs +1 -1
  10. package/dist/bundle.d.cts +4 -4
  11. package/dist/bundle.d.ts +4 -4
  12. package/dist/bundle.js +1 -1
  13. package/dist/{computeLegacyProductId-CCAZvLa5.d.cts → index-BV5D9DsW.d.ts} +91 -37
  14. package/dist/{siweAuthenticate-CnCZ7mok.d.ts → index-BphwTmKA.d.cts} +122 -8
  15. package/dist/{computeLegacyProductId-b5cUWdAm.d.ts → index-Dwmo109y.d.cts} +91 -37
  16. package/dist/{siweAuthenticate-CVigMOxz.d.cts → index-_f8EuN_1.d.ts} +122 -8
  17. package/dist/index.cjs +1 -1
  18. package/dist/index.d.cts +3 -3
  19. package/dist/index.d.ts +3 -3
  20. package/dist/index.js +1 -1
  21. package/dist/{openSso-B0g7-807.d.cts → openSso-BwEK2M98.d.cts} +283 -44
  22. package/dist/{openSso-CMzwvaCa.d.ts → openSso-C1Wzl5-i.d.ts} +283 -44
  23. package/dist/src-B1eliIi6.cjs +13 -0
  24. package/dist/src-C0UH1GsN.js +13 -0
  25. package/dist/trackEvent-BqJqRZ-u.cjs +1 -0
  26. package/dist/trackEvent-Bqq4jd6R.js +1 -0
  27. package/package.json +11 -12
  28. package/src/actions/displayEmbeddedWallet.ts +6 -2
  29. package/src/actions/displayModal.ts +6 -2
  30. package/src/actions/displaySharingPage.ts +49 -0
  31. package/src/actions/ensureIdentity.ts +2 -2
  32. package/src/actions/getMerchantInformation.test.ts +13 -1
  33. package/src/actions/getMerchantInformation.ts +20 -5
  34. package/src/actions/getMergeToken.ts +33 -0
  35. package/src/actions/getUserReferralStatus.ts +42 -0
  36. package/src/actions/index.ts +8 -1
  37. package/src/actions/referral/setupReferral.test.ts +79 -0
  38. package/src/actions/referral/setupReferral.ts +32 -0
  39. package/src/actions/trackPurchaseStatus.test.ts +32 -20
  40. package/src/actions/trackPurchaseStatus.ts +3 -5
  41. package/src/actions/wrapper/modalBuilder.test.ts +4 -2
  42. package/src/actions/wrapper/modalBuilder.ts +6 -8
  43. package/src/clients/createIFrameFrakClient.ts +151 -27
  44. package/src/clients/transports/iframeLifecycleManager.test.ts +14 -94
  45. package/src/clients/transports/iframeLifecycleManager.ts +35 -53
  46. package/src/index.ts +17 -4
  47. package/src/stubs/rrweb.ts +9 -0
  48. package/src/types/config.ts +10 -3
  49. package/src/types/index.ts +13 -1
  50. package/src/types/lifecycle/client.ts +22 -27
  51. package/src/types/lifecycle/iframe.ts +7 -8
  52. package/src/types/resolvedConfig.ts +128 -0
  53. package/src/types/rpc/displaySharingPage.ts +82 -0
  54. package/src/types/rpc/embedded/index.ts +1 -1
  55. package/src/types/rpc/interaction.ts +4 -0
  56. package/src/types/rpc/userReferralStatus.ts +20 -0
  57. package/src/types/rpc.ts +54 -5
  58. package/src/utils/backendUrl.test.ts +2 -2
  59. package/src/utils/backendUrl.ts +1 -1
  60. package/src/utils/cache/index.ts +7 -0
  61. package/src/utils/cache/lruMap.test.ts +55 -0
  62. package/src/utils/cache/lruMap.ts +38 -0
  63. package/src/utils/cache/withCache.test.ts +168 -0
  64. package/src/utils/cache/withCache.ts +124 -0
  65. package/src/utils/inAppBrowser.ts +60 -0
  66. package/src/utils/index.ts +6 -4
  67. package/src/utils/sdkConfigStore.test.ts +405 -0
  68. package/src/utils/sdkConfigStore.ts +263 -0
  69. package/src/utils/sso.ts +3 -7
  70. package/dist/setupClient-BduY6Sym.cjs +0 -13
  71. package/dist/setupClient-ftmdQ-I8.js +0 -13
  72. package/dist/siweAuthenticate-BWmI2_TN.cjs +0 -1
  73. package/dist/siweAuthenticate-zczqxm0a.js +0 -1
  74. package/dist/trackEvent-CeLFVzZn.js +0 -1
  75. package/dist/trackEvent-Ew5r5zfI.cjs +0 -1
  76. package/src/utils/merchantId.test.ts +0 -653
  77. package/src/utils/merchantId.ts +0 -143
@@ -1,143 +0,0 @@
1
- /**
2
- * Merchant ID utilities for auto-fetching from backend
3
- */
4
-
5
- import { getBackendUrl } from "./backendUrl";
6
-
7
- const MERCHANT_ID_STORAGE_KEY = "frak-merchant-id";
8
-
9
- /**
10
- * Response from the merchant lookup endpoint
11
- */
12
- type MerchantLookupResponse = {
13
- merchantId: string;
14
- name: string;
15
- domain: string;
16
- };
17
-
18
- /**
19
- * In-memory cache for merchantId lookups
20
- * Persists for the session to avoid repeated API calls
21
- */
22
- let cachedMerchantId: string | undefined;
23
- let cachePromise: Promise<string | undefined> | undefined;
24
-
25
- /**
26
- * Fetch merchantId from backend by domain
27
- *
28
- * @param domain - The domain to lookup (defaults to current hostname)
29
- * @param walletUrl - Optional wallet URL to derive backend URL
30
- * @returns The merchantId if found, undefined otherwise
31
- *
32
- * @example
33
- * ```ts
34
- * const merchantId = await fetchMerchantId("shop.example.com");
35
- * if (merchantId) {
36
- * // Use merchantId for tracking
37
- * }
38
- * ```
39
- */
40
- export async function fetchMerchantId(
41
- domain?: string,
42
- walletUrl?: string
43
- ): Promise<string | undefined> {
44
- // Use in-memory cache if available
45
- if (cachedMerchantId) {
46
- return cachedMerchantId;
47
- }
48
-
49
- // Check sessionStorage (survives page navigations)
50
- if (typeof window !== "undefined") {
51
- const stored = window.sessionStorage.getItem(MERCHANT_ID_STORAGE_KEY);
52
- if (stored) {
53
- cachedMerchantId = stored;
54
- return stored;
55
- }
56
- }
57
-
58
- // If a fetch is already in progress, wait for it
59
- if (cachePromise) {
60
- return cachePromise;
61
- }
62
-
63
- // Start the fetch and cache the promise
64
- cachePromise = fetchMerchantIdInternal(domain, walletUrl);
65
- const result = await cachePromise;
66
- cachePromise = undefined;
67
- return result;
68
- }
69
-
70
- /**
71
- * Internal fetch logic
72
- */
73
- async function fetchMerchantIdInternal(
74
- domain?: string,
75
- walletUrl?: string
76
- ): Promise<string | undefined> {
77
- const targetDomain =
78
- domain ??
79
- (typeof window !== "undefined" ? window.location.hostname : "");
80
- if (!targetDomain) {
81
- return undefined;
82
- }
83
-
84
- try {
85
- const backendUrl = getBackendUrl(walletUrl);
86
- const response = await fetch(
87
- `${backendUrl}/user/merchant/resolve?domain=${encodeURIComponent(targetDomain)}`
88
- );
89
-
90
- if (!response.ok) {
91
- console.warn(
92
- `[Frak SDK] Merchant lookup failed for domain ${targetDomain}: ${response.status}`
93
- );
94
- return undefined;
95
- }
96
-
97
- const data = (await response.json()) as MerchantLookupResponse;
98
- cachedMerchantId = data.merchantId;
99
- // Persist to sessionStorage so it survives page navigations
100
- if (typeof window !== "undefined") {
101
- window.sessionStorage.setItem(
102
- MERCHANT_ID_STORAGE_KEY,
103
- data.merchantId
104
- );
105
- }
106
- return cachedMerchantId;
107
- } catch (error) {
108
- console.warn("[Frak SDK] Failed to fetch merchantId:", error);
109
- return undefined;
110
- }
111
- }
112
-
113
- /**
114
- * Clear the cached merchantId
115
- * Useful for testing or when switching domains
116
- */
117
- export function clearMerchantIdCache(): void {
118
- cachedMerchantId = undefined;
119
- cachePromise = undefined;
120
- if (typeof window !== "undefined") {
121
- window.sessionStorage.removeItem(MERCHANT_ID_STORAGE_KEY);
122
- }
123
- }
124
-
125
- /**
126
- * Get merchantId from config or auto-fetch from backend
127
- *
128
- * @param config - The SDK config that may contain merchantId
129
- * @param walletUrl - Optional wallet URL to derive backend URL
130
- * @returns The merchantId if available (from config or fetch), undefined otherwise
131
- */
132
- export async function resolveMerchantId(
133
- config: { metadata?: { merchantId?: string } },
134
- walletUrl?: string
135
- ): Promise<string | undefined> {
136
- // First, check config
137
- if (config.metadata?.merchantId) {
138
- return config.metadata.merchantId;
139
- }
140
-
141
- // Otherwise, try to fetch from backend
142
- return fetchMerchantId(undefined, walletUrl);
143
- }