@yuno-payments/sdk-event-log 0.3.0-beta.12 → 0.3.0-beta.8

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.
Files changed (49) hide show
  1. package/dist/cjs/api/__tests__/api.test.js +9 -5
  2. package/dist/cjs/api/api.d.ts +5 -3
  3. package/dist/cjs/api/api.js +10 -12
  4. package/dist/cjs/cron/cron.d.ts +5 -5
  5. package/dist/cjs/cron/cron.js +16 -26
  6. package/dist/cjs/cron/cron.test.js +11 -15
  7. package/dist/cjs/database/database.d.ts +2 -1
  8. package/dist/cjs/database/database.js +4 -3
  9. package/dist/cjs/database/database.test.js +2 -2
  10. package/dist/cjs/database/database.types.d.ts +2 -2
  11. package/dist/cjs/database/index.d.ts +0 -1
  12. package/dist/cjs/database/index.js +0 -1
  13. package/dist/cjs/database/local-storage/local-storage.d.ts +3 -3
  14. package/dist/cjs/database/local-storage/local-storage.js +9 -32
  15. package/dist/cjs/database/local-storage/local-storage.test.js +6 -7
  16. package/dist/cjs/database/memory-storage/memory-storage.d.ts +2 -4
  17. package/dist/cjs/database/memory-storage/memory-storage.js +5 -10
  18. package/dist/cjs/database/memory-storage/memory-storage.test.js +2 -2
  19. package/dist/cjs/index.d.ts +9 -7
  20. package/dist/cjs/index.js +21 -43
  21. package/dist/cjs/index.test.js +86 -146
  22. package/dist/cjs/index.types.d.ts +4 -4
  23. package/dist/cjs/index.utils.d.ts +1 -2
  24. package/dist/cjs/index.utils.js +3 -2
  25. package/dist/esm/api/__tests__/api.test.js +9 -5
  26. package/dist/esm/api/api.d.ts +5 -3
  27. package/dist/esm/api/api.js +10 -12
  28. package/dist/esm/cron/cron.d.ts +5 -5
  29. package/dist/esm/cron/cron.js +18 -17
  30. package/dist/esm/cron/cron.test.js +11 -15
  31. package/dist/esm/database/database.d.ts +2 -1
  32. package/dist/esm/database/database.js +4 -3
  33. package/dist/esm/database/database.test.js +2 -2
  34. package/dist/esm/database/database.types.d.ts +2 -2
  35. package/dist/esm/database/index.d.ts +0 -1
  36. package/dist/esm/database/index.js +0 -1
  37. package/dist/esm/database/local-storage/local-storage.d.ts +3 -3
  38. package/dist/esm/database/local-storage/local-storage.js +9 -32
  39. package/dist/esm/database/local-storage/local-storage.test.js +6 -7
  40. package/dist/esm/database/memory-storage/memory-storage.d.ts +2 -4
  41. package/dist/esm/database/memory-storage/memory-storage.js +5 -10
  42. package/dist/esm/database/memory-storage/memory-storage.test.js +2 -2
  43. package/dist/esm/index.d.ts +9 -7
  44. package/dist/esm/index.js +21 -32
  45. package/dist/esm/index.test.js +77 -137
  46. package/dist/esm/index.types.d.ts +4 -4
  47. package/dist/esm/index.utils.d.ts +1 -2
  48. package/dist/esm/index.utils.js +3 -2
  49. package/package.json +1 -2
@@ -16,13 +16,17 @@ const publicApiKey = PUBLIC_API_KEY;
16
16
  const platform = 'web';
17
17
  const STATUS_CREATED = 201;
