@wf-financing/logger 1.0.0 → 1.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/index.cjs.js +1 -1
- package/dist/index.es.js +84 -30
- package/dist/logger/const.d.ts +3 -0
- package/dist/logger/index.d.ts +2 -0
- package/dist/logger/sessionManager.d.ts +7 -0
- package/dist/utils/index.d.ts +6 -0
- package/package.json +1 -1
package/dist/index.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
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
|
|
3
|
-
var
|
|
4
|
-
const
|
|
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
|
-
},
|
|
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,
|
|
12
|
-
|
|
13
|
-
this
|
|
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,
|
|
61
|
+
static initialize(t, s) {
|
|
22
62
|
n.getGlobalInstance() && console.warn("Logger re-initialized");
|
|
23
|
-
const
|
|
24
|
-
n.setGlobalInstance(
|
|
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,
|
|
33
|
-
const
|
|
34
|
-
...
|
|
35
|
-
Authorization: `Bearer ${
|
|
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
|
|
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:
|
|
92
|
+
properties: i
|
|
45
93
|
})
|
|
46
94
|
});
|
|
47
|
-
if (
|
|
48
|
-
return await
|
|
49
|
-
} catch (
|
|
50
|
-
console.error("Failed to log event",
|
|
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,
|
|
54
|
-
const
|
|
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
|
|
107
|
+
const i = {
|
|
108
|
+
...n.getMandatoryProperties(e.companyToken),
|
|
109
|
+
...s
|
|
110
|
+
}, c = await fetch(e.baseUrl + I.error, {
|
|
57
111
|
method: "POST",
|
|
58
|
-
headers:
|
|
112
|
+
headers: r,
|
|
59
113
|
body: JSON.stringify({
|
|
60
114
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
61
115
|
message: t,
|
|
62
|
-
properties:
|
|
116
|
+
properties: i
|
|
63
117
|
})
|
|
64
118
|
});
|
|
65
|
-
if (
|
|
66
|
-
return await
|
|
67
|
-
} catch (
|
|
68
|
-
console.
|
|
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
|
}
|
package/dist/logger/const.d.ts
CHANGED
package/dist/logger/index.d.ts
CHANGED
|
@@ -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>;
|