@wf-financing/headless-sdk 4.0.0 → 4.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/dist/config/applicationVersion.d.ts +1 -0
- package/dist/config/index.d.ts +1 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.es.js +155 -23
- package/package.json +3 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const APPLICATION_VERSION: string;
|
package/dist/config/index.d.ts
CHANGED
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 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;
|
package/dist/index.es.js
CHANGED
|
@@ -1,36 +1,168 @@
|
|
|
1
|
-
var
|
|
2
|
-
const
|
|
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) => {
|
|
3
|
+
try {
|
|
4
|
+
return JSON.parse(atob(s.split(".")[1]));
|
|
5
|
+
} catch {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
}, d = {
|
|
9
|
+
"Content-Type": "application/json"
|
|
10
|
+
}, y = {
|
|
11
|
+
event: "/financing/v1/company/event/",
|
|
12
|
+
error: "/financing/v1/error/"
|
|
13
|
+
}, S = "wf_session_v1", k = 1800 * 1e3, A = 1440 * 60 * 1e3;
|
|
14
|
+
class D {
|
|
15
|
+
getSessionId() {
|
|
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;
|
|
19
|
+
}
|
|
20
|
+
isSessionExpired(e, t) {
|
|
21
|
+
const n = t - e.lastActivity > k, i = t - e.createdAt > A;
|
|
22
|
+
return n || i;
|
|
23
|
+
}
|
|
24
|
+
mintNewSession(e) {
|
|
25
|
+
return {
|
|
26
|
+
sessionId: crypto.randomUUID(),
|
|
27
|
+
createdAt: e,
|
|
28
|
+
lastActivity: e
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
loadSession() {
|
|
32
|
+
try {
|
|
33
|
+
const e = sessionStorage.getItem(S);
|
|
34
|
+
return e ? JSON.parse(e) : null;
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
saveSession(e) {
|
|
40
|
+
try {
|
|
41
|
+
sessionStorage.setItem(S, JSON.stringify(e));
|
|
42
|
+
} catch {
|
|
43
|
+
console.warn("Failed to save session context");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
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();
|
|
50
|
+
}
|
|
51
|
+
static getGlobalInstance() {
|
|
52
|
+
return window.WayflyerLogger || null;
|
|
53
|
+
}
|
|
54
|
+
static setGlobalInstance(e) {
|
|
55
|
+
window.WayflyerLogger = e;
|
|
56
|
+
}
|
|
57
|
+
static initialize(e, t) {
|
|
58
|
+
a.getGlobalInstance() && console.warn("Logger re-initialized");
|
|
59
|
+
const n = new a(e, t == null ? void 0 : t.isSandbox);
|
|
60
|
+
a.setGlobalInstance(n);
|
|
61
|
+
}
|
|
62
|
+
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 };
|
|
65
|
+
}
|
|
66
|
+
static getInstance() {
|
|
67
|
+
const e = a.getGlobalInstance();
|
|
68
|
+
if (!e)
|
|
69
|
+
throw new Error("Logger not initialized. Call initialize() first.");
|
|
70
|
+
return e;
|
|
71
|
+
}
|
|
72
|
+
static async logEvent(e, t) {
|
|
73
|
+
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
|
+
};
|
|
79
|
+
try {
|
|
80
|
+
const r = {
|
|
81
|
+
...a.getMandatoryProperties(n.companyToken),
|
|
82
|
+
...t
|
|
83
|
+
}, l = await fetch(n.baseUrl + y.event, {
|
|
84
|
+
method: "POST",
|
|
85
|
+
headers: o,
|
|
86
|
+
body: JSON.stringify({
|
|
87
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
88
|
+
event_name: e,
|
|
89
|
+
properties: r
|
|
90
|
+
})
|
|
91
|
+
});
|
|
92
|
+
if (l.status === 200)
|
|
93
|
+
return await l.json();
|
|
94
|
+
} catch (r) {
|
|
95
|
+
console.error("Failed to log event", r);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
static async logError(e, t) {
|
|
99
|
+
if (this.isDevEnvironment) {
|
|
100
|
+
console.error(e);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const n = a.getInstance(), i = n.sessionManager.getSessionId(), o = {
|
|
104
|
+
...d,
|
|
105
|
+
"X-Session-ID": i
|
|
106
|
+
};
|
|
107
|
+
try {
|
|
108
|
+
const r = {
|
|
109
|
+
...a.getMandatoryProperties(n.companyToken),
|
|
110
|
+
...t
|
|
111
|
+
}, l = await fetch(n.baseUrl + y.error, {
|
|
112
|
+
method: "POST",
|
|
113
|
+
headers: o,
|
|
114
|
+
body: JSON.stringify({
|
|
115
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
116
|
+
message: e,
|
|
117
|
+
properties: r
|
|
118
|
+
})
|
|
119
|
+
});
|
|
120
|
+
if (l.status === 200)
|
|
121
|
+
return await l.json();
|
|
122
|
+
} catch (r) {
|
|
123
|
+
console.warn("Failed to log error", r);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
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) => {
|
|
3
130
|
if (!window.WayflyerHeadlessSdk)
|
|
4
131
|
throw new Error("Failed to load WayflyerHeadlessSdk from the script.");
|
|
5
|
-
const
|
|
6
|
-
return window.waylflyerHeadlessSdk || (window.waylflyerHeadlessSdk = new
|
|
7
|
-
},
|
|
8
|
-
|
|
132
|
+
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 = () => {
|
|
9
136
|
try {
|
|
10
|
-
|
|
11
|
-
} catch (
|
|
12
|
-
|
|
137
|
+
n(f(e, t));
|
|
138
|
+
} catch (o) {
|
|
139
|
+
i(o);
|
|
13
140
|
}
|
|
14
141
|
};
|
|
15
|
-
}),
|
|
142
|
+
}), N = async (s, e) => {
|
|
16
143
|
try {
|
|
17
|
-
const
|
|
144
|
+
const t = document.getElementById(p);
|
|
18
145
|
if (window.WayflyerHeadlessSdk)
|
|
19
|
-
return
|
|
20
|
-
if (
|
|
21
|
-
return t
|
|
22
|
-
const
|
|
23
|
-
return
|
|
24
|
-
} catch (
|
|
25
|
-
throw console.error("Error in loading headless SDK:",
|
|
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");
|
|
26
153
|
}
|
|
27
154
|
};
|
|
28
|
-
class
|
|
29
|
-
static async loadSdk(
|
|
30
|
-
|
|
155
|
+
class H {
|
|
156
|
+
static async loadSdk(e, t) {
|
|
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;
|
|
31
163
|
}
|
|
32
164
|
}
|
|
33
165
|
export {
|
|
34
|
-
|
|
35
|
-
|
|
166
|
+
m as CtaStateType,
|
|
167
|
+
H as WayflyerHeadlessSdk
|
|
36
168
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wf-financing/headless-sdk",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.0",
|
|
4
4
|
"main": "dist/index.cjs.js",
|
|
5
5
|
"module": "dist/index.es.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -15,7 +15,8 @@
|
|
|
15
15
|
"dist"
|
|
16
16
|
],
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@wf-financing/embedded-types": "1.
|
|
18
|
+
"@wf-financing/embedded-types": "1.1.0",
|
|
19
|
+
"@wf-financing/logger": "2.1.0"
|
|
19
20
|
},
|
|
20
21
|
"devDependencies": {
|
|
21
22
|
"vite": "^6.3.5",
|