18
18
  describe('Api', () => {
19
- let api;
20
19
  beforeEach(() => {
21
20
  // Reset the instance before each test
22
- api = new api_1.Api(platform, true);
21
+ api_1.api.init(publicApiKey, platform);
22
+ });
23
+ it('should thorw an error if the instance is not initialized well', () => {
24
+ expect(() => api_1.api.init('', '')).toThrowError();
23
25
  });
24
26
  it('should initialize the instance with the correct headers', () => {
25
- const instance = api.getInstances();
27
+ const instance = api_1.api.getInstances();
28
+ expect(instance === null || instance === void 0 ? void 0 : instance.defaults.baseURL).toBe('https://api-staging.y.uno/v1');
29
+ expect(instance === null || instance === void 0 ? void 0 : instance.defaults.headers['public-api-key']).toBe(publicApiKey);
26
30
  expect(instance === null || instance === void 0 ? void 0 : instance.defaults.headers['X-Platform']).toBe(platform);
27
31
  });
28
32
  it('should send a batch of events to the server', () => __awaiter(void 0, void 0, void 0, function* () {
@@ -35,11 +39,11 @@ describe('Api', () => {
35
39
  type: types_1.EventLogType.EVENT,
36
40
  },
37
41
  ];
38
- const response = yield api.sendBatch(batches, publicApiKey);
42
+ const response = yield api_1.api.sendBatch(batches);
39
43
  expect(response === null || response === void 0 ? void 0 : response.status).toBe(STATUS_CREATED);
40
44
  }));
41
45
  it('should return the instance', () => {
42
- const instance = api.getInstances();
46
+ const instance = api_1.api.getInstances();
43
47
  expect(instance).not.toBeNull();
44
48
  });
45
49
  });
@@ -1,8 +1,10 @@
1
1
  import { AxiosInstance } from 'axios';
2
2
  import { Batch } from '../types';
