@yuno-payments/sdk-event-log 0.2.0 → 0.3.0-beta.10

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.
@@ -16,15 +16,16 @@ const publicApiKey = PUBLIC_API_KEY;
16
16
  const platform = 'web';
17
17
  const STATUS_CREATED = 201;
18
18
  describe('Api', () => {
19
+ let api;
19
20
  beforeEach(() => {
20
21
  // Reset the instance before each test
21
- api_1.api.init(publicApiKey, platform);
22
+ api = new api_1.Api(publicApiKey, platform);
22
23
  });
23
24
  it('should thorw an error if the instance is not initialized well', () => {
24
- expect(() => api_1.api.init('', '')).toThrowError();
25
+ expect(() => new api_1.Api('', '')).toThrowError();
25
26
  });
26
27
  it('should initialize the instance with the correct headers', () => {
27
- const instance = api_1.api.getInstances();
28
+ const instance = api.getInstances();
28
29
  expect(instance === null || instance === void 0 ? void 0 : instance.defaults.baseURL).toBe('https://api-staging.y.uno/v1');
29
30
  expect(instance === null || instance === void 0 ? void 0 : instance.defaults.headers['public-api-key']).toBe(publicApiKey);
30
31
  expect(instance === null || instance === void 0 ? void 0 : instance.defaults.headers['X-Platform']).toBe(platform);
@@ -39,11 +40,11 @@ describe('Api', () => {
39
40
  type: types_1.EventLogType.EVENT,
40
41
  },
41
42
  ];
42
- const response = yield api_1.api.sendBatch(batches);
43
+ const response = yield api.sendBatch(batches);
43
44
  expect(response === null || response === void 0 ? void 0 : response.status).toBe(STATUS_CREATED);
44
45
  }));
45
46
  it('should return the instance', () => {
46
- const instance = api_1.api.getInstances();
47
+ const instance = api.getInstances();
47
48
  expect(instance).not.toBeNull();
48
49
  });
49
50
  });
@@ -1,10 +1,8 @@
1
1
  import { AxiosInstance } from 'axios';
2
2
  import { Batch } from '../types';
3
- declare class Api {
3
+ export declare class Api {
4
4
  #private;
5
- init(publicApiKey: string, platform: string): void;
5
+ constructor(publicApiKey: string, platform: string, debug?: boolean);
6
6
  sendBatch(batches: Batch[]): Promise<import("axios").AxiosResponse<any, any>> | undefined;
7
7
  getInstances(): AxiosInstance | null;
8
8
  }
9
- export declare const api: Api;
10
- export {};
@@ -13,16 +13,16 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
13
13
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
15
  };
16
- var _Api_instance;
16
+ var _Api_instance, _Api_debug;
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.api = void 0;
18
+ exports.Api = void 0;
19
19
  const axios_1 = __importDefault(require("axios"));
20
20
  const api_utils_1 = require("./api.utils");
