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

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 (116) hide show
  1. package/package.json +22 -17
  2. package/src/actions/displayEmbeddedWallet.test.ts +194 -0
  3. package/src/actions/displayEmbeddedWallet.ts +20 -0
  4. package/src/actions/displayModal.test.ts +387 -0
  5. package/src/actions/displayModal.ts +131 -0
  6. package/src/actions/getProductInformation.test.ts +133 -0
  7. package/src/actions/getProductInformation.ts +14 -0
  8. package/src/actions/index.ts +29 -0
  9. package/src/actions/openSso.test.ts +407 -0
  10. package/src/actions/openSso.ts +116 -0
  11. package/src/actions/prepareSso.test.ts +223 -0
  12. package/src/actions/prepareSso.ts +48 -0
  13. package/src/actions/referral/processReferral.ts +230 -0
  14. package/src/actions/referral/referralInteraction.ts +57 -0
  15. package/src/actions/sendInteraction.test.ts +219 -0
  16. package/src/actions/sendInteraction.ts +32 -0
  17. package/src/actions/trackPurchaseStatus.test.ts +287 -0
  18. package/src/actions/trackPurchaseStatus.ts +53 -0
  19. package/src/actions/watchWalletStatus.test.ts +372 -0
  20. package/src/actions/watchWalletStatus.ts +94 -0
  21. package/src/actions/wrapper/modalBuilder.ts +212 -0
  22. package/src/actions/wrapper/sendTransaction.ts +62 -0
  23. package/src/actions/wrapper/siweAuthenticate.ts +94 -0
  24. package/src/bundle.ts +3 -0
  25. package/src/clients/DebugInfo.ts +182 -0
  26. package/src/clients/createIFrameFrakClient.ts +287 -0
  27. package/src/clients/index.ts +3 -0
  28. package/src/clients/setupClient.ts +73 -0
  29. package/src/clients/transports/iframeLifecycleManager.ts +90 -0
  30. package/src/constants/interactionTypes.ts +44 -0
  31. package/src/constants/locales.ts +14 -0
  32. package/src/constants/productTypes.ts +33 -0
  33. package/src/index.ts +101 -0
  34. package/src/interactions/index.ts +5 -0
  35. package/src/interactions/pressEncoder.test.ts +215 -0
  36. package/src/interactions/pressEncoder.ts +53 -0
  37. package/src/interactions/purchaseEncoder.test.ts +291 -0
  38. package/src/interactions/purchaseEncoder.ts +99 -0
  39. package/src/interactions/referralEncoder.test.ts +170 -0
  40. package/src/interactions/referralEncoder.ts +47 -0
  41. package/src/interactions/retailEncoder.test.ts +107 -0
  42. package/src/interactions/retailEncoder.ts +37 -0
  43. package/src/interactions/webshopEncoder.test.ts +56 -0
  44. package/src/interactions/webshopEncoder.ts +30 -0
  45. package/src/types/client.ts +14 -0
  46. package/src/types/compression.ts +22 -0
  47. package/src/types/config.ts +111 -0
  48. package/src/types/context.ts +13 -0
  49. package/src/types/index.ts +71 -0
  50. package/src/types/lifecycle/client.ts +46 -0
  51. package/src/types/lifecycle/iframe.ts +35 -0
  52. package/src/types/lifecycle/index.ts +2 -0
  53. package/src/types/rpc/displayModal.ts +84 -0
  54. package/src/types/rpc/embedded/index.ts +68 -0
  55. package/src/types/rpc/embedded/loggedIn.ts +55 -0
  56. package/src/types/rpc/embedded/loggedOut.ts +28 -0
  57. package/src/types/rpc/interaction.ts +43 -0
  58. package/src/types/rpc/modal/final.ts +46 -0
  59. package/src/types/rpc/modal/generic.ts +46 -0
  60. package/src/types/rpc/modal/index.ts +20 -0
  61. package/src/types/rpc/modal/login.ts +32 -0
  62. package/src/types/rpc/modal/openSession.ts +25 -0
  63. package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
  64. package/src/types/rpc/modal/transaction.ts +33 -0
  65. package/src/types/rpc/productInformation.ts +59 -0
  66. package/src/types/rpc/sso.ts +80 -0
  67. package/src/types/rpc/walletStatus.ts +35 -0
  68. package/src/types/rpc.ts +158 -0
  69. package/src/types/transport.ts +34 -0
  70. package/src/utils/FrakContext.test.ts +338 -0
  71. package/src/utils/FrakContext.ts +158 -0
  72. package/src/utils/compression/b64.test.ts +181 -0
  73. package/src/utils/compression/b64.ts +29 -0
  74. package/src/utils/compression/compress.test.ts +123 -0
  75. package/src/utils/compression/compress.ts +11 -0
  76. package/src/utils/compression/decompress.test.ts +145 -0
  77. package/src/utils/compression/decompress.ts +11 -0
  78. package/src/utils/compression/index.ts +3 -0
  79. package/src/utils/computeProductId.test.ts +80 -0
  80. package/src/utils/computeProductId.ts +11 -0
  81. package/src/utils/constants.test.ts +23 -0
  82. package/src/utils/constants.ts +4 -0
  83. package/src/utils/formatAmount.test.ts +113 -0
  84. package/src/utils/formatAmount.ts +18 -0
  85. package/src/utils/getCurrencyAmountKey.test.ts +44 -0
  86. package/src/utils/getCurrencyAmountKey.ts +15 -0
  87. package/src/utils/getSupportedCurrency.test.ts +51 -0
  88. package/src/utils/getSupportedCurrency.ts +14 -0
  89. package/src/utils/getSupportedLocale.test.ts +64 -0
  90. package/src/utils/getSupportedLocale.ts +16 -0
  91. package/src/utils/iframeHelper.test.ts +450 -0
  92. package/src/utils/iframeHelper.ts +143 -0
  93. package/src/utils/index.ts +21 -0
  94. package/src/utils/sso.test.ts +361 -0
  95. package/src/utils/sso.ts +119 -0
  96. package/src/utils/ssoUrlListener.ts +60 -0
  97. package/src/utils/trackEvent.test.ts +162 -0
  98. package/src/utils/trackEvent.ts +26 -0
  99. package/cdn/bundle.js +0 -19
  100. package/cdn/bundle.js.LICENSE.txt +0 -10
  101. package/dist/actions.cjs +0 -1
  102. package/dist/actions.d.cts +0 -1481
  103. package/dist/actions.d.ts +0 -1481
  104. package/dist/actions.js +0 -1
  105. package/dist/bundle.cjs +0 -13
  106. package/dist/bundle.d.cts +0 -2087
  107. package/dist/bundle.d.ts +0 -2087
  108. package/dist/bundle.js +0 -13
  109. package/dist/index.cjs +0 -13
  110. package/dist/index.d.cts +0 -1387
  111. package/dist/index.d.ts +0 -1387
  112. package/dist/index.js +0 -13
  113. package/dist/interactions.cjs +0 -1
  114. package/dist/interactions.d.cts +0 -182
  115. package/dist/interactions.d.ts +0 -182
  116. package/dist/interactions.js +0 -1