3
- export declare class Api {
3
+ declare class Api {
4
4
  #private;
5
- constructor(platform: string, debug?: boolean);
6
- sendBatch(batches: Batch[], publicApiKey: string): Promise<import("axios").AxiosResponse<any, any>> | undefined;
5
+ init(publicApiKey: string, platform: string, debug?: boolean): void;
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 {};
@@ -15,34 +15,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
15
15
  };
16
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(platform, debug = false) {
22
+ constructor() {
23
23
  _Api_instance.set(this, null);
24
24
  _Api_debug.set(this, false);
25
+ }
26
+ init(publicApiKey, platform, debug = false) {
25
27
  __classPrivateFieldSet(this, _Api_debug, debug, "f");
26
28
  __classPrivateFieldSet(this, _Api_instance, axios_1.default.create({
29
+ baseURL: (0, api_utils_1.getBaseUrl)(publicApiKey),
27
30
  // eslint-disable-next-line @typescript-eslint/naming-convention
28
- headers: { 'X-Platform': platform },
31
+ headers: { 'public-api-key': publicApiKey, 'X-Platform': platform },
29
32
  }), "f");
30
33
  }
31
- sendBatch(batches, publicApiKey) {
34
+ sendBatch(batches) {
32
35
  var _a;
33
- if (__classPrivateFieldGet(this, _Api_debug, "f")) {
36
+ if (!__classPrivateFieldGet(this, _Api_debug, "f")) {
34
37
  console.dir(batches, { depth: null });
35
- console.log(publicApiKey);
36
38
  }
37
- return (_a = __classPrivateFieldGet(this, _Api_instance, "f")) === null || _a === void 0 ? void 0 : _a.post('/sdk/event-log/publish', { batch: batches }, {
38
- baseURL: (0, api_utils_1.getBaseUrl)(publicApiKey),
39
- // eslint-disable-next-line @typescript-eslint/naming-convention
40
- headers: { 'public-api-key': publicApiKey }
41
- });
39
+ return (_a = __classPrivateFieldGet(this, _Api_instance, "f")) === null || _a === void 0 ? void 0 : _a.post('/sdk/event-log/publish', { batch: batches });
42
40
  }
43
41
  getInstances() {
44
42
  return __classPrivateFieldGet(this, _Api_instance, "f");
45
43
  }
46
44
  }
47
- exports.Api = Api;
48
45
  _Api_instance = new WeakMap(), _Api_debug = new WeakMap();
46
+ exports.api = new Api();
@@ -1,10 +1,10 @@
1
1
  /// <reference types="node" />
2
- import { Api } from '../api';
3
- import { Database } from '../database';
4
- export declare class Cron {
2
+ declare class Cron {
5
3
  #private;
6
- constructor(batchTime: number, api: Api, database: Database);
7
- sendBatch(): Promise<void>;
4
+ init(batchTime: number): void;
5
+ sendBatch(): void;
8
6
  stop(): void;
9
7
  getInterval(): NodeJS.Timeout | undefined;
10
8
  }
9
+ export declare const cron: Cron;
10
+ export {};
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
3
  if (kind === "m") throw new TypeError("Private method is not writable");
13
4
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
@@ -19,28 +10,27 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
19
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");
20
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
12
  };
22
- var _Cron_interval, _Cron_api, _Cron_database;
13
+ var _Cron_interval;
23
14
  Object.defineProperty(exports, "__esModule", { value: true });
24
- exports.Cron = void 0;
15
+ exports.cron = void 0;
16
+ const api_1 = require("../api");
17
+ const database_1 = require("../database");
25
18
  const NONE_ITEMS = 0;
26
19
  class Cron {
27
- constructor(batchTime, api, database) {
20
+ constructor() {
28
21
  _Cron_interval.set(this, void 0);
29
- _Cron_api.set(this, void 0);
30
- _Cron_database.set(this, void 0);
22
+ }
23
+ init(batchTime) {
31
24
  this.stop();
32
- __classPrivateFieldSet(this, _Cron_api, api, "f");
33
- __classPrivateFieldSet(this, _Cron_database, database, "f");
34
- __classPrivateFieldSet(this, _Cron_interval, setInterval(this.sendBatch.bind(this), batchTime), "f");
25
+ __classPrivateFieldSet(this, _Cron_interval, setInterval(this.sendBatch, batchTime), "f");
26
+ console.log('cron started');
35
27
  }
36
28
  sendBatch() {
37
- return __awaiter(this, void 0, void 0, function* () {
38
- const items = yield __classPrivateFieldGet(this, _Cron_database, "f").getAllItems();
39
- const keys = Object.keys(items);
40
- if (keys.length > NONE_ITEMS) {
41
- for (const key of keys) {
42
- __classPrivateFieldGet(this, _Cron_api, "f").sendBatch(items[key], key);
43
- }
29
+ database_1.database.getAllItems().then((items) => {
30
+ var _a;
31
+ console.log('items', items);
32
+ if (items.length > NONE_ITEMS) {
33
+ (_a = api_1.api.sendBatch(items)) === null || _a === void 0 ? void 0 : _a.then(() => database_1.database.reset());
44
34
  }
45
35
  });
46
36
  }
@@ -54,5 +44,5 @@ class Cron {
54
44
  return __classPrivateFieldGet(this, _Cron_interval, "f");
55
45
  }
56
46
  }
57
- exports.Cron = Cron;
58
- _Cron_interval = new WeakMap(), _Cron_api = new WeakMap(), _Cron_database = new WeakMap();
47
+ _Cron_interval = new WeakMap();
48
+ exports.cron = new Cron();
@@ -16,17 +16,11 @@ const types_1 = require("../types");
16
16
  const TIME_INTERVAL = 1000;
17
17
  describe('Cron', () => {
18
18
  let spy;
19
- let cron;
20
- let api;
21
- let database;
22
19
  beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
23
20
  // restore the spy created with spyOn
24
21
  jest.restoreAllMocks();
25
- api = new api_1.Api('web', true);
26
- database = (0, database_1.databaseFactory)('testOrganization');
27
- cron = new cron_1.Cron(TIME_INTERVAL, api, database);
28
- spy = jest.spyOn(api, 'sendBatch');
29
- yield database.reset();
22
+ spy = jest.spyOn(api_1.api, 'sendBatch');
23
+ yield database_1.database.reset();
30
24
  }));
31
25
  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* () {
32
26
  const batch = {
@@ -36,21 +30,23 @@ describe('Cron', () => {
36
30
  original_created_at: new Date().toISOString(),
37
31
  type: types_1.EventLogType.EVENT,
38
32
  };
39
- database.setItem(batch, 'sandbox_1');
40
- yield cron.sendBatch();
41
- expect(spy).toHaveBeenCalledWith([batch], 'sandbox_1');
33
+ database_1.database.setItem(batch);
34
+ yield cron_1.cron.sendBatch();
35
+ expect(spy).toHaveBeenCalledWith([batch]);
42
36
  }));
43
37
  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* () {
44
- yield cron.sendBatch();
38
+ yield cron_1.cron.sendBatch();
45
39
  expect(spy).not.toHaveBeenCalled();
46
40
  }));
47
41
  it('should stop the interval when stop is called', () => {
48
42
  jest.useFakeTimers();
49
- cron.stop();
50
- expect(cron.getInterval()).toBeUndefined();
43
+ cron_1.cron.init(TIME_INTERVAL);
44
+ cron_1.cron.stop();
45
+ expect(cron_1.cron.getInterval()).toBeUndefined();
51
46
  });
52
47
  it('should start the interval when init is called', () => {
53
48
  jest.useFakeTimers();
54
- expect(cron.getInterval()).toBeDefined();
49
+ cron_1.cron.init(TIME_INTERVAL);
50
+ expect(cron_1.cron.getInterval()).toBeDefined();
55
51
  });
56
52
  });
@@ -1,2 +1,3 @@
1
1
  import { Database } from './database.types';
2
- export declare function databaseFactory(organizationName: string): Database;
2
+ export declare function databaseFactory(): Database;
3
+ export declare const database: Database;
@@ -1,15 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.databaseFactory = void 0;
3
+ exports.database = exports.databaseFactory = void 0;
4
4
  const local_storage_1 = require("./local-storage");
5
5
  const memory_storage_1 = require("./memory-storage");
6
- function databaseFactory(organizationName) {
6
+ function databaseFactory() {
7
7
  const databaseType = isLocalStorageEnabled()
8
8
  ? 'local-storage'
9
9
  : 'memory-storage';
10
10
  switch (databaseType) {
11
11
  case 'local-storage':
12
- return new local_storage_1.LocalStorage(organizationName);
12
+ return new local_storage_1.LocalStorage();
13
13
  case 'memory-storage':
14
14
  return new memory_storage_1.MemoryStorage();
15
15
  default:
@@ -28,3 +28,4 @@ function isLocalStorageEnabled() {
28
28
  return false;
29
29
  }
30
30
  }
31
+ exports.database = databaseFactory();
@@ -11,7 +11,7 @@ describe('databaseFactory', () => {
11
11
  it('should return a LocalStorage instance if local storage is enabled', () => {
12
12
  jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => { });
13
13
  jest.spyOn(Storage.prototype, 'removeItem').mockImplementation(() => { });
14
- const result = (0, database_1.databaseFactory)('testOrganization');
14
+ const result = (0, database_1.databaseFactory)();
15
15
  expect(result).toBeInstanceOf(local_storage_1.LocalStorage);
16
16
  });
17
17
  it('should return a MemoryStorage instance if local storage is not enabled', () => {
@@ -19,7 +19,7 @@ describe('databaseFactory', () => {
19
19
  throw new Error('Local storage not available');
20
20
  });
21
21
  jest.spyOn(Storage.prototype, 'removeItem').mockImplementation(() => { });
22
- const result = (0, database_1.databaseFactory)('testOrganization');
22
+ const result = (0, database_1.databaseFactory)();
23
23
  expect(result).toBeInstanceOf(memory_storage_1.MemoryStorage);
24
24
  });
25
25
  });
@@ -1,8 +1,8 @@
1
1
  import { Batch } from '../types';
2
2
  export interface Database {
3
- setItem(item: Batch, publicApiKey: string): Promise<void>;
3
+ setItem(item: Batch): Promise<void>;
4
4
  getItem(id: string): Promise<Batch>;
5
5
  removeItem(id: string): Promise<void>;
6
- getAllItems(): Promise<Record<string, Batch[]>>;
6
+ getAllItems(): Promise<Batch[]>;
7
7
  reset(): Promise<void>;
8
8
  }
@@ -1,2 +1 @@
1
1
  export * from './database';
2
- export * from './database.types';
@@ -15,4 +15,3 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./database"), exports);
18
- __exportStar(require("./database.types"), exports);
@@ -2,10 +2,10 @@ import { Batch } from '../../types';
2
2
  import { Database } from '../database.types';
3
3
  export declare class LocalStorage implements Database {
4
4
  #private;
5
- constructor(organizationName: string);
5
+ constructor();
6
6
  reset(): Promise<void>;
7
- setItem(item: Batch, publicApiKey: string): Promise<void>;
7
+ setItem(item: Batch): Promise<void>;
8
8
  getItem(id: string): Promise<Batch>;
9
9
  removeItem(id: string): Promise<void>;
10
- getAllItems(): Promise<Record<string, Batch[]>>;
10
+ getAllItems(): Promise<Batch[]>;
11
11
  }
@@ -19,36 +19,29 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
19
19
  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");
20
20
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
21
  };
22
- var _LocalStorage_instances, _LocalStorage_localStorage, _LocalStorage_databaseName, _LocalStorage_getDatabase, _LocalStorage_stringify;
22
+ var _LocalStorage_instances, _LocalStorage_localStorage, _LocalStorage_databaseName, _LocalStorage_getDatabase;
23
23
  Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.LocalStorage = void 0;
25
- const DATABASE_NAME_SPACE = 'YUNO_EVENT_LOGS';
26
- const DO_NOT_EXIST = -1;
27
25
  class LocalStorage {
28
- constructor(organizationName) {
26
+ constructor() {
29
27
  _LocalStorage_instances.add(this);
30
28
  _LocalStorage_localStorage.set(this, void 0);
31
- _LocalStorage_databaseName.set(this, void 0);
29
+ _LocalStorage_databaseName.set(this, 'YUNO_EVENT_LOGS');
32
30
  __classPrivateFieldSet(this, _LocalStorage_localStorage, window.localStorage, "f");
33
- __classPrivateFieldSet(this, _LocalStorage_databaseName, `${DATABASE_NAME_SPACE}_${organizationName}`, "f");
34
31
  const database = __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").getItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"));
35
32
  if (database === null || database === undefined) {
36
- this.reset();
33
+ __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), JSON.stringify([]));
37
34
  }
38
35
  }
