@wf-financing/headless-sdk 4.3.0 → 4.3.2

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