21
21
  class Api {
22
- constructor() {
22
+ constructor(publicApiKey, platform, debug = false) {
23
23
  _Api_instance.set(this, null);
24
- }
25
- init(publicApiKey, platform) {
24
+ _Api_debug.set(this, false);
25
+ __classPrivateFieldSet(this, _Api_debug, debug, "f");
26
26
  __classPrivateFieldSet(this, _Api_instance, axios_1.default.create({
27
27
  baseURL: (0, api_utils_1.getBaseUrl)(publicApiKey),
28
28
  // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -31,11 +31,14 @@ class Api {
31
31
  }
32
32
  sendBatch(batches) {
33
33
  var _a;
34
- return (_a = __classPrivateFieldGet(this, _Api_instance, "f")) === null || _a === void 0 ? void 0 : _a.post('/sdk-event-log/publish', { batch: batches });
34
+ if (__classPrivateFieldGet(this, _Api_debug, "f")) {
35
+ console.dir(batches, { depth: null });
36
+ }
37
+ return (_a = __classPrivateFieldGet(this, _Api_instance, "f")) === null || _a === void 0 ? void 0 : _a.post('/sdk/event-log/publish', { batch: batches });
35
38
  }
36
39
  getInstances() {
37
40
  return __classPrivateFieldGet(this, _Api_instance, "f");
38
41
  }
39
42
  }
40
- _Api_instance = new WeakMap();
41
- exports.api = new Api();
43
+ exports.Api = Api;
44
+ _Api_instance = new WeakMap(), _Api_debug = new WeakMap();
@@ -1,10 +1,9 @@
1
1
  /// <reference types="node" />
2
- declare class Cron {
2
+ import { Api } from '../api';
3
+ export declare class Cron {
3
4
  #private;
4
- init(batchTime: number): void;
5
+ constructor(batchTime: number, api: Api);
5
6
  sendBatch(): void;
6
7
  stop(): void;
7
8
  getInterval(): NodeJS.Timeout | undefined;
8
9
  }
9
- export declare const cron: Cron;
10
- export {};
@@ -10,25 +10,24 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _Cron_interval;
13
+ var _Cron_interval, _Cron_api;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.cron = void 0;
16
- const api_1 = require("../api");
15
+ exports.Cron = void 0;
17
16
  const database_1 = require("../database");
18
17
  const NONE_ITEMS = 0;
19
18
  class Cron {
20
- constructor() {
19
+ constructor(batchTime, api) {
21
20
  _Cron_interval.set(this, void 0);
22
- }
23
- init(batchTime) {
21
+ _Cron_api.set(this, void 0);
24
22
  this.stop();
25
- __classPrivateFieldSet(this, _Cron_interval, setInterval(this.sendBatch, batchTime), "f");
23
+ __classPrivateFieldSet(this, _Cron_api, api, "f");
24
+ __classPrivateFieldSet(this, _Cron_interval, setInterval(this.sendBatch.bind(this), batchTime), "f");
26
25
  }
27
26
  sendBatch() {
28
27
  database_1.database.getAllItems().then((items) => {
29
28
  var _a;
30
29
  if (items.length > NONE_ITEMS) {
31
- (_a = api_1.api.sendBatch(items)) === null || _a === void 0 ? void 0 : _a.then(() => database_1.database.reset());
30
+ (_a = __classPrivateFieldGet(this, _Cron_api, "f").sendBatch(items)) === null || _a === void 0 ? void 0 : _a.then(() => database_1.database.reset());
32
31
  }
33
32
  });
34
33
  }
@@ -42,5 +41,5 @@ class Cron {
42
41
  return __classPrivateFieldGet(this, _Cron_interval, "f");
43
42
  }
44
43
  }
45
- _Cron_interval = new WeakMap();
46
- exports.cron = new Cron();
44
+ exports.Cron = Cron;
45
+ _Cron_interval = new WeakMap(), _Cron_api = new WeakMap();
@@ -13,13 +13,18 @@ const cron_1 = require("./cron");
13
13
  const api_1 = require("../api");
14
14
  const database_1 = require("../database");
15
15
  const types_1 = require("../types");
16
+ const PUBLIC_API_KEY = 'staging_gAAAAABj-NC2WEf3XA4lHnN6RfGbjETf6qL27jlU7c1tm01ERV-Y-E6ddee1QPyV_CA8cxjVlGy-qDNA2-mqa9msQlTJit-Fmbvv6f2AZdfbFd_CUo1stvixnF4_8MzM_IYVWYz7KK86VfEBvxNpjRCyp7BDef-QzrT1-yqa8HAYTuA1GU3Hjjo=';
16
17
  const TIME_INTERVAL = 1000;
17
18
  describe('Cron', () => {
18
19
  let spy;
20
+ let cron;
21
+ let api;
19
22
  beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
20
23
  // restore the spy created with spyOn
21
24
  jest.restoreAllMocks();
22
- spy = jest.spyOn(api_1.api, 'sendBatch');
25
+ api = new api_1.Api(PUBLIC_API_KEY, 'test');
26
+ cron = new cron_1.Cron(TIME_INTERVAL, api);
27
+ spy = jest.spyOn(api, 'sendBatch');
23
28
  yield database_1.database.reset();
24
29
  }));
25
30
  it('should send a batch of items to the API if there are items in the database', () => __awaiter(void 0, void 0, void 0, function* () {
@@ -31,22 +36,20 @@ describe('Cron', () => {
31
36
  type: types_1.EventLogType.EVENT,
32
37
  };
33
38
  database_1.database.setItem(batch);
34
- yield cron_1.cron.sendBatch();
39
+ yield cron.sendBatch();
35
40
  expect(spy).toHaveBeenCalledWith([batch]);
36
41
  }));
37
42
  it('should not send a batch of items to the API if there are no items in the database', () => __awaiter(void 0, void 0, void 0, function* () {
38
- yield cron_1.cron.sendBatch();
43
+ yield cron.sendBatch();
39
44
  expect(spy).not.toHaveBeenCalled();
40
45
  }));
41
46
  it('should stop the interval when stop is called', () => {
42
47
  jest.useFakeTimers();
43
- cron_1.cron.init(TIME_INTERVAL);
44
- cron_1.cron.stop();
45
- expect(cron_1.cron.getInterval()).toBeUndefined();
48
+ cron.stop();
49
+ expect(cron.getInterval()).toBeUndefined();
46
50
  });
47
51
  it('should start the interval when init is called', () => {
48
52
  jest.useFakeTimers();
49
- cron_1.cron.init(TIME_INTERVAL);
50
- expect(cron_1.cron.getInterval()).toBeDefined();
53
+ expect(cron.getInterval()).toBeDefined();
51
54
  });
52
55
  });
@@ -1,15 +1,15 @@
1
1
  import { InitArgs } from './index.types';
2
2
  import './database';
3
3
  import { Event, Log } from './types';
4
- declare class EventLog {
4
+ export declare class EventLog {
5
5
  #private;
6
- init({ publicApiKey, platform, batchTime, enableEvents, enableLogs }: InitArgs): void;
6
+ init({ publicApiKey, platform, batchTime, enableEvents, enableLogs, debug }: InitArgs): void;
7
7
  event(eventArgs: Omit<Event, 'type'>): Promise<void> | undefined;
8
8
  logger: {
9
9
  error: (errorArgs: Omit<Log, 'level' | 'type'>) => Promise<void> | undefined;
10
10
  debug: (debugArgs: Omit<Log, 'level' | 'type'>) => Promise<void> | undefined;
11
11
  info: (infoArgs: Omit<Log, 'level' | 'type'>) => Promise<void> | undefined;
12
12
  };
13
+ sendBatch(): void;
13
14
  }
14
15
  export declare const eventLog: EventLog;
15
- export {};
package/dist/cjs/index.js CHANGED
@@ -10,9 +10,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _EventLog_enableEvents, _EventLog_enableLogs;
13
+ var _EventLog_enableEvents, _EventLog_enableLogs, _EventLog_api;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.eventLog = void 0;
15
+ exports.eventLog = exports.EventLog = void 0;
16
16
  const api_1 = require("./api");
17
17
  require("./database");
18
18
  const cron_1 = require("./cron");
@@ -23,6 +23,7 @@ class EventLog {
23
23
  constructor() {
24
24
  _EventLog_enableEvents.set(this, false);
25
25
  _EventLog_enableLogs.set(this, false);
26
+ _EventLog_api.set(this, void 0);
26
27
  this.logger = {
27
28
  error: (errorArgs) => {
28
29
  return (0, index_utils_1.setLog)(errorArgs, types_1.Level.ERROR, __classPrivateFieldGet(this, _EventLog_enableLogs, "f"));
@@ -35,11 +36,13 @@ class EventLog {
35
36
  }
36
37
  };
37
38
  }
38
- init({ publicApiKey, platform = 'web', batchTime, enableEvents, enableLogs }) {
39
+ init({ publicApiKey, platform = 'web', batchTime, enableEvents, enableLogs, debug = false }) {
39
40
  __classPrivateFieldSet(this, _EventLog_enableEvents, enableEvents, "f");
40
41
  __classPrivateFieldSet(this, _EventLog_enableLogs, enableLogs, "f");
41
- api_1.api.init(publicApiKey, platform);
42
- cron_1.cron.init(batchTime);
42
+ __classPrivateFieldSet(this, _EventLog_api, new api_1.Api(publicApiKey, platform, debug), "f");
43
+ if (batchTime) {
44
+ new cron_1.Cron(batchTime, __classPrivateFieldGet(this, _EventLog_api, "f"));
45
+ }
43
46
  }
44
47
  event(eventArgs) {
45
48
  if (!__classPrivateFieldGet(this, _EventLog_enableEvents, "f")) {
@@ -47,6 +50,15 @@ class EventLog {
47
50
  }
48
51
  return database_1.database.setItem(Object.assign(Object.assign({}, eventArgs), { type: types_1.EventLogType.EVENT }));
49
52
  }
53
+ sendBatch() {
54
+ database_1.database.getAllItems().then((items) => {
55
+ var _a, _b;
56
+ if (items.length) {
57
+ (_b = (_a = __classPrivateFieldGet(this, _EventLog_api, "f")) === null || _a === void 0 ? void 0 : _a.sendBatch(items)) === null || _b === void 0 ? void 0 : _b.then(() => database_1.database.reset());
58
+ }
59
+ });
60
+ }
50
61
  }
51
- _EventLog_enableEvents = new WeakMap(), _EventLog_enableLogs = new WeakMap();
62
+ exports.EventLog = EventLog;
63
+ _EventLog_enableEvents = new WeakMap(), _EventLog_enableLogs = new WeakMap(), _EventLog_api = new WeakMap();
52
64
  exports.eventLog = new EventLog();
@@ -11,7 +11,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const index_1 = require("./index");
13
13
  const database_1 = require("./database");
14
- const cron_1 = require("./cron");
15
14
  const PUBLIC_API_KEY = 'staging_gAAAAABj-NC2WEf3XA4lHnN6RfGbjETf6qL27jlU7c1tm01ERV-Y-E6ddee1QPyV_CA8cxjVlGy-qDNA2-mqa9msQlTJit-Fmbvv6f2AZdfbFd_CUo1stvixnF4_8MzM_IYVWYz7KK86VfEBvxNpjRCyp7BDef-QzrT1-yqa8HAYTuA1GU3Hjjo=';
16
15
  const eventArgs = {
17
16
  source: 'test',
@@ -40,7 +39,6 @@ describe('EventLog', () => {
40
39
  });
41
40
  afterEach(() => __awaiter(void 0, void 0, void 0, function* () {
42
41
  yield new Promise((resolve) => setTimeout(resolve, 1000));
43
- cron_1.cron.stop();
44
42
  }));
45
43
  it('should add an event to the database', () => __awaiter(void 0, void 0, void 0, function* () {
46
44
  index_1.eventLog.event(eventArgs);
@@ -60,10 +58,12 @@ describe('EventLog', () => {
60
58
  expect(events).toHaveLength(0);
61
59
  }));
62
60
  it('should log an error', () => __awaiter(void 0, void 0, void 0, function* () {
61
+ index_1.eventLog.logger.error(logArgs);
63
62
  index_1.eventLog.logger.error(logArgs);
64
63
  const events = yield database_1.database.getAllItems();
65
- expect(events).toHaveLength(1);
64
+ expect(events).toHaveLength(2);
66
65
  expect(events[0]).toMatchObject(logArgs);
66
+ expect(events[1]).toMatchObject(logArgs);
67
67
  }));
68
68
  it('should not log an error if logs are disabled', () => __awaiter(void 0, void 0, void 0, function* () {
69
69
  index_1.eventLog.init({
@@ -1,7 +1,8 @@
1
1
  export type InitArgs = {
2
2
  publicApiKey: string;
3
- batchTime: number;
3
+ batchTime?: number;
4
4
  enableEvents: boolean;
5
5
  enableLogs: boolean;
6
6
  platform?: string;
7
+ debug?: boolean;
7
8
  };
@@ -9,23 +9,23 @@ export declare enum Level {
9
9
  }
10
10
  type BaseEventLog = {
11
11
  type: EventLogType;
12
- customer_session?: string;
13
- checkout_session?: string;
12
+ customer_session?: string | null;
13
+ checkout_session?: string | null;
14
14
  original_created_at: string;
15
15
  };
16
16
  export type Log = BaseEventLog & {
17
17
  sdk_version: string;
18
- os?: string;
19
- url: string;
20
- method: string;
21
- headers?: object;
22
- request?: object;
23
- response?: object;
24
- status_code?: number;
25
- trace_id?: string;
26
- metadata?: unknown;
27
- country: string;
28
- organization_name?: string;
18
+ os?: string | null;
19
+ url?: string | null;
20
+ method?: string | null;
21
+ headers?: object | null;
22
+ request?: object | null;
23
+ response?: object | null;
24
+ status_code?: string | null;
25
+ trace_id?: string | null;
26
+ metadata?: unknown | null;
27
+ country?: string | null;
28
+ organization_name?: string | null;
29
29
  level: Level;
30
30
  stack_trace?: string;
31
31
  };
@@ -7,15 +7,16 @@ const publicApiKey = PUBLIC_API_KEY;
7
7
  const platform = 'web';
8
8
  const STATUS_CREATED = 201;
9
9
  describe('Api', () => {
10
+ let api;
10
11
  beforeEach(() => {
11
12
  // Reset the instance before each test
12
- api_1.api.init(publicApiKey, platform);
13
+ api = new api_1.Api(publicApiKey, platform);
13
14
  });
14
15
  it('should thorw an error if the instance is not initialized well', () => {
15
- expect(() => api_1.api.init('', '')).toThrowError();
16
+ expect(() => new api_1.Api('', '')).toThrowError();
16
17
  });
17
18
  it('should initialize the instance with the correct headers', () => {
18
- const instance = api_1.api.getInstances();
19
+ const instance = api.getInstances();
19
20
  expect(instance === null || instance === void 0 ? void 0 : instance.defaults.baseURL).toBe('https://api-staging.y.uno/v1');
20
21
  expect(instance === null || instance === void 0 ? void 0 : instance.defaults.headers['public-api-key']).toBe(publicApiKey);
21
22
  expect(instance === null || instance === void 0 ? void 0 : instance.defaults.headers['X-Platform']).toBe(platform);
@@ -30,11 +31,11 @@ describe('Api', () => {
30
31
  type: types_1.EventLogType.EVENT,
31
32
  },
32
33
  ];
33
- const response = await api_1.api.sendBatch(batches);
34
+ const response = await api.sendBatch(batches);
34
35
  expect(response === null || response === void 0 ? void 0 : response.status).toBe(STATUS_CREATED);
35
36
  });
36
37
  it('should return the instance', () => {
37
- const instance = api_1.api.getInstances();
38
+ const instance = api.getInstances();
38
39
  expect(instance).not.toBeNull();
39
40
  });
40
41
  });
@@ -1,10 +1,8 @@
1
1
  import { AxiosInstance } from 'axios';
2
2
  import { Batch } from '../types';
3
- declare class Api {
3
+ export declare class Api {
4
4
  #private;
5
- init(publicApiKey: string, platform: string): void;
5
+ constructor(publicApiKey: string, platform: string, debug?: boolean);
6
6
  sendBatch(batches: Batch[]): Promise<import("axios").AxiosResponse<any, any>> | undefined;
7
7
  getInstances(): AxiosInstance | null;
8
8
  }
9
- export declare const api: Api;
10
- export {};
@@ -13,16 +13,16 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
13
13
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
15
  };
16
- var _Api_instance;
16
+ var _Api_instance, _Api_debug;
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.api = void 0;
18
+ exports.Api = void 0;
19
19
  const axios_1 = __importDefault(require("axios"));
20
20
  const api_utils_1 = require("./api.utils");
21
21
  class Api {
22
- constructor() {
22
+ constructor(publicApiKey, platform, debug = false) {
23
23
  _Api_instance.set(this, null);
24
- }
25
- init(publicApiKey, platform) {
24
+ _Api_debug.set(this, false);
25
+ __classPrivateFieldSet(this, _Api_debug, debug, "f");
26
26
  __classPrivateFieldSet(this, _Api_instance, axios_1.default.create({
27
27
  baseURL: (0, api_utils_1.getBaseUrl)(publicApiKey),
28
28
  // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -31,11 +31,14 @@ class Api {
31
31
  }
32
32
  sendBatch(batches) {
33
33
  var _a;
34
- return (_a = __classPrivateFieldGet(this, _Api_instance, "f")) === null || _a === void 0 ? void 0 : _a.post('/sdk-event-log/publish', { batch: batches });
34
+ if (__classPrivateFieldGet(this, _Api_debug, "f")) {
35
+ console.dir(batches, { depth: null });
36
+ }
37
+ return (_a = __classPrivateFieldGet(this, _Api_instance, "f")) === null || _a === void 0 ? void 0 : _a.post('/sdk/event-log/publish', { batch: batches });
35
38
  }
36
39
  getInstances() {
37
40
  return __classPrivateFieldGet(this, _Api_instance, "f");
38
41
  }
39
42
  }
40
- _Api_instance = new WeakMap();
41
- exports.api = new Api();
43
+ exports.Api = Api;
44
+ _Api_instance = new WeakMap(), _Api_debug = new WeakMap();
@@ -1,10 +1,9 @@
1
1
  /// <reference types="node" />
2
- declare class Cron {
2
+ import { Api } from '../api';
3
+ export declare class Cron {
3
4
  #private;
4
- init(batchTime: number): void;
5
+ constructor(batchTime: number, api: Api);
5
6
  sendBatch(): void;
6
7
  stop(): void;
7
8
  getInterval(): NodeJS.Timeout | undefined;
8
9
  }
9
- export declare const cron: Cron;
10
- export {};
@@ -10,25 +10,24 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _Cron_interval;
13
+ var _Cron_interval, _Cron_api;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.cron = void 0;
16
- const api_1 = require("../api");
15
+ exports.Cron = void 0;
17
16
  const database_1 = require("../database");
18
17
  const NONE_ITEMS = 0;
19
18
  class Cron {
20
- constructor() {
19
+ constructor(batchTime, api) {
21
20
  _Cron_interval.set(this, void 0);
22
- }
23
- init(batchTime) {
21
+ _Cron_api.set(this, void 0);
24
22
  this.stop();
25
- __classPrivateFieldSet(this, _Cron_interval, setInterval(this.sendBatch, batchTime), "f");
23
+ __classPrivateFieldSet(this, _Cron_api, api, "f");
24
+ __classPrivateFieldSet(this, _Cron_interval, setInterval(this.sendBatch.bind(this), batchTime), "f");
26
25
  }
27
26
  sendBatch() {
28
27
  database_1.database.getAllItems().then((items) => {
29
28
  var _a;
30
29
  if (items.length > NONE_ITEMS) {
31
- (_a = api_1.api.sendBatch(items)) === null || _a === void 0 ? void 0 : _a.then(() => database_1.database.reset());
30
+ (_a = __classPrivateFieldGet(this, _Cron_api, "f").sendBatch(items)) === null || _a === void 0 ? void 0 : _a.then(() => database_1.database.reset());
32
31
  }
33
32
  });
34
33
  }
@@ -42,5 +41,5 @@ class Cron {
42
41
  return __classPrivateFieldGet(this, _Cron_interval, "f");
43
42
  }
44
43
  }
45
- _Cron_interval = new WeakMap();
46
- exports.cron = new Cron();
44
+ exports.Cron = Cron;
45
+ _Cron_interval = new WeakMap(), _Cron_api = new WeakMap();
@@ -4,13 +4,18 @@ const cron_1 = require("./cron");
4
4
  const api_1 = require("../api");
5
5
  const database_1 = require("../database");
6
6
  const types_1 = require("../types");
7
+ const PUBLIC_API_KEY = 'staging_gAAAAABj-NC2WEf3XA4lHnN6RfGbjETf6qL27jlU7c1tm01ERV-Y-E6ddee1QPyV_CA8cxjVlGy-qDNA2-mqa9msQlTJit-Fmbvv6f2AZdfbFd_CUo1stvixnF4_8MzM_IYVWYz7KK86VfEBvxNpjRCyp7BDef-QzrT1-yqa8HAYTuA1GU3Hjjo=';
7
8
  const TIME_INTERVAL = 1000;
8
9
  describe('Cron', () => {
9
10
  let spy;
11
+ let cron;
12
+ let api;
10
13
  beforeEach(async () => {
11
14
  // restore the spy created with spyOn
12
15
  jest.restoreAllMocks();
13
- spy = jest.spyOn(api_1.api, 'sendBatch');
16
+ api = new api_1.Api(PUBLIC_API_KEY, 'test');
17
+ cron = new cron_1.Cron(TIME_INTERVAL, api);
18
+ spy = jest.spyOn(api, 'sendBatch');
14
19
  await database_1.database.reset();
15
20
  });
16
21
  it('should send a batch of items to the API if there are items in the database', async () => {
@@ -22,22 +27,20 @@ describe('Cron', () => {
22
27
  type: types_1.EventLogType.EVENT,
23
28
  };
24
29
  database_1.database.setItem(batch);
25
- await cron_1.cron.sendBatch();
30
+ await cron.sendBatch();
26
31
  expect(spy).toHaveBeenCalledWith([batch]);
27
32
  });
28
33
  it('should not send a batch of items to the API if there are no items in the database', async () => {
29
- await cron_1.cron.sendBatch();
34
+ await cron.sendBatch();
30
35
  expect(spy).not.toHaveBeenCalled();
31
36
  });
32
37
  it('should stop the interval when stop is called', () => {
33
38
  jest.useFakeTimers();
34
- cron_1.cron.init(TIME_INTERVAL);
35
- cron_1.cron.stop();
36
- expect(cron_1.cron.getInterval()).toBeUndefined();
39
+ cron.stop();
40
+ expect(cron.getInterval()).toBeUndefined();
37
41
  });
38
42
  it('should start the interval when init is called', () => {
39
43
  jest.useFakeTimers();
40
- cron_1.cron.init(TIME_INTERVAL);
41
- expect(cron_1.cron.getInterval()).toBeDefined();
44
+ expect(cron.getInterval()).toBeDefined();
42
45
  });
43
46
  });
@@ -1,15 +1,15 @@
1
1
  import { InitArgs } from './index.types';
2
2
  import './database';
3
3
  import { Event, Log } from './types';
4
- declare class EventLog {
4
+ export declare class EventLog {
5
5
  #private;
6
- init({ publicApiKey, platform, batchTime, enableEvents, enableLogs }: InitArgs): void;
6
+ init({ publicApiKey, platform, batchTime, enableEvents, enableLogs, debug }: InitArgs): void;
7
7
  event(eventArgs: Omit<Event, 'type'>): Promise<void> | undefined;
8
8
  logger: {
9
9
  error: (errorArgs: Omit<Log, 'level' | 'type'>) => Promise<void> | undefined;
10
10
  debug: (debugArgs: Omit<Log, 'level' | 'type'>) => Promise<void> | undefined;
11
11
  info: (infoArgs: Omit<Log, 'level' | 'type'>) => Promise<void> | undefined;
12
12
  };
13
+ sendBatch(): void;
13
14
  }
14
15
  export declare const eventLog: EventLog;
15
- export {};
package/dist/esm/index.js CHANGED
@@ -10,9 +10,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _EventLog_enableEvents, _EventLog_enableLogs;
13
+ var _EventLog_enableEvents, _EventLog_enableLogs, _EventLog_api;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.eventLog = void 0;
15
+ exports.eventLog = exports.EventLog = void 0;
16
16
  const api_1 = require("./api");
17
17
  require("./database");
18
18
  const cron_1 = require("./cron");
@@ -23,6 +23,7 @@ class EventLog {
23
23
  constructor() {
24
24
  _EventLog_enableEvents.set(this, false);
25
25
  _EventLog_enableLogs.set(this, false);
26
+ _EventLog_api.set(this, void 0);
26
27
  this.logger = {
27
28
  error: (errorArgs) => {
28
29
  return (0, index_utils_1.setLog)(errorArgs, types_1.Level.ERROR, __classPrivateFieldGet(this, _EventLog_enableLogs, "f"));
@@ -35,11 +36,13 @@ class EventLog {
35
36
  }
36
37
  };
37
38
  }
38
- init({ publicApiKey, platform = 'web', batchTime, enableEvents, enableLogs }) {
39
+ init({ publicApiKey, platform = 'web', batchTime, enableEvents, enableLogs, debug = false }) {
39
40
  __classPrivateFieldSet(this, _EventLog_enableEvents, enableEvents, "f");
40
41
  __classPrivateFieldSet(this, _EventLog_enableLogs, enableLogs, "f");
41
- api_1.api.init(publicApiKey, platform);
42
- cron_1.cron.init(batchTime);
42
+ __classPrivateFieldSet(this, _EventLog_api, new api_1.Api(publicApiKey, platform, debug), "f");
43
+ if (batchTime) {
44
+ new cron_1.Cron(batchTime, __classPrivateFieldGet(this, _EventLog_api, "f"));
45
+ }
43
46
  }
44
47
  event(eventArgs) {
45
48
  if (!__classPrivateFieldGet(this, _EventLog_enableEvents, "f")) {
@@ -47,6 +50,15 @@ class EventLog {
47
50
  }
48
51
  return database_1.database.setItem({ ...eventArgs, type: types_1.EventLogType.EVENT });
49
52
  }
53
+ sendBatch() {
54
+ database_1.database.getAllItems().then((items) => {
55
+ var _a, _b;
56
+ if (items.length) {
57
+ (_b = (_a = __classPrivateFieldGet(this, _EventLog_api, "f")) === null || _a === void 0 ? void 0 : _a.sendBatch(items)) === null || _b === void 0 ? void 0 : _b.then(() => database_1.database.reset());
58
+ }
59
+ });
60
+ }
50
61
  }
51
- _EventLog_enableEvents = new WeakMap(), _EventLog_enableLogs = new WeakMap();
62
+ exports.EventLog = EventLog;
63
+ _EventLog_enableEvents = new WeakMap(), _EventLog_enableLogs = new WeakMap(), _EventLog_api = new WeakMap();
52
64
  exports.eventLog = new EventLog();
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const index_1 = require("./index");
4
4
  const database_1 = require("./database");
5
- const cron_1 = require("./cron");
6
5
  const PUBLIC_API_KEY = 'staging_gAAAAABj-NC2WEf3XA4lHnN6RfGbjETf6qL27jlU7c1tm01ERV-Y-E6ddee1QPyV_CA8cxjVlGy-qDNA2-mqa9msQlTJit-Fmbvv6f2AZdfbFd_CUo1stvixnF4_8MzM_IYVWYz7KK86VfEBvxNpjRCyp7BDef-QzrT1-yqa8HAYTuA1GU3Hjjo=';
7
6
  const eventArgs = {
8
7
  source: 'test',
@@ -31,7 +30,6 @@ describe('EventLog', () => {
31
30
  });
32
31
  afterEach(async () => {
33
32
  await new Promise((resolve) => setTimeout(resolve, 1000));
34
- cron_1.cron.stop();
35
33
  });
36
34
  it('should add an event to the database', async () => {
37
35
  index_1.eventLog.event(eventArgs);
@@ -51,10 +49,12 @@ describe('EventLog', () => {
51
49
  expect(events).toHaveLength(0);
52
50
  });
53
51
  it('should log an error', async () => {
52
+ index_1.eventLog.logger.error(logArgs);
54
53
  index_1.eventLog.logger.error(logArgs);
55
54
  const events = await database_1.database.getAllItems();
56
- expect(events).toHaveLength(1);
55
+ expect(events).toHaveLength(2);
57
56
  expect(events[0]).toMatchObject(logArgs);
57
+ expect(events[1]).toMatchObject(logArgs);
58
58
  });
59
59
  it('should not log an error if logs are disabled', async () => {
60
60
  index_1.eventLog.init({
@@ -1,7 +1,8 @@
1
1
  export type InitArgs = {
2
2
  publicApiKey: string;
3
- batchTime: number;
3
+ batchTime?: number;
4
4
  enableEvents: boolean;
5
5
  enableLogs: boolean;
6
6
  platform?: string;
7
+ debug?: boolean;
7
8
  };
@@ -9,23 +9,23 @@ export declare enum Level {
9
9
  }
10
10
  type BaseEventLog = {
11
11
  type: EventLogType;
12
- customer_session?: string;
13
- checkout_session?: string;
12
+ customer_session?: string | null;
13
+ checkout_session?: string | null;
14
14
  original_created_at: string;
15
15
  };
16
16
  export type Log = BaseEventLog & {
17
17
  sdk_version: string;
18
- os?: string;
19
- url: string;
20
- method: string;
21
- headers?: object;
22
- request?: object;
23
- response?: object;
24
- status_code?: number;
25
- trace_id?: string;
26
- metadata?: unknown;
27
- country: string;
28
- organization_name?: string;
18
+ os?: string | null;
19
+ url?: string | null;
20
+ method?: string | null;
21
+ headers?: object | null;
22
+ request?: object | null;
23
+ response?: object | null;
24
+ status_code?: string | null;
25
+ trace_id?: string | null;
26
+ metadata?: unknown | null;
27
+ country?: string | null;
28
+ organization_name?: string | null;
29
29
  level: Level;
30
30
  stack_trace?: string;
31
31
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuno-payments/sdk-event-log",
3
- "version": "0.2.0",
3
+ "version": "0.3.0-beta.10",
4
4
  "description": "Register application logs and events",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",