@frak-labs/core-sdk 0.0.19 → 0.1.0-beta.00226d62
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.
- package/cdn/bundle.iife.js +14 -0
- package/dist/actions-CEEObPYc.js +1 -0
- package/dist/actions-DbQhWYx8.cjs +1 -0
- package/dist/actions.cjs +1 -1
- package/dist/actions.d.cts +3 -1400
- package/dist/actions.d.ts +3 -1400
- package/dist/actions.js +1 -1
- package/dist/bundle.cjs +1 -13
- package/dist/bundle.d.cts +6 -2022
- package/dist/bundle.d.ts +6 -2022
- package/dist/bundle.js +1 -13
- package/dist/index-7OZ39x1U.d.ts +195 -0
- package/dist/index-C6FxkWPC.d.cts +511 -0
- package/dist/index-UFX7xCg3.d.ts +351 -0
- package/dist/index-d8xS4ryI.d.ts +511 -0
- package/dist/index-p4FqSp8z.d.cts +351 -0
- package/dist/index-zDq-VlKx.d.cts +195 -0
- package/dist/index.cjs +1 -13
- package/dist/index.d.cts +4 -1373
- package/dist/index.d.ts +4 -1373
- package/dist/index.js +1 -13
- package/dist/interaction-DMJ3ZfaF.d.cts +45 -0
- package/dist/interaction-KX1h9a7V.d.ts +45 -0
- package/dist/interactions-DnfM3oe0.js +1 -0
- package/dist/interactions-EIXhNLf6.cjs +1 -0
- package/dist/interactions.cjs +1 -1
- package/dist/interactions.d.cts +2 -182
- package/dist/interactions.d.ts +2 -182
- package/dist/interactions.js +1 -1
- package/dist/openSso-D--Airj6.d.cts +1018 -0
- package/dist/openSso-DsKJ4y0j.d.ts +1018 -0
- package/dist/productTypes-BUkXJKZ7.cjs +1 -0
- package/dist/productTypes-CGb1MmBF.js +1 -0
- package/dist/src-B_xO0AR6.cjs +13 -0
- package/dist/src-D2d52OZa.js +13 -0
- package/dist/trackEvent-CHnYa85W.js +1 -0
- package/dist/trackEvent-GuQm_1Nm.cjs +1 -0
- package/package.json +27 -18
- package/src/actions/displayEmbeddedWallet.test.ts +194 -0
- package/src/actions/displayEmbeddedWallet.ts +20 -0
- package/src/actions/displayModal.test.ts +387 -0
- package/src/actions/displayModal.ts +131 -0
- package/src/actions/getProductInformation.test.ts +133 -0
- package/src/actions/getProductInformation.ts +14 -0
- package/src/actions/index.ts +29 -0
- package/src/actions/openSso.test.ts +407 -0
- package/src/actions/openSso.ts +116 -0
- package/src/actions/prepareSso.test.ts +223 -0
- package/src/actions/prepareSso.ts +48 -0
- package/src/actions/referral/processReferral.test.ts +357 -0
- package/src/actions/referral/processReferral.ts +230 -0
- package/src/actions/referral/referralInteraction.test.ts +153 -0
- package/src/actions/referral/referralInteraction.ts +57 -0
- package/src/actions/sendInteraction.test.ts +219 -0
- package/src/actions/sendInteraction.ts +32 -0
- package/src/actions/trackPurchaseStatus.test.ts +287 -0
- package/src/actions/trackPurchaseStatus.ts +53 -0
- package/src/actions/watchWalletStatus.test.ts +372 -0
- package/src/actions/watchWalletStatus.ts +94 -0
- package/src/actions/wrapper/modalBuilder.test.ts +253 -0
- package/src/actions/wrapper/modalBuilder.ts +212 -0
- package/src/actions/wrapper/sendTransaction.test.ts +164 -0
- package/src/actions/wrapper/sendTransaction.ts +62 -0
- package/src/actions/wrapper/siweAuthenticate.test.ts +290 -0
- package/src/actions/wrapper/siweAuthenticate.ts +94 -0
- package/src/bundle.ts +3 -0
- package/src/clients/DebugInfo.test.ts +418 -0
- package/src/clients/DebugInfo.ts +182 -0
- package/src/clients/createIFrameFrakClient.ts +287 -0
- package/src/clients/index.ts +3 -0
- package/src/clients/setupClient.test.ts +343 -0
- package/src/clients/setupClient.ts +73 -0
- package/src/clients/transports/iframeLifecycleManager.test.ts +399 -0
- package/src/clients/transports/iframeLifecycleManager.ts +90 -0
- package/src/constants/interactionTypes.test.ts +128 -0
- package/src/constants/interactionTypes.ts +44 -0
- package/src/constants/locales.ts +14 -0
- package/src/constants/productTypes.test.ts +130 -0
- package/src/constants/productTypes.ts +33 -0
- package/src/index.ts +101 -0
- package/src/interactions/index.ts +5 -0
- package/src/interactions/pressEncoder.test.ts +215 -0
- package/src/interactions/pressEncoder.ts +53 -0
- package/src/interactions/purchaseEncoder.test.ts +291 -0
- package/src/interactions/purchaseEncoder.ts +99 -0
- package/src/interactions/referralEncoder.test.ts +170 -0
- package/src/interactions/referralEncoder.ts +47 -0
- package/src/interactions/retailEncoder.test.ts +107 -0
- package/src/interactions/retailEncoder.ts +37 -0
- package/src/interactions/webshopEncoder.test.ts +56 -0
- package/src/interactions/webshopEncoder.ts +30 -0
- package/src/types/client.ts +14 -0
- package/src/types/compression.ts +22 -0
- package/src/types/config.ts +111 -0
- package/src/types/context.ts +13 -0
- package/src/types/index.ts +71 -0
- package/src/types/lifecycle/client.ts +46 -0
- package/src/types/lifecycle/iframe.ts +35 -0
- package/src/types/lifecycle/index.ts +2 -0
- package/src/types/rpc/displayModal.ts +84 -0
- package/src/types/rpc/embedded/index.ts +68 -0
- package/src/types/rpc/embedded/loggedIn.ts +55 -0
- package/src/types/rpc/embedded/loggedOut.ts +28 -0
- package/src/types/rpc/interaction.ts +43 -0
- package/src/types/rpc/modal/final.ts +46 -0
- package/src/types/rpc/modal/generic.ts +46 -0
- package/src/types/rpc/modal/index.ts +20 -0
- package/src/types/rpc/modal/login.ts +32 -0
- package/src/types/rpc/modal/openSession.ts +25 -0
- package/src/types/rpc/modal/siweAuthenticate.ts +37 -0
- package/src/types/rpc/modal/transaction.ts +33 -0
- package/src/types/rpc/productInformation.ts +59 -0
- package/src/types/rpc/sso.ts +80 -0
- package/src/types/rpc/walletStatus.ts +35 -0
- package/src/types/rpc.ts +158 -0
- package/src/types/transport.ts +34 -0
- package/src/utils/FrakContext.test.ts +407 -0
- package/src/utils/FrakContext.ts +158 -0
- package/src/utils/compression/b64.test.ts +181 -0
- package/src/utils/compression/b64.ts +29 -0
- package/src/utils/compression/compress.test.ts +123 -0
- package/src/utils/compression/compress.ts +11 -0
- package/src/utils/compression/decompress.test.ts +145 -0
- package/src/utils/compression/decompress.ts +11 -0
- package/src/utils/compression/index.ts +3 -0
- package/src/utils/computeProductId.test.ts +80 -0
- package/src/utils/computeProductId.ts +11 -0
- package/src/utils/constants.test.ts +23 -0
- package/src/utils/constants.ts +4 -0
- package/src/utils/formatAmount.test.ts +113 -0
- package/src/utils/formatAmount.ts +18 -0
- package/src/utils/getCurrencyAmountKey.test.ts +44 -0
- package/src/utils/getCurrencyAmountKey.ts +15 -0
- package/src/utils/getSupportedCurrency.test.ts +51 -0
- package/src/utils/getSupportedCurrency.ts +14 -0
- package/src/utils/getSupportedLocale.test.ts +64 -0
- package/src/utils/getSupportedLocale.ts +16 -0
- package/src/utils/iframeHelper.test.ts +450 -0
- package/src/utils/iframeHelper.ts +143 -0
- package/src/utils/index.ts +21 -0
- package/src/utils/sso.test.ts +361 -0
- package/src/utils/sso.ts +119 -0
- package/src/utils/ssoUrlListener.test.ts +252 -0
- package/src/utils/ssoUrlListener.ts +60 -0
- package/src/utils/trackEvent.test.ts +162 -0
- package/src/utils/trackEvent.ts +26 -0
- package/cdn/bundle.js +0 -19
- package/cdn/bundle.js.LICENSE.txt +0 -10
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Deferred } from "@frak-labs/frame-connector";
|
|
2
|
+
import type { FrakClient } from "../types/client";
|
|
3
|
+
import type { WalletStatusReturnType } from "../types/rpc/walletStatus";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Function used to watch the current frak wallet status
|
|
7
|
+
* @param client - The current Frak Client
|
|
8
|
+
* @param callback - The callback that will receive any wallet status change
|
|
9
|
+
* @returns A promise resolving with the initial wallet status
|
|
10
|
+
*
|
|
11
|
+
* @description This function will return the current wallet status, and will listen to any change in the wallet status.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* await watchWalletStatus(frakConfig, (status: WalletStatusReturnType) => {
|
|
15
|
+
* if (status.key === "connected") {
|
|
16
|
+
* console.log("Wallet connected:", status.wallet);
|
|
17
|
+
* console.log("Current interaction session:", status.interactionSession);
|
|
18
|
+
* } else {
|
|
19
|
+
* console.log("Wallet not connected");
|
|
20
|
+
* }
|
|
21
|
+
* });
|
|
22
|
+
*/
|
|
23
|
+
export function watchWalletStatus(
|
|
24
|
+
client: FrakClient,
|
|
25
|
+
callback?: (status: WalletStatusReturnType) => void
|
|
26
|
+
): Promise<WalletStatusReturnType> {
|
|
27
|
+
// If no callback is provided, just do a request with deferred result
|
|
28
|
+
if (!callback) {
|
|
29
|
+
return client
|
|
30
|
+
.request({ method: "frak_listenToWalletStatus" })
|
|
31
|
+
.then((result) => {
|
|
32
|
+
// Handle side effects of this request
|
|
33
|
+
walletStatusSideEffect(client, result);
|
|
34
|
+
|
|
35
|
+
// Return the result
|
|
36
|
+
return result;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Otherwise, listen to the wallet status and return the first one received
|
|
41
|
+
const firstResult = new Deferred<WalletStatusReturnType>();
|
|
42
|
+
let hasResolved = false;
|
|
43
|
+
|
|
44
|
+
// Start the listening request, and return the first result
|
|
45
|
+
client.listenerRequest(
|
|
46
|
+
{
|
|
47
|
+
method: "frak_listenToWalletStatus",
|
|
48
|
+
},
|
|
49
|
+
(status) => {
|
|
50
|
+
// Handle side effects of this request
|
|
51
|
+
walletStatusSideEffect(client, status);
|
|
52
|
+
|
|
53
|
+
// Transmit the status to the callback
|
|
54
|
+
callback(status);
|
|
55
|
+
|
|
56
|
+
// If the promise hasn't resolved yet, resolve it
|
|
57
|
+
if (!hasResolved) {
|
|
58
|
+
firstResult.resolve(status);
|
|
59
|
+
hasResolved = true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
return firstResult.promise;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Helper to save a potential interaction token
|
|
69
|
+
* @param interactionToken
|
|
70
|
+
*/
|
|
71
|
+
function walletStatusSideEffect(
|
|
72
|
+
client: FrakClient,
|
|
73
|
+
status: WalletStatusReturnType
|
|
74
|
+
) {
|
|
75
|
+
if (typeof window === "undefined") {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Update the global properties
|
|
80
|
+
client.openPanel?.setGlobalProperties({
|
|
81
|
+
wallet: status.wallet ?? null,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
if (status.interactionToken) {
|
|
85
|
+
// If we got an interaction token, save it
|
|
86
|
+
window.sessionStorage.setItem(
|
|
87
|
+
"frak-wallet-interaction-token",
|
|
88
|
+
status.interactionToken
|
|
89
|
+
);
|
|
90
|
+
} else {
|
|
91
|
+
// Otherwise, remove it
|
|
92
|
+
window.sessionStorage.removeItem("frak-wallet-interaction-token");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
vi.mock("../displayModal", () => ({
|
|
4
|
+
displayModal: vi.fn(),
|
|
5
|
+
}));
|
|
6
|
+
|
|
7
|
+
import type { Address } from "viem";
|
|
8
|
+
import { describe, expect, it } from "../../../tests/vitest-fixtures";
|
|
9
|
+
import type { FrakClient } from "../../types";
|
|
10
|
+
import { modalBuilder } from "./modalBuilder";
|
|
11
|
+
|
|
12
|
+
describe("modalBuilder", () => {
|
|
13
|
+
const mockClient = {
|
|
14
|
+
config: {
|
|
15
|
+
metadata: {
|
|
16
|
+
name: "Test App",
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
request: vi.fn(),
|
|
20
|
+
} as unknown as FrakClient;
|
|
21
|
+
|
|
22
|
+
describe("initialization", () => {
|
|
23
|
+
it("should create builder with base params", () => {
|
|
24
|
+
const builder = modalBuilder(mockClient, {});
|
|
25
|
+
|
|
26
|
+
expect(builder.params).toBeDefined();
|
|
27
|
+
expect(builder.params.steps.login).toEqual({});
|
|
28
|
+
expect(builder.params.steps.openSession).toEqual({});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("should create builder with custom login params", () => {
|
|
32
|
+
const builder = modalBuilder(mockClient, {
|
|
33
|
+
login: { allowSso: true },
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
expect(builder.params.steps.login).toEqual({ allowSso: true });
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should create builder with custom openSession params", () => {
|
|
40
|
+
const builder = modalBuilder(mockClient, {
|
|
41
|
+
openSession: {},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
expect(builder.params.steps.openSession).toEqual({});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should create builder with metadata", () => {
|
|
48
|
+
const builder = modalBuilder(mockClient, {
|
|
49
|
+
metadata: {
|
|
50
|
+
header: { title: "Test Title" },
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
expect(builder.params.metadata).toEqual({
|
|
55
|
+
header: { title: "Test Title" },
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("sendTx step", () => {
|
|
61
|
+
it("should add sendTransaction step", () => {
|
|
62
|
+
const builder = modalBuilder(mockClient, {});
|
|
63
|
+
const withTx = builder.sendTx({
|
|
64
|
+
tx: {
|
|
65
|
+
to: "0x1234567890123456789012345678901234567890" as Address,
|
|
66
|
+
data: "0xdata" as `0x${string}`,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(withTx.params.steps.sendTransaction).toEqual({
|
|
71
|
+
tx: {
|
|
72
|
+
to: "0x1234567890123456789012345678901234567890",
|
|
73
|
+
data: "0xdata",
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should preserve previous steps when adding sendTx", () => {
|
|
79
|
+
const builder = modalBuilder(mockClient, {
|
|
80
|
+
login: { allowSso: true },
|
|
81
|
+
});
|
|
82
|
+
const withTx = builder.sendTx({
|
|
83
|
+
tx: {
|
|
84
|
+
to: "0x1234567890123456789012345678901234567890" as Address,
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
expect(withTx.params.steps.login).toEqual({ allowSso: true });
|
|
89
|
+
expect(withTx.params.steps.sendTransaction).toBeDefined();
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe("reward step", () => {
|
|
94
|
+
it("should add reward final step", () => {
|
|
95
|
+
const builder = modalBuilder(mockClient, {});
|
|
96
|
+
const withReward = builder.reward();
|
|
97
|
+
|
|
98
|
+
expect(withReward.params.steps.final).toEqual({
|
|
99
|
+
action: { key: "reward" },
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should add reward step with options", () => {
|
|
104
|
+
const builder = modalBuilder(mockClient, {});
|
|
105
|
+
const withReward = builder.reward({
|
|
106
|
+
autoSkip: true,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
expect(withReward.params.steps.final).toEqual({
|
|
110
|
+
autoSkip: true,
|
|
111
|
+
action: { key: "reward" },
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("sharing step", () => {
|
|
117
|
+
it("should add sharing final step", () => {
|
|
118
|
+
const builder = modalBuilder(mockClient, {});
|
|
119
|
+
const withSharing = builder.sharing({
|
|
120
|
+
popupTitle: "Share!",
|
|
121
|
+
text: "Check this out",
|
|
122
|
+
link: "https://example.com",
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
expect(withSharing.params.steps.final).toEqual({
|
|
126
|
+
action: {
|
|
127
|
+
key: "sharing",
|
|
128
|
+
options: {
|
|
129
|
+
popupTitle: "Share!",
|
|
130
|
+
text: "Check this out",
|
|
131
|
+
link: "https://example.com",
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("should add sharing step with additional options", () => {
|
|
138
|
+
const builder = modalBuilder(mockClient, {});
|
|
139
|
+
const withSharing = builder.sharing(
|
|
140
|
+
{ text: "Share text", link: "https://example.com" },
|
|
141
|
+
{ autoSkip: false }
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
expect(withSharing.params.steps.final).toEqual({
|
|
145
|
+
autoSkip: false,
|
|
146
|
+
action: {
|
|
147
|
+
key: "sharing",
|
|
148
|
+
options: {
|
|
149
|
+
text: "Share text",
|
|
150
|
+
link: "https://example.com",
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe("chaining", () => {
|
|
158
|
+
it("should chain sendTx and reward", () => {
|
|
159
|
+
const builder = modalBuilder(mockClient, {});
|
|
160
|
+
const chained = builder
|
|
161
|
+
.sendTx({
|
|
162
|
+
tx: {
|
|
163
|
+
to: "0x1234567890123456789012345678901234567890" as Address,
|
|
164
|
+
},
|
|
165
|
+
})
|
|
166
|
+
.reward();
|
|
167
|
+
|
|
168
|
+
expect(chained.params.steps.sendTransaction).toBeDefined();
|
|
169
|
+
expect(chained.params.steps.final).toEqual({
|
|
170
|
+
action: { key: "reward" },
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("should chain sendTx and sharing", () => {
|
|
175
|
+
const builder = modalBuilder(mockClient, {});
|
|
176
|
+
const chained = builder
|
|
177
|
+
.sendTx({
|
|
178
|
+
tx: {
|
|
179
|
+
to: "0x1234567890123456789012345678901234567890" as Address,
|
|
180
|
+
},
|
|
181
|
+
})
|
|
182
|
+
.sharing({ link: "https://example.com" });
|
|
183
|
+
|
|
184
|
+
expect(chained.params.steps.sendTransaction).toBeDefined();
|
|
185
|
+
expect(chained.params.steps.final?.action).toEqual({
|
|
186
|
+
key: "sharing",
|
|
187
|
+
options: { link: "https://example.com" },
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
describe("display", () => {
|
|
193
|
+
it("should call displayModal when display is invoked", async () => {
|
|
194
|
+
const { displayModal } = await import("../displayModal");
|
|
195
|
+
|
|
196
|
+
const mockResponse = {
|
|
197
|
+
login: {
|
|
198
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
199
|
+
},
|
|
200
|
+
openSession: {
|
|
201
|
+
startTimestamp: 1234567890,
|
|
202
|
+
endTimestamp: 1234567900,
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
vi.mocked(displayModal).mockResolvedValue(mockResponse as any);
|
|
206
|
+
|
|
207
|
+
const builder = modalBuilder(mockClient, {});
|
|
208
|
+
await builder.display();
|
|
209
|
+
|
|
210
|
+
expect(displayModal).toHaveBeenCalledWith(
|
|
211
|
+
mockClient,
|
|
212
|
+
builder.params
|
|
213
|
+
);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("should apply metadata override when provided", async () => {
|
|
217
|
+
const { displayModal } = await import("../displayModal");
|
|
218
|
+
|
|
219
|
+
vi.mocked(displayModal).mockResolvedValue({} as any);
|
|
220
|
+
|
|
221
|
+
const builder = modalBuilder(mockClient, {
|
|
222
|
+
metadata: { header: { title: "Original" } },
|
|
223
|
+
});
|
|
224
|
+
await builder.display(() => ({
|
|
225
|
+
header: { title: "Overridden" },
|
|
226
|
+
}));
|
|
227
|
+
|
|
228
|
+
expect(displayModal).toHaveBeenCalledWith(
|
|
229
|
+
mockClient,
|
|
230
|
+
expect.objectContaining({
|
|
231
|
+
metadata: { header: { title: "Overridden" } },
|
|
232
|
+
})
|
|
233
|
+
);
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("should return displayModal result", async () => {
|
|
237
|
+
const { displayModal } = await import("../displayModal");
|
|
238
|
+
|
|
239
|
+
const mockResponse = {
|
|
240
|
+
login: {
|
|
241
|
+
wallet: "0x1234567890123456789012345678901234567890" as Address,
|
|
242
|
+
},
|
|
243
|
+
openSession: {},
|
|
244
|
+
};
|
|
245
|
+
vi.mocked(displayModal).mockResolvedValue(mockResponse as any);
|
|
246
|
+
|
|
247
|
+
const builder = modalBuilder(mockClient, {});
|
|
248
|
+
const result = await builder.display();
|
|
249
|
+
|
|
250
|
+
expect(result).toEqual(mockResponse);
|
|
251
|
+
});
|
|
252
|
+
});
|
|
253
|
+
});
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DisplayModalParamsType,
|
|
3
|
+
FinalActionType,
|
|
4
|
+
FinalModalStepType,
|
|
5
|
+
FrakClient,
|
|
6
|
+
LoginModalStepType,
|
|
7
|
+
ModalRpcMetadata,
|
|
8
|
+
ModalRpcStepsResultType,
|
|
9
|
+
ModalStepTypes,
|
|
10
|
+
OpenInteractionSessionModalStepType,
|
|
11
|
+
SendTransactionModalStepType,
|
|
12
|
+
} from "../../types";
|
|
13
|
+
import { displayModal } from "../displayModal";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Represent the type of the modal step builder
|
|
17
|
+
*/
|
|
18
|
+
export type ModalStepBuilder<
|
|
19
|
+
Steps extends ModalStepTypes[] = ModalStepTypes[],
|
|
20
|
+
> = {
|
|
21
|
+
/**
|
|
22
|
+
* The current modal params
|
|
23
|
+
*/
|
|
24
|
+
params: DisplayModalParamsType<Steps>;
|
|
25
|
+
/**
|
|
26
|
+
* Add a send transaction step to the modal
|
|
27
|
+
*/
|
|
28
|
+
sendTx: (
|
|
29
|
+
options: SendTransactionModalStepType["params"]
|
|
30
|
+
) => ModalStepBuilder<[...Steps, SendTransactionModalStepType]>;
|
|
31
|
+
/**
|
|
32
|
+
* Add a final step of type reward to the modal
|
|
33
|
+
*/
|
|
34
|
+
reward: (
|
|
35
|
+
options?: Omit<FinalModalStepType["params"], "action">
|
|
36
|
+
) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
|
|
37
|
+
/**
|
|
38
|
+
* Add a final step of type sharing to the modal
|
|
39
|
+
*/
|
|
40
|
+
sharing: (
|
|
41
|
+
sharingOptions?: Extract<
|
|
42
|
+
FinalActionType,
|
|
43
|
+
{ key: "sharing" }
|
|
44
|
+
>["options"],
|
|
45
|
+
options?: Omit<FinalModalStepType["params"], "action">
|
|
46
|
+
) => ModalStepBuilder<[...Steps, FinalModalStepType]>;
|
|
47
|
+
/**
|
|
48
|
+
* Display the modal
|
|
49
|
+
* @param metadataOverride - Function returning optional metadata to override the current modal metadata
|
|
50
|
+
*/
|
|
51
|
+
display: (
|
|
52
|
+
metadataOverride?: (
|
|
53
|
+
current?: ModalRpcMetadata
|
|
54
|
+
) => ModalRpcMetadata | undefined
|
|
55
|
+
) => Promise<ModalRpcStepsResultType<Steps>>;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Represent the output type of the modal builder
|
|
60
|
+
*/
|
|
61
|
+
export type ModalBuilder = ModalStepBuilder<
|
|
62
|
+
[LoginModalStepType, OpenInteractionSessionModalStepType]
|
|
63
|
+
>;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Helper to craft Frak modal, and share a base initial config
|
|
67
|
+
* @param client - The current Frak Client
|
|
68
|
+
* @param args
|
|
69
|
+
* @param args.metadata - Common modal metadata (customisation, language etc)
|
|
70
|
+
* @param args.login - Login step parameters
|
|
71
|
+
* @param args.openSession - Open session step parameters
|
|
72
|
+
*
|
|
73
|
+
* @description This function will create a modal builder with the provided metadata, login and open session parameters.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* Here is an example of how to use the `modalBuilder` to create and display a sharing modal:
|
|
77
|
+
*
|
|
78
|
+
* ```js
|
|
79
|
+
* // Create the modal builder
|
|
80
|
+
* const modalBuilder = window.FrakSDK.modalBuilder(frakClient, baseModalConfig);
|
|
81
|
+
*
|
|
82
|
+
* // Configure the information to be shared via the sharing link
|
|
83
|
+
* const sharingConfig = {
|
|
84
|
+
* popupTitle: "Share this with your friends",
|
|
85
|
+
* text: "Discover our product!",
|
|
86
|
+
* link: window.location.href,
|
|
87
|
+
* };
|
|
88
|
+
*
|
|
89
|
+
* // Display the sharing modal
|
|
90
|
+
* function modalShare() {
|
|
91
|
+
* modalBuilder.sharing(sharingConfig).display();
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @see {@link ModalStepTypes} for more info about each modal step types and their parameters
|
|
96
|
+
* @see {@link ModalRpcMetadata} for more info about the metadata that can be passed to the modal
|
|
97
|
+
* @see {@link ModalRpcStepsResultType} for more info about the result of each modal steps
|
|
98
|
+
* @see {@link displayModal} for more info about how the modal is displayed
|
|
99
|
+
*/
|
|
100
|
+
export function modalBuilder(
|
|
101
|
+
client: FrakClient,
|
|
102
|
+
{
|
|
103
|
+
metadata,
|
|
104
|
+
login,
|
|
105
|
+
openSession,
|
|
106
|
+
}: {
|
|
107
|
+
metadata?: ModalRpcMetadata;
|
|
108
|
+
login?: LoginModalStepType["params"];
|
|
109
|
+
openSession?: OpenInteractionSessionModalStepType["params"];
|
|
110
|
+
}
|
|
111
|
+
): ModalBuilder {
|
|
112
|
+
// Build the initial modal params
|
|
113
|
+
const baseParams: DisplayModalParamsType<
|
|
114
|
+
[LoginModalStepType, OpenInteractionSessionModalStepType]
|
|
115
|
+
> = {
|
|
116
|
+
steps: {
|
|
117
|
+
login: login ?? {},
|
|
118
|
+
openSession: openSession ?? {},
|
|
119
|
+
},
|
|
120
|
+
metadata,
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// Return the step builder
|
|
124
|
+
return modalStepsBuilder(client, baseParams);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Modal step builder, allowing to add new steps to the modal, and to build and display it
|
|
129
|
+
*/
|
|
130
|
+
function modalStepsBuilder<CurrentSteps extends ModalStepTypes[]>(
|
|
131
|
+
client: FrakClient,
|
|
132
|
+
params: DisplayModalParamsType<CurrentSteps>
|
|
133
|
+
): ModalStepBuilder<CurrentSteps> {
|
|
134
|
+
// Function add the send tx step
|
|
135
|
+
function sendTx(options: SendTransactionModalStepType["params"]) {
|
|
136
|
+
return modalStepsBuilder<
|
|
137
|
+
[...CurrentSteps, SendTransactionModalStepType]
|
|
138
|
+
>(client, {
|
|
139
|
+
...params,
|
|
140
|
+
steps: {
|
|
141
|
+
...params.steps,
|
|
142
|
+
sendTransaction: options,
|
|
143
|
+
},
|
|
144
|
+
} as DisplayModalParamsType<
|
|
145
|
+
[...CurrentSteps, SendTransactionModalStepType]
|
|
146
|
+
>);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Function to add a reward step at the end
|
|
150
|
+
function reward(options?: Omit<FinalModalStepType["params"], "action">) {
|
|
151
|
+
return modalStepsBuilder<[...CurrentSteps, FinalModalStepType]>(
|
|
152
|
+
client,
|
|
153
|
+
{
|
|
154
|
+
...params,
|
|
155
|
+
steps: {
|
|
156
|
+
...params.steps,
|
|
157
|
+
final: {
|
|
158
|
+
...options,
|
|
159
|
+
action: { key: "reward" },
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
} as DisplayModalParamsType<[...CurrentSteps, FinalModalStepType]>
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Function to add sharing step at the end
|
|
167
|
+
function sharing(
|
|
168
|
+
sharingOptions?: Extract<
|
|
169
|
+
FinalActionType,
|
|
170
|
+
{ key: "sharing" }
|
|
171
|
+
>["options"],
|
|
172
|
+
options?: Omit<FinalModalStepType["params"], "action">
|
|
173
|
+
) {
|
|
174
|
+
return modalStepsBuilder<[...CurrentSteps, FinalModalStepType]>(
|
|
175
|
+
client,
|
|
176
|
+
{
|
|
177
|
+
...params,
|
|
178
|
+
steps: {
|
|
179
|
+
...params.steps,
|
|
180
|
+
final: {
|
|
181
|
+
...options,
|
|
182
|
+
action: { key: "sharing", options: sharingOptions },
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
} as DisplayModalParamsType<[...CurrentSteps, FinalModalStepType]>
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Function to display it
|
|
190
|
+
async function display(
|
|
191
|
+
metadataOverride?: (
|
|
192
|
+
current?: ModalRpcMetadata
|
|
193
|
+
) => ModalRpcMetadata | undefined
|
|
194
|
+
) {
|
|
195
|
+
// If we have a metadata override, apply it
|
|
196
|
+
if (metadataOverride) {
|
|
197
|
+
params.metadata = metadataOverride(params.metadata ?? {});
|
|
198
|
+
}
|
|
199
|
+
return await displayModal(client, params);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
// Access current modal params
|
|
204
|
+
params,
|
|
205
|
+
// Function to add new steps
|
|
206
|
+
sendTx,
|
|
207
|
+
reward,
|
|
208
|
+
sharing,
|
|
209
|
+
// Display the modal
|
|
210
|
+
display,
|
|
211
|
+
};
|
|
212
|
+
}
|