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