@wf-financing/ui 4.7.0 → 4.7.1

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/dist/index.es.js CHANGED
@@ -1 +1 @@
1
- import "./index-BN3wo8mJ.mjs";
1
+ import "./index-B6i2pWgP.mjs";
package/global.d.ts CHANGED
@@ -4,5 +4,7 @@ declare global {
4
4
  interface Window {
5
5
  WayflyerUiSdk: IWayflyerUiSdkConstructor;
6
6
  WayflyerHeadlessSdk: IWayflyerHeadlessSdkConstructor;
7
+
8
+ WayflyerHostedCapitalBaseUrl: string;
7
9
  }
8
10
  }
package/index.ts CHANGED
@@ -5,7 +5,8 @@ const addUiSdkToWindow = () => {
5
5
 
6
6
  window.WayflyerUiSdk = WayflyerUiSdk;
7
7
  /**
8
- * Trigger event handler in entry point that notifies it that the SDK is ready to use. See packages/ui-sdk-entry/utils/loadUiSdkScript.ts
8
+ * Trigger event handler in entry point that notifies it that the SDK is ready to use.
9
+ * See packages/ui-sdk-entry/utils/loadUiSdkScript.ts
9
10
  */
10
11
  window.dispatchEvent(new CustomEvent(UI_SDK_READY_EVENT));
11
12
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wf-financing/ui",
3
- "version": "4.7.0",
3
+ "version": "4.7.1",
4
4
  "exports": {
5
5
  ".": {
6
6
  "import": "./dist/index.es.js",
@@ -38,6 +38,7 @@
38
38
  "react-markdown": "^10.1.0",
39
39
  "styled-components": "^6.1.19",
40
40
  "@wf-financing/embedded-types": "1.1.1",
41
+ "@wf-financing/headless": "3.3.3",
41
42
  "@wf-financing/logger": "2.2.2",
42
43
  "@wf-financing/ui-assets": "1.1.3",
43
44
  "@wf-financing/embedded-journey": "0.2.0"
@@ -1,52 +1,47 @@
1
- import { afterEach, describe, expect, Mock, test, vi } from 'vitest';
1
+ import { beforeEach, describe, expect, test, vi } from 'vitest';
2
2
 
3
- vi.mock('./getHeadlessSdkInstance', () => ({
4
- getHeadlessSdkInstance: vi.fn(),
3
+ const mockSetCompanyToken = vi.fn();
4
+ const MockHeadlessSdk = vi.fn(() => ({
5
+ setCompanyToken: mockSetCompanyToken,
5
6
  }));
6
7
 
7
- import { getHeadlessSdkInstance } from './getHeadlessSdkInstance';
8
- import { startHostedApplication } from './startHostedApplication';
9
-
10
- const fakeSdk = {
11
- getCta: vi.fn(),
12
- startHostedApplication: vi.fn(),
13
- };
14
-
15
- const options = {
16
- isSandbox: true,
17
- };
8
+ vi.mock('@wf-financing/headless/sdk', () => ({
9
+ HeadlessSdk: MockHeadlessSdk,
10
+ }));
18
11
 
19
- describe('startHostedApplication', () => {
20
- afterEach(() => {
12
+ describe('getHeadlessSdkInstance', () => {
13
+ beforeEach(() => {
21
14
  vi.clearAllMocks();
15
+ vi.resetModules();
22
16
  });
23
17
 
24
- test('calls sdk.startHostedApplication with partnerData and returns nextUrl', async () => {
25
- const fakePartnerData = { foo: 1 };
26
- const fakeNextUrl = '/next-url';
27
- const partnerCallback = vi.fn().mockResolvedValue(fakePartnerData);
28
-
29
- (getHeadlessSdkInstance as unknown as Mock).mockResolvedValueOnce(fakeSdk);
30
- fakeSdk.startHostedApplication.mockResolvedValueOnce(fakeNextUrl);
18
+ test('creates a new HeadlessSdk instance on first call', async () => {
19
+ const { getHeadlessSdkInstance } = await import('./getHeadlessSdkInstance');
31
20
 
32
- const result = await startHostedApplication('TOKEN', partnerCallback, options);
21
+ const sdk = await getHeadlessSdkInstance('TOKEN_1');
33
22
 
34
- expect(getHeadlessSdkInstance).toHaveBeenCalledWith('TOKEN', options);
35
- expect(partnerCallback).toHaveBeenCalled();
36
- expect(fakeSdk.startHostedApplication).toHaveBeenCalledWith(fakePartnerData);
37
- expect(result).toBe(fakeNextUrl);
23
+ expect(MockHeadlessSdk).toHaveBeenCalledWith('TOKEN_1', undefined);
24
+ expect(sdk).toEqual(expect.objectContaining({ setCompanyToken: expect.any(Function) }));
38
25
  });
39
26
 
40
- test('calls sdk.startHostedApplication with empty object if partnerCallback returns falsy', async () => {
41
- const partnerCallback = vi.fn().mockResolvedValue(undefined);
42
- const fakeNextUrl = '/next-url';
27
+ test('creates a new HeadlessSdk instance with options', async () => {
28
+ const { getHeadlessSdkInstance } = await import('./getHeadlessSdkInstance');
29
+ const options = { isSandbox: true };
30
+
31
+ const sdk = await getHeadlessSdkInstance('TOKEN_1', options);
32
+
33
+ expect(MockHeadlessSdk).toHaveBeenCalledWith('TOKEN_1', options);
34
+ expect(sdk).toEqual(expect.objectContaining({ setCompanyToken: expect.any(Function) }));
35
+ });
43
36
 
44
- (getHeadlessSdkInstance as unknown as Mock).mockResolvedValueOnce(fakeSdk);
45
- fakeSdk.startHostedApplication.mockResolvedValueOnce(fakeNextUrl);
37
+ test('returns cached instance and calls setCompanyToken on subsequent calls', async () => {
38
+ const { getHeadlessSdkInstance } = await import('./getHeadlessSdkInstance');
46
39
 
47
- const result = await startHostedApplication('TOKEN', partnerCallback);
40
+ const firstSdk = await getHeadlessSdkInstance('TOKEN_1');
41
+ const secondSdk = await getHeadlessSdkInstance('TOKEN_2');
48
42
 
49
- expect(fakeSdk.startHostedApplication).toHaveBeenCalledWith({});
50
- expect(result).toBe(fakeNextUrl);
43
+ expect(MockHeadlessSdk).toHaveBeenCalledTimes(1);
44
+ expect(firstSdk).toBe(secondSdk);
45
+ expect(mockSetCompanyToken).toHaveBeenCalledWith('TOKEN_2');
51
46
  });
52
47
  });
@@ -1,43 +1,17 @@
1
- import { IWayflyerHeadlessSdk, HeadlessSdkOptions } from '@wf-financing/embedded-types';
2
-
3
- import { Logger } from '@wf-financing/logger';
4
- import { HEADLESS_SDK_URL, WAYFLYER_HEADLESS_SDK_ID } from '../config';
5
- import { initializeHeadlessSdk, loadScriptAndInitializeSdk } from '../utils';
1
+ import { HeadlessSdk } from '@wf-financing/headless/sdk';
2
+ import type { IWayflyerHeadlessSdk, HeadlessSdkOptions } from '@wf-financing/embedded-types';
6
3
 
7
4
  let cachedSdkInstance: IWayflyerHeadlessSdk | null = null;
8
5
 
9
- export const getHeadlessSdkInstance = async (companyToken: string, options?: HeadlessSdkOptions) => {
10
- try {
11
- if (cachedSdkInstance) {
12
- cachedSdkInstance.setCompanyToken(companyToken);
13
-
14
- return cachedSdkInstance;
15
- }
16
-
17
- const existingScript = document.getElementById(WAYFLYER_HEADLESS_SDK_ID) as HTMLScriptElement;
18
-
19
- if (window.WayflyerHeadlessSdk) {
20
- return initializeHeadlessSdk(companyToken, options);
21
- }
22
-
23
- if (existingScript) {
24
- return loadScriptAndInitializeSdk(existingScript, companyToken, options);
25
- }
26
-
27
- const script = document.createElement('script');
28
- script.src = HEADLESS_SDK_URL;
29
- script.type = 'module';
30
- script.id = WAYFLYER_HEADLESS_SDK_ID;
31
- script.async = true;
32
- script.onerror = () => Logger.logError('Error in loading headless SDK script');
33
-
34
- document.head.appendChild(script);
35
- const headlessSdk: IWayflyerHeadlessSdk = await loadScriptAndInitializeSdk(script, companyToken, options);
36
- cachedSdkInstance = headlessSdk;
37
-
38
- return headlessSdk;
39
- } catch {
40
- Logger.logError('Error in loading headless SDK');
41
- throw new Error('Failed to load script');
6
+ export const getHeadlessSdkInstance = async (
7
+ companyToken: string,
8
+ options?: HeadlessSdkOptions,
9
+ ): Promise<IWayflyerHeadlessSdk> => {
10
+ if (cachedSdkInstance) {
11
+ cachedSdkInstance.setCompanyToken(companyToken);
12
+ return cachedSdkInstance;
42
13
  }
14
+
15
+ cachedSdkInstance = new HeadlessSdk(companyToken, options);
16
+ return cachedSdkInstance;
43
17
  };
@@ -1,5 +1,3 @@
1
1
  export { ROOTS_CONFIG } from './rootsParameters';
2
- export { WAYFLYER_HEADLESS_SDK_ID } from './scriptId';
3
- export { HEADLESS_SDK_URL } from './url';
4
2
  export { APPLICATION_VERSION } from './applicationVersion';
5
3
  export { IS_EMBEDDED_JOURNEY } from './isEmbeddedJourney';
@@ -1,8 +1,6 @@
1
1
  export { applyStyles } from './applyStyles';
2
2
  export { createRoots } from './createRoots';
3
3
  export { getPartnerIdFromToken } from './getPartnerIdFromToken';
4
- export { initializeHeadlessSdk } from './initializeHeadlessSdk';
5
- export { loadScriptAndInitializeSdk } from './loadScriptAndInitializeSdk';
6
4
  export { PartnerContext, type PartnerContextType } from './partnerContext';
7
5
  export { replacePlaceholdersForMainText } from './replacePlaceholdersForMainText.ts';
8
6
  export { getModalItems } from './getModalItems';
@@ -1 +0,0 @@
1
- export const WAYFLYER_HEADLESS_SDK_ID = 'wayflyer-headless-sdk';
package/src/config/url.ts DELETED
@@ -1 +0,0 @@
1
- export const HEADLESS_SDK_URL = 'https://embedded-finance-frontend.vercel.app/npm/@wf-financing/headless@3';
@@ -1,17 +0,0 @@
1
- import { IWayflyerHeadlessSdk, HeadlessSdkOptions } from '@wf-financing/embedded-types';
2
-
3
- export const initializeHeadlessSdk = (companyToken: string, options?: HeadlessSdkOptions): IWayflyerHeadlessSdk => {
4
- if (!window.WayflyerHeadlessSdk) {
5
- throw new Error('Failed to load WayflyerHeadlessSdk from the script.');
6
- }
7
-
8
- const WayflyerHeadlessSdk = window.WayflyerHeadlessSdk;
9
-
10
- if (options) {
11
- const wayflyerSdk = new WayflyerHeadlessSdk(companyToken, options);
12
-
13
- return wayflyerSdk;
14
- }
15
-
16
- return new WayflyerHeadlessSdk(companyToken);
17
- };
@@ -1,19 +0,0 @@
1
- import { IWayflyerHeadlessSdk, HeadlessSdkOptions } from '@wf-financing/embedded-types';
2
-
3
- import { initializeHeadlessSdk } from './initializeHeadlessSdk';
4
-
5
- export const loadScriptAndInitializeSdk = (
6
- script: HTMLScriptElement,
7
- companyToken: string,
8
- options?: HeadlessSdkOptions,
9
- ): Promise<IWayflyerHeadlessSdk> => {
10
- return new Promise<IWayflyerHeadlessSdk>((resolve, reject) => {
11
- script.onload = () => {
12
- try {
13
- resolve(initializeHeadlessSdk(companyToken, options));
14
- } catch (error) {
15
- reject(error);
16
- }
17
- };
18
- });
19
- };