@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.
- package/README.md +1 -2
- package/cdn/bundle.js +3 -3
- package/dist/actions-D4aBXbdp.cjs +1 -0
- package/dist/actions-Dq_uN-wn.js +1 -0
- package/dist/actions.cjs +1 -1
- package/dist/actions.d.cts +3 -3
- package/dist/actions.d.ts +3 -3
- package/dist/actions.js +1 -1
- package/dist/bundle.cjs +1 -1
- package/dist/bundle.d.cts +4 -4
- package/dist/bundle.d.ts +4 -4
- package/dist/bundle.js +1 -1
- package/dist/{computeLegacyProductId-CCAZvLa5.d.cts → index-BV5D9DsW.d.ts} +91 -37
- package/dist/{siweAuthenticate-CnCZ7mok.d.ts → index-BphwTmKA.d.cts} +122 -8
- package/dist/{computeLegacyProductId-b5cUWdAm.d.ts → index-Dwmo109y.d.cts} +91 -37
- package/dist/{siweAuthenticate-CVigMOxz.d.cts → index-_f8EuN_1.d.ts} +122 -8
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -1
- package/dist/{openSso-B0g7-807.d.cts → openSso-BwEK2M98.d.cts} +283 -44
- package/dist/{openSso-CMzwvaCa.d.ts → openSso-C1Wzl5-i.d.ts} +283 -44
- package/dist/src-B1eliIi6.cjs +13 -0
- package/dist/src-C0UH1GsN.js +13 -0
- package/dist/trackEvent-BqJqRZ-u.cjs +1 -0
- package/dist/trackEvent-Bqq4jd6R.js +1 -0
- package/package.json +11 -12
- package/src/actions/displayEmbeddedWallet.ts +6 -2
- package/src/actions/displayModal.ts +6 -2
- package/src/actions/displaySharingPage.ts +49 -0
- package/src/actions/ensureIdentity.ts +2 -2
- package/src/actions/getMerchantInformation.test.ts +13 -1
- package/src/actions/getMerchantInformation.ts +20 -5
- package/src/actions/getMergeToken.ts +33 -0
- package/src/actions/getUserReferralStatus.ts +42 -0
- package/src/actions/index.ts +8 -1
- package/src/actions/referral/setupReferral.test.ts +79 -0
- package/src/actions/referral/setupReferral.ts +32 -0
- package/src/actions/trackPurchaseStatus.test.ts +32 -20
- package/src/actions/trackPurchaseStatus.ts +3 -5
- package/src/actions/wrapper/modalBuilder.test.ts +4 -2
- package/src/actions/wrapper/modalBuilder.ts +6 -8
- package/src/clients/createIFrameFrakClient.ts +151 -27
- package/src/clients/transports/iframeLifecycleManager.test.ts +14 -94
- package/src/clients/transports/iframeLifecycleManager.ts +35 -53
- package/src/index.ts +17 -4
- package/src/stubs/rrweb.ts +9 -0
- package/src/types/config.ts +10 -3
- package/src/types/index.ts +13 -1
- package/src/types/lifecycle/client.ts +22 -27
- package/src/types/lifecycle/iframe.ts +7 -8
- package/src/types/resolvedConfig.ts +128 -0
- package/src/types/rpc/displaySharingPage.ts +82 -0
- package/src/types/rpc/embedded/index.ts +1 -1
- package/src/types/rpc/interaction.ts +4 -0
- package/src/types/rpc/userReferralStatus.ts +20 -0
- package/src/types/rpc.ts +54 -5
- package/src/utils/backendUrl.test.ts +2 -2
- package/src/utils/backendUrl.ts +1 -1
- package/src/utils/cache/index.ts +7 -0
- package/src/utils/cache/lruMap.test.ts +55 -0
- package/src/utils/cache/lruMap.ts +38 -0
- package/src/utils/cache/withCache.test.ts +168 -0
- package/src/utils/cache/withCache.ts +124 -0
- package/src/utils/inAppBrowser.ts +60 -0
- package/src/utils/index.ts +6 -4
- package/src/utils/sdkConfigStore.test.ts +405 -0
- package/src/utils/sdkConfigStore.ts +263 -0
- package/src/utils/sso.ts +3 -7
- package/dist/setupClient-BduY6Sym.cjs +0 -13
- package/dist/setupClient-ftmdQ-I8.js +0 -13
- package/dist/siweAuthenticate-BWmI2_TN.cjs +0 -1
- package/dist/siweAuthenticate-zczqxm0a.js +0 -1
- package/dist/trackEvent-CeLFVzZn.js +0 -1
- package/dist/trackEvent-Ew5r5zfI.cjs +0 -1
- package/src/utils/merchantId.test.ts +0 -653
- package/src/utils/merchantId.ts +0 -143
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
* @param args
|
|
12
12
|
* @param args.steps - The different steps of the modal
|
|
13
13
|
* @param args.metadata - The metadata for the modal (customization, etc)
|
|
14
|
+
* @param placement - Optional placement ID to associate with this modal display
|
|
14
15
|
* @returns The result of each modal steps
|
|
15
16
|
*
|
|
16
17
|
* @description This function will display a modal to the user with the provided steps and metadata.
|
|
@@ -111,10 +112,13 @@ export async function displayModal<
|
|
|
111
112
|
T extends ModalStepTypes[] = ModalStepTypes[],
|
|
112
113
|
>(
|
|
113
114
|
client: FrakClient,
|
|
114
|
-
{ steps, metadata }: DisplayModalParamsType<T
|
|
115
|
+
{ steps, metadata }: DisplayModalParamsType<T>,
|
|
116
|
+
placement?: string
|
|
115
117
|
): Promise<ModalRpcStepsResultType<T>> {
|
|
116
118
|
return (await client.request({
|
|
117
119
|
method: "frak_displayModal",
|
|
118
|
-
params:
|
|
120
|
+
params: placement
|
|
121
|
+
? [steps, metadata, client.config.metadata, placement]
|
|
122
|
+
: [steps, metadata, client.config.metadata],
|
|
119
123
|
})) as ModalRpcStepsResultType<T>;
|
|
120
124
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DisplaySharingPageParamsType,
|
|
3
|
+
DisplaySharingPageResultType,
|
|
4
|
+
FrakClient,
|
|
5
|
+
} from "../types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Function used to display a sharing page
|
|
9
|
+
* @param client - The current Frak Client
|
|
10
|
+
* @param params - The parameters to customize the sharing page (products, link override, metadata)
|
|
11
|
+
* @param placement - Optional placement ID to associate with this display request
|
|
12
|
+
* @returns The result indicating the user's action (shared, copied, or dismissed)
|
|
13
|
+
*
|
|
14
|
+
* @description This function will display a full-page sharing UI to the user,
|
|
15
|
+
* showing product info, estimated rewards, sharing steps, FAQ, and share/copy buttons.
|
|
16
|
+
* The sharing link is generated from the user's wallet context + merchant info.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* - The promise resolves on the first user action (share or copy) but the page stays visible
|
|
20
|
+
* - The user can continue to share/copy multiple times after the initial resolution
|
|
21
|
+
* - Dismissing the page after a share/copy action is a no-op (promise already resolved)
|
|
22
|
+
* - If the user dismisses without any action, the promise resolves with `{ action: "dismissed" }`
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const result = await displaySharingPage(frakClient, {
|
|
27
|
+
* products: [
|
|
28
|
+
* {
|
|
29
|
+
* title: "Babies camel cuir velours bout carré",
|
|
30
|
+
* imageUrl: "https://example.com/product.jpg",
|
|
31
|
+
* },
|
|
32
|
+
* ],
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* console.log("User action:", result.action); // "shared" | "copied" | "dismissed"
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export async function displaySharingPage(
|
|
39
|
+
client: FrakClient,
|
|
40
|
+
params: DisplaySharingPageParamsType,
|
|
41
|
+
placement?: string
|
|
42
|
+
): Promise<DisplaySharingPageResultType> {
|
|
43
|
+
return await client.request({
|
|
44
|
+
method: "frak_displaySharingPage",
|
|
45
|
+
params: placement
|
|
46
|
+
? [params, client.config.metadata, placement]
|
|
47
|
+
: [params, client.config.metadata],
|
|
48
|
+
});
|
|
49
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getBackendUrl } from "../utils/backendUrl";
|
|
2
2
|
import { getClientId } from "../utils/clientId";
|
|
3
|
-
import {
|
|
3
|
+
import { sdkConfigStore } from "../utils/sdkConfigStore";
|
|
4
4
|
|
|
5
5
|
const ENSURE_STORAGE_PREFIX = "frak-identity-ensured-";
|
|
6
6
|
|
|
@@ -36,7 +36,7 @@ export async function ensureIdentity(interactionToken: string): Promise<void> {
|
|
|
36
36
|
return;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
const merchantId = await
|
|
39
|
+
const merchantId = await sdkConfigStore.resolveMerchantId();
|
|
40
40
|
if (!merchantId) {
|
|
41
41
|
return;
|
|
42
42
|
}
|
|
@@ -1,9 +1,21 @@
|
|
|
1
1
|
import type { Address, Hex } from "viem";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
beforeEach,
|
|
4
|
+
describe,
|
|
5
|
+
expect,
|
|
6
|
+
it,
|
|
7
|
+
vi,
|
|
8
|
+
} from "../../tests/vitest-fixtures";
|
|
3
9
|
import type { FrakClient, GetMerchantInformationReturnType } from "../types";
|
|
10
|
+
import { clearAllCache } from "../utils/cache";
|
|
4
11
|
import { getMerchantInformation } from "./getMerchantInformation";
|
|
5
12
|
|
|
6
13
|
describe("getMerchantInformation", () => {
|
|
14
|
+
// Clear cache between tests to ensure isolation
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
clearAllCache();
|
|
17
|
+
});
|
|
18
|
+
|
|
7
19
|
describe("success cases", () => {
|
|
8
20
|
it("should call client.request with correct method", async () => {
|
|
9
21
|
const mockResponse: GetMerchantInformationReturnType = {
|
|
@@ -1,16 +1,31 @@
|
|
|
1
1
|
import type { FrakClient, GetMerchantInformationReturnType } from "../types";
|
|
2
|
+
import { withCache } from "../utils/cache";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
|
-
* Fetch the current merchant information (name, rewards, tiers) from the wallet iframe
|
|
5
|
+
* Fetch the current merchant information (name, rewards, tiers) from the wallet iframe.
|
|
6
|
+
*
|
|
7
|
+
* Results are cached in memory for 30 seconds by default. Concurrent calls
|
|
8
|
+
* while a request is in-flight are deduplicated automatically.
|
|
9
|
+
*
|
|
5
10
|
* @param client - The current Frak Client
|
|
11
|
+
* @param options - Optional cache configuration
|
|
12
|
+
* @param options.cacheTime - Time in ms to cache the result. Default: 30_000 (30s). Set to 0 to disable.
|
|
6
13
|
* @returns The merchant information including available reward tiers
|
|
7
14
|
*
|
|
8
15
|
* @see {@link @frak-labs/core-sdk!index.GetMerchantInformationReturnType | `GetMerchantInformationReturnType`} for the return type shape
|
|
9
16
|
*/
|
|
10
17
|
export async function getMerchantInformation(
|
|
11
|
-
client: FrakClient
|
|
18
|
+
client: FrakClient,
|
|
19
|
+
options?: { cacheTime?: number }
|
|
12
20
|
): Promise<GetMerchantInformationReturnType> {
|
|
13
|
-
return
|
|
14
|
-
|
|
15
|
-
|
|
21
|
+
return withCache(
|
|
22
|
+
() =>
|
|
23
|
+
client.request({
|
|
24
|
+
method: "frak_getMerchantInformation",
|
|
25
|
+
}),
|
|
26
|
+
{
|
|
27
|
+
cacheKey: "frak_getMerchantInformation",
|
|
28
|
+
cacheTime: options?.cacheTime,
|
|
29
|
+
}
|
|
30
|
+
);
|
|
16
31
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { FrakClient } from "../types";
|
|
2
|
+
import { withCache } from "../utils/cache";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Fetch a merge token for the current anonymous identity.
|
|
6
|
+
*
|
|
7
|
+
* Used by in-app browser redirect flows to preserve identity
|
|
8
|
+
* when switching from a WebView to the system browser.
|
|
9
|
+
* The token is appended as `?fmt=` to the redirect URL.
|
|
10
|
+
*
|
|
11
|
+
* Results are cached in memory for 30 seconds by default. Concurrent calls
|
|
12
|
+
* while a request is in-flight are deduplicated automatically.
|
|
13
|
+
*
|
|
14
|
+
* @param client - The current Frak Client
|
|
15
|
+
* @param options - Optional cache configuration
|
|
16
|
+
* @param options.cacheTime - Time in ms to cache the result. Default: 30_000 (30s). Set to 0 to disable.
|
|
17
|
+
* @returns The merge token string, or null if unavailable
|
|
18
|
+
*/
|
|
19
|
+
export async function getMergeToken(
|
|
20
|
+
client: FrakClient,
|
|
21
|
+
options?: { cacheTime?: number }
|
|
22
|
+
): Promise<string | null> {
|
|
23
|
+
return withCache(
|
|
24
|
+
() =>
|
|
25
|
+
client.request({
|
|
26
|
+
method: "frak_getMergeToken",
|
|
27
|
+
}),
|
|
28
|
+
{
|
|
29
|
+
cacheKey: "frak_getMergeToken",
|
|
30
|
+
cacheTime: options?.cacheTime,
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { FrakClient, UserReferralStatusType } from "../types";
|
|
2
|
+
import { withCache } from "../utils/cache";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Fetch the current user's referral status on the current merchant.
|
|
6
|
+
*
|
|
7
|
+
* The listener resolves the user's identity (via clientId or wallet session)
|
|
8
|
+
* and checks whether a referral link exists where the user is the referee.
|
|
9
|
+
*
|
|
10
|
+
* Results are cached in memory for 30 seconds by default. Concurrent calls
|
|
11
|
+
* while a request is in-flight are deduplicated automatically.
|
|
12
|
+
*
|
|
13
|
+
* Returns `null` when the user's identity cannot be resolved.
|
|
14
|
+
*
|
|
15
|
+
* @param client - The current Frak Client
|
|
16
|
+
* @param options - Optional cache configuration
|
|
17
|
+
* @param options.cacheTime - Time in ms to cache the result. Default: 30_000 (30s). Set to 0 to disable.
|
|
18
|
+
* @returns The user's referral status, or `null` if identity cannot be resolved
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const status = await getUserReferralStatus(client);
|
|
23
|
+
* if (status?.isReferred) {
|
|
24
|
+
* console.log("User was referred to this merchant");
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export async function getUserReferralStatus(
|
|
29
|
+
client: FrakClient,
|
|
30
|
+
options?: { cacheTime?: number }
|
|
31
|
+
): Promise<UserReferralStatusType | null> {
|
|
32
|
+
return withCache(
|
|
33
|
+
() =>
|
|
34
|
+
client.request({
|
|
35
|
+
method: "frak_getUserReferralStatus",
|
|
36
|
+
}),
|
|
37
|
+
{
|
|
38
|
+
cacheKey: "frak_getUserReferralStatus",
|
|
39
|
+
cacheTime: options?.cacheTime,
|
|
40
|
+
}
|
|
41
|
+
);
|
|
42
|
+
}
|
package/src/actions/index.ts
CHANGED
|
@@ -1,15 +1,22 @@
|
|
|
1
1
|
export { displayEmbeddedWallet } from "./displayEmbeddedWallet";
|
|
2
2
|
export { displayModal } from "./displayModal";
|
|
3
|
+
export { displaySharingPage } from "./displaySharingPage";
|
|
3
4
|
export { ensureIdentity } from "./ensureIdentity";
|
|
4
5
|
export { getMerchantInformation } from "./getMerchantInformation";
|
|
6
|
+
export { getMergeToken } from "./getMergeToken";
|
|
7
|
+
export { getUserReferralStatus } from "./getUserReferralStatus";
|
|
5
8
|
export { openSso } from "./openSso";
|
|
6
9
|
export { prepareSso } from "./prepareSso";
|
|
7
10
|
export {
|
|
8
11
|
type ProcessReferralOptions,
|
|
9
12
|
processReferral,
|
|
10
13
|
} from "./referral/processReferral";
|
|
11
|
-
// Referral
|
|
14
|
+
// Referral
|
|
12
15
|
export { referralInteraction } from "./referral/referralInteraction";
|
|
16
|
+
export {
|
|
17
|
+
REFERRAL_SUCCESS_EVENT,
|
|
18
|
+
setupReferral,
|
|
19
|
+
} from "./referral/setupReferral";
|
|
13
20
|
export { sendInteraction } from "./sendInteraction";
|
|
14
21
|
// Helper to track the purchase status
|
|
15
22
|
export { trackPurchaseStatus } from "./trackPurchaseStatus";
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { REFERRAL_SUCCESS_EVENT, setupReferral } from "./setupReferral";
|
|
3
|
+
|
|
4
|
+
vi.mock("./referralInteraction", () => ({
|
|
5
|
+
referralInteraction: vi.fn(),
|
|
6
|
+
}));
|
|
7
|
+
|
|
8
|
+
import { referralInteraction } from "./referralInteraction";
|
|
9
|
+
|
|
10
|
+
describe("setupReferral", () => {
|
|
11
|
+
beforeEach(() => {
|
|
12
|
+
vi.clearAllMocks();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
vi.restoreAllMocks();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should dispatch referral success event on successful referral", async () => {
|
|
20
|
+
vi.mocked(referralInteraction).mockResolvedValue("success");
|
|
21
|
+
const listener = vi.fn();
|
|
22
|
+
window.addEventListener(REFERRAL_SUCCESS_EVENT, listener);
|
|
23
|
+
|
|
24
|
+
await setupReferral({ config: {} } as any);
|
|
25
|
+
|
|
26
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
27
|
+
expect(listener).toHaveBeenCalledWith(expect.any(Event));
|
|
28
|
+
|
|
29
|
+
window.removeEventListener(REFERRAL_SUCCESS_EVENT, listener);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should not dispatch event when referral state is not success", async () => {
|
|
33
|
+
vi.mocked(referralInteraction).mockResolvedValue("no-referrer");
|
|
34
|
+
const listener = vi.fn();
|
|
35
|
+
window.addEventListener(REFERRAL_SUCCESS_EVENT, listener);
|
|
36
|
+
|
|
37
|
+
await setupReferral({ config: {} } as any);
|
|
38
|
+
|
|
39
|
+
expect(listener).not.toHaveBeenCalled();
|
|
40
|
+
|
|
41
|
+
window.removeEventListener(REFERRAL_SUCCESS_EVENT, listener);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should not dispatch event when referral returns undefined", async () => {
|
|
45
|
+
vi.mocked(referralInteraction).mockResolvedValue(undefined);
|
|
46
|
+
const listener = vi.fn();
|
|
47
|
+
window.addEventListener(REFERRAL_SUCCESS_EVENT, listener);
|
|
48
|
+
|
|
49
|
+
await setupReferral({ config: {} } as any);
|
|
50
|
+
|
|
51
|
+
expect(listener).not.toHaveBeenCalled();
|
|
52
|
+
|
|
53
|
+
window.removeEventListener(REFERRAL_SUCCESS_EVENT, listener);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should silently catch errors and log warning", async () => {
|
|
57
|
+
vi.mocked(referralInteraction).mockRejectedValue(
|
|
58
|
+
new Error("network failure")
|
|
59
|
+
);
|
|
60
|
+
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
61
|
+
const listener = vi.fn();
|
|
62
|
+
window.addEventListener(REFERRAL_SUCCESS_EVENT, listener);
|
|
63
|
+
|
|
64
|
+
await setupReferral({ config: {} } as any);
|
|
65
|
+
|
|
66
|
+
expect(listener).not.toHaveBeenCalled();
|
|
67
|
+
expect(warnSpy).toHaveBeenCalledWith(
|
|
68
|
+
"[Frak] Referral setup failed",
|
|
69
|
+
expect.any(Error)
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
window.removeEventListener(REFERRAL_SUCCESS_EVENT, listener);
|
|
73
|
+
warnSpy.mockRestore();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should export the correct event name constant", () => {
|
|
77
|
+
expect(REFERRAL_SUCCESS_EVENT).toBe("frak:referral-success");
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { FrakClient } from "../../types";
|
|
2
|
+
import { referralInteraction } from "./referralInteraction";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Custom event name dispatched on successful referral processing.
|
|
6
|
+
*
|
|
7
|
+
* Fired once per page load when a valid referral context is found in the URL
|
|
8
|
+
* and successfully tracked. Consumers (e.g. `<frak-banner>`) listen for this
|
|
9
|
+
* to display a referral success message.
|
|
10
|
+
*/
|
|
11
|
+
export const REFERRAL_SUCCESS_EVENT = "frak:referral-success";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Process referral context and emit a DOM event on success.
|
|
15
|
+
*
|
|
16
|
+
* - Calls {@link referralInteraction} to detect and track any referral in the URL
|
|
17
|
+
* - On `"success"`, dispatches a bare {@link REFERRAL_SUCCESS_EVENT} on `window`
|
|
18
|
+
* - Silently swallows errors (fire-and-forget during SDK init)
|
|
19
|
+
*
|
|
20
|
+
* @param client - The initialized Frak client
|
|
21
|
+
*/
|
|
22
|
+
export async function setupReferral(client: FrakClient): Promise<void> {
|
|
23
|
+
try {
|
|
24
|
+
const state = await referralInteraction(client);
|
|
25
|
+
|
|
26
|
+
if (state === "success") {
|
|
27
|
+
window.dispatchEvent(new Event(REFERRAL_SUCCESS_EVENT));
|
|
28
|
+
}
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.warn("[Frak] Referral setup failed", error);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -11,12 +11,14 @@ vi.mock("../utils/clientId", () => ({
|
|
|
11
11
|
getClientId: vi.fn().mockReturnValue("test-client-id"),
|
|
12
12
|
}));
|
|
13
13
|
|
|
14
|
-
vi.mock("../utils/
|
|
15
|
-
|
|
14
|
+
vi.mock("../utils/sdkConfigStore", () => ({
|
|
15
|
+
sdkConfigStore: {
|
|
16
|
+
resolveMerchantId: vi.fn().mockResolvedValue(undefined),
|
|
17
|
+
},
|
|
16
18
|
}));
|
|
17
19
|
|
|
18
20
|
import { getClientId } from "../utils/clientId";
|
|
19
|
-
import {
|
|
21
|
+
import { sdkConfigStore } from "../utils/sdkConfigStore";
|
|
20
22
|
import { trackPurchaseStatus } from "./trackPurchaseStatus";
|
|
21
23
|
|
|
22
24
|
describe.sequential("trackPurchaseStatus", () => {
|
|
@@ -100,7 +102,9 @@ describe.sequential("trackPurchaseStatus", () => {
|
|
|
100
102
|
});
|
|
101
103
|
|
|
102
104
|
vi.mocked(getClientId).mockReturnValue("test-client-id");
|
|
103
|
-
vi.mocked(
|
|
105
|
+
vi.mocked(sdkConfigStore.resolveMerchantId).mockResolvedValue(
|
|
106
|
+
undefined
|
|
107
|
+
);
|
|
104
108
|
|
|
105
109
|
fetchSpy = vi.fn().mockResolvedValue({
|
|
106
110
|
ok: true,
|
|
@@ -228,12 +232,15 @@ describe.sequential("trackPurchaseStatus", () => {
|
|
|
228
232
|
test("should resolve merchantId from explicit param first", async () => {
|
|
229
233
|
setupStorage({
|
|
230
234
|
interactionToken: "token-123",
|
|
231
|
-
merchantId:
|
|
235
|
+
merchantId: null,
|
|
232
236
|
clientId: "test-client-id",
|
|
233
237
|
});
|
|
234
|
-
vi.mocked(
|
|
235
|
-
|
|
236
|
-
|
|
238
|
+
vi.mocked(sdkConfigStore.resolveMerchantId).mockResolvedValue(
|
|
239
|
+
"fetched-merchant-id"
|
|
240
|
+
);
|
|
241
|
+
const merchantLookupCallsBefore = vi.mocked(
|
|
242
|
+
sdkConfigStore.resolveMerchantId
|
|
243
|
+
).mock.calls.length;
|
|
237
244
|
|
|
238
245
|
await trackPurchaseStatus({
|
|
239
246
|
customerId: "cust-1",
|
|
@@ -253,19 +260,20 @@ describe.sequential("trackPurchaseStatus", () => {
|
|
|
253
260
|
merchantId: "explicit-merchant-id",
|
|
254
261
|
})
|
|
255
262
|
);
|
|
256
|
-
expect(
|
|
257
|
-
|
|
258
|
-
);
|
|
263
|
+
expect(
|
|
264
|
+
vi.mocked(sdkConfigStore.resolveMerchantId).mock.calls.length
|
|
265
|
+
).toBe(merchantLookupCallsBefore);
|
|
259
266
|
});
|
|
260
267
|
|
|
261
268
|
test("should fall back to sessionStorage for merchantId", async () => {
|
|
269
|
+
vi.mocked(sdkConfigStore.resolveMerchantId).mockResolvedValue(
|
|
270
|
+
"session-merchant-id"
|
|
271
|
+
);
|
|
262
272
|
setupStorage({
|
|
263
273
|
interactionToken: "token-123",
|
|
264
|
-
merchantId:
|
|
274
|
+
merchantId: null,
|
|
265
275
|
clientId: "test-client-id",
|
|
266
276
|
});
|
|
267
|
-
const merchantLookupCallsBefore =
|
|
268
|
-
vi.mocked(fetchMerchantId).mock.calls.length;
|
|
269
277
|
|
|
270
278
|
await trackPurchaseStatus({
|
|
271
279
|
customerId: "cust-1",
|
|
@@ -284,18 +292,20 @@ describe.sequential("trackPurchaseStatus", () => {
|
|
|
284
292
|
merchantId: "session-merchant-id",
|
|
285
293
|
})
|
|
286
294
|
);
|
|
287
|
-
expect(
|
|
288
|
-
|
|
289
|
-
);
|
|
295
|
+
expect(
|
|
296
|
+
vi.mocked(sdkConfigStore.resolveMerchantId)
|
|
297
|
+
).toHaveBeenCalled();
|
|
290
298
|
});
|
|
291
299
|
|
|
292
|
-
test("should fall back to
|
|
300
|
+
test("should fall back to resolveMerchantId when no explicit merchantId", async () => {
|
|
293
301
|
setupStorage({
|
|
294
302
|
interactionToken: "token-123",
|
|
295
303
|
merchantId: null,
|
|
296
304
|
clientId: "test-client-id",
|
|
297
305
|
});
|
|
298
|
-
vi.mocked(
|
|
306
|
+
vi.mocked(sdkConfigStore.resolveMerchantId).mockResolvedValue(
|
|
307
|
+
"fetched-merchant-id"
|
|
308
|
+
);
|
|
299
309
|
|
|
300
310
|
await trackPurchaseStatus({
|
|
301
311
|
customerId: "cust-1",
|
|
@@ -322,7 +332,9 @@ describe.sequential("trackPurchaseStatus", () => {
|
|
|
322
332
|
merchantId: null,
|
|
323
333
|
clientId: "test-client-id",
|
|
324
334
|
});
|
|
325
|
-
vi.mocked(
|
|
335
|
+
vi.mocked(sdkConfigStore.resolveMerchantId).mockResolvedValue(
|
|
336
|
+
undefined
|
|
337
|
+
);
|
|
326
338
|
const callCountBefore = getTrackingRequests().length;
|
|
327
339
|
|
|
328
340
|
await trackPurchaseStatus({
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { getBackendUrl } from "../utils/backendUrl";
|
|
2
2
|
import { getClientId } from "../utils/clientId";
|
|
3
|
-
import {
|
|
3
|
+
import { sdkConfigStore } from "../utils/sdkConfigStore";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Function used to track the status of a purchase
|
|
@@ -26,7 +26,7 @@ import { fetchMerchantId } from "../utils/merchantId";
|
|
|
26
26
|
* }
|
|
27
27
|
*
|
|
28
28
|
* @remarks
|
|
29
|
-
* - Merchant id is resolved in this order: explicit `args.merchantId`, `
|
|
29
|
+
* - Merchant id is resolved in this order: explicit `args.merchantId`, then `sdkConfigStore.resolveMerchantId()` (config store → sessionStorage → backend fetch).
|
|
30
30
|
* - This function supports anonymous users and will use the `x-frak-client-id` header when available.
|
|
31
31
|
* - At least one identity source must exist (`frak-wallet-interaction-token` or `x-frak-client-id`), otherwise the tracking request is skipped.
|
|
32
32
|
* - This function will print a warning if used in a non-browser environment or if no identity / merchant id can be resolved.
|
|
@@ -52,10 +52,8 @@ export async function trackPurchaseStatus(args: {
|
|
|
52
52
|
return;
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
const merchantIdFromStorage =
|
|
56
|
-
window.sessionStorage.getItem("frak-merchant-id");
|
|
57
55
|
const merchantId =
|
|
58
|
-
args.merchantId ??
|
|
56
|
+
args.merchantId ?? (await sdkConfigStore.resolveMerchantId());
|
|
59
57
|
|
|
60
58
|
if (!merchantId) {
|
|
61
59
|
console.warn("[Frak] No merchant id found, skipping purchase check");
|
|
@@ -196,7 +196,8 @@ describe("modalBuilder", () => {
|
|
|
196
196
|
|
|
197
197
|
expect(displayModal).toHaveBeenCalledWith(
|
|
198
198
|
mockClient,
|
|
199
|
-
builder.params
|
|
199
|
+
builder.params,
|
|
200
|
+
undefined
|
|
200
201
|
);
|
|
201
202
|
});
|
|
202
203
|
|
|
@@ -216,7 +217,8 @@ describe("modalBuilder", () => {
|
|
|
216
217
|
mockClient,
|
|
217
218
|
expect.objectContaining({
|
|
218
219
|
metadata: { header: { title: "Overridden" } },
|
|
219
|
-
})
|
|
220
|
+
}),
|
|
221
|
+
undefined
|
|
220
222
|
);
|
|
221
223
|
});
|
|
222
224
|
|
|
@@ -46,11 +46,13 @@ export type ModalStepBuilder<
|
|
|
46
46
|
/**
|
|
47
47
|
* Display the modal
|
|
48
48
|
* @param metadataOverride - Function returning optional metadata to override the current modal metadata
|
|
49
|
+
* @param placement - Optional placement ID to associate with this modal display
|
|
49
50
|
*/
|
|
50
51
|
display: (
|
|
51
52
|
metadataOverride?: (
|
|
52
53
|
current?: ModalRpcMetadata
|
|
53
|
-
) => ModalRpcMetadata | undefined
|
|
54
|
+
) => ModalRpcMetadata | undefined,
|
|
55
|
+
placement?: string
|
|
54
56
|
) => Promise<ModalRpcStepsResultType<Steps>>;
|
|
55
57
|
};
|
|
56
58
|
|
|
@@ -177,27 +179,23 @@ function modalStepsBuilder<CurrentSteps extends ModalStepTypes[]>(
|
|
|
177
179
|
);
|
|
178
180
|
}
|
|
179
181
|
|
|
180
|
-
// Function to display it
|
|
181
182
|
async function display(
|
|
182
183
|
metadataOverride?: (
|
|
183
184
|
current?: ModalRpcMetadata
|
|
184
|
-
) => ModalRpcMetadata | undefined
|
|
185
|
+
) => ModalRpcMetadata | undefined,
|
|
186
|
+
placement?: string
|
|
185
187
|
) {
|
|
186
|
-
// If we have a metadata override, apply it
|
|
187
188
|
if (metadataOverride) {
|
|
188
189
|
params.metadata = metadataOverride(params.metadata ?? {});
|
|
189
190
|
}
|
|
190
|
-
return await displayModal(client, params);
|
|
191
|
+
return await displayModal(client, params, placement);
|
|
191
192
|
}
|
|
192
193
|
|
|
193
194
|
return {
|
|
194
|
-
// Access current modal params
|
|
195
195
|
params,
|
|
196
|
-
// Function to add new steps
|
|
197
196
|
sendTx,
|
|
198
197
|
reward,
|
|
199
198
|
sharing,
|
|
200
|
-
// Display the modal
|
|
201
199
|
display,
|
|
202
200
|
};
|
|
203
201
|
}
|