@wf-financing/headless-sdk 4.3.1 → 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 +1 -1
- package/dist/index.es.js +86 -85
- package/package.json +2 -2
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 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
|
|
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
|
-
},
|
|
8
|
+
}, b = {
|
|
9
9
|
"Content-Type": "application/json"
|
|
10
|
-
},
|
|
10
|
+
}, S = {
|
|
11
11
|
event: "/financing/v1/company/event/",
|
|
12
12
|
error: "/financing/v1/error/"
|
|
13
|
-
},
|
|
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
|
|
18
|
-
return (!
|
|
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,
|
|
21
|
-
const
|
|
22
|
-
return
|
|
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(
|
|
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(
|
|
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
|
|
48
|
-
constructor(e,
|
|
49
|
-
|
|
50
|
-
...
|
|
51
|
-
...
|
|
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(),
|
|
55
|
+
const e = a.getInstance(), s = e.sessionManager.getSessionId();
|
|
56
56
|
return {
|
|
57
57
|
Authorization: `Bearer ${e.companyToken}`,
|
|
58
|
-
"X-Session-ID":
|
|
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,
|
|
68
|
+
static initialize(e, s, i, t) {
|
|
69
69
|
a.getGlobalInstance() && console.warn("Logger re-initialized");
|
|
70
|
-
const
|
|
71
|
-
a.setGlobalInstance(
|
|
70
|
+
const n = new a(e, s, i, t == null ? void 0 : t.isSandbox);
|
|
71
|
+
a.setGlobalInstance(n);
|
|
72
72
|
}
|
|
73
|
-
static getMandatoryProperties(
|
|
74
|
-
const t = k(
|
|
75
|
-
return {
|
|
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,
|
|
89
|
+
static async logEvent(e, s) {
|
|
84
90
|
if (this.isDevEnvironment) return;
|
|
85
|
-
const
|
|
91
|
+
const i = a.getInstance();
|
|
86
92
|
try {
|
|
87
|
-
const
|
|
88
|
-
...a.getMandatoryProperties(
|
|
89
|
-
...
|
|
90
|
-
},
|
|
93
|
+
const t = {
|
|
94
|
+
...a.getMandatoryProperties(),
|
|
95
|
+
...s
|
|
96
|
+
}, n = await fetch(i.baseUrl + S.event, {
|
|
91
97
|
method: "POST",
|
|
92
|
-
headers:
|
|
98
|
+
headers: i.requestHeaders,
|
|
93
99
|
body: JSON.stringify({
|
|
94
100
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
95
101
|
event_name: e,
|
|
96
|
-
properties:
|
|
102
|
+
properties: t
|
|
97
103
|
})
|
|
98
104
|
});
|
|
99
|
-
if (
|
|
100
|
-
return await
|
|
101
|
-
} catch (
|
|
102
|
-
console.error("Failed to log event",
|
|
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,
|
|
111
|
+
static async logError(e, s) {
|
|
106
112
|
if (this.isDevEnvironment) {
|
|
107
113
|
console.error(e);
|
|
108
114
|
return;
|
|
109
115
|
}
|
|
110
|
-
const
|
|
116
|
+
const i = a.getInstance();
|
|
111
117
|
try {
|
|
112
|
-
const
|
|
113
|
-
...a.getMandatoryProperties(
|
|
114
|
-
...
|
|
115
|
-
},
|
|
118
|
+
const t = {
|
|
119
|
+
...a.getMandatoryProperties(),
|
|
120
|
+
...s
|
|
121
|
+
}, n = await fetch(i.baseUrl + S.error, {
|
|
116
122
|
method: "POST",
|
|
117
|
-
headers:
|
|
123
|
+
headers: i.requestHeaders,
|
|
118
124
|
body: JSON.stringify({
|
|
119
125
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
120
126
|
message: e,
|
|
121
|
-
properties:
|
|
127
|
+
properties: t
|
|
122
128
|
})
|
|
123
129
|
});
|
|
124
|
-
if (
|
|
125
|
-
return await
|
|
126
|
-
} catch (
|
|
127
|
-
console.warn("Failed to log error",
|
|
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
|
-
|
|
132
|
-
let
|
|
133
|
-
const
|
|
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
|
|
137
|
-
return window.waylflyerHeadlessSdk || (window.waylflyerHeadlessSdk = new
|
|
138
|
-
},
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
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
|
|
144
|
-
const
|
|
145
|
-
|
|
149
|
+
const l = () => {
|
|
150
|
+
const y = g(r, e);
|
|
151
|
+
i(y);
|
|
146
152
|
};
|
|
147
|
-
window.addEventListener(
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
|
160
|
-
return
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
-
|
|
175
|
-
|
|
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.
|
|
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",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
],
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@wf-financing/embedded-types": "1.1.1",
|
|
19
|
-
"@wf-financing/logger": "2.2.
|
|
19
|
+
"@wf-financing/logger": "2.2.2"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"vite": "^6.3.5",
|