@frak-labs/core-sdk 0.2.0 → 0.2.1-beta.06c52c98

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 (67) hide show
  1. package/README.md +1 -2
  2. package/cdn/bundle.js +55 -3
  3. package/dist/actions.cjs +1 -1
  4. package/dist/actions.d.cts +2 -2
  5. package/dist/actions.d.ts +2 -2
  6. package/dist/actions.js +1 -1
  7. package/dist/bundle.cjs +1 -1
  8. package/dist/bundle.d.cts +4 -4
  9. package/dist/bundle.d.ts +4 -4
  10. package/dist/bundle.js +1 -1
  11. package/dist/{computeLegacyProductId-Raks6FXg.d.cts → computeLegacyProductId-BP-ciVsp.d.cts} +73 -88
  12. package/dist/{computeLegacyProductId-BkyJ4rEY.d.ts → computeLegacyProductId-DiJd7RNo.d.ts} +73 -88
  13. package/dist/index.cjs +1 -1
  14. package/dist/index.d.cts +3 -3
  15. package/dist/index.d.ts +3 -3
  16. package/dist/index.js +1 -1
  17. package/dist/{openSso-BCJGchIb.d.cts → openSso-B8v3Vtnh.d.ts} +157 -52
  18. package/dist/{openSso-DG-_9CED.d.ts → openSso-n_B4LSuW.d.cts} +157 -52
  19. package/dist/setupClient-Dr_UYfTD.cjs +13 -0
  20. package/dist/setupClient-TuhDjVJx.js +13 -0
  21. package/dist/siweAuthenticate-0UPcUqI1.js +1 -0
  22. package/dist/{siweAuthenticate-Btem4QHs.d.ts → siweAuthenticate-CDCsp8EJ.d.ts} +35 -36
  23. package/dist/siweAuthenticate-CfQibjZR.cjs +1 -0
  24. package/dist/{siweAuthenticate-BH7Dn7nZ.d.cts → siweAuthenticate-yITE-iKh.d.cts} +35 -36
  25. package/dist/trackEvent-5j5kkOCj.js +1 -0
  26. package/dist/trackEvent-B2uom25e.cjs +1 -0
  27. package/package.json +8 -8
  28. package/src/actions/displayEmbeddedWallet.ts +6 -2
  29. package/src/actions/displayModal.ts +6 -2
  30. package/src/actions/ensureIdentity.ts +2 -2
  31. package/src/actions/referral/processReferral.test.ts +109 -125
  32. package/src/actions/referral/processReferral.ts +134 -180
  33. package/src/actions/referral/referralInteraction.test.ts +3 -5
  34. package/src/actions/referral/referralInteraction.ts +2 -7
  35. package/src/actions/trackPurchaseStatus.test.ts +32 -20
  36. package/src/actions/trackPurchaseStatus.ts +3 -5
  37. package/src/actions/wrapper/modalBuilder.test.ts +4 -2
  38. package/src/actions/wrapper/modalBuilder.ts +6 -8
  39. package/src/clients/createIFrameFrakClient.ts +146 -25
  40. package/src/clients/transports/iframeLifecycleManager.test.ts +0 -80
  41. package/src/clients/transports/iframeLifecycleManager.ts +0 -44
  42. package/src/index.ts +8 -3
  43. package/src/types/config.ts +10 -3
  44. package/src/types/context.ts +48 -6
  45. package/src/types/index.ts +8 -2
  46. package/src/types/lifecycle/client.ts +22 -27
  47. package/src/types/lifecycle/iframe.ts +0 -8
  48. package/src/types/resolvedConfig.ts +104 -0
  49. package/src/types/rpc/interaction.ts +9 -0
  50. package/src/types/rpc.ts +7 -5
  51. package/src/types/tracking.ts +5 -34
  52. package/src/utils/FrakContext.test.ts +270 -186
  53. package/src/utils/FrakContext.ts +78 -56
  54. package/src/utils/backendUrl.test.ts +2 -2
  55. package/src/utils/backendUrl.ts +1 -1
  56. package/src/utils/index.ts +1 -5
  57. package/src/utils/sdkConfigStore.test.ts +405 -0
  58. package/src/utils/sdkConfigStore.ts +277 -0
  59. package/src/utils/sso.ts +3 -7
  60. package/dist/setupClient-CQrMDGyZ.js +0 -13
  61. package/dist/setupClient-Ccv3XxwL.cjs +0 -13
  62. package/dist/siweAuthenticate-BJHbtty4.js +0 -1
  63. package/dist/siweAuthenticate-Cwj3HP0m.cjs +0 -1
  64. package/dist/trackEvent-M2RLTQ2p.js +0 -1
  65. package/dist/trackEvent-T_R9ER2S.cjs +0 -1
  66. package/src/utils/merchantId.test.ts +0 -653
  67. package/src/utils/merchantId.ts +0 -143
