@wf-financing/logger 1.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 ADDED
@@ -0,0 +1,50 @@
1
+ # Wayflyer Embedded Finance Logger
2
+
3
+ The `@wf-financing/logger` package provides a singleton logging utility. It allows logging business events and errors.
4
+
5
+ ## Usage
6
+
7
+ Before logging any events or errors, the Logger must be initialized. This usually happens at the entry point of your application.
8
+
9
+ The Logger utilizes a global singleton pattern to ensure the instance is shared across different bundles (e.g., between your main application and injected UI scripts).
10
+
11
+ ```jsx
12
+ import { Logger } from '@wf-financing/logger';
13
+ import { SdkOptionsType } from '@wf-financing/types';
14
+
15
+ const companyToken = 'your-company-token';
16
+
17
+ // Optional: Configure for Sandbox environment
18
+ const options: SdkOptionsType = { isSandbox: true };
19
+
20
+ // Initialize the singleton
21
+ Logger.initialize(companyToken, options);
22
+ ```
23
+
24
+ Note: The companyToken should be minted using the Company Token endpoint on the partner's backend. See the Authentication section [here](https://docs.wayflyer.com/embedded-finance/authentication) for more details.
25
+
26
+ ## Logging Events & Errors
27
+
28
+ Once initialized, you can import Logger anywhere in your application (or in separate bundles loaded on the same page) to log data.
29
+
30
+ ```jsx
31
+ import { Logger } from '@wf-financing/logger';
32
+
33
+ // Log a standard event
34
+ await Logger.logEvent('banner_viewed', {
35
+ user_id: 123,
36
+ });
37
+
38
+ // Log an error
39
+ try {
40
+ // ... some operation
41
+ } catch (err) {
42
+ await Logger.logError('failed_to_load_financing', {
43
+ error_message: err.message,
44
+ });
45
+ }
46
+ ```
47
+
48
+ ## Shared Scope
49
+
50
+ This package uses the global window scope to store the Logger instance `window.WayflyerLogger`. This ensures that if you are loading multiple bundles (e.g., a main SDK and a dynamic UI script), they all share the same Logger instance without needing to re-initialize or pass tokens manually.
@@ -0,0 +1 @@
1
+ export { API_BASE_URL, SANDBOX_API_BASE_URL } from './urls';
@@ -0,0 +1,2 @@
1
+ export declare const API_BASE_URL: any;
2
+ export declare const SANDBOX_API_BASE_URL: any;
@@ -0,0 +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;
@@ -0,0 +1 @@
1
+ export { Logger } from './logger';
@@ -0,0 +1,74 @@
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 = {
5
+ "Content-Type": "application/json"
6
+ }, l = {
7
+ event: "/financing/v1/company/event/",
8
+ error: "/financing/v1/error/"
9
+ };
10
+ class n {
11
+ constructor(t, e) {
12
+ i(this, "baseUrl", null);
13
+ this.companyToken = t, this.baseUrl = e ? p : f;
14
+ }
15
+ static getGlobalInstance() {
16
+ return window.WayflyerLogger || null;
17
+ }
18
+ static setGlobalInstance(t) {
19
+ window.WayflyerLogger = t;
20
+ }
21
+ static initialize(t, e) {
22
+ n.getGlobalInstance() && console.warn("Logger re-initialized");
23
+ const a = new n(t, e == null ? void 0 : e.isSandbox);
24
+ n.setGlobalInstance(a);
25
+ }
26
+ static getInstance() {
27
+ const t = n.getGlobalInstance();
28
+ if (!t)
29
+ throw new Error("Logger not initialized. Call initialize() first.");
30
+ return t;
31
+ }
32
+ static async logEvent(t, e) {
33
+ const a = n.getInstance(), r = {
34
+ ...c,
35
+ Authorization: `Bearer ${a.companyToken}`
36
+ };
37
+ try {
38
+ const o = await fetch(a.baseUrl + l.event, {
39
+ method: "POST",
40
+ headers: r,
41
+ body: JSON.stringify({
42
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
43
+ event_name: t,
44
+ properties: e || {}
45
+ })
46
+ });
47
+ if (o.status === 200)
48
+ return await o.json();
49
+ } catch (o) {
50
+ console.error("Failed to log event", o);
51
+ }
52
+ }
53
+ static async logError(t, e) {
54
+ const a = n.getInstance();
55
+ try {
56
+ const r = await fetch(a.baseUrl + l.error, {
57
+ method: "POST",
58
+ headers: c,
59
+ body: JSON.stringify({
60
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
61
+ message: t,
62
+ properties: e || {}
63
+ })
64
+ });
65
+ if (r.status === 200)
66
+ return await r.json();
67
+ } catch (r) {
68
+ console.error("Failed to log error", r);
69
+ }
70
+ }
71
+ }
72
+ export {
73
+ n as Logger
74
+ };
@@ -0,0 +1,7 @@
1
+ export declare const defaultHeaders: {
2
+ 'Content-Type': string;
3
+ };
4
+ export declare const endpoints: {
5
+ event: string;
6
+ error: string;
7
+ };
@@ -0,0 +1,17 @@
1
+ import { SdkOptionsType } from '@wf-financing/embedded-types';
2
+
3
+ type Properties = {
4
+ [key: string]: string | number | null;
5
+ };
6
+ export declare class Logger {
7
+ private readonly companyToken;
8
+ private readonly baseUrl;
9
+ private constructor();
10
+ private static getGlobalInstance;
11
+ private static setGlobalInstance;
12
+ static initialize(companyToken: string, options?: SdkOptionsType): void;
13
+ private static getInstance;
14
+ static logEvent(event_name: string, options?: Properties): Promise<void | any>;
15
+ static logError(message: string, options?: Properties): Promise<void | any>;
16
+ }
17
+ export {};
@@ -0,0 +1,2 @@
1
+ declare const _default: import('vite').UserConfig;
2
+ export default _default;
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@wf-financing/logger",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.cjs.js",
5
+ "module": "dist/index.es.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.es.js",
10
+ "types": "./dist/index.d.ts",
11
+ "require": "./dist/index.cjs.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "dependencies": {
18
+ "@wf-financing/embedded-types": "0.6.0"
19
+ },
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "scripts": {
24
+ "clean": "rm -rf dist",
25
+ "build": "vite build",
26
+ "prepare-logger": "pnpm clean && pnpm build && pnpm pack",
27
+ "publish-logger": "pnpm clean && pnpm build && pnpm publish --access public --no-git-checks",
28
+ "test": "vitest",
29
+ "test:coverage": "vitest --coverage"
30
+ }
31
+ }