39
36
  reset() {
40
37
  return __awaiter(this, void 0, void 0, function* () {
41
- __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), JSON.stringify({}));
38
+ __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), JSON.stringify([]));
42
39
  });
43
40
  }
44
- setItem(item, publicApiKey) {
41
+ setItem(item) {
45
42
  return __awaiter(this, void 0, void 0, function* () {
46
43
  const database = __classPrivateFieldGet(this, _LocalStorage_instances, "m", _LocalStorage_getDatabase).call(this);
47
- if (database[publicApiKey] === undefined) {
48
- database[publicApiKey] = [];
49
- }
50
- database[publicApiKey].push(item);
51
- __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), __classPrivateFieldGet(this, _LocalStorage_instances, "m", _LocalStorage_stringify).call(this, database));
44
+ __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), JSON.stringify([...database, item]));
52
45
  });
53
46
  }
54
47
  getItem(id) {
@@ -58,31 +51,15 @@ class LocalStorage {
58
51
  throw new Error('Method not implemented.' + id);
59
52
  }
60
53
  getAllItems() {
61
- const database = __classPrivateFieldGet(this, _LocalStorage_instances, "m", _LocalStorage_getDatabase).call(this);
62
- this.reset();
63
- return Promise.resolve(database);
54
+ return Promise.resolve(__classPrivateFieldGet(this, _LocalStorage_instances, "m", _LocalStorage_getDatabase).call(this));
64
55
  }
65
56
  }
