@wf-financing/logger 2.1.0 → 2.2.1

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";var I=Object.defineProperty;var u=(o,t,e)=>t in o?I(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e;var l=(o,t,e)=>u(o,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p="https://api.wayflyer.com",h="https://sandbox-api.wayflyer.com",w=o=>{try{return JSON.parse(atob(o.split(".")[1]))}catch{return null}},d={"Content-Type":"application/json"},S={event:"/financing/v1/company/event/",error:"/financing/v1/error/"},y="wf_session_v1",f=1800*1e3,m=1440*60*1e3;class v{getSessionId(){const t=Date.now();let e=this.loadSession();return(!e||this.isSessionExpired(e,t))&&(e=this.mintNewSession(t)),e.lastActivity=t,this.saveSession(e),e.sessionId}isSessionExpired(t,e){const s=e-t.lastActivity>f,i=e-t.createdAt>m;return s||i}mintNewSession(t){return{sessionId:crypto.randomUUID(),createdAt:t,lastActivity:t}}loadSession(){try{const t=sessionStorage.getItem(y);return t?JSON.parse(t):null}catch{return null}}saveSession(t){try{sessionStorage.setItem(y,JSON.stringify(t))}catch{console.warn("Failed to save session context")}}}const n=class n{constructor(t,e){l(this,"baseUrl",null);l(this,"sessionManager");this.companyToken=t,this.baseUrl=e?h:p,this.sessionManager=new v}static getGlobalInstance(){return window.WayflyerLogger||null}static setGlobalInstance(t){window.WayflyerLogger=t}static initialize(t,e){n.getGlobalInstance()&&console.warn("Logger re-initialized");const s=new n(t,e==null?void 0:e.isSandbox);n.setGlobalInstance(s)}static getMandatoryProperties(t){const e=w(t),[s,i]=(e==null?void 0:e.sub.split("|"))||[],a=e==null?void 0:e.user_id;return{partner_id:s,company_id:i,user_id:a}}static getInstance(){const t=n.getGlobalInstance();if(!t)throw new Error("Logger not initialized. Call initialize() first.");return t}static async logEvent(t,e){if(this.isDevEnvironment)return;const s=n.getInstance(),i=s.sessionManager.getSessionId(),a={...d,Authorization:`Bearer ${s.companyToken}`,"X-Session-ID":i};try{const r={...n.getMandatoryProperties(s.companyToken),...e},c=await fetch(s.baseUrl+S.event,{method:"POST",headers:a,body:JSON.stringify({timestamp:new Date().toISOString(),event_name:t,properties:r})});if(c.status===200)return await c.json()}catch(r){console.error("Failed to log event",r)}}static async logError(t,e){if(this.isDevEnvironment){console.error(t);return}const s=n.getInstance(),i=s.sessionManager.getSessionId(),a={...d,"X-Session-ID":i};try{const r={...n.getMandatoryProperties(s.companyToken),...e},c=await fetch(s.baseUrl+S.error,{method:"POST",headers:a,body:JSON.stringify({timestamp:new Date().toISOString(),message:t,properties:r})});if(c.status===200)return await c.json()}catch(r){console.warn("Failed to log error",r)}}};l(n,"isDevEnvironment",!1);let g=n;exports.Logger=g;
1
+ "use strict";var g=Object.defineProperty;var u=(o,t,e)=>t in o?g(o,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):o[t]=e;var i=(o,t,e)=>u(o,typeof t!="symbol"?t+"":t,e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y="https://api.wayflyer.com",S="https://sandbox-api.wayflyer.com",h=o=>{try{return JSON.parse(atob(o.split(".")[1]))}catch{return null}},I={"Content-Type":"application/json"},l={event:"/financing/v1/company/event/",error:"/financing/v1/error/"},d="wf_session_v1",p=1800*1e3,w=1440*60*1e3;class f{getSessionId(){const t=Date.now();let e=this.loadSession();return(!e||this.isSessionExpired(e,t))&&(e=this.mintNewSession(t)),e.lastActivity=t,this.saveSession(e),e.sessionId}isSessionExpired(t,e){const s=e-t.lastActivity>p,n=e-t.createdAt>w;return s||n}mintNewSession(t){return{sessionId:crypto.randomUUID(),createdAt:t,lastActivity:t}}loadSession(){try{const t=sessionStorage.getItem(d);return t?JSON.parse(t):null}catch{return null}}saveSession(t){try{sessionStorage.setItem(d,JSON.stringify(t))}catch{console.warn("Failed to save session context")}}}const r=class r{constructor(t,e,s){i(this,"baseUrl",null);i(this,"sessionManager");i(this,"headers");this.companyToken=t,this.baseUrl=s?S:y,this.sessionManager=new f,this.headers={...I,...e?{"X-SDK-Version":e}:{}}}get requestHeaders(){const t=r.getInstance(),e=t.sessionManager.getSessionId();return{Authorization:`Bearer ${t.companyToken}`,"X-Session-ID":e,...t.headers}}static getGlobalInstance(){return window.WayflyerLogger||null}static setGlobalInstance(t){window.WayflyerLogger=t}static initialize(t,e,s){r.getGlobalInstance()&&console.warn("Logger re-initialized");const n=new r(t,e,s==null?void 0:s.isSandbox);r.setGlobalInstance(n)}static getMandatoryProperties(t){const e=h(t),[s,n]=(e==null?void 0:e.sub.split("|"))||[],a=e==null?void 0:e.user_id;return{partner_id:s,company_id:n,user_id:a}}static getInstance(){const t=r.getGlobalInstance();if(!t)throw new Error("Logger not initialized. Call initialize() first.");return t}static async logEvent(t,e){if(this.isDevEnvironment)return;const s=r.getInstance();try{const n={...r.getMandatoryProperties(s.companyToken),...e},a=await fetch(s.baseUrl+l.event,{method:"POST",headers:s.requestHeaders,body:JSON.stringify({timestamp:new Date().toISOString(),event_name:t,properties:n})});if(a.status===200)return await a.json()}catch(n){console.error("Failed to log event",n)}}static async logError(t,e){if(this.isDevEnvironment){console.error(t);return}const s=r.getInstance();try{const n={...r.getMandatoryProperties(s.companyToken),...e},a=await fetch(s.baseUrl+l.error,{method:"POST",headers:s.requestHeaders,body:JSON.stringify({timestamp:new Date().toISOString(),message:t,properties:n})});if(a.status===200)return await a.json()}catch(n){console.warn("Failed to log error",n)}}};i(r,"isDevEnvironment",!1);let c=r;exports.Logger=c;
package/dist/index.es.js CHANGED
@@ -1,27 +1,27 @@
1
- var I = Object.defineProperty;
2
- var p = (o, t, e) => t in o ? I(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
3
- var l = (o, t, e) => p(o, typeof t != "symbol" ? t + "" : t, e);
4
- const u = "https://api.wayflyer.com", h = "https://sandbox-api.wayflyer.com", w = (o) => {
1
+ var g = Object.defineProperty;
2
+ var y = (o, t, e) => t in o ? g(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
3
+ var i = (o, t, e) => y(o, typeof t != "symbol" ? t + "" : t, e);
4
+ const S = "https://api.wayflyer.com", u = "https://sandbox-api.wayflyer.com", h = (o) => {
5
5
  try {
6
6
  return JSON.parse(atob(o.split(".")[1]));
7
7
  } catch {
8
8
  return null;
9
9
  }
10
- }, d = {
10
+ }, I = {
11
11
  "Content-Type": "application/json"
12
- }, g = {
12
+ }, c = {
13
13
  event: "/financing/v1/company/event/",
14
14
  error: "/financing/v1/error/"
15
- }, S = "wf_session_v1", f = 1800 * 1e3, m = 1440 * 60 * 1e3;
16
- class v {
15
+ }, l = "wf_session_v1", p = 1800 * 1e3, w = 1440 * 60 * 1e3;
16
+ class f {
17
17
  getSessionId() {
18
18
  const t = Date.now();
19
19
  let e = this.loadSession();
20
20
  return (!e || this.isSessionExpired(e, t)) && (e = this.mintNewSession(t)), e.lastActivity = t, this.saveSession(e), e.sessionId;
21
21
  }
22
22
  isSessionExpired(t, e) {
23
- const s = e - t.lastActivity > f, i = e - t.createdAt > m;
24
- return s || i;
23
+ const s = e - t.lastActivity > p, n = e - t.createdAt > w;
24
+ return s || n;
25
25
  }
26
26
  mintNewSession(t) {
27
27
  return {
@@ -32,7 +32,7 @@ class v {
32
32
  }
33
33
  loadSession() {
34
34
  try {
35
- const t = sessionStorage.getItem(S);
35
+ const t = sessionStorage.getItem(l);
36
36
  return t ? JSON.parse(t) : null;
37
37
  } catch {
38
38
  return null;
@@ -40,17 +40,29 @@ class v {
40
40
  }
41
41
  saveSession(t) {
42
42
  try {
43
- sessionStorage.setItem(S, JSON.stringify(t));
43
+ sessionStorage.setItem(l, JSON.stringify(t));
44
44
  } catch {
45
45
  console.warn("Failed to save session context");
46
46
  }
47
47
  }
48
48
  }
49
- const n = class n {
50
- constructor(t, e) {
51
- l(this, "baseUrl", null);
52
- l(this, "sessionManager");
53
- this.companyToken = t, this.baseUrl = e ? h : u, this.sessionManager = new v();
49
+ const r = class r {
50
+ constructor(t, e, s) {
51
+ i(this, "baseUrl", null);
52
+ i(this, "sessionManager");
53
+ i(this, "headers");
54
+ this.companyToken = t, this.baseUrl = s ? u : S, this.sessionManager = new f(), this.headers = {
55
+ ...I,
56
+ ...e ? { "X-SDK-Version": e } : {}
57
+ };
58
+ }
59
+ get requestHeaders() {
60
+ const t = r.getInstance(), e = t.sessionManager.getSessionId();
61
+ return {
62
+ Authorization: `Bearer ${t.companyToken}`,
63
+ "X-Session-ID": e,
64
+ ...t.headers
65
+ };
54
66
  }
55
67
  static getGlobalInstance() {
56
68
  return window.WayflyerLogger || null;
@@ -58,45 +70,41 @@ const n = class n {
58
70
  static setGlobalInstance(t) {
59
71
  window.WayflyerLogger = t;
60
72
  }
61
- static initialize(t, e) {
62
- n.getGlobalInstance() && console.warn("Logger re-initialized");
63
- const s = new n(t, e == null ? void 0 : e.isSandbox);
64
- n.setGlobalInstance(s);
73
+ static initialize(t, e, s) {
74
+ r.getGlobalInstance() && console.warn("Logger re-initialized");
75
+ const n = new r(t, e, s == null ? void 0 : s.isSandbox);
76
+ r.setGlobalInstance(n);
65
77
  }
66
78
  static getMandatoryProperties(t) {
67
- const e = w(t), [s, i] = (e == null ? void 0 : e.sub.split("|")) || [], a = e == null ? void 0 : e.user_id;
68
- return { partner_id: s, company_id: i, user_id: a };
79
+ const e = h(t), [s, n] = (e == null ? void 0 : e.sub.split("|")) || [], a = e == null ? void 0 : e.user_id;
80
+ return { partner_id: s, company_id: n, user_id: a };
69
81
  }
70
82
  static getInstance() {
71
- const t = n.getGlobalInstance();
83
+ const t = r.getGlobalInstance();
72
84
  if (!t)
73
85
  throw new Error("Logger not initialized. Call initialize() first.");
74
86
  return t;
75
87
  }
76
88
  static async logEvent(t, e) {
77
89
  if (this.isDevEnvironment) return;
78
- const s = n.getInstance(), i = s.sessionManager.getSessionId(), a = {
79
- ...d,
80
- Authorization: `Bearer ${s.companyToken}`,
81
- "X-Session-ID": i
82
- };
90
+ const s = r.getInstance();
83
91
  try {
84
- const r = {
85
- ...n.getMandatoryProperties(s.companyToken),
92
+ const n = {
93
+ ...r.getMandatoryProperties(s.companyToken),
86
94
  ...e
87
- }, c = await fetch(s.baseUrl + g.event, {
95
+ }, a = await fetch(s.baseUrl + c.event, {
88
96
  method: "POST",
89
- headers: a,
97
+ headers: s.requestHeaders,
90
98
  body: JSON.stringify({
91
99
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
92
100
  event_name: t,
93
- properties: r
101
+ properties: n
94
102
  })
95
103
  });
96
- if (c.status === 200)
97
- return await c.json();
98
- } catch (r) {
99
- console.error("Failed to log event", r);
104
+ if (a.status === 200)
105
+ return await a.json();
106
+ } catch (n) {
107
+ console.error("Failed to log event", n);
100
108
  }
101
109
  }
102
110
  static async logError(t, e) {
@@ -104,32 +112,29 @@ const n = class n {
104
112
  console.error(t);
105
113
  return;
106
114
  }
107
- const s = n.getInstance(), i = s.sessionManager.getSessionId(), a = {
108
- ...d,
109
- "X-Session-ID": i
110
- };
115
+ const s = r.getInstance();
111
116
  try {
112
- const r = {
113
- ...n.getMandatoryProperties(s.companyToken),
117
+ const n = {
118
+ ...r.getMandatoryProperties(s.companyToken),
114
119
  ...e
115
- }, c = await fetch(s.baseUrl + g.error, {
120
+ }, a = await fetch(s.baseUrl + c.error, {
116
121
  method: "POST",
117
- headers: a,
122
+ headers: s.requestHeaders,
118
123
  body: JSON.stringify({
119
124
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
120
125
  message: t,
121
- properties: r
126
+ properties: n
122
127
  })
123
128
  });
124
- if (c.status === 200)
125
- return await c.json();
126
- } catch (r) {
127
- console.warn("Failed to log error", r);
129
+ if (a.status === 200)
130
+ return await a.json();
131
+ } catch (n) {
132
+ console.warn("Failed to log error", n);
128
133
  }
129
134
  }
130
135
  };
131
- l(n, "isDevEnvironment", !1);
132
- let y = n;
136
+ i(r, "isDevEnvironment", !1);
137
+ let d = r;
133
138
  export {
134
- y as Logger
139
+ d as Logger
135
140
  };
@@ -8,10 +8,12 @@ export declare class Logger {
8
8
  private static readonly isDevEnvironment;
9
9
  private readonly baseUrl;
10
10
  private readonly sessionManager;
11
+ private readonly headers;
11
12
  private constructor();
13
+ private get requestHeaders();
12
14
  private static getGlobalInstance;
13
15
  private static setGlobalInstance;
14
- static initialize(companyToken: string, options?: UiSdkOptions): void;
16
+ static initialize(companyToken: string, sdkVersion?: string, options?: UiSdkOptions): void;
15
17
  private static getMandatoryProperties;
16
18
  private static getInstance;
17
19
  static logEvent(event_name: string, options?: Properties): Promise<void | any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wf-financing/logger",
3
- "version": "2.1.0",
3
+ "version": "2.2.1",
4
4
  "main": "dist/index.cjs.js",
5
5
  "module": "dist/index.es.js",
6
6
  "types": "dist/index.d.ts",
@@ -15,7 +15,7 @@
15
15
  "dist"
16
16
  ],
17
17
  "dependencies": {
18
- "@wf-financing/embedded-types": "1.1.0"
18
+ "@wf-financing/embedded-types": "1.1.1"
19
19
  },
20
20
  "publishConfig": {
21
21
  "access": "public"