@wf-financing/headless-sdk 4.2.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 +1 -1
- package/dist/index.es.js +64 -60
- package/dist/sdk/index.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var
|
|
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,5 +1,5 @@
|
|
|
1
|
-
var
|
|
2
|
-
const
|
|
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
4
|
return JSON.parse(atob(r.split(".")[1]));
|
|
5
5
|
} catch {
|
|
@@ -10,16 +10,16 @@ const E = "https://api.wayflyer.com", b = "https://sandbox-api.wayflyer.com", k
|
|
|
10
10
|
}, y = {
|
|
11
11
|
event: "/financing/v1/company/event/",
|
|
12
12
|
error: "/financing/v1/error/"
|
|
13
|
-
},
|
|
14
|
-
class
|
|
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
|
|
22
|
-
return
|
|
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 {
|
|
@@ -30,7 +30,7 @@ class O {
|
|
|
30
30
|
}
|
|
31
31
|
loadSession() {
|
|
32
32
|
try {
|
|
33
|
-
const e = sessionStorage.getItem(
|
|
33
|
+
const e = sessionStorage.getItem(S);
|
|
34
34
|
return e ? JSON.parse(e) : null;
|
|
35
35
|
} catch {
|
|
36
36
|
return null;
|
|
@@ -38,15 +38,15 @@ class O {
|
|
|
38
38
|
}
|
|
39
39
|
saveSession(e) {
|
|
40
40
|
try {
|
|
41
|
-
sessionStorage.setItem(
|
|
41
|
+
sessionStorage.setItem(S, JSON.stringify(e));
|
|
42
42
|
} catch {
|
|
43
43
|
console.warn("Failed to save session context");
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
const g = class a {
|
|
48
|
-
constructor(e, t,
|
|
49
|
-
|
|
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
50
|
...D,
|
|
51
51
|
...t ? { "X-SDK-Version": t } : {}
|
|
52
52
|
};
|
|
@@ -65,14 +65,14 @@ const g = class a {
|
|
|
65
65
|
static setGlobalInstance(e) {
|
|
66
66
|
window.WayflyerLogger = e;
|
|
67
67
|
}
|
|
68
|
-
static initialize(e, t,
|
|
68
|
+
static initialize(e, t, n) {
|
|
69
69
|
a.getGlobalInstance() && console.warn("Logger re-initialized");
|
|
70
|
-
const
|
|
71
|
-
a.setGlobalInstance(
|
|
70
|
+
const s = new a(e, t, n == null ? void 0 : n.isSandbox);
|
|
71
|
+
a.setGlobalInstance(s);
|
|
72
72
|
}
|
|
73
73
|
static getMandatoryProperties(e) {
|
|
74
|
-
const t = k(e), [
|
|
75
|
-
return { partner_id:
|
|
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 };
|
|
76
76
|
}
|
|
77
77
|
static getInstance() {
|
|
78
78
|
const e = a.getGlobalInstance();
|
|
@@ -82,24 +82,24 @@ const g = class a {
|
|
|
82
82
|
}
|
|
83
83
|
static async logEvent(e, t) {
|
|
84
84
|
if (this.isDevEnvironment) return;
|
|
85
|
-
const
|
|
85
|
+
const n = a.getInstance();
|
|
86
86
|
try {
|
|
87
|
-
const
|
|
88
|
-
...a.getMandatoryProperties(
|
|
87
|
+
const s = {
|
|
88
|
+
...a.getMandatoryProperties(n.companyToken),
|
|
89
89
|
...t
|
|
90
|
-
}, i = await fetch(
|
|
90
|
+
}, i = await fetch(n.baseUrl + y.event, {
|
|
91
91
|
method: "POST",
|
|
92
|
-
headers:
|
|
92
|
+
headers: n.requestHeaders,
|
|
93
93
|
body: JSON.stringify({
|
|
94
94
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
95
95
|
event_name: e,
|
|
96
|
-
properties:
|
|
96
|
+
properties: s
|
|
97
97
|
})
|
|
98
98
|
});
|
|
99
99
|
if (i.status === 200)
|
|
100
100
|
return await i.json();
|
|
101
|
-
} catch (
|
|
102
|
-
console.error("Failed to log event",
|
|
101
|
+
} catch (s) {
|
|
102
|
+
console.error("Failed to log event", s);
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
static async logError(e, t) {
|
|
@@ -107,66 +107,70 @@ const g = class a {
|
|
|
107
107
|
console.error(e);
|
|
108
108
|
return;
|
|
109
109
|
}
|
|
110
|
-
const
|
|
110
|
+
const n = a.getInstance();
|
|
111
111
|
try {
|
|
112
|
-
const
|
|
113
|
-
...a.getMandatoryProperties(
|
|
112
|
+
const s = {
|
|
113
|
+
...a.getMandatoryProperties(n.companyToken),
|
|
114
114
|
...t
|
|
115
|
-
}, i = await fetch(
|
|
115
|
+
}, i = await fetch(n.baseUrl + y.error, {
|
|
116
116
|
method: "POST",
|
|
117
|
-
headers:
|
|
117
|
+
headers: n.requestHeaders,
|
|
118
118
|
body: JSON.stringify({
|
|
119
119
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
120
120
|
message: e,
|
|
121
|
-
properties:
|
|
121
|
+
properties: s
|
|
122
122
|
})
|
|
123
123
|
});
|
|
124
124
|
if (i.status === 200)
|
|
125
125
|
return await i.json();
|
|
126
|
-
} catch (
|
|
127
|
-
console.warn("Failed to log error",
|
|
126
|
+
} catch (s) {
|
|
127
|
+
console.warn("Failed to log error", s);
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
130
|
};
|
|
131
|
-
|
|
132
|
-
let
|
|
133
|
-
const
|
|
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) => {
|
|
134
134
|
if (!window.WayflyerHeadlessSdk)
|
|
135
135
|
throw new Error("SDK code was fetched successfully, but WayflyerHeadlessSdk is unavailable on window");
|
|
136
136
|
const t = window.WayflyerHeadlessSdk;
|
|
137
137
|
return window.waylflyerHeadlessSdk || (window.waylflyerHeadlessSdk = new t(r, e)), window.waylflyerHeadlessSdk;
|
|
138
|
-
},
|
|
138
|
+
}, f = "HEADLESS_SDK_READY_EVENT", O = async (r, e) => window.WayflyerHeadlessSdk ? u(r, e) : new Promise((n, s) => {
|
|
139
139
|
const i = document.createElement("script");
|
|
140
|
-
i.src =
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}, document.head.appendChild(i);
|
|
150
|
-
}),
|
|
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
|
+
};
|
|
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;
|
|
151
152
|
class L {
|
|
152
|
-
static
|
|
153
|
-
|
|
154
|
-
|
|
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();
|
|
155
158
|
try {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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;
|
|
160
170
|
}
|
|
161
|
-
const o = Date.now() - s, f = n.sdkVersion;
|
|
162
|
-
return c.initialize(e, f, t), c.logEvent("headless_sdk_initialized", {
|
|
163
|
-
entry_point_version: w,
|
|
164
|
-
entry_point_type: N,
|
|
165
|
-
sdk_initialization_latency: o
|
|
166
|
-
}), n;
|
|
167
171
|
}
|
|
168
172
|
}
|
|
169
173
|
export {
|
|
170
|
-
|
|
174
|
+
h as CtaStateType,
|
|
171
175
|
L as WayflyerHeadlessSdk
|
|
172
176
|
};
|
package/dist/sdk/index.d.ts
CHANGED