@wf-financing/headless-sdk 0.1.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 ADDED
@@ -0,0 +1,117 @@
1
+ # Wayflyer Financing Headless SDK
2
+
3
+ Wayflyer provides a `@wf-financing/headless-sdk` package that can be used as a client-side headless SDK to interact with the Embedded Finance API.
4
+
5
+ ## Installation
6
+ Install the package directly from NPM with `npm install @wf-financing/headless-sdk`.
7
+ To minimize bundle size and reduce the impact on partners' page load times, the SDK uses dynamic imports to load main part of functionality.
8
+
9
+ ## Instantiation
10
+ Initialize the Wayflyer headless SDK by passing the `companyToken` and a pointer that specifies whether the SDK should work in mocked mode.
11
+
12
+ ```jsx
13
+ import { type IHeadlessWayflyerSdk, WayflyerHeadlessSdk } from '@wf-financing/headless-sdk';
14
+
15
+ const wayflyerSdk = (await WayflyerHeadlessCtaSdk.loadSdk('token')) as IHeadlessWayflyerSdk;
16
+ ```
17
+ Note: The companyToken should be minted using the Company Token endpoint on the partner's backend. See the Authentication section [here](https://docs.wayflyer.com/embedded-finance/authentication) for more details.
18
+
19
+ ## SDK methods
20
+ ### `getCta()`
21
+ Returns the configuration for the CTA to show to the user.
22
+
23
+ ```jsx
24
+ import type { CtaResponseType } from '@wf-financing/headless-sdk';
25
+
26
+ const cta: CtaResponseType = await wayflyerSdk.getCta();
27
+ ```
28
+
29
+ ### `startHostedApplication(applicationRequest)`
30
+ Returns a URL to the Wayflyer landing page after the user has given consent for sharing personal data.
31
+
32
+ ```jsx
33
+ import type { StartHostedApplicationRequestType, StartHostedApplicationResponseType } from '@wf-financing/headless-sdk';
34
+
35
+ const merchantData: StartHostedApplicationRequestType = {
36
+ company_data: {},
37
+ user_data: {},
38
+ partner_data: {},
39
+ };
40
+
41
+ const startHostedApplication: StartHostedApplicationResponseType = await wayflyerSdk.startHostedApplication(merchantData);
42
+ ```
43
+
44
+ ### `continueHostedApplication()`
45
+ Returns a URL to the Wayflyer landing page after the submission of the merchant funding application.
46
+
47
+ ```jsx
48
+ import type { ContinueHostedApplicationResponseType } from '@wf-financing/headless-sdk';
49
+
50
+ const startHostedApplication: ContinueHostedApplicationResponseType = await wayflyerSdk.startHostedApplication(merchantData);
51
+ ```
52
+
53
+ ## Mocked mode
54
+ To simplify the testing process, the SDK can be initialized in mock mode. To do so, pass a second argument with the value `true`.
55
+
56
+ ```jsx
57
+ import { WayflyerHeadlessCtaSdk } from '@wf-financing/headless-sdk';
58
+
59
+ const wayflyerSdk = (await WayflyerHeadlessCtaSdk.loadSdk('token', true)) as IHeadlessWayflyerSdk;
60
+ ```
61
+ In mock mode, partner can manually set responses for SDK methods via additional methods:
62
+ 1. `setCtaResponse(responseType: CtaResponseTypes)`
63
+ 2. `setStartHostedApplicationResponse(responseType: StartHostedApplicationResponseTypes)`
64
+ 3. `setContinueHostedApplicationResponse(responseType: ContinueHostedApplicationResponseTypes)`
65
+ 4. `setSdkScenario(sdkScenario: SdkScenarios)`
66
+
67
+ ### `setCtaResponse(responseType: CtaResponseTypes)`
68
+ Sets a mocked response for the `getCta()` method.
69
+
70
+ ```jsx
71
+ import { CtaResponseTypes } from '@wf-financing/headless-sdk';
72
+
73
+ wayflyerSdk.setCtaResponse(CtaResponseTypes.GENERIC_OFFER);
74
+ wayflyerSdk.setCtaResponse(CtaResponseTypes.INDICATIVE_OFFER);
75
+ wayflyerSdk.setCtaResponse(CtaResponseTypes.CONTINUE_HOSTED_APPLICATION);
76
+ wayflyerSdk.setCtaResponse(CtaResponseTypes.NO_CTA);
77
+ wayflyerSdk.setCtaResponse(CtaResponseTypes.INVALID_TOKEN);
78
+ ```
79
+
80
+ ### `setCtaResponse(responseType: CtaResponseTypes)`
81
+ Sets a mocked response for the `startHostedApplication(applicationRequest: StartHostedApplicationRequestType)` method.
82
+
83
+ ```jsx
84
+ import { StartHostedApplicationResponseTypes } from '@wf-financing/headless-sdk';
85
+
86
+ wayflyerSdk.setStartHostedApplicationResponse(StartHostedApplicationResponseTypes.REDIRECT_URL);
87
+ wayflyerSdk.setStartHostedApplicationResponse(StartHostedApplicationResponseTypes.BAD_REQUEST);
88
+ wayflyerSdk.setStartHostedApplicationResponse(StartHostedApplicationResponseTypes.INVALID_TOKEN);
89
+ ```
90
+
91
+ ### `setContinueHostedApplicationResponse(responseType: ContinueHostedApplicationResponseTypes)`
92
+ Sets a mocked response for the `continueHostedApplication()` method.
93
+
94
+ ```jsx
95
+ import { ContinueHostedApplicationResponseTypes } from '@wf-financing/headless-sdk';
96
+
97
+ wayflyerSdk.setContinueHostedApplicationResponse(ContinueHostedApplicationResponseTypes.REDIRECT_URL);
98
+ wayflyerSdk.setContinueHostedApplicationResponse(ContinueHostedApplicationResponseTypes.BAD_REQUEST);
99
+ wayflyerSdk.setContinueHostedApplicationResponse(ContinueHostedApplicationResponseTypes.INVALID_TOKEN);
100
+ ```
101
+
102
+ ### `setSdkScenario(sdkScenario: SdkScenarios)`
103
+ Provides complex mocked scenarios for complete E2E testing. This ensures that partners do not encounter incompatible responses for different methods, such as `startHostedApplication(applicationRequest: StartHostedApplicationRequestType)` and `continueHostedApplication()`, which cannot return valid responses at the same time.
104
+
105
+ ```jsx
106
+ import { SdkScenarios } from '@wf-financing/headless-sdk';
107
+
108
+ wayflyerSdk.setSdkScenario(SdkScenarios.INDICATIVE_NEW_APPLICATION);
109
+ wayflyerSdk.setSdkScenario(SdkScenarios.GENERIC_NEW_APPLICATION);
110
+ wayflyerSdk.setSdkScenario(SdkScenarios.CONTINUE_APPLICATION);
111
+ wayflyerSdk.setSdkScenario(SdkScenarios.NO_CTA);
112
+ ```
113
+
114
+ Note: After calling `setSdkScenario(sdkScenario: SdkScenarios)`, other methods for setting manual responses will no longer have any effect.
115
+
116
+ ## Next Steps
117
+ For further details on using the SDK, troubleshooting, or expanding on these examples, check out our [full documentation](https://docs.wayflyer.com/).
@@ -0,0 +1,2 @@
1
+ export { WAYFLYER_HEADLESS_SDK_ID } from './scriptId';
2
+ export { HEADLESS_PACKAGE_URL } from './url';
@@ -0,0 +1 @@
1
+ export declare const WAYFLYER_HEADLESS_SDK_ID = "wayflyer-headless-sdk";
@@ -0,0 +1 @@
1
+ export declare const HEADLESS_PACKAGE_URL = "https://unpkg.com/@wf-financing/headless@1/dist/index.es.js";
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var s=(e=>(e.GENERIC_OFFER="generic_offer",e.INDICATIVE_OFFER="indicative_offer",e.CONTINUE_HOSTED_APPLICATION="continue_hosted_application",e.NO_CTA="no_cta",e.INVALID_TOKEN="invalid_token",e))(s||{}),d=(e=>(e.REDIRECT_URL="redirect_url",e.BAD_REQUEST="bad_request",e.INVALID_TOKEN="invalid_token",e))(d||{}),o=(e=>(e.REDIRECT_URL="redirect_url",e.BAD_REQUEST="bad_request",e.INVALID_TOKEN="invalid_token",e))(o||{}),l=(e=>(e.NO_CTA="no_cta",e.CONTINUE_APPLICATION="continue_application",e.GENERIC_NEW_APPLICATION="generic_new_application",e.INDICATIVE_NEW_APPLICATION="indicative_new_application",e))(l||{}),c=(e=>(e.GENERIC_OFFER="generic_offer",e.INDICATIVE_OFFER="indicative_offer",e.CONTINUE_APPLICATION="continue_application",e))(c||{});const n="wayflyer-headless-sdk",y="https://unpkg.com/@wf-financing/headless@1/dist/index.es.js",E=(e,a)=>{if(!window.WayflyerHeadlessSdk)throw new Error("Failed to load WayflyerHeadlessSdk from the script.");const t=window.WayflyerHeadlessSdk;return new t(e,a)},i=(e,a,t)=>new Promise((r,I)=>{e.onload=()=>{try{r(E(a,t))}catch(p){I(p)}}}),S=async(e,a)=>{try{const t=document.getElementById(n);if(window.WayflyerHeadlessSdk)return E(e,a);if(t)return i(t,e,a);const r=document.createElement("script");return r.src=y,r.type="module",r.id=n,r.async=!0,document.head.appendChild(r),i(r,e,a)}catch(t){throw console.error("Error in loading headless SDK:",t),new Error("Failed to load script")}};class _{static async loadSdk(a,t){return await S(a,t)}}exports.ContinueHostedApplicationResponseTypes=o;exports.CtaResponseTypes=s;exports.CtaStateType=c;exports.SdkScenarios=l;exports.StartHostedApplicationResponseTypes=d;exports.WayflyerHeadlessCtaSdk=_;
@@ -0,0 +1,3 @@
1
+ export { ContinueHostedApplicationResponseTypes, CtaResponseTypes, CtaStateType, StartHostedApplicationResponseTypes, SdkScenarios, } from '@wf-financing/embedded-types';
2
+ export type { CanadianProvinceCode, CompanyDataType, ContinueHostedApplicationResponseType, CountryCode, CtaContinueFundingType, CtaGenericOfferType, CtaIndicativeOfferType, CtaResponseType, IHeadlessWayflyerSdk, StartHostedApplicationRequestType, StartHostedApplicationResponseType, UserDataType, USStateCode, } from '@wf-financing/embedded-types';
3
+ export { WayflyerHeadlessCtaSdk } from './sdk';
@@ -0,0 +1,40 @@
1
+ var l = /* @__PURE__ */ ((e) => (e.GENERIC_OFFER = "generic_offer", e.INDICATIVE_OFFER = "indicative_offer", e.CONTINUE_HOSTED_APPLICATION = "continue_hosted_application", e.NO_CTA = "no_cta", e.INVALID_TOKEN = "invalid_token", e))(l || {}), c = /* @__PURE__ */ ((e) => (e.REDIRECT_URL = "redirect_url", e.BAD_REQUEST = "bad_request", e.INVALID_TOKEN = "invalid_token", e))(c || {}), E = /* @__PURE__ */ ((e) => (e.REDIRECT_URL = "redirect_url", e.BAD_REQUEST = "bad_request", e.INVALID_TOKEN = "invalid_token", e))(E || {}), I = /* @__PURE__ */ ((e) => (e.NO_CTA = "no_cta", e.CONTINUE_APPLICATION = "continue_application", e.GENERIC_NEW_APPLICATION = "generic_new_application", e.INDICATIVE_NEW_APPLICATION = "indicative_new_application", e))(I || {}), p = /* @__PURE__ */ ((e) => (e.GENERIC_OFFER = "generic_offer", e.INDICATIVE_OFFER = "indicative_offer", e.CONTINUE_APPLICATION = "continue_application", e))(p || {});
2
+ const r = "wayflyer-headless-sdk", _ = "https://unpkg.com/@wf-financing/headless@1/dist/index.es.js", s = (e, a) => {
3
+ if (!window.WayflyerHeadlessSdk)
4
+ throw new Error("Failed to load WayflyerHeadlessSdk from the script.");
5
+ const t = window.WayflyerHeadlessSdk;
6
+ return new t(e, a);
7
+ }, i = (e, a, t) => new Promise((n, d) => {
8
+ e.onload = () => {
9
+ try {
10
+ n(s(a, t));
11
+ } catch (o) {
12
+ d(o);
13
+ }
14
+ };
15
+ }), A = async (e, a) => {
16
+ try {
17
+ const t = document.getElementById(r);
18
+ if (window.WayflyerHeadlessSdk)
19
+ return s(e, a);
20
+ if (t)
21
+ return i(t, e, a);
22
+ const n = document.createElement("script");
23
+ return n.src = _, n.type = "module", n.id = r, n.async = !0, document.head.appendChild(n), i(n, e, a);
24
+ } catch (t) {
25
+ throw console.error("Error in loading headless SDK:", t), new Error("Failed to load script");
26
+ }
27
+ };
28
+ class N {
29
+ static async loadSdk(a, t) {
30
+ return await A(a, t);
31
+ }
32
+ }
33
+ export {
34
+ E as ContinueHostedApplicationResponseTypes,
35
+ l as CtaResponseTypes,
36
+ p as CtaStateType,
37
+ I as SdkScenarios,
38
+ c as StartHostedApplicationResponseTypes,
39
+ N as WayflyerHeadlessCtaSdk
40
+ };
@@ -0,0 +1,3 @@
1
+ export declare class WayflyerHeadlessCtaSdk {
2
+ static loadSdk(companyToken: string, isMockedMode?: boolean): Promise<void | import('@wf-financing/embedded-types').IHeadlessWayflyerSdk>;
3
+ }
@@ -0,0 +1 @@
1
+ export { loadHeadlessSdkMode } from './loadHeadlessMode';
@@ -0,0 +1,5 @@
1
+ import { IHeadlessWayflyerSdk } from '@wf-financing/embedded-types';
2
+
3
+ type LoadHeadlessSdkModeType = (companyToken: string, isMockedMode?: boolean) => Promise<IHeadlessWayflyerSdk | void>;
4
+ export declare const loadHeadlessSdkMode: LoadHeadlessSdkModeType;
5
+ export {};
@@ -0,0 +1,2 @@
1
+ export { initializeHeadlessSdk } from './initializeHeadlessSdk';
2
+ export { loadScriptAndInitializeSdk } from './loadScriptAndInitializeSdk';
@@ -0,0 +1,3 @@
1
+ import { IHeadlessWayflyerSdk } from '@wf-financing/embedded-types';
2
+
3
+ export declare const initializeHeadlessSdk: (companyToken: string, isMockedMode?: boolean) => IHeadlessWayflyerSdk;
@@ -0,0 +1,3 @@
1
+ import { IHeadlessWayflyerSdk } from '@wf-financing/embedded-types';
2
+
3
+ export declare const loadScriptAndInitializeSdk: (script: HTMLScriptElement, companyToken: string, isMockedMode?: boolean) => Promise<IHeadlessWayflyerSdk>;
@@ -0,0 +1,2 @@
1
+ declare const _default: import('vite').UserConfig;
2
+ export default _default;
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@wf-financing/headless-sdk",
3
+ "version": "0.1.0",
4
+ "main": "dist/index.cjs.js",
5
+ "module": "dist/index.es.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.es.js",
10
+ "types": "./dist/index.d.ts",
11
+ "require": "./dist/index.cjs.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "dependencies": {
18
+ "@wf-financing/embedded-types": "0.2.6"
19
+ },
20
+ "devDependencies": {
21
+ "vite": "^6.3.5",
22
+ "vite-plugin-dts": "^3.4.0",
23
+ "typescript": "^5.0.0"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "scripts": {
29
+ "clean": "rm -rf dist",
30
+ "build": "vite build",
31
+ "prepare-sdk-cta": "pnpm clean && pnpm build && pnpm pack",
32
+ "publish-sdk-cta": "pnpm clean && pnpm build && pnpm publish --access public --no-git-checks",
33
+ "test": "vitest",
34
+ "test:coverage": "vitest --coverage"
35
+ }
36
+ }