@@ -1,6 +1,6 @@
1
- import { OpenPanel } from "@openpanel/web";
2
- import { LifecycleMessage, RpcClient } from "@frak-labs/frame-connector";
3
1
  import { Address, Hex } from "viem";
2
+ import { LifecycleMessage, RpcClient } from "@frak-labs/frame-connector";
3
+ import { OpenPanel } from "@openpanel/web";
4
4
  import { SiweMessage } from "viem/siwe";
5
5
 
6
6
  //#region src/types/config.d.ts
@@ -31,7 +31,7 @@ type FrakWalletSdkConfig = {
31
31
  /**
32
32
  * Your application name (will be displayed in a few modals and in SSO)
33
33
  */
34
- name: string;
34
+ name?: string;
35
35
  /**
36
36
  * Your merchant ID from the Frak dashboard (UUID format)
37
37
  * Used for referral tracking and analytics
@@ -75,6 +75,13 @@ type FrakWalletSdkConfig = {
75
75
  * @defaultValue window.location.host
76
76
  */
77
77
  domain?: string;
78
+ /**
79
+ * Wait for backend config before rendering components.
80
+ * When true (default), components show a spinner until backend config is resolved.
81
+ * When false, components render immediately with SDK static config / HTML attributes.
82
+ * @defaultValue true
83
+ */
84
+ waitForBackendConfig?: boolean;
78
85
  };
79
86
  /**
80
87
  * Custom i18n configuration for the modal
@@ -111,19 +118,97 @@ type FrakWalletSdkConfig = {
111
118
  */
112
119
  type I18nConfig = Record<Language, LocalizedI18nConfig> | LocalizedI18nConfig;
113
120
  /**
114
- * A localized i18n config
121
+ * A localized i18n config (inline objects only — URL-based i18n removed)
115
122
  * @category Config
116
123
  */
