@wf-financing/headless-sdk 4.0.0 → 4.2.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 +164 -28
- package/dist/utils/index.d.ts +1 -1
- package/dist/{sdk-mode/loadHeadlessMode.d.ts → utils/loadHeadlessSdkScript.d.ts} +2 -2
- package/package.json +3 -2
- package/dist/sdk-mode/index.d.ts +0 -1
- package/dist/utils/loadScriptAndInitializeSdk.d.ts +0 -3
|
@@ -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=(r=>(r.GENERIC_OFFER="generic_offer",r.INDICATIVE_OFFER="indicative_offer",r.CONTINUE_APPLICATION="continue_application",r))(p||{}),v=Object.defineProperty,I=(r,e,t)=>e in r?v(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,c=(r,e,t)=>I(r,typeof e!="symbol"?e+"":e,t);const E="https://api.wayflyer.com",b="https://sandbox-api.wayflyer.com",k=r=>{try{return JSON.parse(atob(r.split(".")[1]))}catch{return null}},D={"Content-Type":"application/json"},y={event:"/financing/v1/company/event/",error:"/financing/v1/error/"},u="wf_session_v1",A=1800*1e3,_=1440*60*1e3;class O{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 s=t-e.lastActivity>A,n=t-e.createdAt>_;return s||n}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 f=class a{constructor(e,t,s){c(this,"baseUrl",null),c(this,"sessionManager"),c(this,"headers"),this.companyToken=e,this.baseUrl=s?b:E,this.sessionManager=new O,this.headers={...D,...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,s){a.getGlobalInstance()&&console.warn("Logger re-initialized");const n=new a(e,t,s==null?void 0:s.isSandbox);a.setGlobalInstance(n)}static getMandatoryProperties(e){const t=k(e),[s,n]=(t==null?void 0:t.sub.split("|"))||[],i=t==null?void 0:t.user_id;return{partner_id:s,company_id:n,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 s=a.getInstance();try{const n={...a.getMandatoryProperties(s.companyToken),...t},i=await fetch(s.baseUrl+y.event,{method:"POST",headers:s.requestHeaders,body:JSON.stringify({timestamp:new Date().toISOString(),event_name:e,properties:n})});if(i.status===200)return await i.json()}catch(n){console.error("Failed to log event",n)}}static async logError(e,t){if(this.isDevEnvironment){console.error(e);return}const s=a.getInstance();try{const n={...a.getMandatoryProperties(s.companyToken),...t},i=await fetch(s.baseUrl+y.error,{method:"POST",headers:s.requestHeaders,body:JSON.stringify({timestamp:new Date().toISOString(),message:e,properties:n})});if(i.status===200)return await i.json()}catch(n){console.warn("Failed to log error",n)}}};c(f,"isDevEnvironment",!1);let d=f;const S="wayflyer-headless-sdk",H="https://embedded-finance-frontend.vercel.app/npm/@wf-financing/headless@3",w="4.2.0",g=(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},z=async(r,e)=>document.getElementById(S)?g(r,e):new Promise((s,n)=>{const i=document.createElement("script");i.src=H,i.type="module",i.id=S,i.async=!0,i.onload=()=>{try{const o=g(r,e);s(o)}catch(o){n(o)}},i.onerror=()=>{n(new Error(`Failed to load headless SDK script from ${i.src}`))},document.head.appendChild(i)}),N="headless";class P{static async loadSdk(e,t){const s=Date.now();let n;try{n=await z(e,t)}catch(l){const m=l instanceof Error?l:new Error(String(l));throw d.initialize(e,void 0,t),d.logError(m.message,{entry_point_version:w}),l}const o=Date.now()-s,h=n.sdkVersion;return d.initialize(e,h,t),d.logEvent("headless_sdk_initialized",{entry_point_version:w,entry_point_type:N,sdk_initialization_latency:o}),n}}exports.CtaStateType=p;exports.WayflyerHeadlessSdk=P;
|
package/dist/index.es.js
CHANGED
|
@@ -1,36 +1,172 @@
|
|
|
1
|
-
var
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
1
|
+
var m = /* @__PURE__ */ ((r) => (r.GENERIC_OFFER = "generic_offer", r.INDICATIVE_OFFER = "indicative_offer", r.CONTINUE_APPLICATION = "continue_application", r))(m || {}), v = Object.defineProperty, I = (r, e, t) => e in r ? v(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, d = (r, e, t) => I(r, typeof e != "symbol" ? e + "" : e, t);
|
|
2
|
+
const E = "https://api.wayflyer.com", b = "https://sandbox-api.wayflyer.com", k = (r) => {
|
|
3
|
+
try {
|
|
4
|
+
return JSON.parse(atob(r.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
|
+
}, u = "wf_session_v1", A = 1800 * 1e3, _ = 1440 * 60 * 1e3;
|
|
14
|
+
class O {
|
|
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 s = t - e.lastActivity > A, n = t - e.createdAt > _;
|
|
22
|
+
return s || n;
|
|
23
|
+
}
|
|
24
|
+
mintNewSession(e) {
|
|
25
|
+
return {
|
|
26
|
+
sessionId: crypto.randomUUID(),
|
|
27
|
+
createdAt: e,
|
|
28
|
+
lastActivity: e
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
loadSession() {
|
|
9
32
|
try {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
33
|
+
const e = sessionStorage.getItem(u);
|
|
34
|
+
return e ? JSON.parse(e) : null;
|
|
35
|
+
} catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
saveSession(e) {
|
|
40
|
+
try {
|
|
41
|
+
sessionStorage.setItem(u, 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, s) {
|
|
49
|
+
d(this, "baseUrl", null), d(this, "sessionManager"), d(this, "headers"), this.companyToken = e, this.baseUrl = s ? b : E, this.sessionManager = new O(), 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
|
+
};
|
|
61
|
+
}
|
|
62
|
+
static getGlobalInstance() {
|
|
63
|
+
return window.WayflyerLogger || null;
|
|
64
|
+
}
|
|
65
|
+
static setGlobalInstance(e) {
|
|
66
|
+
window.WayflyerLogger = e;
|
|
67
|
+
}
|
|
68
|
+
static initialize(e, t, s) {
|
|
69
|
+
a.getGlobalInstance() && console.warn("Logger re-initialized");
|
|
70
|
+
const n = new a(e, t, s == null ? void 0 : s.isSandbox);
|
|
71
|
+
a.setGlobalInstance(n);
|
|
72
|
+
}
|
|
73
|
+
static getMandatoryProperties(e) {
|
|
74
|
+
const t = k(e), [s, n] = (t == null ? void 0 : t.sub.split("|")) || [], i = t == null ? void 0 : t.user_id;
|
|
75
|
+
return { partner_id: s, company_id: n, user_id: i };
|
|
76
|
+
}
|
|
77
|
+
static getInstance() {
|
|
78
|
+
const e = a.getGlobalInstance();
|
|
79
|
+
if (!e)
|
|
80
|
+
throw new Error("Logger not initialized. Call initialize() first.");
|
|
81
|
+
return e;
|
|
82
|
+
}
|
|
83
|
+
static async logEvent(e, t) {
|
|
84
|
+
if (this.isDevEnvironment) return;
|
|
85
|
+
const s = a.getInstance();
|
|
86
|
+
try {
|
|
87
|
+
const n = {
|
|
88
|
+
...a.getMandatoryProperties(s.companyToken),
|
|
89
|
+
...t
|
|
90
|
+
}, i = await fetch(s.baseUrl + y.event, {
|
|
91
|
+
method: "POST",
|
|
92
|
+
headers: s.requestHeaders,
|
|
93
|
+
body: JSON.stringify({
|
|
94
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
95
|
+
event_name: e,
|
|
96
|
+
properties: n
|
|
97
|
+
})
|
|
98
|
+
});
|
|
99
|
+
if (i.status === 200)
|
|
100
|
+
return await i.json();
|
|
101
|
+
} catch (n) {
|
|
102
|
+
console.error("Failed to log event", n);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
static async logError(e, t) {
|
|
106
|
+
if (this.isDevEnvironment) {
|
|
107
|
+
console.error(e);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const s = a.getInstance();
|
|
111
|
+
try {
|
|
112
|
+
const n = {
|
|
113
|
+
...a.getMandatoryProperties(s.companyToken),
|
|
114
|
+
...t
|
|
115
|
+
}, i = await fetch(s.baseUrl + y.error, {
|
|
116
|
+
method: "POST",
|
|
117
|
+
headers: s.requestHeaders,
|
|
118
|
+
body: JSON.stringify({
|
|
119
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
120
|
+
message: e,
|
|
121
|
+
properties: n
|
|
122
|
+
})
|
|
123
|
+
});
|
|
124
|
+
if (i.status === 200)
|
|
125
|
+
return await i.json();
|
|
126
|
+
} catch (n) {
|
|
127
|
+
console.warn("Failed to log error", n);
|
|
13
128
|
}
|
|
14
|
-
};
|
|
15
|
-
}), y = async (e, d) => {
|
|
16
|
-
try {
|
|
17
|
-
const r = document.getElementById(s);
|
|
18
|
-
if (window.WayflyerHeadlessSdk)
|
|
19
|
-
return l(e, d);
|
|
20
|
-
if (r)
|
|
21
|
-
return t(r, e, d);
|
|
22
|
-
const a = document.createElement("script");
|
|
23
|
-
return a.src = c, a.type = "module", a.id = s, a.async = !0, document.head.appendChild(a), t(a, e, d);
|
|
24
|
-
} catch (r) {
|
|
25
|
-
throw console.error("Error in loading headless SDK:", r), new Error("Failed to load script");
|
|
26
129
|
}
|
|
27
130
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
131
|
+
d(g, "isDevEnvironment", !1);
|
|
132
|
+
let c = g;
|
|
133
|
+
const S = "wayflyer-headless-sdk", H = "https://embedded-finance-frontend.vercel.app/npm/@wf-financing/headless@3", w = "4.2.0", p = (r, e) => {
|
|
134
|
+
if (!window.WayflyerHeadlessSdk)
|
|
135
|
+
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
|
+
}, z = async (r, e) => document.getElementById(S) ? p(r, e) : new Promise((s, n) => {
|
|
139
|
+
const i = document.createElement("script");
|
|
140
|
+
i.src = H, i.type = "module", i.id = S, i.async = !0, i.onload = () => {
|
|
141
|
+
try {
|
|
142
|
+
const o = p(r, e);
|
|
143
|
+
s(o);
|
|
144
|
+
} catch (o) {
|
|
145
|
+
n(o);
|
|
146
|
+
}
|
|
147
|
+
}, i.onerror = () => {
|
|
148
|
+
n(new Error(`Failed to load headless SDK script from ${i.src}`));
|
|
149
|
+
}, document.head.appendChild(i);
|
|
150
|
+
}), N = "headless";
|
|
151
|
+
class L {
|
|
152
|
+
static async loadSdk(e, t) {
|
|
153
|
+
const s = Date.now();
|
|
154
|
+
let n;
|
|
155
|
+
try {
|
|
156
|
+
n = await z(e, t);
|
|
157
|
+
} catch (l) {
|
|
158
|
+
const h = l instanceof Error ? l : new Error(String(l));
|
|
159
|
+
throw c.initialize(e, void 0, t), c.logError(h.message, { entry_point_version: w }), l;
|
|
160
|
+
}
|
|
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;
|
|
31
167
|
}
|
|
32
168
|
}
|
|
33
169
|
export {
|
|
34
|
-
|
|
35
|
-
|
|
170
|
+
m as CtaStateType,
|
|
171
|
+
L as WayflyerHeadlessSdk
|
|
36
172
|
};
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { initializeHeadlessSdk } from './initializeHeadlessSdk';
|
|
2
|
-
export {
|
|
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
|
|
4
|
-
export declare const
|
|
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.
|
|
3
|
+
"version": "4.2.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.2.0"
|
|
19
20
|
},
|
|
20
21
|
"devDependencies": {
|
|
21
22
|
"vite": "^6.3.5",
|
package/dist/sdk-mode/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { loadHeadlessSdkMode } from './loadHeadlessMode';
|