@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,120 @@
1
+ import type {
2
+ DisplayModalParamsType,
3
+ FrakClient,
4
+ ModalRpcStepsResultType,
5
+ ModalStepTypes,
6
+ } from "../types";
7
+
8
+ /**
9
+ * Function used to display a modal
10
+ * @param client - The current Frak Client
11
+ * @param args
12
+ * @param args.steps - The different steps of the modal
13
+ * @param args.metadata - The metadata for the modal (customization, etc)
14
+ * @returns The result of each modal steps
15
+ *
16
+ * @description This function will display a modal to the user with the provided steps and metadata.
17
+ *
18
+ * @remarks
19
+ * - The UI of the displayed modal can be configured with the `customCss` property in the `customizations.css` field of the top-level config.
20
+ * - The `login` step will be automatically skipped if the user is already logged in. It's safe to include this step in all cases to ensure proper user state.
21
+ * - Steps are automatically reordered in the following sequence:
22
+ * 1. `login` (if needed)
23
+ * 2. All other steps in the order specified
24
+ * 3. `success` (if included, always last)
25
+ *
26
+ * @example
27
+ * Simple sharing modal with steps:
28
+ * 1. Login (Skipped if already logged in)
29
+ * 2. Display a success message with sharing link option
30
+ *
31
+ * ```ts
32
+ * const results = await displayModal(frakConfig, {
33
+ * steps: {
34
+ * // Simple login with no SSO, nor customization
35
+ * login: { allowSso: false },
36
+ * // Success message
37
+ * final: {
38
+ * action: { key: "reward" },
39
+ * // Skip this step, it will be only displayed in the stepper within the modal
40
+ * autoSkip: true,
41
+ * },
42
+ * },
43
+ * });
44
+ *
45
+ * console.log("Login step - wallet", results.login.wallet);
46
+ * ```
47
+ *
48
+ * @example
49
+ * A full modal example, with a few customization options, with the steps:
50
+ * 1. Login (Skipped if already logged in)
51
+ * 2. Authenticate via SIWE
52
+ * 3. Send a transaction
53
+ * 4. Display a success message with sharing link options
54
+ *
55
+ * ```ts
56
+ * const results = await displayModal(frakConfig, {
57
+ * steps: {
58
+ * // Login step
59
+ * login: {
60
+ * allowSso: true,
61
+ * ssoMetadata: {
62
+ * logoUrl: "https://my-app.com/logo.png",
63
+ * homepageLink: "https://my-app.com",
64
+ * },
65
+ * },
66
+ * // Siwe authentication
67
+ * siweAuthenticate: {
68
+ * siwe: {
69
+ * domain: "my-app.com",
70
+ * uri: "https://my-app.com/",
71
+ * nonce: generateSiweNonce(),
72
+ * version: "1",
73
+ * },
74
+ * },
75
+ * // Send batched transaction
76
+ * sendTransaction: {
77
+ * tx: [
78
+ * { to: "0xdeadbeef", data: "0xdeadbeef" },
79
+ * { to: "0xdeadbeef", data: "0xdeadbeef" },
80
+ * ],
81
+ * },
82
+ * // Success message with sharing options
83
+ * final: {
84
+ * action: {
85
+ * key: "sharing",
86
+ * options: {
87
+ * popupTitle: "Share the app",
88
+ * text: "Discover my super app website",
89
+ * link: "https://my-app.com",
90
+ * },
91
+ * },
92
+ * dismissedMetadata: {
93
+ * title: "Dismiss",
94
+ * description: "You won't be rewarded for this sharing action",
95
+ * },
96
+ * },
97
+ * },
98
+ * metadata: {
99
+ * // Header of desktop modals
100
+ * header: {
101
+ * title: "My-App",
102
+ * icon: "https://my-app.com/logo.png",
103
+ * },
104
+ * // Context that will be present in every modal steps
105
+ * context: "My-app overkill flow",
106
+ * },
107
+ * });
108
+ * ```
109
+ */
110
+ export async function displayModal<
111
+ T extends ModalStepTypes[] = ModalStepTypes[],
112
+ >(
113
+ client: FrakClient,
114
+ { steps, metadata }: DisplayModalParamsType<T>
115
+ ): Promise<ModalRpcStepsResultType<T>> {
116
+ return (await client.request({
117
+ method: "frak_displayModal",
118
+ params: [steps, metadata, client.config.metadata],
119
+ })) as ModalRpcStepsResultType<T>;
120
+ }
@@ -0,0 +1,116 @@
1
+ import type { Address, Hex } from "viem";
2
+ import { describe, expect, it, vi } from "../../tests/vitest-fixtures";
3
+ import type { FrakClient, GetMerchantInformationReturnType } from "../types";
4
+ import { getMerchantInformation } from "./getMerchantInformation";
5
+
6
+ describe("getMerchantInformation", () => {
7
+ describe("success cases", () => {
8
+ it("should call client.request with correct method", async () => {
9
+ const mockResponse: GetMerchantInformationReturnType = {
10
+ id: "0x1234567890123456789012345678901234567890123456789012345678901234" as Hex,
11
+ onChainMetadata: {
12
+ name: "Test Merchant",
13
+ domain: "example.com",
14
+ },
15
+ rewards: [],
16
+ };
17
+
18
+ const mockClient = {
19
+ request: vi.fn().mockResolvedValue(mockResponse),
20
+ } as unknown as FrakClient;
21
+
22
+ await getMerchantInformation(mockClient);
23
+
24
+ expect(mockClient.request).toHaveBeenCalledWith({
25
+ method: "frak_getMerchantInformation",
26
+ });
27
+ });
28
+
29
+ it("should return merchant information", async () => {
30
+ const mockResponse: GetMerchantInformationReturnType = {
31
+ id: "0x1234567890123456789012345678901234567890123456789012345678901234" as Hex,
32
+ onChainMetadata: {
33
+ name: "Test Merchant",
34
+ domain: "example.com",
35
+ },
36
+ rewards: [],
37
+ };
38
+
39
+ const mockClient = {
40
+ request: vi.fn().mockResolvedValue(mockResponse),
41
+ } as unknown as FrakClient;
42
+
43
+ const result = await getMerchantInformation(mockClient);
44
+
45
+ expect(result).toEqual(mockResponse);
46
+ });
47
+
48
+ it("should return merchant information with rewards", async () => {
49
+ const mockResponse: GetMerchantInformationReturnType = {
50
+ id: "0x1234567890123456789012345678901234567890123456789012345678901234" as Hex,
51
+ onChainMetadata: {
52
+ name: "Test Merchant",
53
+ domain: "example.com",
54
+ },
55
+ rewards: [
56
+ {
57
+ token: "0x1234567890123456789012345678901234567890" as Address,
58
+ campaignId: "campaign-1",
59
+ interactionTypeKey: "referral",
60
+ referrer: {
61
+ payoutType: "fixed",
62
+ amount: {
63
+ amount: 10,
64
+ eurAmount: 1,
65
+ usdAmount: 1.2,
66
+ gbpAmount: 0.9,
67
+ },
68
+ },
69
+ referee: {
70
+ payoutType: "fixed",
71
+ amount: {
72
+ amount: 5,
73
+ eurAmount: 0.5,
74
+ usdAmount: 0.6,
75
+ gbpAmount: 0.45,
76
+ },
77
+ },
78
+ },
79
+ ],
80
+ };
81
+
82
+ const mockClient = {
83
+ request: vi.fn().mockResolvedValue(mockResponse),
84
+ } as unknown as FrakClient;
85
+
86
+ const result = await getMerchantInformation(mockClient);
87
+
88
+ expect(result).toEqual(mockResponse);
89
+ expect(result.rewards).toHaveLength(1);
90
+ });
91
+ });
92
+
93
+ describe("error handling", () => {
94
+ it("should propagate errors from client.request", async () => {
95
+ const error = new Error("RPC request failed");
96
+ const mockClient = {
97
+ request: vi.fn().mockRejectedValue(error),
98
+ } as unknown as FrakClient;
99
+
100
+ await expect(getMerchantInformation(mockClient)).rejects.toThrow(
101
+ "RPC request failed"
102
+ );
103
+ });
104
+
105
+ it("should handle network timeout errors", async () => {
106
+ const error = new Error("Request timeout");
107
+ const mockClient = {
108
+ request: vi.fn().mockRejectedValue(error),
109
+ } as unknown as FrakClient;
110
+
111
+ await expect(getMerchantInformation(mockClient)).rejects.toThrow(
112
+ "Request timeout"
113
+ );
114
+ });
115
+ });
116
+ });
@@ -0,0 +1,9 @@
1
+ import type { FrakClient, GetMerchantInformationReturnType } from "../types";
2
+
3
+ export async function getMerchantInformation(
4
+ client: FrakClient
5
+ ): Promise<GetMerchantInformationReturnType> {
6
+ return await client.request({
7
+ method: "frak_getMerchantInformation",
8
+ });
9
+ }
@@ -0,0 +1,29 @@
1
+ export { displayEmbeddedWallet } from "./displayEmbeddedWallet";
2
+ export { displayModal } from "./displayModal";
3
+ export { getMerchantInformation } from "./getMerchantInformation";
4
+ export { openSso } from "./openSso";
5
+ export { prepareSso } from "./prepareSso";
6
+ export {
7
+ type ProcessReferralOptions,
8
+ processReferral,
9
+ } from "./referral/processReferral";
10
+ // Referral interaction
11
+ export { referralInteraction } from "./referral/referralInteraction";
12
+ export { sendInteraction } from "./sendInteraction";
13
+ // Helper to track the purchase status
14
+ export { trackPurchaseStatus } from "./trackPurchaseStatus";
15
+ export { watchWalletStatus } from "./watchWalletStatus";
16
+ // Modal wrappers
17
+ export {
18
+ type ModalBuilder,
19
+ type ModalStepBuilder,
20
+ modalBuilder,
21
+ } from "./wrapper/modalBuilder";
22
+ export {
23
+ type SendTransactionParams,
24
+ sendTransaction,
25
+ } from "./wrapper/sendTransaction";
26
+ export {
27
+ type SiweAuthenticateModalParams,
28
+ siweAuthenticate,
29
+ } from "./wrapper/siweAuthenticate";
@@ -0,0 +1,116 @@
1
+ import type {
2
+ FrakClient,
3
+ OpenSsoParamsType,
4
+ OpenSsoReturnType,
5
+ } from "../types";
6
+ import { computeLegacyProductId } from "../utils/computeLegacyProductId";
7
+ import { generateSsoUrl } from "../utils/sso";
8
+
9
+ // SSO popup configuration
10
+ export const ssoPopupFeatures =
11
+ "menubar=no,status=no,scrollbars=no,fullscreen=no,width=500, height=800";
12
+ export const ssoPopupName = "frak-sso";
13
+
14
+ /**
15
+ * Function used to open the SSO
16
+ * @param client - The current Frak Client
17
+ * @param args - The SSO parameters
18
+ *
19
+ * @description Two SSO flow modes:
20
+ *
21
+ * **Redirect Mode** (openInSameWindow: true):
22
+ * - Wallet generates URL and triggers redirect
23
+ * - Used when redirectUrl is provided
24
+ *
25
+ * **Popup Mode** (openInSameWindow: false/omitted):
26
+ * - SDK generates URL client-side (or uses provided ssoPopupUrl)
27
+ * - Opens popup synchronously (prevents popup blockers)
28
+ * - Waits for SSO completion via postMessage
29
+ *
30
+ * @example
31
+ * First we build the sso metadata
32
+ * ```ts
33
+ * // Build the metadata
34
+ * const metadata: SsoMetadata = {
35
+ * logoUrl: "https://my-app.com/logo.png",
36
+ * homepageLink: "https://my-app.com",
37
+ * };
38
+ * ```
39
+ *
40
+ * Then, either use it with direct exit (and so user is directly redirected to your website), or a custom redirect URL
41
+ * :::code-group
42
+ * ```ts [Popup (default)]
43
+ * // Opens in popup, SDK generates URL automatically
44
+ * await openSso(frakConfig, {
45
+ * directExit: true,
46
+ * metadata,
47
+ * });
48
+ * ```
49
+ * ```ts [Redirect]
50
+ * // Opens in same window with redirect
51
+ * await openSso(frakConfig, {
52
+ * redirectUrl: "https://my-app.com/frak-sso",
53
+ * metadata,
54
+ * openInSameWindow: true,
55
+ * });
56
+ * ```
57
+ * ```ts [Custom popup URL]
58
+ * // Advanced: provide custom SSO URL
59
+ * const { ssoUrl } = await prepareSso(frakConfig, { metadata });
60
+ * await openSso(frakConfig, {
61
+ * metadata,
62
+ * ssoPopupUrl: `${ssoUrl}&custom=param`,
63
+ * });
64
+ * ```
65
+ * :::
66
+ */
67
+ export async function openSso(
68
+ client: FrakClient,
69
+ args: OpenSsoParamsType
70
+ ): Promise<OpenSsoReturnType> {
71
+ const { metadata, customizations, walletUrl } = client.config;
72
+
73
+ // Check if redirect mode (default to true if redirectUrl present)
74
+ const isRedirectMode = args.openInSameWindow ?? !!args.redirectUrl;
75
+
76
+ if (isRedirectMode) {
77
+ // Redirect flow: Wallet generates URL and triggers redirect via lifecycle event
78
+ // This must happen on wallet side because only the iframe can trigger the redirect
79
+ return await client.request({
80
+ method: "frak_openSso",
81
+ params: [args, metadata.name, customizations?.css],
82
+ });
83
+ }
84
+
85
+ // Popup flow: Generate URL on SDK side and open synchronously
86
+ // This ensures window.open() is called in same tick as user gesture (no popup blocker)
87
+
88
+ // Step 1: Generate or use provided SSO URL
89
+ const ssoUrl =
90
+ args.ssoPopupUrl ??
91
+ generateSsoUrl(
92
+ walletUrl ?? "https://wallet.frak.id",
93
+ args,
94
+ computeLegacyProductId(),
95
+ metadata.name,
96
+ customizations?.css
97
+ );
98
+
99
+ // Step 2: Open popup synchronously (critical for popup blocker prevention)
100
+ const popup = window.open(ssoUrl, ssoPopupName, ssoPopupFeatures);
101
+ if (!popup) {
102
+ throw new Error(
103
+ "Popup was blocked. Please allow popups for this site."
104
+ );
105
+ }
106
+ popup.focus();
107
+
108
+ // Step 3: Wait for SSO completion via RPC
109
+ // The wallet iframe will resolve this when SSO page sends sso_complete message
110
+ const result = await client.request({
111
+ method: "frak_openSso",
112
+ params: [args, metadata.name, customizations?.css],
113
+ });
114
+
115
+ return result ?? {};
116
+ }
@@ -0,0 +1,223 @@
1
+ /**
2
+ * Tests for prepareSso action
3
+ * Tests SSO URL generation via RPC
4
+ */
5
+
6
+ import { describe, expect, it, vi } from "../../tests/vitest-fixtures";
7
+ import type {
8
+ FrakClient,
9
+ PrepareSsoParamsType,
10
+ PrepareSsoReturnType,
11
+ } from "../types";
12
+ import { prepareSso } from "./prepareSso";
13
+
14
+ describe("prepareSso", () => {
15
+ describe("success cases", () => {
16
+ it("should call client.request with correct method and params", async () => {
17
+ const mockResponse: PrepareSsoReturnType = {
18
+ ssoUrl: "https://wallet.frak.id/sso?params=xyz",
19
+ };
20
+
21
+ const mockClient = {
22
+ config: {
23
+ metadata: {
24
+ name: "Test App",
25
+ },
26
+ },
27
+ request: vi.fn().mockResolvedValue(mockResponse),
28
+ } as unknown as FrakClient;
29
+
30
+ const params: PrepareSsoParamsType = {
31
+ directExit: true,
32
+ };
33
+
34
+ await prepareSso(mockClient, params);
35
+
36
+ expect(mockClient.request).toHaveBeenCalledWith({
37
+ method: "frak_prepareSso",
38
+ params: [params, "Test App", undefined],
39
+ });
40
+ });
41
+
42
+ it("should return SSO URL", async () => {
43
+ const mockResponse: PrepareSsoReturnType = {
44
+ ssoUrl: "https://wallet.frak.id/sso?params=abc123",
45
+ };
46
+
47
+ const mockClient = {
48
+ config: {
49
+ metadata: {
50
+ name: "Test App",
51
+ },
52
+ },
53
+ request: vi.fn().mockResolvedValue(mockResponse),
54
+ } as unknown as FrakClient;
55
+
56
+ const params: PrepareSsoParamsType = {
57
+ directExit: false,
58
+ };
59
+
60
+ const result = await prepareSso(mockClient, params);
61
+
62
+ expect(result).toEqual(mockResponse);
63
+ expect(result.ssoUrl).toBe(
64
+ "https://wallet.frak.id/sso?params=abc123"
65
+ );
66
+ });
67
+
68
+ it("should include custom CSS when provided", async () => {
69
+ const mockResponse: PrepareSsoReturnType = {
70
+ ssoUrl: "https://wallet.frak.id/sso?params=custom",
71
+ };
72
+
73
+ const mockClient = {
74
+ config: {
75
+ metadata: {
76
+ name: "Styled App",
77
+ },
78
+ customizations: {
79
+ css: "body { background: red; }",
80
+ },
81
+ },
82
+ request: vi.fn().mockResolvedValue(mockResponse),
83
+ } as unknown as FrakClient;
84
+
85
+ const params: PrepareSsoParamsType = {
86
+ directExit: true,
87
+ };
88
+
89
+ await prepareSso(mockClient, params);
90
+
91
+ expect(mockClient.request).toHaveBeenCalledWith({
92
+ method: "frak_prepareSso",
93
+ params: [params, "Styled App", "body { background: red; }"],
94
+ });
95
+ });
96
+
97
+ it("should handle params with redirectUrl", async () => {
98
+ const mockResponse: PrepareSsoReturnType = {
99
+ ssoUrl: "https://wallet.frak.id/sso?redirect=https://example.com",
100
+ };
101
+
102
+ const mockClient = {
103
+ config: {
104
+ metadata: {
105
+ name: "Test App",
106
+ },
107
+ },
108
+ request: vi.fn().mockResolvedValue(mockResponse),
109
+ } as unknown as FrakClient;
110
+
111
+ const params: PrepareSsoParamsType = {
112
+ redirectUrl: "https://example.com/callback",
113
+ directExit: false,
114
+ };
115
+
116
+ await prepareSso(mockClient, params);
117
+
118
+ expect(mockClient.request).toHaveBeenCalledWith({
119
+ method: "frak_prepareSso",
120
+ params: [params, "Test App", undefined],
121
+ });
122
+ });
123
+
124
+ it("should handle params with metadata", async () => {
125
+ const mockResponse: PrepareSsoReturnType = {
126
+ ssoUrl: "https://wallet.frak.id/sso?metadata=xyz",
127
+ };
128
+
129
+ const mockClient = {
130
+ config: {
131
+ metadata: {
132
+ name: "App with Metadata",
133
+ logoUrl: "https://example.com/logo.png",
134
+ },
135
+ },
136
+ request: vi.fn().mockResolvedValue(mockResponse),
137
+ } as unknown as FrakClient;
138
+
139
+ const params: PrepareSsoParamsType = {
140
+ metadata: {
141
+ logoUrl: "https://custom.com/logo.png",
142
+ homepageLink: "https://custom.com",
143
+ },
144
+ directExit: true,
145
+ };
146
+
147
+ await prepareSso(mockClient, params);
148
+
149
+ expect(mockClient.request).toHaveBeenCalledWith({
150
+ method: "frak_prepareSso",
151
+ params: [params, "App with Metadata", undefined],
152
+ });
153
+ });
154
+
155
+ it("should pass client metadata name to request", async () => {
156
+ const mockResponse: PrepareSsoReturnType = {
157
+ ssoUrl: "https://wallet.frak.id/sso?name=MyApp",
158
+ };
159
+
160
+ const mockClient = {
161
+ config: {
162
+ metadata: {
163
+ name: "MyApp",
164
+ logoUrl: "https://example.com/logo.png",
165
+ },
166
+ customizations: {
167
+ css: "body { color: blue; }",
168
+ },
169
+ },
170
+ request: vi.fn().mockResolvedValue(mockResponse),
171
+ } as unknown as FrakClient;
172
+
173
+ const params: PrepareSsoParamsType = {};
174
+
175
+ await prepareSso(mockClient, params);
176
+
177
+ expect(mockClient.request).toHaveBeenCalledWith({
178
+ method: "frak_prepareSso",
179
+ params: [params, "MyApp", "body { color: blue; }"],
180
+ });
181
+ });
182
+ });
183
+
184
+ describe("error handling", () => {
185
+ it("should propagate errors from client.request", async () => {
186
+ const error = new Error("SSO preparation failed");
187
+ const mockClient = {
188
+ config: {
189
+ metadata: {
190
+ name: "Test App",
191
+ },
192
+ },
193
+ request: vi.fn().mockRejectedValue(error),
194
+ } as unknown as FrakClient;
195
+
196
+ const params: PrepareSsoParamsType = {
197
+ directExit: true,
198
+ };
199
+
200
+ await expect(prepareSso(mockClient, params)).rejects.toThrow(
201
+ "SSO preparation failed"
202
+ );
203
+ });
204
+
205
+ it("should handle network errors", async () => {
206
+ const error = new Error("Network timeout");
207
+ const mockClient = {
208
+ config: {
209
+ metadata: {
210
+ name: "Test App",
211
+ },
212
+ },
213
+ request: vi.fn().mockRejectedValue(error),
214
+ } as unknown as FrakClient;
215
+
216
+ const params: PrepareSsoParamsType = {};
217
+
218
+ await expect(prepareSso(mockClient, params)).rejects.toThrow(
219
+ "Network timeout"
220
+ );
221
+ });
222
+ });
223
+ });
@@ -0,0 +1,48 @@
1
+ import type {
2
+ FrakClient,
3
+ PrepareSsoParamsType,
4
+ PrepareSsoReturnType,
5
+ } from "../types";
6
+
7
+ /**
8
+ * Generate SSO URL without opening popup
9
+ *
10
+ * This is a **synchronous**, client-side function that generates the SSO URL
11
+ * without any RPC calls to the wallet iframe. Use this when you need:
12
+ * - Custom URL modifications before opening popup
13
+ * - Pre-generation for advanced popup strategies
14
+ * - URL inspection/logging before SSO flow
15
+ *
16
+ * @param client - The current Frak Client
17
+ * @param args - The SSO parameters
18
+ * @returns Object containing the generated ssoUrl
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * // Generate URL for inspection
23
+ * const { ssoUrl } = prepareSso(client, {
24
+ * metadata: { logoUrl: "..." },
25
+ * directExit: true
26
+ * });
27
+ * console.log("Opening SSO:", ssoUrl);
28
+ *
29
+ * // Add custom params
30
+ * const customUrl = `${ssoUrl}&tracking=abc123`;
31
+ * await openSso(client, { metadata, ssoPopupUrl: customUrl });
32
+ * ```
33
+ *
34
+ * @remarks
35
+ * For most use cases, just use `openSso()` which handles URL generation automatically.
36
+ * Only use `prepareSso()` when you need explicit control over the URL.
37
+ */
38
+ export async function prepareSso(
39
+ client: FrakClient,
40
+ args: PrepareSsoParamsType
41
+ ): Promise<PrepareSsoReturnType> {
42
+ const { metadata, customizations } = client.config;
43
+
44
+ return await client.request({
45
+ method: "frak_prepareSso",
46
+ params: [args, metadata.name, customizations?.css],
47
+ });
48
+ }