117
- type LocalizedI18nConfig = `${string}.css` | {
124
+ type LocalizedI18nConfig = {
118
125
  [key: string]: string;
119
126
  };
120
127
  //#endregion
128
+ //#region src/types/resolvedConfig.d.ts
129
+ /**
130
+ * Response from the merchant resolve endpoint
131
+ * @category Config
132
+ */
133
+ type MerchantConfigResponse = {
134
+ merchantId: string;
135
+ name: string;
136
+ domain: string;
137
+ allowedDomains: string[];
138
+ sdkConfig?: ResolvedSdkConfig;
139
+ };
140
+ /**
141
+ * Resolved placement config from backend
142
+ * Translations already flattened: default + lang-specific merged into one record
143
+ * @category Config
144
+ */
145
+ type ResolvedPlacement = {
146
+ /** Per-component configuration within this placement */components?: {
147
+ buttonShare?: {
148
+ text?: string;
149
+ noRewardText?: string;
150
+ clickAction?: "embedded-wallet" | "share-modal";
151
+ useReward?: boolean;
152
+ css?: string;
153
+ };
154
+ buttonWallet?: {
155
+ position?: "bottom-right" | "bottom-left";
156
+ css?: string;
157
+ };
158
+ openInApp?: {
159
+ text?: string;
160
+ css?: string;
161
+ };
162
+ };
163
+ targetInteraction?: string; /** Already flattened: default + lang-specific merged into one record */
164
+ translations?: Record<string, string>; /** Global placement CSS (applied to modals/listener) */
165
+ css?: string;
166
+ };
167
+ /**
168
+ * Resolved SDK config from backend `/resolve` endpoint
169
+ * Language resolution and translation merging already applied
170
+ * @category Config
171
+ */
172
+ type ResolvedSdkConfig = {
173
+ name?: string;
174
+ logoUrl?: string;
175
+ homepageLink?: string;
176
+ currency?: Currency;
177
+ lang?: Language; /** When true, all SDK components should be hidden */
178
+ hidden?: boolean;
179
+ css?: string;
180
+ translations?: Record<string, string>;
181
+ placements?: Record<string, ResolvedPlacement>;
182
+ };
183
+ /**
184
+ * Internal SDK config store state
185
+ * Merged config: backend > SDK static > defaults
186
+ * Components subscribe to this reactively
187
+ * @category Config
188
+ */
189
+ type SdkResolvedConfig = {
190
+ /** Whether the backend config has been resolved */isResolved: boolean; /** Merchant ID from resolution */
191
+ merchantId: string; /** Domain returned by the resolve endpoint */
192
+ domain?: string; /** Domains allowed for this merchant (used by iframe trust check) */
193
+ allowedDomains?: string[]; /** Whether the resolve returned a backend sdkConfig object */
194
+ hasRawSdkConfig?: boolean; /** Merged metadata fields */
195
+ name?: string;
196
+ logoUrl?: string;
197
+ homepageLink?: string;
198
+ lang?: Language;
199
+ currency?: Currency; /** When true, all SDK components should be hidden */
200
+ hidden?: boolean; /** Global CSS from backend config (passed to iframe) */
201
+ css?: string; /** Global translations (for reference / component fallback) */
202
+ translations?: Record<string, string>; /** Named placements (keyed by placement ID) */
203
+ placements?: Record<string, ResolvedPlacement>;
204
+ };
205
+ //#endregion
121
206
  //#region src/types/lifecycle/client.d.ts
122
207
  /**
123
208
  * Event related to the iframe lifecycle
124
209
  * @ignore
125
210
  */
126
- type ClientLifecycleEvent = CustomCssEvent | CustomI18nEvent | RestoreBackupEvent | HearbeatEvent | HandshakeResponse | SsoRedirectCompleteEvent | DeepLinkFailedEvent;
211
+ type ClientLifecycleEvent = CustomCssEvent | CustomI18nEvent | RestoreBackupEvent | HearbeatEvent | SsoRedirectCompleteEvent | DeepLinkFailedEvent | ResolvedConfigEvent;
127
212
  type CustomCssEvent = {
128
213
  clientLifecycle: "modal-css";
129
214
  data: {
@@ -146,30 +231,6 @@ type HearbeatEvent = {
146
231
  clientLifecycle: "heartbeat";
147
232
  data?: never;
148
233
  };
149
- type HandshakeResponse = {
150
- clientLifecycle: "handshake-response";
151
- data: {
152
- token: string;
153
- currentUrl: string;
154
- /**
155
- * Pending merge token extracted from URL (?fmt= parameter)
156
- * When present, listener should execute identity merge in background
157
- * URL is cleaned after handshake response is sent
158
- */
159
- pendingMergeToken?: string;
160
- /**
161
- * Client ID for identity tracking (belt & suspenders fallback)
162
- * Primary delivery is via iframe URL query param; handshake is backup for SSR
163
- */
164
- clientId?: string;
165
- /**
166
- * Explicit domain from SDK config (FrakWalletSdkConfig.domain)
167
- * When present, listener should prefer this over URL-derived domain
168
- * for merchant resolution (handles proxied/tunneled environments)
169
- */
170
- configDomain?: string;
171
- };
172
- };
173
234
  type SsoRedirectCompleteEvent = {
174
235
  clientLifecycle: "sso-redirect-complete";
175
236
  data: {
@@ -182,6 +243,21 @@ type DeepLinkFailedEvent = {
182
243
  originalUrl: string;
183
244
  };
184
245
  };
246
+ type ResolvedConfigEvent = {
247
+ clientLifecycle: "resolved-config";
248
+ data: {
249
+ merchantId: string; /** The domain the backend resolved this config for */
250
+ domain: string; /** All domains registered for this merchant (for domain proof) */
251
+ allowedDomains: string[]; /** Full URL of the parent page (for interaction tracking) */
252
+ sourceUrl: string;
253
+ /**
254
+ * Pending merge token extracted from URL (?fmt= parameter).
255
+ * When present, listener should execute identity merge in background.
256
+ */
257
+ pendingMergeToken?: string;
258
+ sdkConfig?: ResolvedSdkConfig;
259
+ };
260
+ };
185
261
  //#endregion
186
262
  //#region src/types/lifecycle/iframe.d.ts
187
263
  /**
@@ -191,19 +267,13 @@ type DeepLinkFailedEvent = {
191
267
  type IFrameLifecycleEvent = {
192
268
  iframeLifecycle: "connected" | "show" | "hide" | "remove-backup";
193
269
  data?: never;
194
- } | DoBackupEvent | HandshakeRequestEvent | RedirectRequestEvent;
270
+ } | DoBackupEvent | RedirectRequestEvent;
195
271
  type DoBackupEvent = {
196
272
  iframeLifecycle: "do-backup";
197
273
  data: {
198
274
  backup?: string;
199
275
  };
200
276
  };
201
- type HandshakeRequestEvent = {
202
- iframeLifecycle: "handshake";
203
- data: {
204
- token: string;
205
- };
206
- };
207
277
  type RedirectRequestEvent = {
208
278
  iframeLifecycle: "redirect";
209
279
  data: {
@@ -679,8 +749,11 @@ type DisplayEmbeddedWalletResultType = {
679
749
  * @group RPC Schema
680
750
  */
681
751
  type SendInteractionParamsType = {
682
- type: "arrival";
752
+ type: "arrival"; /** @deprecated V1 legacy — use referrerClientId for v2 */
683
753
  referrerWallet?: Address;
754
+ referrerClientId?: string;
755
+ referrerMerchantId?: string; /** Epoch seconds timestamp from the referral link creation */
756
+ referralTimestamp?: number;
684
757
  landingUrl?: string;
685
758
  utmSource?: string;
686
759
  utmMedium?: string;
@@ -688,7 +761,9 @@ type SendInteractionParamsType = {
688
761
  utmTerm?: string;
689
762
  utmContent?: string;
690
763
  } | {
691
- type: "sharing";
764
+ type: "sharing"; /** Epoch seconds timestamp matching the V2 context `t` field embedded in the referral link URL, used for backend correlation */
765
+ sharingTimestamp?: number; /** Merchant order ID linking this sharing event to a purchase (stays server-side, never in URL) */
766
+ purchaseId?: string;
692
767
  } | {
693
768
  type: "custom";
694
769
  customType: string;
@@ -812,7 +887,7 @@ type WalletNotConnected = {
812
887
  * - Response Type: stream (emits updates when wallet status changes)
813
888
  *
814
889
  * #### frak_displayModal
815
- * - Params: [requests: {@link ModalRpcStepsInput}, metadata?: {@link ModalRpcMetadata}, configMetadata: {@link FrakWalletSdkConfig}["metadata"]]
890
+ * - Params: [requests: {@link ModalRpcStepsInput}, metadata?: {@link ModalRpcMetadata}, configMetadata: {@link FrakWalletSdkConfig}["metadata"], placement?: string]
816
891
  * - Returns: {@link ModalRpcStepsResultType}
817
892
  * - Response Type: promise (one-shot)
818
893
  *
@@ -827,7 +902,7 @@ type WalletNotConnected = {
827
902
  * - Response Type: promise (one-shot)
828
903
  *
829
904
  * #### frak_displayEmbeddedWallet
830
- * - Params: [request: {@link DisplayEmbeddedWalletParamsType}, metadata: {@link FrakWalletSdkConfig}["metadata"]]
905
+ * - Params: [request: {@link DisplayEmbeddedWalletParamsType}, metadata: {@link FrakWalletSdkConfig}["metadata"], placement?: string]
831
906
  * - Returns: {@link DisplayEmbeddedWalletResultType}
832
907
  * - Response Type: promise (one-shot)
833
908
  */
@@ -847,7 +922,7 @@ type IFrameRpcSchema = [
847
922
  */
848
923
  {
849
924
  Method: "frak_displayModal";
850
- Parameters: [requests: ModalRpcStepsInput, metadata: ModalRpcMetadata | undefined, configMetadata: FrakWalletSdkConfig["metadata"]];
925
+ Parameters: [requests: ModalRpcStepsInput, metadata: ModalRpcMetadata | undefined, configMetadata: FrakWalletSdkConfig["metadata"], placement?: string];
851
926
  ReturnType: ModalRpcStepsResultType;
852
927
  },
853
928
  /**
@@ -857,7 +932,7 @@ type IFrameRpcSchema = [
857
932
  */
858
933
  {
859
934
  Method: "frak_prepareSso";
860
- Parameters: [params: PrepareSsoParamsType, name: string, customCss?: string];
935
+ Parameters: [params: PrepareSsoParamsType, name?: string, customCss?: string];
861
936
  ReturnType: PrepareSsoReturnType;
862
937
  },
863
938
  /**
@@ -868,7 +943,7 @@ type IFrameRpcSchema = [
868
943
  */
869
944
  {
870
945
  Method: "frak_openSso";
871
- Parameters: [params: OpenSsoParamsType, name: string, customCss?: string];
946
+ Parameters: [params: OpenSsoParamsType, name?: string, customCss?: string];
872
947
  ReturnType: OpenSsoReturnType;
873
948
  },
874
949
  /**
@@ -889,14 +964,14 @@ type IFrameRpcSchema = [
889
964
  */
890
965
  {
891
966
  Method: "frak_displayEmbeddedWallet";
892
- Parameters: [request: DisplayEmbeddedWalletParamsType, metadata: FrakWalletSdkConfig["metadata"]];
967
+ Parameters: [request: DisplayEmbeddedWalletParamsType, metadata: FrakWalletSdkConfig["metadata"], placement?: string];
893
968
  ReturnType: DisplayEmbeddedWalletResultType;
894
969
  },
895
970
  /**
896
971
  * Method to send interactions (arrival, sharing, custom events)
897
972
  * Fire-and-forget method - no return value expected
898
973
  * merchantId is resolved from context
899
- * clientId is passed via metadata as safeguard against handshake race condition
974
+ * clientId is passed via metadata as safeguard against race conditions
900
975
  */
901
976
  {
902
977
  Method: "frak_sendInteraction";
@@ -951,15 +1026,45 @@ type FrakClient = {
951
1026
  //#endregion
952
1027
  //#region src/types/context.d.ts
953
1028
  /**
954
- * The current Frak Context
1029
+ * V1 (legacy) Frak Context — contains only the referrer wallet address.
1030
+ * Used for backward compatibility with old sharing links.
1031
+ * @ignore
1032
+ */
1033
+ type FrakContextV1 = {
1034
+ /** Referrer wallet address */r: Address;
1035
+ };
1036
+ /**
1037
+ * V2 Frak Context — anonymous-first referral context.
1038
+ * Contains the sharer's clientId, merchantId, and link creation timestamp.
1039
+ * @ignore
1040
+ */
1041
+ type FrakContextV2 = {
1042
+ /** Version discriminator */v: 2; /** Sharer's anonymous clientId (UUID from localStorage) */
1043
+ c: string; /** Merchant ID (UUID) */
1044
+ m: string; /** Link creation timestamp (epoch seconds) */
1045
+ t: number;
1046
+ };
1047
+ /**
1048
+ * The current Frak Context — union of all versions.
955
1049
  *
956
- * For now, only contain a referrer address.
1050
+ * - No `v` field V1 (legacy wallet address)
1051
+ * - `v: 2` → V2 (anonymous clientId-based)
957
1052
  *
958
1053
  * @ignore
959
1054
  */
960
- type FrakContext = {
961
- r: Address;
962
- };
1055
+ type FrakContext = FrakContextV1 | FrakContextV2;
1056
+ /**
1057
+ * Type guard: check if a context is V1 (legacy wallet address).
1058
+ * @param ctx - The Frak context to check
1059
+ * @returns True if the context is a V1 context
1060
+ */
1061
+ declare function isV1Context(ctx: FrakContext): ctx is FrakContextV1;
1062
+ /**
1063
+ * Type guard: check if a context is V2 (anonymous clientId-based).
1064
+ * @param ctx - The Frak context to check
1065
+ * @returns True if the context is a V2 context
1066
+ */
1067
+ declare function isV2Context(ctx: FrakContext): ctx is FrakContextV2;
963
1068
  //#endregion
964
1069
  //#region src/actions/openSso.d.ts
965
1070
  declare const ssoPopupFeatures = "menubar=no,status=no,scrollbars=no,fullscreen=no,width=500, height=800";
@@ -1019,4 +1124,4 @@ declare const ssoPopupName = "frak-sso";
1019
1124
  */
1020
1125
  declare function openSso(client: FrakClient, args: OpenSsoParamsType): Promise<OpenSsoReturnType>;
1021
1126
  //#endregion
1022
- export { SiweAuthenticateReturnType as A, ModalStepMetadata as B, ModalRpcStepsInput as C, SendTransactionReturnType as D, SendTransactionModalStepType as E, PrepareSsoParamsType as F, FrakWalletSdkConfig as G, IFrameLifecycleEvent as H, PrepareSsoReturnType as I, LocalizedI18nConfig as J, I18nConfig as K, SsoMetadata as L, LoginModalStepType as M, OpenSsoParamsType as N, SendTransactionTxType as O, OpenSsoReturnType as P, FinalActionType as R, ModalRpcMetadata as S, ModalStepTypes as T, ClientLifecycleEvent as U, InteractionTypeKey as V, Currency as W, LoggedOutEmbeddedView as _, FrakClient as a, LoggedInEmbeddedView as b, IFrameRpcSchema as c, GetMerchantInformationReturnType as d, RewardTier as f, DisplayEmbeddedWalletResultType as g, DisplayEmbeddedWalletParamsType as h, FrakContext as i, SiweAuthenticationParams as j, SiweAuthenticateModalStepType as k, WalletStatusReturnType as l, SendInteractionParamsType as m, ssoPopupFeatures as n, FrakLifecycleEvent as o, TokenAmountType as p, Language as q, ssoPopupName as r, IFrameTransport as s, openSso as t, EstimatedReward as u, EmbeddedViewActionReferred as v, ModalRpcStepsResultType as w, DisplayModalParamsType as x, EmbeddedViewActionSharing as y, FinalModalStepType as z };
1127
+ export { FrakWalletSdkConfig as $, SendTransactionModalStepType as A, PrepareSsoReturnType as B, EmbeddedViewActionSharing as C, ModalRpcStepsInput as D, ModalRpcMetadata as E, SiweAuthenticationParams as F, InteractionTypeKey as G, FinalActionType as H, LoginModalStepType as I, MerchantConfigResponse as J, IFrameLifecycleEvent as K, OpenSsoParamsType as L, SendTransactionTxType as M, SiweAuthenticateModalStepType as N, ModalRpcStepsResultType as O, SiweAuthenticateReturnType as P, Currency as Q, OpenSsoReturnType as R, EmbeddedViewActionReferred as S, DisplayModalParamsType as T, FinalModalStepType as U, SsoMetadata as V, ModalStepMetadata as W, ResolvedSdkConfig as X, ResolvedPlacement as Y, SdkResolvedConfig as Z, TokenAmountType as _, FrakContextV1 as a, DisplayEmbeddedWalletResultType as b, isV2Context as c, IFrameTransport as d, I18nConfig as et, IFrameRpcSchema as f, RewardTier as g, GetMerchantInformationReturnType as h, FrakContext as i, SendTransactionReturnType as j, ModalStepTypes as k, FrakClient as l, EstimatedReward as m, ssoPopupFeatures as n, LocalizedI18nConfig as nt, FrakContextV2 as o, WalletStatusReturnType as p, ClientLifecycleEvent as q, ssoPopupName as r, isV1Context as s, openSso as t, Language as tt, FrakLifecycleEvent as u, SendInteractionParamsType as v, LoggedInEmbeddedView as w, LoggedOutEmbeddedView as x, DisplayEmbeddedWalletParamsType as y, PrepareSsoParamsType as z };