@wf-financing/ui-sdk 3.2.0 → 3.4.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 CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var m=Object.defineProperty,h=(i,e,t)=>e in i?m(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t,l=(i,e,t)=>h(i,typeof e!="symbol"?e+"":e,t);const v="https://api.wayflyer.com",I="https://sandbox-api.wayflyer.com",b=i=>{try{return JSON.parse(atob(i.split(".")[1]))}catch{return null}},y={"Content-Type":"application/json"},u={event:"/financing/v1/company/event/",error:"/financing/v1/error/"},g="wf_session_v1",k=1800*1e3,U=1440*60*1e3;class E{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,s=t-e.createdAt>U;return n||s}mintNewSession(e){return{sessionId:crypto.randomUUID(),createdAt:e,lastActivity:e}}loadSession(){try{const e=sessionStorage.getItem(g);return e?JSON.parse(e):null}catch{return null}}saveSession(e){try{sessionStorage.setItem(g,JSON.stringify(e))}catch{console.warn("Failed to save session context")}}}const w=class a{constructor(e,t){l(this,"baseUrl",null),l(this,"sessionManager"),this.companyToken=e,this.baseUrl=t?I:v,this.sessionManager=new E}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,s]=(t==null?void 0:t.sub.split("|"))||[],o=t==null?void 0:t.user_id;return{partner_id:n,company_id:s,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(),s=n.sessionManager.getSessionId(),o={...y,Authorization:`Bearer ${n.companyToken}`,"X-Session-ID":s};try{const r={...a.getMandatoryProperties(n.companyToken),...t},c=await fetch(n.baseUrl+u.event,{method:"POST",headers:o,body:JSON.stringify({timestamp:new Date().toISOString(),event_name:e,properties:r})});if(c.status===200)return await c.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(),s=n.sessionManager.getSessionId(),o={...y,"X-Session-ID":s};try{const r={...a.getMandatoryProperties(n.companyToken),...t},c=await fetch(n.baseUrl+u.error,{method:"POST",headers:o,body:JSON.stringify({timestamp:new Date().toISOString(),message:e,properties:r})});if(c.status===200)return await c.json()}catch(r){console.warn("Failed to log error",r)}}};l(w,"isDevEnvironment",!1);let d=w;const p="wayflyer-ui-package",A="https://embedded-finance-frontend.vercel.app/npm/@wf-financing/ui@4",z="3.2.0",f=(i,e)=>{if(!window.WayflyerUiSdk)throw new Error("Failed to load WayflyerUiSdk from the script.");const t=window.WayflyerUiSdk;return new t(i,e)},S=(i,e,t)=>new Promise((n,s)=>{i.onload=()=>{try{n(f(e,t))}catch(o){s(o)}}}),D=async(i,e)=>{try{const t=document.getElementById(p);if(window.WayflyerUiSdk)return f(i,e);if(t)return S(t,i,e);const n=document.createElement("script");return n.src=A,n.type="module",n.id=p,n.async=!0,n.onerror=()=>d.logError("Failed to load ui SDK script"),document.head.appendChild(n),S(n,i,e)}catch{throw new Error("Failed to load script")}};class O{static async loadSdk(e,t){const n=Date.now();d.initialize(e,t);const s=await D(e,t),r=Date.now()-n;return d.logEvent("ui_sdk_initialized",{entry_point_version:z,sdk_version:(s==null?void 0:s.sdkVersion)??"unknown version",sdk_initialization_latency:r}),s}}exports.WayflyerUiSdk=O;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});var m=Object.defineProperty,h=(r,e,t)=>e in r?m(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,l=(r,e,t)=>h(r,typeof e!="symbol"?e+"":e,t);const v="https://api.wayflyer.com",I="https://sandbox-api.wayflyer.com",E=r=>{try{return JSON.parse(atob(r.split(".")[1]))}catch{return null}},b={"Content-Type":"application/json"},u={event:"/financing/v1/company/event/",error:"/financing/v1/error/"},y="wf_session_v1",k=1800*1e3,U=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 i=t-e.lastActivity>k,n=t-e.createdAt>U;return i||n}mintNewSession(e){return{sessionId:crypto.randomUUID(),createdAt:e,lastActivity:e}}loadSession(){try{const e=sessionStorage.getItem(y);return e?JSON.parse(e):null}catch{return null}}saveSession(e){try{sessionStorage.setItem(y,JSON.stringify(e))}catch{console.warn("Failed to save session context")}}}const p=class a{constructor(e,t,i){l(this,"baseUrl",null),l(this,"sessionManager"),l(this,"headers"),this.companyToken=e,this.baseUrl=i?I:v,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,i){a.getGlobalInstance()&&console.warn("Logger re-initialized");const n=new a(e,t,i==null?void 0:i.isSandbox);a.setGlobalInstance(n)}static getMandatoryProperties(e){const t=E(e),[i,n]=(t==null?void 0:t.sub.split("|"))||[],s=t==null?void 0:t.user_id;return{partner_id:i,company_id:n,user_id:s}}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 i=a.getInstance();try{const n={...a.getMandatoryProperties(i.companyToken),...t},s=await fetch(i.baseUrl+u.event,{method:"POST",headers:i.requestHeaders,body:JSON.stringify({timestamp:new Date().toISOString(),event_name:e,properties:n})});if(s.status===200)return await s.json()}catch(n){console.error("Failed to log event",n)}}static async logError(e,t){if(this.isDevEnvironment){console.error(e);return}const i=a.getInstance();try{const n={...a.getMandatoryProperties(i.companyToken),...t},s=await fetch(i.baseUrl+u.error,{method:"POST",headers:i.requestHeaders,body:JSON.stringify({timestamp:new Date().toISOString(),message:e,properties:n})});if(s.status===200)return await s.json()}catch(n){console.warn("Failed to log error",n)}}};l(p,"isDevEnvironment",!1);let o=p;const S=(r,e)=>{if(!window.WayflyerUiSdk)throw new Error("SDK code was fetched successfully, but WayflyerUiSdk is unavailable on window");const t=window.WayflyerUiSdk;return new t(r,e)},D="wayflyer-ui-package",A="https://embedded-finance-frontend.vercel.app/npm/@wf-financing/ui@4",g="3.4.0",w="UI_SDK_READY_EVENT",P=async(r,e)=>window.WayflyerUiSdk?S(r,e):new Promise((i,n)=>{const s=document.createElement("script");s.src=A,s.type="module",s.id=D,s.async=!0,s.onerror=()=>{n(new Error(`Failed to load UI SDK script from ${s.src}`))};const d=()=>{const f=S(r,e);i(f)};window.addEventListener(w,d,{once:!0}),setTimeout(()=>{n(new Error("UI SDK initialization timeout exceeded")),window.removeEventListener(w,d)},1e3),document.head.appendChild(s)}),z="ui";let c=null;class O{static loadSdk(e,t){return c||(c=this.getLoadSdkPromise(e,t),c)}static async getLoadSdkPromise(e,t){const i=Date.now();try{const n=await P(e,t),s=Date.now()-i;return o.initialize(e,n.sdkVersion,t),o.logEvent("ui_sdk_initialized",{entry_point_version:g,entry_point_type:z,sdk_initialization_latency:s}),n}catch(n){const s=n instanceof Error?n:new Error(String(n));throw o.initialize(e,void 0,t),o.logError(s.message,{entry_point_version:g}),n}}}exports.WayflyerUiSdk=O;
package/dist/index.es.js CHANGED
@@ -1,25 +1,25 @@
1
- var f = Object.defineProperty, h = (i, e, t) => e in i ? f(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t, l = (i, e, t) => h(i, typeof e != "symbol" ? e + "" : e, t);
2
- const v = "https://api.wayflyer.com", I = "https://sandbox-api.wayflyer.com", k = (i) => {
1
+ var h = Object.defineProperty, m = (r, e, t) => e in r ? h(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t, l = (r, e, t) => m(r, typeof e != "symbol" ? e + "" : e, t);
2
+ const v = "https://api.wayflyer.com", I = "https://sandbox-api.wayflyer.com", E = (r) => {
3
3
  try {
4
- return JSON.parse(atob(i.split(".")[1]));
4
+ return JSON.parse(atob(r.split(".")[1]));
5
5
  } catch {
6
6
  return null;
7
7
  }
8
- }, y = {
8
+ }, b = {
9
9
  "Content-Type": "application/json"
10
10
  }, u = {
11
11
  event: "/financing/v1/company/event/",
12
12
  error: "/financing/v1/error/"
13
- }, g = "wf_session_v1", b = 1800 * 1e3, E = 1440 * 60 * 1e3;
14
- class U {
13
+ }, y = "wf_session_v1", k = 1800 * 1e3, U = 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 n = t - e.lastActivity > b, s = t - e.createdAt > E;
22
- return n || s;
21
+ const s = t - e.lastActivity > k, n = t - e.createdAt > U;
22
+ return s || n;
23
23
  }
24
24
  mintNewSession(e) {
25
25
  return {
@@ -30,7 +30,7 @@ class U {
30
30
  }
31
31
  loadSession() {
32
32
  try {
33
- const e = sessionStorage.getItem(g);
33
+ const e = sessionStorage.getItem(y);
34
34
  return e ? JSON.parse(e) : null;
35
35
  } catch {
36
36
  return null;
@@ -38,15 +38,26 @@ class U {
38
38
  }
39
39
  saveSession(e) {
40
40
  try {
41
- sessionStorage.setItem(g, JSON.stringify(e));
41
+ sessionStorage.setItem(y, JSON.stringify(e));
42
42
  } catch {
43
43
  console.warn("Failed to save session context");
44
44
  }
45
45
  }
46
46
  }
47
- const S = class a {
48
- constructor(e, t) {
49
- l(this, "baseUrl", null), l(this, "sessionManager"), this.companyToken = e, this.baseUrl = t ? I : v, this.sessionManager = new U();
47
+ const p = class a {
48
+ constructor(e, t, s) {
49
+ l(this, "baseUrl", null), l(this, "sessionManager"), l(this, "headers"), this.companyToken = e, this.baseUrl = s ? I : v, this.sessionManager = new _(), this.headers = {
50
+ ...b,
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
+ };
50
61
  }
51
62
  static getGlobalInstance() {
52
63
  return window.WayflyerLogger || null;
@@ -54,14 +65,14 @@ const S = class a {
54
65
  static setGlobalInstance(e) {
55
66
  window.WayflyerLogger = e;
56
67
  }
57
- static initialize(e, t) {
68
+ static initialize(e, t, s) {
58
69
  a.getGlobalInstance() && console.warn("Logger re-initialized");
59
- const n = new a(e, t == null ? void 0 : t.isSandbox);
70
+ const n = new a(e, t, s == null ? void 0 : s.isSandbox);
60
71
  a.setGlobalInstance(n);
61
72
  }
62
73
  static getMandatoryProperties(e) {
63
- const t = k(e), [n, s] = (t == null ? void 0 : t.sub.split("|")) || [], o = t == null ? void 0 : t.user_id;
64
- return { partner_id: n, company_id: s, user_id: o };
74
+ const t = E(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 };
65
76
  }
66
77
  static getInstance() {
67
78
  const e = a.getGlobalInstance();
@@ -71,28 +82,24 @@ const S = class a {
71
82
  }
72
83
  static async logEvent(e, t) {
73
84
  if (this.isDevEnvironment) return;
74
- const n = a.getInstance(), s = n.sessionManager.getSessionId(), o = {
75
- ...y,
76
- Authorization: `Bearer ${n.companyToken}`,
77
- "X-Session-ID": s
78
- };
85
+ const s = a.getInstance();
79
86
  try {
80
- const r = {
81
- ...a.getMandatoryProperties(n.companyToken),
87
+ const n = {
88
+ ...a.getMandatoryProperties(s.companyToken),
82
89
  ...t
83
- }, c = await fetch(n.baseUrl + u.event, {
90
+ }, i = await fetch(s.baseUrl + u.event, {
84
91
  method: "POST",
85
- headers: o,
92
+ headers: s.requestHeaders,
86
93
  body: JSON.stringify({
87
94
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
88
95
  event_name: e,
89
- properties: r
96
+ properties: n
90
97
  })
91
98
  });
92
- if (c.status === 200)
93
- return await c.json();
94
- } catch (r) {
95
- console.error("Failed to log event", r);
99
+ if (i.status === 200)
100
+ return await i.json();
101
+ } catch (n) {
102
+ console.error("Failed to log event", n);
96
103
  }
97
104
  }
98
105
  static async logError(e, t) {
@@ -100,70 +107,69 @@ const S = class a {
100
107
  console.error(e);
101
108
  return;
102
109
  }
103
- const n = a.getInstance(), s = n.sessionManager.getSessionId(), o = {
104
- ...y,
105
- "X-Session-ID": s
106
- };
110
+ const s = a.getInstance();
107
111
  try {
108
- const r = {
109
- ...a.getMandatoryProperties(n.companyToken),
112
+ const n = {
113
+ ...a.getMandatoryProperties(s.companyToken),
110
114
  ...t
111
- }, c = await fetch(n.baseUrl + u.error, {
115
+ }, i = await fetch(s.baseUrl + u.error, {
112
116
  method: "POST",
113
- headers: o,
117
+ headers: s.requestHeaders,
114
118
  body: JSON.stringify({
115
119
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
116
120
  message: e,
117
- properties: r
121
+ properties: n
118
122
  })
119
123
  });
120
- if (c.status === 200)
121
- return await c.json();
122
- } catch (r) {
123
- console.warn("Failed to log error", r);
124
+ if (i.status === 200)
125
+ return await i.json();
126
+ } catch (n) {
127
+ console.warn("Failed to log error", n);
124
128
  }
125
129
  }
126
130
  };
127
- l(S, "isDevEnvironment", !1);
128
- let d = S;
129
- const p = "wayflyer-ui-package", A = "https://embedded-finance-frontend.vercel.app/npm/@wf-financing/ui@4", z = "3.2.0", m = (i, e) => {
131
+ l(p, "isDevEnvironment", !1);
132
+ let o = p;
133
+ const S = (r, e) => {
130
134
  if (!window.WayflyerUiSdk)
131
- throw new Error("Failed to load WayflyerUiSdk from the script.");
135
+ throw new Error("SDK code was fetched successfully, but WayflyerUiSdk is unavailable on window");
132
136
  const t = window.WayflyerUiSdk;
133
- return new t(i, e);
134
- }, w = (i, e, t) => new Promise((n, s) => {
135
- i.onload = () => {
137
+ return new t(r, e);
138
+ }, D = "wayflyer-ui-package", A = "https://embedded-finance-frontend.vercel.app/npm/@wf-financing/ui@4", w = "3.4.0", g = "UI_SDK_READY_EVENT", P = async (r, e) => window.WayflyerUiSdk ? S(r, e) : new Promise((s, n) => {
139
+ const i = document.createElement("script");
140
+ i.src = A, i.type = "module", i.id = D, i.async = !0, i.onerror = () => {
141
+ n(new Error(`Failed to load UI SDK script from ${i.src}`));
142
+ };
143
+ const d = () => {
144
+ const f = S(r, e);
145
+ s(f);
146
+ };
147
+ window.addEventListener(g, d, { once: !0 }), setTimeout(() => {
148
+ n(new Error("UI SDK initialization timeout exceeded")), window.removeEventListener(g, d);
149
+ }, 1e3), document.head.appendChild(i);
150
+ }), z = "ui";
151
+ let c = null;
152
+ class L {
153
+ static loadSdk(e, t) {
154
+ return c || (c = this.getLoadSdkPromise(e, t), c);
155
+ }
156
+ static async getLoadSdkPromise(e, t) {
157
+ const s = Date.now();
136
158
  try {
137
- n(m(e, t));
138
- } catch (o) {
139
- s(o);
159
+ const n = await P(e, t), i = Date.now() - s;
160
+ return o.initialize(e, n.sdkVersion, t), o.logEvent("ui_sdk_initialized", {
161
+ entry_point_version: w,
162
+ entry_point_type: z,
163
+ sdk_initialization_latency: i
164
+ }), n;
165
+ } catch (n) {
166
+ const i = n instanceof Error ? n : new Error(String(n));
167
+ throw o.initialize(e, void 0, t), o.logError(i.message, {
168
+ entry_point_version: w
169
+ }), n;
140
170
  }
141
- };
142
- }), D = async (i, e) => {
143
- try {
144
- const t = document.getElementById(p);
145
- if (window.WayflyerUiSdk)
146
- return m(i, e);
147
- if (t)
148
- return w(t, i, e);
149
- const n = document.createElement("script");
150
- return n.src = A, n.type = "module", n.id = p, n.async = !0, n.onerror = () => d.logError("Failed to load ui SDK script"), document.head.appendChild(n), w(n, i, e);
151
- } catch {
152
- throw new Error("Failed to load script");
153
- }
154
- };
155
- class O {
156
- static async loadSdk(e, t) {
157
- const n = Date.now();
158
- d.initialize(e, t);
159
- const s = await D(e, t), r = Date.now() - n;
160
- return d.logEvent("ui_sdk_initialized", {
161
- entry_point_version: z,
162
- sdk_version: (s == null ? void 0 : s.sdkVersion) ?? "unknown version",
163
- sdk_initialization_latency: r
164
- }), s;
165
171
  }
166
172
  }
167
173
  export {
168
- O as WayflyerUiSdk
174
+ L as WayflyerUiSdk
169
175
  };
@@ -1,5 +1,6 @@
1
- import { IWayflyerUiSdk, UiSdkOptions } from '@wf-financing/embedded-types';
1
+ import { UiSdkOptions, IWayflyerUiSdk } from '@wf-financing/embedded-types';
2
2
 
3
3
  export declare class WayflyerUiSdk {
4
4
  static loadSdk(companyToken: string, options?: UiSdkOptions): Promise<IWayflyerUiSdk>;
5
+ private static getLoadSdkPromise;
5
6
  }
@@ -1,2 +1,2 @@
1
1
  export { initializeUiSdk } from './initializeUiSdk';
2
- export { loadScriptAndInitializeSdk } from './loadScriptAndInitializeSdk';
2
+ export { loadUiSdkScript } from './loadUiSdkScript';
@@ -0,0 +1,5 @@
1
+ import { IWayflyerUiSdk, UiSdkOptions } from '@wf-financing/embedded-types';
2
+
3
+ type LoadUiSdkScript = (companyToken: string, options?: UiSdkOptions) => Promise<IWayflyerUiSdk>;
4
+ export declare const loadUiSdkScript: LoadUiSdkScript;
5
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wf-financing/ui-sdk",
3
- "version": "3.2.0",
3
+ "version": "3.4.0",
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.0",
19
- "@wf-financing/logger": "2.1.0"
19
+ "@wf-financing/logger": "2.2.0"
20
20
  },
21
21
  "devDependencies": {
22
22
  "vite": "^6.3.5",
@@ -31,7 +31,7 @@
31
31
  "build": "vite build",
32
32
  "link": "node ../../tools/scripts/link-local.mjs",
33
33
  "unlink": "node ../../tools/scripts/unlink-local.mjs",
34
- "prepare-sdk-cta": "pnpm clean && pnpm build && pnpm pack",
34
+ "prepare-sdk-cta": "pnpm clean && NODE_ENV=development pnpm build --mode development && pnpm pack",
35
35
  "publish-sdk-cta": "pnpm clean && pnpm build && pnpm publish --access public --no-git-checks",
36
36
  "test": "vitest",
37
37
  "test:coverage": "vitest --coverage"
@@ -1 +0,0 @@
1
- export { loadUiSdkMode } from './loadSdkUiMode';
@@ -1,5 +0,0 @@
1
- import { IWayflyerUiSdk, UiSdkOptions } from '@wf-financing/embedded-types';
2
-
3
- type LoadUiSdkModeType = (companyToken: string, options?: UiSdkOptions) => Promise<IWayflyerUiSdk | void>;
4
- export declare const loadUiSdkMode: LoadUiSdkModeType;
5
- export {};
@@ -1,3 +0,0 @@
1
- import { IWayflyerUiSdk, UiSdkOptions } from '@wf-financing/embedded-types';
2
-
3
- export declare const loadScriptAndInitializeSdk: (script: HTMLScriptElement, companyToken: string, options?: UiSdkOptions) => Promise<IWayflyerUiSdk>;