@@ -0,0 +1,131 @@
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` and `openSession` steps will be automatically skipped if the user is already logged in or has an active session. It's safe to include these steps in all cases to ensure proper user state.
21
+ * - Steps are automatically reordered in the following sequence:
22
+ * 1. `login` (if needed)
23
+ * 2. `openSession` (if needed)
24
+ * 3. All other steps in the order specified
25
+ * 4. `success` (if included, always last)
26
+ *
27
+ * @example
28
+ * Simple sharing modal with steps:
29
+ * 1. Login (Skipped if already logged in)
30
+ * 2. Open a session (Skipped if already opened)
31
+ * 3. Display a success message with sharing link option
32
+ *
33
+ * ```ts
34
+ * const results = await displayModal(frakConfig, {
35
+ * steps: {
36
+ * // Simple login with no SSO, nor customization
37
+ * login: { allowSso: false },
38
+ * // Simple session opening, with no customization
39
+ * openSession: {},
40
+ * // Success message
41
+ * final: {
42
+ * action: { key: "reward" },
43
+ * // Skip this step, it will be only displayed in the stepper within the modal
44
+ * autoSkip: true,
45
+ * },
46
+ * },
47
+ * });
48
+ *
49
+ * console.log("Login step - wallet", results.login.wallet);
50
+ * console.log("Open session step - start + end", {
51
+ * start: results.openSession.startTimestamp,
52
+ * end: results.openSession.endTimestamp,
53
+ * });
54
+ * ```
55
+ *
56
+ * @example
57
+ * A full modal example, with a few customization options, with the steps:
58
+ * 1. Login (Skipped if already logged in)
59
+ * 2. Open a session (Skipped if already opened)
60
+ * 3. Authenticate via SIWE
61
+ * 4. Send a transaction
62
+ * 5. Display a success message with sharing link options
63
+ *
64
+ * ```ts
65
+ * const results = await displayModal(frakConfig, {
66
+ * steps: {
67
+ * // Login step
68
+ * login: {
69
+ * allowSso: true,
70
+ * ssoMetadata: {
71
+ * logoUrl: "https://my-app.com/logo.png",
72
+ * homepageLink: "https://my-app.com",
73
+ * },
74
+ * },
75
+ * // Simple session opening, with no customisation
76
+ * openSession: {},
77
+ * // Siwe authentication
78
+ * siweAuthenticate: {
79
+ * siwe: {
80
+ * domain: "my-app.com",
81
+ * uri: "https://my-app.com/",
82
+ * nonce: generateSiweNonce(),
83
+ * version: "1",
84
+ * },
85
+ * },
86
+ * // Send batched transaction
87
+ * sendTransaction: {
88
+ * tx: [
89
+ * { to: "0xdeadbeef", data: "0xdeadbeef" },
90
+ * { to: "0xdeadbeef", data: "0xdeadbeef" },
91
+ * ],
92
+ * },
93
+ * // Success message with sharing options
94
+ * final: {
95
+ * action: {
96
+ * key: "sharing",
97
+ * options: {
98
+ * popupTitle: "Share the app",
99
+ * text: "Discover my super app website",
100
+ * link: "https://my-app.com",
101
+ * },
102
+ * },
103
+ * dismissedMetadata: {
104
+ * title: "Dismiss",
105
+ * description: "You won't be rewarded for this sharing action",
106
+ * },
107
+ * },
108
+ * },
109
+ * metadata: {
110
+ * // Header of desktop modals
111
+ * header: {
112
+ * title: "My-App",
113
+ * icon: "https://my-app.com/logo.png",
114
+ * },
115
+ * // Context that will be present in every modal steps
116
+ * context: "My-app overkill flow",
117
+ * },
118
+ * });
119
+ * ```
120
+ */
121
+ export async function displayModal<
122
+ T extends ModalStepTypes[] = ModalStepTypes[],
123
+ >(
124
+ client: FrakClient,
125
+ { steps, metadata }: DisplayModalParamsType<T>
126
+ ): Promise<ModalRpcStepsResultType<T>> {
127
+ return (await client.request({
128
+ method: "frak_displayModal",
129
+ params: [steps, metadata, client.config.metadata],
130
+ })) as ModalRpcStepsResultType<T>;
131
+ }
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Tests for getProductInformation action
3
+ * Tests fetching product information via RPC
4
+ */
5
+
6
+ import type { Address, Hex } from "viem";
7
+ import { describe, expect, it, vi } from "../../tests/vitest-fixtures";
8
+ import type { FrakClient, GetProductInformationReturnType } from "../types";
9
+ import { getProductInformation } from "./getProductInformation";
10
+
11
+ describe("getProductInformation", () => {
12
+ describe("success cases", () => {
13
+ it("should call client.request with correct method", async () => {
14
+ const mockResponse: GetProductInformationReturnType = {
15
+ id: "0x1234567890123456789012345678901234567890123456789012345678901234" as Hex,
16
+ onChainMetadata: {
17
+ name: "Test Product",
18
+ domain: "example.com",
19
+ productTypes: ["press"],
20
+ },
21
+ rewards: [],
22
+ };
23
+
24
+ const mockClient = {
25
+ request: vi.fn().mockResolvedValue(mockResponse),
26
+ } as unknown as FrakClient;
27
+
28
+ await getProductInformation(mockClient);
29
+
30
+ expect(mockClient.request).toHaveBeenCalledWith({
31
+ method: "frak_getProductInformation",
32
+ });
33
+ });
34
+
35
+ it("should return product information", async () => {
36
+ const mockResponse: GetProductInformationReturnType = {
37
+ id: "0x1234567890123456789012345678901234567890123456789012345678901234" as Hex,
38
+ onChainMetadata: {
39
+ name: "Test Product",
40
+ domain: "example.com",
41
+ productTypes: ["press"],
42
+ },
43
+ rewards: [],
44
+ };
45
+
46
+ const mockClient = {
47
+ request: vi.fn().mockResolvedValue(mockResponse),
48
+ } as unknown as FrakClient;
49
+
50
+ const result = await getProductInformation(mockClient);
51
+
52
+ expect(result).toEqual(mockResponse);
53
+ });
54
+
55
+ it("should return product information with rewards", async () => {
56
+ const mockResponse: GetProductInformationReturnType = {
57
+ id: "0x1234567890123456789012345678901234567890123456789012345678901234" as Hex,
58
+ onChainMetadata: {
59
+ name: "Test Product",
60
+ domain: "example.com",
61
+ productTypes: ["press", "purchase"],
62
+ },
63
+ maxReferrer: {
64
+ amount: 100,
65
+ eurAmount: 10,
66
+ usdAmount: 12,
67
+ gbpAmount: 9,
68
+ },
69
+ maxReferee: {
70
+ amount: 50,
71
+ eurAmount: 5,
72
+ usdAmount: 6,
73
+ gbpAmount: 4.5,
74
+ },
75
+ rewards: [
76
+ {
77
+ token: "0x1234567890123456789012345678901234567890" as Address,
78
+ campaign:
79
+ "0xabcdefabcdefabcdefabcdefabcdefabcdefabcd" as Address,
80
+ interactionTypeKey: "press.readArticle",
81
+ referrer: {
82
+ amount: 10,
83
+ eurAmount: 1,
84
+ usdAmount: 1.2,
85
+ gbpAmount: 0.9,
86
+ },
87
+ referee: {
88
+ amount: 5,
89
+ eurAmount: 0.5,
90
+ usdAmount: 0.6,
91
+ gbpAmount: 0.45,
92
+ },
93
+ },
94
+ ],
95
+ };
96
+
97
+ const mockClient = {
98
+ request: vi.fn().mockResolvedValue(mockResponse),
99
+ } as unknown as FrakClient;
100
+
101
+ const result = await getProductInformation(mockClient);
102
+
103
+ expect(result).toEqual(mockResponse);
104
+ expect(result.rewards).toHaveLength(1);
105
+ expect(result.maxReferrer).toBeDefined();
106
+ expect(result.maxReferee).toBeDefined();
107
+ });
108
+ });
109
+
110
+ describe("error handling", () => {
111
+ it("should propagate errors from client.request", async () => {
112
+ const error = new Error("RPC request failed");
113
+ const mockClient = {
114
+ request: vi.fn().mockRejectedValue(error),
115
+ } as unknown as FrakClient;
116
+
117
+ await expect(getProductInformation(mockClient)).rejects.toThrow(
118
+ "RPC request failed"
119
+ );
120
+ });
121
+
122
+ it("should handle network timeout errors", async () => {
123
+ const error = new Error("Request timeout");
124
+ const mockClient = {
125
+ request: vi.fn().mockRejectedValue(error),
126
+ } as unknown as FrakClient;
127
+
128
+ await expect(getProductInformation(mockClient)).rejects.toThrow(
129
+ "Request timeout"
130
+ );
131
+ });
132
+ });
133
+ });
@@ -0,0 +1,14 @@
1
+ import type { FrakClient, GetProductInformationReturnType } from "../types";
2
+
3
+ /**
4
+ * Function used to get the current product information
5
+ * @param client - The current Frak Client
6
+ * @returns The product information in a promise
7
+ */
8
+ export async function getProductInformation(
9
+ client: FrakClient
10
+ ): Promise<GetProductInformationReturnType> {
11
+ return await client.request({
12
+ method: "frak_getProductInformation",
13
+ });
14
+ }
@@ -0,0 +1,29 @@
1
+ export { displayEmbeddedWallet } from "./displayEmbeddedWallet";
2
+ export { displayModal } from "./displayModal";
3
+ export { getProductInformation } from "./getProductInformation";
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
+ export {
17
+ type ModalBuilder,
18
+ type ModalStepBuilder,
19
+ modalBuilder,
20
+ } from "./wrapper/modalBuilder";
21
+ export {
22
+ type SendTransactionParams,
23
+ sendTransaction,
24
+ } from "./wrapper/sendTransaction";
25
+ // Modal wrappers
26
+ export {
27
+ type SiweAuthenticateModalParams,
28
+ siweAuthenticate,
29
+ } from "./wrapper/siweAuthenticate";