66
57
  exports.LocalStorage = LocalStorage;
67
58
  _LocalStorage_localStorage = new WeakMap(), _LocalStorage_databaseName = new WeakMap(), _LocalStorage_instances = new WeakSet(), _LocalStorage_getDatabase = function _LocalStorage_getDatabase() {
68
59
  let database = __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").getItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"));
69
60
  if (database === null || database === undefined) {
70
- database = JSON.stringify({});
61
+ database = JSON.stringify([]);
71
62
  __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), database);
72
63
  }
73
64
  return JSON.parse(database);
74
- }, _LocalStorage_stringify = function _LocalStorage_stringify(obj) {
75
- const cache = [];
76
- const str = JSON.stringify(obj, function (key, value) {
77
- if (typeof value === 'object' && value !== null) {
78
- if (cache.indexOf(value) !== DO_NOT_EXIST) {
79
- // Circular reference found, discard key
80
- return;
81
- }
82
- // Store value in our collection
83
- cache.push(value);
84
- }
85
- return value;
86
- });
87
- return str;
88
65
  };
@@ -21,20 +21,19 @@ const batch = {
21
21
  describe('LocalStorageDatabase', () => {
22
22
  let localStorageDatabase;
23
23
  beforeEach(() => {
24
- localStorageDatabase = new local_storage_1.LocalStorage('test');
24
+ localStorageDatabase = new local_storage_1.LocalStorage();
25
25
  });
26
26
  afterEach(() => {
27
27
  localStorage.clear();
28
28
  });
29
29
  it('should set an item in the database', () => __awaiter(void 0, void 0, void 0, function* () {
30
- const testKey = 'testKey';
31
- yield localStorageDatabase.setItem(batch, testKey);
32
- const database = JSON.parse(localStorage.getItem('YUNO_EVENT_LOGS_test') || '[]');
33
- expect(database).toEqual({ [testKey]: [batch] });
30
+ yield localStorageDatabase.setItem(batch);
31
+ const database = JSON.parse(localStorage.getItem('YUNO_EVENT_LOGS') || '[]');
32
+ expect(database).toContainEqual(batch);
34
33
  }));
35
34
  it('should get an item from the database', () => __awaiter(void 0, void 0, void 0, function* () {
36
- localStorage.setItem('YUNO_EVENT_LOGS_test', JSON.stringify({ testKey: [batch] }));
35
+ localStorage.setItem('YUNO_EVENT_LOGS', JSON.stringify([batch]));
37
36
  const result = yield localStorageDatabase.getAllItems();
38
- expect(result).toEqual({ testKey: [batch] });
37
+ expect(result).toEqual([batch]);
39
38
  }));
40
39
  });
@@ -4,10 +4,8 @@ export declare class MemoryStorage implements Database {
4
4
  #private;
5
5
  constructor();
6
6
  reset(): Promise<void>;
7
- setItem(item: Batch, publicApiKey: string): Promise<void>;
7
+ setItem(item: Batch): Promise<void>;
8
8
  getItem(id: string): Promise<Batch>;
9
9
  removeItem(id: string): Promise<void>;
10
- getAllItems(): Promise<{
11
- [x: string]: Batch[];
12
- }>;
10
+ getAllItems(): Promise<Batch[]>;
13
11
  }
@@ -25,19 +25,16 @@ exports.MemoryStorage = void 0;
25
25
  class MemoryStorage {
26
26
  constructor() {
27
27
  _MemoryStorage_items.set(this, void 0);
28
- __classPrivateFieldSet(this, _MemoryStorage_items, {}, "f");
28
+ __classPrivateFieldSet(this, _MemoryStorage_items, [], "f");
29
29
  }
30
30
  reset() {
31
31
  return __awaiter(this, void 0, void 0, function* () {
32
- __classPrivateFieldSet(this, _MemoryStorage_items, {}, "f");
32
+ __classPrivateFieldSet(this, _MemoryStorage_items, [], "f");
33
33
  });
34
34
  }
35
- setItem(item, publicApiKey) {
35
+ setItem(item) {
36
36
  return __awaiter(this, void 0, void 0, function* () {
37
- if (__classPrivateFieldGet(this, _MemoryStorage_items, "f")[publicApiKey] === undefined) {
38
- __classPrivateFieldGet(this, _MemoryStorage_items, "f")[publicApiKey] = [];
39
- }
40
- __classPrivateFieldGet(this, _MemoryStorage_items, "f")[publicApiKey].push(item);
37
+ __classPrivateFieldGet(this, _MemoryStorage_items, "f").push(item);
41
38
  });
42
39
  }
43
40
  getItem(id) {
@@ -47,9 +44,7 @@ class MemoryStorage {
47
44
  throw new Error('Method not implemented.' + id);
48
45
  }
49
46
  getAllItems() {
50
- const items = Object.assign({}, __classPrivateFieldGet(this, _MemoryStorage_items, "f"));
51
- this.reset();
52
- return Promise.resolve(items);
47
+ return Promise.resolve(__classPrivateFieldGet(this, _MemoryStorage_items, "f"));
53
48
  }
54
49
  }
55
50
  exports.MemoryStorage = MemoryStorage;
@@ -27,8 +27,8 @@ describe('MemoryStorage', () => {
27
27
  memoryStorage.reset();
28
28
  });
29
29
  it('should set an item in the database', () => __awaiter(void 0, void 0, void 0, function* () {
30
- yield memoryStorage.setItem(batch, 'testKey');
30
+ yield memoryStorage.setItem(batch);
31
31
  const database = yield memoryStorage.getAllItems();
32
- expect(database).toEqual({ testKey: [batch] });
32
+ expect(database).toEqual([batch]);
33
33
  }));
34
34
  });
@@ -1,13 +1,15 @@
1
1
  import { InitArgs } from './index.types';
2
+ import './database';
2
3
  import { Event, Log } from './types';
3
- export declare class EventLog {
4
+ declare class EventLog {
4
5
  #private;
5
- constructor({ organizationName, platform, batchTime, enableEvents, enableLogs, debug, }: InitArgs);
6
- event(eventArgs: Omit<Event, 'type'>, publicApiKey: string): Promise<void> | undefined;
6
+ init({ publicApiKey, platform, batchTime, enableEvents, enableLogs, debug }: InitArgs): void;
7
+ event(eventArgs: Omit<Event, 'type'>): Promise<void> | undefined;
7
8
  logger: {
8
- error: (errorArgs: Omit<Log, 'level' | 'type'>, publicApiKey: string) => Promise<void> | undefined;
9
- debug: (debugArgs: Omit<Log, 'level' | 'type'>, publicApiKey: string) => Promise<void> | undefined;
10
- info: (infoArgs: Omit<Log, 'level' | 'type'>, publicApiKey: string) => Promise<void> | undefined;
9
+ error: (errorArgs: Omit<Log, 'level' | 'type'>) => Promise<void> | undefined;
10
+ debug: (debugArgs: Omit<Log, 'level' | 'type'>) => Promise<void> | undefined;
11
+ info: (infoArgs: Omit<Log, 'level' | 'type'>) => Promise<void> | undefined;
11
12
  };
12
- sendBatch(): Promise<void>;
13
13
  }
14
+ export declare const eventLog: EventLog;
15
+ export {};
package/dist/cjs/index.js CHANGED
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
3
  if (kind === "m") throw new TypeError("Private method is not writable");
13
4
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
@@ -19,56 +10,43 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
19
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");
20
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
12
  };
22
- var _EventLog_enableEvents, _EventLog_enableLogs, _EventLog_api, _EventLog_database;
13
+ var _EventLog_enableEvents, _EventLog_enableLogs;
23
14
  Object.defineProperty(exports, "__esModule", { value: true });
24
- exports.EventLog = void 0;
15
+ exports.eventLog = void 0;
25
16
  const api_1 = require("./api");
17
+ require("./database");
26
18
  const cron_1 = require("./cron");
27
19
  const types_1 = require("./types");
28
20
  const database_1 = require("./database");
29
21
  const index_utils_1 = require("./index.utils");
30
22
  class EventLog {
31
- constructor({ organizationName, platform = 'web', batchTime, enableEvents = true, enableLogs = true, debug = false, }) {
32
- _EventLog_enableEvents.set(this, void 0);
33
- _EventLog_enableLogs.set(this, void 0);
34
- _EventLog_api.set(this, void 0);
35
- _EventLog_database.set(this, void 0);
23
+ constructor() {
24
+ _EventLog_enableEvents.set(this, false);
25
+ _EventLog_enableLogs.set(this, false);
36
26
  this.logger = {
37
- error: (errorArgs, publicApiKey) => {
38
- return (0, index_utils_1.setLog)(errorArgs, types_1.Level.ERROR, __classPrivateFieldGet(this, _EventLog_enableLogs, "f"), __classPrivateFieldGet(this, _EventLog_database, "f"), publicApiKey);
39
- },
40
- debug: (debugArgs, publicApiKey) => {
41
- return (0, index_utils_1.setLog)(debugArgs, types_1.Level.DEBUG, __classPrivateFieldGet(this, _EventLog_enableLogs, "f"), __classPrivateFieldGet(this, _EventLog_database, "f"), publicApiKey);
27
+ error: (errorArgs) => {
28
+ return (0, index_utils_1.setLog)(errorArgs, types_1.Level.ERROR, __classPrivateFieldGet(this, _EventLog_enableLogs, "f"));
42
29
  },
43
- info: (infoArgs, publicApiKey) => {
44
- return (0, index_utils_1.setLog)(infoArgs, types_1.Level.INFO, __classPrivateFieldGet(this, _EventLog_enableLogs, "f"), __classPrivateFieldGet(this, _EventLog_database, "f"), publicApiKey);
30
+ debug: (debugArgs) => {
31
+ return (0, index_utils_1.setLog)(debugArgs, types_1.Level.DEBUG, __classPrivateFieldGet(this, _EventLog_enableLogs, "f"));
45
32
  },
33
+ info: (infoArgs) => {
34
+ return (0, index_utils_1.setLog)(infoArgs, types_1.Level.INFO, __classPrivateFieldGet(this, _EventLog_enableLogs, "f"));
35
+ }
46
36
  };
37
+ }
38
+ init({ publicApiKey, platform = 'web', batchTime, enableEvents, enableLogs, debug = false }) {
47
39
  __classPrivateFieldSet(this, _EventLog_enableEvents, enableEvents, "f");
48
40
  __classPrivateFieldSet(this, _EventLog_enableLogs, enableLogs, "f");
49
- __classPrivateFieldSet(this, _EventLog_api, new api_1.Api(platform, debug), "f");
50
- __classPrivateFieldSet(this, _EventLog_database, (0, database_1.databaseFactory)(organizationName), "f");
51
- if (batchTime) {
52
- new cron_1.Cron(batchTime, __classPrivateFieldGet(this, _EventLog_api, "f"), __classPrivateFieldGet(this, _EventLog_database, "f"));
53
- }
41
+ api_1.api.init(publicApiKey, platform, debug);
42
+ cron_1.cron.init(batchTime);
54
43
  }
55
- event(eventArgs, publicApiKey) {
44
+ event(eventArgs) {
56
45
  if (!__classPrivateFieldGet(this, _EventLog_enableEvents, "f")) {
57
46
  return;
58
47
  }
59
- return __classPrivateFieldGet(this, _EventLog_database, "f").setItem(Object.assign(Object.assign({}, eventArgs), { type: types_1.EventLogType.EVENT }), publicApiKey);
60
- }
61
- sendBatch() {
62
- return __awaiter(this, void 0, void 0, function* () {
63
- const items = yield __classPrivateFieldGet(this, _EventLog_database, "f").getAllItems();
64
- const keys = Object.keys(items);
65
- if (keys.length) {
66
- for (const key of keys) {
67
- __classPrivateFieldGet(this, _EventLog_api, "f").sendBatch(items[key], key);
68
- }
69
- }
70
- });
48
+ return database_1.database.setItem(Object.assign(Object.assign({}, eventArgs), { type: types_1.EventLogType.EVENT }));
71
49
  }
72
50
  }
73
- exports.EventLog = EventLog;
74
- _EventLog_enableEvents = new WeakMap(), _EventLog_enableLogs = new WeakMap(), _EventLog_api = new WeakMap(), _EventLog_database = new WeakMap();
51
+ _EventLog_enableEvents = new WeakMap(), _EventLog_enableLogs = new WeakMap();
52
+ exports.eventLog = new EventLog();