@wf-financing/headless-sdk 4.1.0 → 4.3.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/dist/index.cjs.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var p=(s=>(s.GENERIC_OFFER="generic_offer",s.INDICATIVE_OFFER="indicative_offer",s.CONTINUE_APPLICATION="continue_application",s))(p||{}),h=Object.defineProperty,I=(s,e,t)=>e in s?h(s,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[e]=t,d=(s,e,t)=>I(s,typeof e!="symbol"?e+"":e,t);const v="https://api.wayflyer.com",E="https://sandbox-api.wayflyer.com",b=s=>{try{return JSON.parse(atob(s.split(".")[1]))}catch{return null}},c={"Content-Type":"application/json"},y={event:"/financing/v1/company/event/",error:"/financing/v1/error/"},S="wf_session_v1",k=1800*1e3,A=1440*60*1e3;class D{getSessionId(){const e=Date.now();let t=this.loadSession();return(!t||this.isSessionExpired(t,e))&&(t=this.mintNewSession(e)),t.lastActivity=e,this.saveSession(t),t.sessionId}isSessionExpired(e,t){const n=t-e.lastActivity>k,i=t-e.createdAt>A;return n||i}mintNewSession(e){return{sessionId:crypto.randomUUID(),createdAt:e,lastActivity:e}}loadSession(){try{const e=sessionStorage.getItem(S);return e?JSON.parse(e):null}catch{return null}}saveSession(e){try{sessionStorage.setItem(S,JSON.stringify(e))}catch{console.warn("Failed to save session context")}}}const f=class a{constructor(e,t){d(this,"baseUrl",null),d(this,"sessionManager"),this.companyToken=e,this.baseUrl=t?E:v,this.sessionManager=new D}static getGlobalInstance(){return window.WayflyerLogger||null}static setGlobalInstance(e){window.WayflyerLogger=e}static initialize(e,t){a.getGlobalInstance()&&console.warn("Logger re-initialized");const n=new a(e,t==null?void 0:t.isSandbox);a.setGlobalInstance(n)}static getMandatoryProperties(e){const t=b(e),[n,i]=(t==null?void 0:t.sub.split("|"))||[],o=t==null?void 0:t.user_id;return{partner_id:n,company_id:i,user_id:o}}static getInstance(){const e=a.getGlobalInstance();if(!e)throw new Error("Logger not initialized. Call initialize() first.");return e}static async logEvent(e,t){if(this.isDevEnvironment)return;const n=a.getInstance(),i=n.sessionManager.getSessionId(),o={...c,Authorization:`Bearer ${n.companyToken}`,"X-Session-ID":i};try{const r={...a.getMandatoryProperties(n.companyToken),...t},l=await fetch(n.baseUrl+y.event,{method:"POST",headers:o,body:JSON.stringify({timestamp:new Date().toISOString(),event_name:e,properties:r})});if(l.status===200)return await l.json()}catch(r){console.error("Failed to log event",r)}}static async logError(e,t){if(this.isDevEnvironment){console.error(e);return}const n=a.getInstance(),i=n.sessionManager.getSessionId(),o={...c,"X-Session-ID":i};try{const r={...a.getMandatoryProperties(n.companyToken),...t},l=await fetch(n.baseUrl+y.error,{method:"POST",headers:o,body:JSON.stringify({timestamp:new Date().toISOString(),message:e,properties:r})});if(l.status===200)return await l.json()}catch(r){console.warn("Failed to log error",r)}}};d(f,"isDevEnvironment",!1);let u=f;const w="wayflyer-headless-sdk",O="https://embedded-finance-frontend.vercel.app/npm/@wf-financing/headless@3",z="4.1.0",m=(s,e)=>{if(!window.WayflyerHeadlessSdk)throw new Error("Failed to load WayflyerHeadlessSdk from the script.");const t=window.WayflyerHeadlessSdk;return window.waylflyerHeadlessSdk||(window.waylflyerHeadlessSdk=new t(s,e)),window.waylflyerHeadlessSdk},g=(s,e,t)=>new Promise((n,i)=>{s.onload=()=>{try{n(m(e,t))}catch(o){i(o)}}}),H=async(s,e)=>{try{const t=document.getElementById(w);if(window.WayflyerHeadlessSdk)return m(s,e);if(t)return g(t,s,e);const n=document.createElement("script");return n.src=O,n.type="module",n.id=w,n.async=!0,document.head.appendChild(n),g(n,s,e)}catch(t){throw console.error("Error in loading headless SDK:",t),new Error("Failed to load script")}};class N{static async loadSdk(e,t){const n=Date.now();u.initialize(e,t);const i=await H(e,t),r=Date.now()-n;return u.logEvent(`headless_sdk_initialized: entry point version: ${z}, SDK version: ${i==null?void 0:i.sdkVersion} SDK initialization latency: ${r}ms`),i}}exports.CtaStateType=p;exports.WayflyerHeadlessSdk=N;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var g=(r=>(r.GENERIC_OFFER="generic_offer",r.INDICATIVE_OFFER="indicative_offer",r.CONTINUE_APPLICATION="continue_application",r))(g||{}),E=Object.defineProperty,v=(r,e,t)=>e in r?E(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,c=(r,e,t)=>v(r,typeof e!="symbol"?e+"":e,t);const m="https://api.wayflyer.com",I="https://sandbox-api.wayflyer.com",k=r=>{try{return JSON.parse(atob(r.split(".")[1]))}catch{return null}},b={"Content-Type":"application/json"},y={event:"/financing/v1/company/event/",error:"/financing/v1/error/"},S="wf_session_v1",D=1800*1e3,H=1440*60*1e3;class _{getSessionId(){const e=Date.now();let t=this.loadSession();return(!t||this.isSessionExpired(t,e))&&(t=this.mintNewSession(e)),t.lastActivity=e,this.saveSession(t),t.sessionId}isSessionExpired(e,t){const n=t-e.lastActivity>D,s=t-e.createdAt>H;return n||s}mintNewSession(e){return{sessionId:crypto.randomUUID(),createdAt:e,lastActivity:e}}loadSession(){try{const e=sessionStorage.getItem(S);return e?JSON.parse(e):null}catch{return null}}saveSession(e){try{sessionStorage.setItem(S,JSON.stringify(e))}catch{console.warn("Failed to save session context")}}}const p=class a{constructor(e,t,n){c(this,"baseUrl",null),c(this,"sessionManager"),c(this,"headers"),this.companyToken=e,this.baseUrl=n?I:m,this.sessionManager=new _,this.headers={...b,...t?{"X-SDK-Version":t}:{}}}get requestHeaders(){const e=a.getInstance(),t=e.sessionManager.getSessionId();return{Authorization:`Bearer ${e.companyToken}`,"X-Session-ID":t,...e.headers}}static getGlobalInstance(){return window.WayflyerLogger||null}static setGlobalInstance(e){window.WayflyerLogger=e}static initialize(e,t,n){a.getGlobalInstance()&&console.warn("Logger re-initialized");const s=new a(e,t,n==null?void 0:n.isSandbox);a.setGlobalInstance(s)}static getMandatoryProperties(e){const t=k(e),[n,s]=(t==null?void 0:t.sub.split("|"))||[],i=t==null?void 0:t.user_id;return{partner_id:n,company_id:s,user_id:i}}static getInstance(){const e=a.getGlobalInstance();if(!e)throw new Error("Logger not initialized. Call initialize() first.");return e}static async logEvent(e,t){if(this.isDevEnvironment)return;const n=a.getInstance();try{const s={...a.getMandatoryProperties(n.companyToken),...t},i=await fetch(n.baseUrl+y.event,{method:"POST",headers:n.requestHeaders,body:JSON.stringify({timestamp:new Date().toISOString(),event_name:e,properties:s})});if(i.status===200)return await i.json()}catch(s){console.error("Failed to log event",s)}}static async logError(e,t){if(this.isDevEnvironment){console.error(e);return}const n=a.getInstance();try{const s={...a.getMandatoryProperties(n.companyToken),...t},i=await fetch(n.baseUrl+y.error,{method:"POST",headers:n.requestHeaders,body:JSON.stringify({timestamp:new Date().toISOString(),message:e,properties:s})});if(i.status===200)return await i.json()}catch(s){console.warn("Failed to log error",s)}}};c(p,"isDevEnvironment",!1);let l=p;const A="wayflyer-headless-sdk",O="https://embedded-finance-frontend.vercel.app/npm/@wf-financing/headless@3",u="4.3.0",w=(r,e)=>{if(!window.WayflyerHeadlessSdk)throw new Error("SDK code was fetched successfully, but WayflyerHeadlessSdk is unavailable on window");const t=window.WayflyerHeadlessSdk;return window.waylflyerHeadlessSdk||(window.waylflyerHeadlessSdk=new t(r,e)),window.waylflyerHeadlessSdk},f="HEADLESS_SDK_READY_EVENT",P=async(r,e)=>window.WayflyerHeadlessSdk?w(r,e):new Promise((n,s)=>{const i=document.createElement("script");i.src=O,i.type="module",i.id=A,i.async=!0,i.onerror=()=>{s(new Error(`Failed to load headless SDK script from ${i.src}`))};const o=()=>{const h=w(r,e);n(h)};window.addEventListener(f,o,{once:!0}),setTimeout(()=>{s(new Error("Headless SDK initialization timeout exceeded")),window.removeEventListener(f,o)},1e3),document.head.appendChild(i)}),N="headless";let d=null;class L{static loadSdk(e,t){return d||(d=this.getHeadlessSdkPromise(e,t),d)}static async getHeadlessSdkPromise(e,t){const n=Date.now();try{const s=await P(e,t),i=Date.now()-n,o=s.sdkVersion;return l.initialize(e,o,t),l.logEvent("headless_sdk_initialized",{entry_point_version:u,entry_point_type:N,sdk_initialization_latency:i}),s}catch(s){const i=s instanceof Error?s:new Error(String(s));throw l.initialize(e,void 0,t),l.logError(i.message,{entry_point_version:u}),s}}}exports.CtaStateType=g;exports.WayflyerHeadlessSdk=L;
package/dist/index.es.js CHANGED
@@ -1,25 +1,25 @@
1
- var m = /* @__PURE__ */ ((s) => (s.GENERIC_OFFER = "generic_offer", s.INDICATIVE_OFFER = "indicative_offer", s.CONTINUE_APPLICATION = "continue_application", s))(m || {}), h = Object.defineProperty, I = (s, e, t) => e in s ? h(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t, c = (s, e, t) => I(s, typeof e != "symbol" ? e + "" : e, t);
2
- const v = "https://api.wayflyer.com", E = "https://sandbox-api.wayflyer.com", b = (s) => {
1
+ var h = /* @__PURE__ */ ((r) => (r.GENERIC_OFFER = "generic_offer", r.INDICATIVE_OFFER = "indicative_offer", r.CONTINUE_APPLICATION = "continue_application", r))(h || {}), E = Object.defineProperty, v = (r, e, t) => e in r ? E(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, c = (r, e, t) => v(r, typeof e != "symbol" ? e + "" : e, t);
2
+ const m = "https://api.wayflyer.com", I = "https://sandbox-api.wayflyer.com", k = (r) => {
3
3
  try {
4
- return JSON.parse(atob(s.split(".")[1]));
4
+ return JSON.parse(atob(r.split(".")[1]));
5
5
  } catch {
6
6
  return null;
7
7
  }
8
- }, d = {
8
+ }, D = {
9
9
  "Content-Type": "application/json"
10
10
  }, y = {
11
11
  event: "/financing/v1/company/event/",
12
12
  error: "/financing/v1/error/"
13
- }, S = "wf_session_v1", k = 1800 * 1e3, A = 1440 * 60 * 1e3;
14
- class D {
13
+ }, S = "wf_session_v1", b = 1800 * 1e3, H = 1440 * 60 * 1e3;
14
+ class _ {
15
15
  getSessionId() {
16
16
  const e = Date.now();
17
17
  let t = this.loadSession();
18
18
  return (!t || this.isSessionExpired(t, e)) && (t = this.mintNewSession(e)), t.lastActivity = e, this.saveSession(t), t.sessionId;
19
19
  }
20
20
  isSessionExpired(e, t) {
21
- const n = t - e.lastActivity > k, i = t - e.createdAt > A;
22
- return n || i;
21
+ const n = t - e.lastActivity > b, s = t - e.createdAt > H;
22
+ return n || s;
23
23
  }
24
24
  mintNewSession(e) {
25
25
  return {
@@ -45,8 +45,19 @@ class D {
45
45
  }
46
46
  }
47
47
  const g = class a {
48
- constructor(e, t) {
49
- c(this, "baseUrl", null), c(this, "sessionManager"), this.companyToken = e, this.baseUrl = t ? E : v, this.sessionManager = new D();
48
+ constructor(e, t, n) {
49
+ c(this, "baseUrl", null), c(this, "sessionManager"), c(this, "headers"), this.companyToken = e, this.baseUrl = n ? I : m, this.sessionManager = new _(), this.headers = {
50
+ ...D,
51
+ ...t ? { "X-SDK-Version": t } : {}
52
+ };
53
+ }
54
+ get requestHeaders() {
55
+ const e = a.getInstance(), t = e.sessionManager.getSessionId();
56
+ return {
57
+ Authorization: `Bearer ${e.companyToken}`,
58
+ "X-Session-ID": t,
59
+ ...e.headers
60
+ };
50
61
  }
51
62
  static getGlobalInstance() {
52
63
  return window.WayflyerLogger || null;
@@ -54,14 +65,14 @@ const g = class a {
54
65
  static setGlobalInstance(e) {
55
66
  window.WayflyerLogger = e;
56
67
  }
57
- static initialize(e, t) {
68
+ static initialize(e, t, n) {
58
69
  a.getGlobalInstance() && console.warn("Logger re-initialized");
59
- const n = new a(e, t == null ? void 0 : t.isSandbox);
60
- a.setGlobalInstance(n);
70
+ const s = new a(e, t, n == null ? void 0 : n.isSandbox);
71
+ a.setGlobalInstance(s);
61
72
  }
62
73
  static getMandatoryProperties(e) {
63
- const t = b(e), [n, i] = (t == null ? void 0 : t.sub.split("|")) || [], o = t == null ? void 0 : t.user_id;
64
- return { partner_id: n, company_id: i, user_id: o };
74
+ const t = k(e), [n, s] = (t == null ? void 0 : t.sub.split("|")) || [], i = t == null ? void 0 : t.user_id;
75
+ return { partner_id: n, company_id: s, user_id: i };
65
76
  }
66
77
  static getInstance() {
67
78
  const e = a.getGlobalInstance();
@@ -71,28 +82,24 @@ const g = class a {
71
82
  }
72
83
  static async logEvent(e, t) {
73
84
  if (this.isDevEnvironment) return;
74
- const n = a.getInstance(), i = n.sessionManager.getSessionId(), o = {
75
- ...d,
76
- Authorization: `Bearer ${n.companyToken}`,
77
- "X-Session-ID": i
78
- };
85
+ const n = a.getInstance();
79
86
  try {
80
- const r = {
87
+ const s = {
81
88
  ...a.getMandatoryProperties(n.companyToken),
82
89
  ...t
83
- }, l = await fetch(n.baseUrl + y.event, {
90
+ }, i = await fetch(n.baseUrl + y.event, {
84
91
  method: "POST",
85
- headers: o,
92
+ headers: n.requestHeaders,
86
93
  body: JSON.stringify({
87
94
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
88
95
  event_name: e,
89
- properties: r
96
+ properties: s
90
97
  })
91
98
  });
92
- if (l.status === 200)
93
- return await l.json();
94
- } catch (r) {
95
- console.error("Failed to log event", r);
99
+ if (i.status === 200)
100
+ return await i.json();
101
+ } catch (s) {
102
+ console.error("Failed to log event", s);
96
103
  }
97
104
  }
98
105
  static async logError(e, t) {
@@ -100,69 +107,70 @@ const g = class a {
100
107
  console.error(e);
101
108
  return;
102
109
  }
103
- const n = a.getInstance(), i = n.sessionManager.getSessionId(), o = {
104
- ...d,
105
- "X-Session-ID": i
106
- };
110
+ const n = a.getInstance();
107
111
  try {
108
- const r = {
112
+ const s = {
109
113
  ...a.getMandatoryProperties(n.companyToken),
110
114
  ...t
111
- }, l = await fetch(n.baseUrl + y.error, {
115
+ }, i = await fetch(n.baseUrl + y.error, {
112
116
  method: "POST",
113
- headers: o,
117
+ headers: n.requestHeaders,
114
118
  body: JSON.stringify({
115
119
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
116
120
  message: e,
117
- properties: r
121
+ properties: s
118
122
  })
119
123
  });
120
- if (l.status === 200)
121
- return await l.json();
122
- } catch (r) {
123
- console.warn("Failed to log error", r);
124
+ if (i.status === 200)
125
+ return await i.json();
126
+ } catch (s) {
127
+ console.warn("Failed to log error", s);
124
128
  }
125
129
  }
126
130
  };
127
131
  c(g, "isDevEnvironment", !1);
128
- let w = g;
129
- const p = "wayflyer-headless-sdk", O = "https://embedded-finance-frontend.vercel.app/npm/@wf-financing/headless@3", z = "4.1.0", f = (s, e) => {
132
+ let l = g;
133
+ const A = "wayflyer-headless-sdk", N = "https://embedded-finance-frontend.vercel.app/npm/@wf-financing/headless@3", w = "4.3.0", u = (r, e) => {
130
134
  if (!window.WayflyerHeadlessSdk)
131
- throw new Error("Failed to load WayflyerHeadlessSdk from the script.");
135
+ throw new Error("SDK code was fetched successfully, but WayflyerHeadlessSdk is unavailable on window");
132
136
  const t = window.WayflyerHeadlessSdk;
133
- return window.waylflyerHeadlessSdk || (window.waylflyerHeadlessSdk = new t(s, e)), window.waylflyerHeadlessSdk;
134
- }, u = (s, e, t) => new Promise((n, i) => {
135
- s.onload = () => {
136
- try {
137
- n(f(e, t));
138
- } catch (o) {
139
- i(o);
140
- }
137
+ return window.waylflyerHeadlessSdk || (window.waylflyerHeadlessSdk = new t(r, e)), window.waylflyerHeadlessSdk;
138
+ }, f = "HEADLESS_SDK_READY_EVENT", O = async (r, e) => window.WayflyerHeadlessSdk ? u(r, e) : new Promise((n, s) => {
139
+ const i = document.createElement("script");
140
+ i.src = N, i.type = "module", i.id = A, i.async = !0, i.onerror = () => {
141
+ s(new Error(`Failed to load headless SDK script from ${i.src}`));
141
142
  };
142
- }), N = async (s, e) => {
143
- try {
144
- const t = document.getElementById(p);
145
- if (window.WayflyerHeadlessSdk)
146
- return f(s, e);
147
- if (t)
148
- return u(t, s, e);
149
- const n = document.createElement("script");
150
- return n.src = O, n.type = "module", n.id = p, n.async = !0, document.head.appendChild(n), u(n, s, e);
151
- } catch (t) {
152
- throw console.error("Error in loading headless SDK:", t), new Error("Failed to load script");
153
- }
154
- };
155
- class H {
156
- static async loadSdk(e, t) {
143
+ const o = () => {
144
+ const p = u(r, e);
145
+ n(p);
146
+ };
147
+ window.addEventListener(f, o, { once: !0 }), setTimeout(() => {
148
+ s(new Error("Headless SDK initialization timeout exceeded")), window.removeEventListener(f, o);
149
+ }, 1e3), document.head.appendChild(i);
150
+ }), P = "headless";
151
+ let d = null;
152
+ class L {
153
+ static loadSdk(e, t) {
154
+ return d || (d = this.getHeadlessSdkPromise(e, t), d);
155
+ }
156
+ static async getHeadlessSdkPromise(e, t) {
157
157
  const n = Date.now();
158
- w.initialize(e, t);
159
- const i = await N(e, t), r = Date.now() - n;
160
- return w.logEvent(
161
- `headless_sdk_initialized: entry point version: ${z}, SDK version: ${i == null ? void 0 : i.sdkVersion} SDK initialization latency: ${r}ms`
162
- ), i;
158
+ try {
159
+ const s = await O(e, t), i = Date.now() - n, o = s.sdkVersion;
160
+ return l.initialize(e, o, t), l.logEvent("headless_sdk_initialized", {
161
+ entry_point_version: w,
162
+ entry_point_type: P,
163
+ sdk_initialization_latency: i
164
+ }), s;
165
+ } catch (s) {
166
+ const i = s instanceof Error ? s : new Error(String(s));
167
+ throw l.initialize(e, void 0, t), l.logError(i.message, {
168
+ entry_point_version: w
169
+ }), s;
170
+ }
163
171
  }
164
172
  }
165
173
  export {
166
- m as CtaStateType,
167
- H as WayflyerHeadlessSdk
174
+ h as CtaStateType,
175
+ L as WayflyerHeadlessSdk
168
176
  };
@@ -2,4 +2,5 @@ import { HeadlessSdkOptions, IWayflyerHeadlessSdk } from '@wf-financing/embedded
2
2
 
3
3
  export declare class WayflyerHeadlessSdk {
4
4
  static loadSdk(companyToken: string, options?: HeadlessSdkOptions): Promise<IWayflyerHeadlessSdk>;
5
+ private static getHeadlessSdkPromise;
5
6
  }
@@ -1,2 +1,2 @@
1
1
  export { initializeHeadlessSdk } from './initializeHeadlessSdk';
2
- export { loadScriptAndInitializeSdk } from './loadScriptAndInitializeSdk';
2
+ export { loadHeadlessSdkScript } from './loadHeadlessSdkScript.ts';
@@ -1,5 +1,5 @@
1
1
  import { IWayflyerHeadlessSdk, HeadlessSdkOptions } from '@wf-financing/embedded-types';
2
2
 
3
- type LoadHeadlessSdkModeType = (companyToken: string, options?: HeadlessSdkOptions) => Promise<IWayflyerHeadlessSdk | void>;
4
- export declare const loadHeadlessSdkMode: LoadHeadlessSdkModeType;
3
+ type LoadHeadlessSdkModeType = (companyToken: string, options?: HeadlessSdkOptions) => Promise<IWayflyerHeadlessSdk>;
4
+ export declare const loadHeadlessSdkScript: LoadHeadlessSdkModeType;
5
5
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wf-financing/headless-sdk",
3
- "version": "4.1.0",
3
+ "version": "4.3.0",
4
4
  "main": "dist/index.cjs.js",
5
5
  "module": "dist/index.es.js",
6
6
  "types": "dist/index.d.ts",
@@ -16,7 +16,7 @@
16
16
  ],
17
17
  "dependencies": {
18
18
  "@wf-financing/embedded-types": "1.1.0",
19
- "@wf-financing/logger": "2.1.0"
19
+ "@wf-financing/logger": "2.2.0"
20
20
  },
21
21
  "devDependencies": {
22
22
  "vite": "^6.3.5",
@@ -1 +0,0 @@
1
- export { loadHeadlessSdkMode } from './loadHeadlessMode';
@@ -1,3 +0,0 @@
1
- import { IWayflyerHeadlessSdk, HeadlessSdkOptions } from '@wf-financing/embedded-types';
2
-
3
- export declare const loadScriptAndInitializeSdk: (script: HTMLScriptElement, companyToken: string, options?: HeadlessSdkOptions) => Promise<IWayflyerHeadlessSdk>;