@wf-financing/logger 1.1.2 → 2.0.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/README.md +2 -2
- package/dist/index.cjs.js +1 -1
- package/dist/index.es.js +45 -38
- package/dist/logger/index.d.ts +3 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -10,12 +10,12 @@ The Logger utilizes a global singleton pattern to ensure the instance is shared
|
|
|
10
10
|
|
|
11
11
|
```jsx
|
|
12
12
|
import { Logger } from '@wf-financing/logger';
|
|
13
|
-
import {
|
|
13
|
+
import { UiSdkOptions } from '@wf-financing/types';
|
|
14
14
|
|
|
15
15
|
const companyToken = 'your-company-token';
|
|
16
16
|
|
|
17
17
|
// Optional: Configure for Sandbox environment
|
|
18
|
-
const options:
|
|
18
|
+
const options: UiSdkOptions = { isSandbox: true };
|
|
19
19
|
|
|
20
20
|
// Initialize the singleton
|
|
21
21
|
Logger.initialize(companyToken, options);
|
package/dist/index.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var I=Object.defineProperty;var
|
|
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,r=e-t.createdAt>m;return s||r}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,r]=(e==null?void 0:e.sub.split("|"))||[],a=e==null?void 0:e.user_id;return{partnerId:s,companyId:r,userId: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(),r=s.sessionManager.getSessionId(),a={...d,Authorization:`Bearer ${s.companyToken}`,"X-Session-ID":r};try{const i={...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:i})});if(c.status===200)return await c.json()}catch(i){console.error("Failed to log event",i)}}static async logError(t,e){if(this.isDevEnvironment){console.error(t);return}const s=n.getInstance(),r=s.sessionManager.getSessionId(),a={...d,"X-Session-ID":r};try{const i={...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:i})});if(c.status===200)return await c.json()}catch(i){console.warn("Failed to log error",i)}}};l(n,"isDevEnvironment",!1);let g=n;exports.Logger=g;
|
package/dist/index.es.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
var y = Object.defineProperty;
|
|
2
|
-
var p = (o, t,
|
|
3
|
-
var l = (o, t,
|
|
4
|
-
const
|
|
2
|
+
var p = (o, t, e) => t in o ? y(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) => {
|
|
5
5
|
try {
|
|
6
6
|
return JSON.parse(atob(o.split(".")[1]));
|
|
7
7
|
} catch {
|
|
@@ -9,19 +9,19 @@ const g = "https://api.wayflyer.com", u = "https://sandbox-api.wayflyer.com", w
|
|
|
9
9
|
}
|
|
10
10
|
}, d = {
|
|
11
11
|
"Content-Type": "application/json"
|
|
12
|
-
},
|
|
12
|
+
}, g = {
|
|
13
13
|
event: "/financing/v1/company/event/",
|
|
14
14
|
error: "/financing/v1/error/"
|
|
15
|
-
},
|
|
16
|
-
class
|
|
15
|
+
}, I = "wf_session_v1", f = 1800 * 1e3, m = 1440 * 60 * 1e3;
|
|
16
|
+
class v {
|
|
17
17
|
getSessionId() {
|
|
18
18
|
const t = Date.now();
|
|
19
|
-
let
|
|
20
|
-
return (!
|
|
19
|
+
let e = this.loadSession();
|
|
20
|
+
return (!e || this.isSessionExpired(e, t)) && (e = this.mintNewSession(t)), e.lastActivity = t, this.saveSession(e), e.sessionId;
|
|
21
21
|
}
|
|
22
|
-
isSessionExpired(t,
|
|
23
|
-
const
|
|
24
|
-
return
|
|
22
|
+
isSessionExpired(t, e) {
|
|
23
|
+
const s = e - t.lastActivity > f, r = e - t.createdAt > m;
|
|
24
|
+
return s || r;
|
|
25
25
|
}
|
|
26
26
|
mintNewSession(t) {
|
|
27
27
|
return {
|
|
@@ -32,7 +32,7 @@ class m {
|
|
|
32
32
|
}
|
|
33
33
|
loadSession() {
|
|
34
34
|
try {
|
|
35
|
-
const t = sessionStorage.getItem(
|
|
35
|
+
const t = sessionStorage.getItem(I);
|
|
36
36
|
return t ? JSON.parse(t) : null;
|
|
37
37
|
} catch {
|
|
38
38
|
return null;
|
|
@@ -40,17 +40,17 @@ class m {
|
|
|
40
40
|
}
|
|
41
41
|
saveSession(t) {
|
|
42
42
|
try {
|
|
43
|
-
sessionStorage.setItem(
|
|
43
|
+
sessionStorage.setItem(I, JSON.stringify(t));
|
|
44
44
|
} catch {
|
|
45
45
|
console.warn("Failed to save session context");
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
|
-
class n {
|
|
50
|
-
constructor(t,
|
|
49
|
+
const n = class n {
|
|
50
|
+
constructor(t, e) {
|
|
51
51
|
l(this, "baseUrl", null);
|
|
52
52
|
l(this, "sessionManager");
|
|
53
|
-
this.companyToken = t, this.baseUrl =
|
|
53
|
+
this.companyToken = t, this.baseUrl = e ? h : u, this.sessionManager = new v();
|
|
54
54
|
}
|
|
55
55
|
static getGlobalInstance() {
|
|
56
56
|
return window.WayflyerLogger || null;
|
|
@@ -58,14 +58,14 @@ class n {
|
|
|
58
58
|
static setGlobalInstance(t) {
|
|
59
59
|
window.WayflyerLogger = t;
|
|
60
60
|
}
|
|
61
|
-
static initialize(t,
|
|
61
|
+
static initialize(t, e) {
|
|
62
62
|
n.getGlobalInstance() && console.warn("Logger re-initialized");
|
|
63
|
-
const
|
|
64
|
-
n.setGlobalInstance(
|
|
63
|
+
const s = new n(t, e == null ? void 0 : e.isSandbox);
|
|
64
|
+
n.setGlobalInstance(s);
|
|
65
65
|
}
|
|
66
66
|
static getMandatoryProperties(t) {
|
|
67
|
-
const
|
|
68
|
-
return { partnerId:
|
|
67
|
+
const e = w(t), [s, r] = (e == null ? void 0 : e.sub.split("|")) || [], a = e == null ? void 0 : e.user_id;
|
|
68
|
+
return { partnerId: s, companyId: r, userId: a };
|
|
69
69
|
}
|
|
70
70
|
static getInstance() {
|
|
71
71
|
const t = n.getGlobalInstance();
|
|
@@ -73,19 +73,20 @@ class n {
|
|
|
73
73
|
throw new Error("Logger not initialized. Call initialize() first.");
|
|
74
74
|
return t;
|
|
75
75
|
}
|
|
76
|
-
static async logEvent(t,
|
|
77
|
-
|
|
76
|
+
static async logEvent(t, e) {
|
|
77
|
+
if (this.isDevEnvironment) return;
|
|
78
|
+
const s = n.getInstance(), r = s.sessionManager.getSessionId(), a = {
|
|
78
79
|
...d,
|
|
79
|
-
Authorization: `Bearer ${
|
|
80
|
-
"X-Session-ID":
|
|
80
|
+
Authorization: `Bearer ${s.companyToken}`,
|
|
81
|
+
"X-Session-ID": r
|
|
81
82
|
};
|
|
82
83
|
try {
|
|
83
84
|
const i = {
|
|
84
|
-
...n.getMandatoryProperties(
|
|
85
|
-
...
|
|
86
|
-
}, c = await fetch(
|
|
85
|
+
...n.getMandatoryProperties(s.companyToken),
|
|
86
|
+
...e
|
|
87
|
+
}, c = await fetch(s.baseUrl + g.event, {
|
|
87
88
|
method: "POST",
|
|
88
|
-
headers:
|
|
89
|
+
headers: a,
|
|
89
90
|
body: JSON.stringify({
|
|
90
91
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
91
92
|
event_name: t,
|
|
@@ -98,18 +99,22 @@ class n {
|
|
|
98
99
|
console.error("Failed to log event", i);
|
|
99
100
|
}
|
|
100
101
|
}
|
|
101
|
-
static async logError(t,
|
|
102
|
-
|
|
102
|
+
static async logError(t, e) {
|
|
103
|
+
if (this.isDevEnvironment) {
|
|
104
|
+
console.error(t);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const s = n.getInstance(), r = s.sessionManager.getSessionId(), a = {
|
|
103
108
|
...d,
|
|
104
|
-
"X-Session-ID":
|
|
109
|
+
"X-Session-ID": r
|
|
105
110
|
};
|
|
106
111
|
try {
|
|
107
112
|
const i = {
|
|
108
|
-
...n.getMandatoryProperties(
|
|
109
|
-
...
|
|
110
|
-
}, c = await fetch(
|
|
113
|
+
...n.getMandatoryProperties(s.companyToken),
|
|
114
|
+
...e
|
|
115
|
+
}, c = await fetch(s.baseUrl + g.error, {
|
|
111
116
|
method: "POST",
|
|
112
|
-
headers:
|
|
117
|
+
headers: a,
|
|
113
118
|
body: JSON.stringify({
|
|
114
119
|
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
115
120
|
message: t,
|
|
@@ -122,7 +127,9 @@ class n {
|
|
|
122
127
|
console.warn("Failed to log error", i);
|
|
123
128
|
}
|
|
124
129
|
}
|
|
125
|
-
}
|
|
130
|
+
};
|
|
131
|
+
l(n, "isDevEnvironment", !1);
|
|
132
|
+
let S = n;
|
|
126
133
|
export {
|
|
127
|
-
|
|
134
|
+
S as Logger
|
|
128
135
|
};
|
package/dist/logger/index.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UiSdkOptions } from '@wf-financing/embedded-types';
|
|
2
2
|
|
|
3
3
|
type Properties = {
|
|
4
4
|
[key: string]: string | number | null;
|
|
5
5
|
};
|
|
6
6
|
export declare class Logger {
|
|
7
7
|
private readonly companyToken;
|
|
8
|
+
private static readonly isDevEnvironment;
|
|
8
9
|
private readonly baseUrl;
|
|
9
10
|
private readonly sessionManager;
|
|
10
11
|
private constructor();
|
|
11
12
|
private static getGlobalInstance;
|
|
12
13
|
private static setGlobalInstance;
|
|
13
|
-
static initialize(companyToken: string, options?:
|
|
14
|
+
static initialize(companyToken: string, options?: UiSdkOptions): void;
|
|
14
15
|
private static getMandatoryProperties;
|
|
15
16
|
private static getInstance;
|
|
16
17
|
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": "
|
|
3
|
+
"version": "2.0.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,7 @@
|
|
|
15
15
|
"dist"
|
|
16
16
|
],
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@wf-financing/embedded-types": "0.
|
|
18
|
+
"@wf-financing/embedded-types": "1.0.0"
|
|
19
19
|
},
|
|
20
20
|
"publishConfig": {
|
|
21
21
|
"access": "public"
|