@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
@@ -10,9 +10,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const index_1 = require("./index");
13
- const types_1 = require("./types");
14
- const node_1 = require("msw/node");
15
- const msw_1 = require("msw");
13
+ const database_1 = require("./database");
14
+ const cron_1 = require("./cron");
15
+ const PUBLIC_API_KEY = 'staging_gAAAAABj-NC2WEf3XA4lHnN6RfGbjETf6qL27jlU7c1tm01ERV-Y-E6ddee1QPyV_CA8cxjVlGy-qDNA2-mqa9msQlTJit-Fmbvv6f2AZdfbFd_CUo1stvixnF4_8MzM_IYVWYz7KK86VfEBvxNpjRCyp7BDef-QzrT1-yqa8HAYTuA1GU3Hjjo=';
16
16
  const eventArgs = {
17
17
  source: 'test',
18
18
  event: 'testEvent',
@@ -24,152 +24,92 @@ const logArgs = {
24
24
  url: '',
25
25
  method: '',
26
26
  country: '',
27
- original_created_at: new Date().toISOString(),
27
+ original_created_at: new Date().toISOString()
28
28
  };
29
- const publicApiKey = 'prod_1';
30
- let mockInterceptRequest = jest.fn();
31
- const server = (0, node_1.setupServer)(
32
- // Describe the requests to mock.
33
- msw_1.rest.post('https://api.y.uno/v1/sdk/event-log/publish', (req, res, ctx) => {
34
- mockInterceptRequest(req.body);
35
- return res(ctx.status(200));
36
- }));
37
29
  describe('EventLog', () => {
38
- let eventLog;
39
- beforeAll(() => {
40
- mockInterceptRequest = jest.fn();
41
- server.listen();
42
- });
43
- // Reset any request handlers that we may add during the tests,
44
- // so they don't affect other tests.
45
- afterEach(() => server.resetHandlers());
46
- // Clean up after the tests are finished.
47
- afterAll(() => server.close());
48
30
  beforeEach(() => {
49
- eventLog = new index_1.EventLog({
50
- organizationName: 'testOrg',
51
- debug: false,
31
+ // Reset the database before each test
32
+ // to ensure a clean slate
33
+ index_1.eventLog.init({
34
+ publicApiKey: PUBLIC_API_KEY,
35
+ batchTime: 500,
36
+ enableEvents: true,
37
+ enableLogs: true,
52
38
  });
39
+ database_1.database.reset();
53
40
  });
54
- afterEach(() => {
55
- jest.clearAllMocks();
56
- });
57
- describe('event', () => {
58
- it('should add an event to the database if events are enabled', () => __awaiter(void 0, void 0, void 0, function* () {
59
- eventLog.event(eventArgs, publicApiKey);
60
- eventLog.sendBatch();
61
- yield new Promise(process.nextTick);
62
- expect(mockInterceptRequest).toHaveBeenCalledWith({
63
- batch: [Object.assign(Object.assign({}, eventArgs), { type: types_1.EventLogType.EVENT })],
64
- });
65
- }));
66
- it('should not add an event to the database if events are disabled', () => __awaiter(void 0, void 0, void 0, function* () {
67
- const publicApiKey = 'testApiKey';
68
- eventLog = new index_1.EventLog({
69
- organizationName: 'testOrg',
70
- enableEvents: false,
71
- });
72
- eventLog.event(eventArgs, publicApiKey);
73
- eventLog.sendBatch();
74
- yield new Promise(process.nextTick);
75
- expect(mockInterceptRequest).not.toHaveBeenCalled();
76
- }));
77
- });
78
- describe('logger', () => {
79
- it('should add an error log to the database if logs are enabled', () => __awaiter(void 0, void 0, void 0, function* () {
80
- eventLog.logger.error(logArgs, publicApiKey);
81
- eventLog.sendBatch();
82
- yield new Promise(process.nextTick);
83
- expect(mockInterceptRequest).toHaveBeenCalledWith({
84
- batch: [Object.assign(Object.assign({}, logArgs), { type: types_1.EventLogType.LOG, level: types_1.Level.ERROR })],
85
- });
86
- }));
87
- it('should not add an error log to the database if logs are disabled', () => __awaiter(void 0, void 0, void 0, function* () {
88
- const publicApiKey = 'testApiKey';
89
- eventLog = new index_1.EventLog({
90
- organizationName: 'testOrg',
91
- enableEvents: true,
92
- enableLogs: false,
93
- debug: false,
94
- });
95
- eventLog.logger.error(logArgs, publicApiKey);
96
- eventLog.sendBatch();
97
- yield new Promise(process.nextTick);
98
- expect(mockInterceptRequest).not.toHaveBeenCalled();
99
- }));
100
- it('should add a debug log to the database if logs are enabled', () => __awaiter(void 0, void 0, void 0, function* () {
101
- eventLog.logger.debug(logArgs, publicApiKey);
102
- eventLog.sendBatch();
103
- yield new Promise(process.nextTick);
104
- expect(mockInterceptRequest).toHaveBeenCalledWith({
105
- batch: [Object.assign(Object.assign({}, logArgs), { type: types_1.EventLogType.LOG, level: types_1.Level.DEBUG })],
106
- });
107
- }));
108
- it('should not add a debug log to the database if logs are disabled', () => __awaiter(void 0, void 0, void 0, function* () {
109
- const publicApiKey = 'testApiKey';
110
- eventLog = new index_1.EventLog({
111
- organizationName: 'testOrg',
112
- platform: 'web',
113
- enableEvents: true,
114
- enableLogs: false,
115
- debug: false,
116
- });
117
- eventLog.logger.debug(logArgs, publicApiKey);
118
- eventLog.sendBatch();
119
- yield new Promise(process.nextTick);
120
- expect(mockInterceptRequest).not.toHaveBeenCalled();
121
- }));
122
- it('should add an info log to the database if logs are enabled', () => __awaiter(void 0, void 0, void 0, function* () {
123
- eventLog.logger.info(logArgs, publicApiKey);
124
- eventLog.sendBatch();
125
- yield new Promise(process.nextTick);
126
- expect(mockInterceptRequest).toHaveBeenCalledWith({
127
- batch: [Object.assign(Object.assign({}, logArgs), { type: types_1.EventLogType.LOG, level: types_1.Level.INFO })],
128
- });
129
- }));
130
- it('should not add an info log to the database if logs are disabled', () => __awaiter(void 0, void 0, void 0, function* () {
131
- eventLog = new index_1.EventLog({
132
- organizationName: 'testOrg',
133
- platform: 'web',
134
- enableEvents: true,
135
- enableLogs: false,
136
- debug: false,
137
- });
138
- eventLog.sendBatch();
139
- yield new Promise(process.nextTick);
140
- expect(mockInterceptRequest).not.toHaveBeenCalled();
141
- }));
142
- });
143
- describe('sendBatch', () => {
144
- 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* () {
145
- yield eventLog.sendBatch();
146
- yield new Promise(process.nextTick);
147
- expect(mockInterceptRequest).not.toHaveBeenCalled();
148
- }));
149
- });
150
- describe('Cron', () => {
151
- it('should send a batch of items to the API', () => __awaiter(void 0, void 0, void 0, function* () {
152
- eventLog = new index_1.EventLog({
153
- organizationName: 'testOrg',
154
- platform: 'web',
155
- batchTime: 1000,
156
- enableEvents: true,
157
- enableLogs: true,
158
- debug: false,
159
- });
160
- yield eventLog.event(eventArgs, publicApiKey);
161
- yield eventLog.logger.error(logArgs, publicApiKey);
162
- yield eventLog.logger.debug(logArgs, publicApiKey);
163
- yield eventLog.logger.info(logArgs, publicApiKey);
164
- yield new Promise((resolve) => setTimeout(resolve, 1200));
165
- expect(mockInterceptRequest).toHaveBeenCalledWith({
166
- batch: [
167
- Object.assign(Object.assign({}, eventArgs), { type: types_1.EventLogType.EVENT }),
168
- Object.assign(Object.assign({}, logArgs), { type: types_1.EventLogType.LOG, level: types_1.Level.ERROR }),
169
- Object.assign(Object.assign({}, logArgs), { type: types_1.EventLogType.LOG, level: types_1.Level.DEBUG }),
170
- Object.assign(Object.assign({}, logArgs), { type: types_1.EventLogType.LOG, level: types_1.Level.INFO }),
171
- ],
172
- });
173
- }));
174
- });
41
+ afterEach(() => __awaiter(void 0, void 0, void 0, function* () {
42
+ yield new Promise((resolve) => setTimeout(resolve, 1000));
43
+ cron_1.cron.stop();
44
+ }));
45
+ it('should add an event to the database', () => __awaiter(void 0, void 0, void 0, function* () {
46
+ index_1.eventLog.event(eventArgs);
47
+ const events = yield database_1.database.getAllItems();
48
+ expect(events).toHaveLength(1);
49
+ expect(events[0]).toMatchObject(eventArgs);
50
+ }));
51
+ it('should not add an event to the database if events are disabled', () => __awaiter(void 0, void 0, void 0, function* () {
52
+ index_1.eventLog.init({
53
+ publicApiKey: PUBLIC_API_KEY,
54
+ batchTime: 1000,
55
+ enableEvents: false,
56
+ enableLogs: true,
57
+ });
58
+ index_1.eventLog.event(eventArgs);
59
+ const events = yield database_1.database.getAllItems();
60
+ expect(events).toHaveLength(0);
61
+ }));
62
+ it('should log an error', () => __awaiter(void 0, void 0, void 0, function* () {
63
+ index_1.eventLog.logger.error(logArgs);
64
+ index_1.eventLog.logger.error(logArgs);
65
+ const events = yield database_1.database.getAllItems();
66
+ expect(events).toHaveLength(2);
67
+ expect(events[0]).toMatchObject(logArgs);
68
+ expect(events[1]).toMatchObject(logArgs);
69
+ }));
70
+ it('should not log an error if logs are disabled', () => __awaiter(void 0, void 0, void 0, function* () {
71
+ index_1.eventLog.init({
72
+ publicApiKey: PUBLIC_API_KEY,
73
+ batchTime: 1000,
74
+ enableEvents: false,
75
+ enableLogs: false,
76
+ });
77
+ index_1.eventLog.logger.error(logArgs);
78
+ const events = yield database_1.database.getAllItems();
79
+ expect(events).toHaveLength(0);
80
+ }));
81
+ it('should log a debug message', () => __awaiter(void 0, void 0, void 0, function* () {
82
+ index_1.eventLog.logger.debug(logArgs);
83
+ const events = yield database_1.database.getAllItems();
84
+ expect(events).toHaveLength(1);
85
+ expect(events[0]).toMatchObject(logArgs);
86
+ }));
87
+ it('should not log a debug message if logs are disabled', () => __awaiter(void 0, void 0, void 0, function* () {
88
+ index_1.eventLog.init({
89
+ publicApiKey: PUBLIC_API_KEY,
90
+ batchTime: 1000,
91
+ enableEvents: false,
92
+ enableLogs: false,
93
+ });
94
+ index_1.eventLog.logger.debug(logArgs);
95
+ const events = yield database_1.database.getAllItems();
96
+ expect(events).toHaveLength(0);
97
+ }));
98
+ it('should log an info message', () => __awaiter(void 0, void 0, void 0, function* () {
99
+ index_1.eventLog.logger.info(logArgs);
100
+ const events = yield database_1.database.getAllItems();
101
+ expect(events).toHaveLength(1);
102
+ expect(events[0]).toMatchObject(logArgs);
103
+ }));
104
+ it('should not log an info message if logs are disabled', () => __awaiter(void 0, void 0, void 0, function* () {
105
+ index_1.eventLog.init({
106
+ publicApiKey: PUBLIC_API_KEY,
107
+ batchTime: 1000,
108
+ enableEvents: false,
109
+ enableLogs: false,
110
+ });
111
+ index_1.eventLog.logger.info(logArgs);
112
+ const events = yield database_1.database.getAllItems();
113
+ expect(events).toHaveLength(0);
114
+ }));
175
115
  });
@@ -1,8 +1,8 @@
1
1
  export type InitArgs = {
2
- organizationName: string;
3
- batchTime?: number;
4
- enableEvents?: boolean;
5
- enableLogs?: boolean;
2
+ publicApiKey: string;
3
+ batchTime: number;
4
+ enableEvents: boolean;
5
+ enableLogs: boolean;
6
6
  platform?: string;
7
7
  debug?: boolean;
8
8
  };
@@ -1,3 +1,2 @@
1
- import { Database } from './database';
2
1
  import { Level, Log } from './types';
3
- export declare function setLog(logArgs: Omit<Log, 'level' | 'type'>, level: Level, enableLog: boolean, database: Database, publicApiKey: string): Promise<void> | undefined;
2
+ export declare function setLog(logArgs: Omit<Log, 'level' | 'type'>, level: Level, enableLog: boolean): Promise<void> | undefined;
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setLog = void 0;
4
+ const database_1 = require("./database");
4
5
  const types_1 = require("./types");
5
- function setLog(logArgs, level, enableLog, database, publicApiKey) {
6
+ function setLog(logArgs, level, enableLog) {
6
7
  if (!enableLog) {
7
8
  return;
8
9
  }
9
- return database.setItem(Object.assign(Object.assign({}, logArgs), { level, type: types_1.EventLogType.LOG }), publicApiKey);
10
+ return database_1.database.setItem(Object.assign(Object.assign({}, logArgs), { level, type: types_1.EventLogType.LOG }));
10
11
  }
11
12
  exports.setLog = setLog;
@@ -7,13 +7,17 @@ const publicApiKey = PUBLIC_API_KEY;
7
7
  const platform = 'web';
8
8
  const STATUS_CREATED = 201;
9
9
  describe('Api', () => {
10
- let api;
11
10
  beforeEach(() => {
12
11
  // Reset the instance before each test
13
- api = new api_1.Api(platform, true);
12
+ api_1.api.init(publicApiKey, platform);
13
+ });
14
+ it('should thorw an error if the instance is not initialized well', () => {
15
+ expect(() => api_1.api.init('', '')).toThrowError();
14
16
  });
15
17
  it('should initialize the instance with the correct headers', () => {
16
- const instance = api.getInstances();
18
+ const instance = api_1.api.getInstances();
19
+ expect(instance === null || instance === void 0 ? void 0 : instance.defaults.baseURL).toBe('https://api-staging.y.uno/v1');
20
+ expect(instance === null || instance === void 0 ? void 0 : instance.defaults.headers['public-api-key']).toBe(publicApiKey);
17
21
  expect(instance === null || instance === void 0 ? void 0 : instance.defaults.headers['X-Platform']).toBe(platform);
18
22
  });
19
23
  it('should send a batch of events to the server', async () => {
@@ -26,11 +30,11 @@ describe('Api', () => {
26
30
  type: types_1.EventLogType.EVENT,
27
31
  },
28
32
  ];
29
- const response = await api.sendBatch(batches, publicApiKey);
33
+ const response = await api_1.api.sendBatch(batches);
30
34
  expect(response === null || response === void 0 ? void 0 : response.status).toBe(STATUS_CREATED);
31
35
  });
32
36
  it('should return the instance', () => {
33
- const instance = api.getInstances();
37
+ const instance = api_1.api.getInstances();
34
38
  expect(instance).not.toBeNull();
35
39
  });
36
40
  });
@@ -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 {};
@@ -10,28 +10,29 @@ 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, _Cron_api, _Cron_database;
13
+ var _Cron_interval;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.Cron = void 0;
15
+ exports.cron = void 0;
16
+ const api_1 = require("../api");
17
+ const database_1 = require("../database");
16
18
  const NONE_ITEMS = 0;
17
19
  class Cron {
18
- constructor(batchTime, api, database) {
20
+ constructor() {
19
21
  _Cron_interval.set(this, void 0);
20
- _Cron_api.set(this, void 0);
21
- _Cron_database.set(this, void 0);
22
+ }
23
+ init(batchTime) {
22
24
  this.stop();
23
- __classPrivateFieldSet(this, _Cron_api, api, "f");
24
- __classPrivateFieldSet(this, _Cron_database, database, "f");
25
- __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');
26
27
  }
27
- async sendBatch() {
28
- const items = await __classPrivateFieldGet(this, _Cron_database, "f").getAllItems();
29
- const keys = Object.keys(items);
30
- if (keys.length > NONE_ITEMS) {
31
- for (const key of keys) {
32
- __classPrivateFieldGet(this, _Cron_api, "f").sendBatch(items[key], key);
28
+ sendBatch() {
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());
33
34
  }
34
- }
35
+ });
35
36
  }
36
37
  stop() {
37
38
  if (__classPrivateFieldGet(this, _Cron_interval, "f")) {
@@ -43,5 +44,5 @@ class Cron {
43
44
  return __classPrivateFieldGet(this, _Cron_interval, "f");
44
45
  }
45
46
  }
46
- exports.Cron = Cron;
47
- _Cron_interval = new WeakMap(), _Cron_api = new WeakMap(), _Cron_database = new WeakMap();
47
+ _Cron_interval = new WeakMap();
48
+ exports.cron = new Cron();
@@ -7,17 +7,11 @@ const types_1 = require("../types");
7
7
  const TIME_INTERVAL = 1000;
8
8
  describe('Cron', () => {
9
9
  let spy;
10
- let cron;
11
- let api;
12
- let database;
13
10
  beforeEach(async () => {
14
11
  // restore the spy created with spyOn
15
12
  jest.restoreAllMocks();
16
- api = new api_1.Api('web', true);
17
- database = (0, database_1.databaseFactory)('testOrganization');
18
- cron = new cron_1.Cron(TIME_INTERVAL, api, database);
19
- spy = jest.spyOn(api, 'sendBatch');
20
- await database.reset();
13
+ spy = jest.spyOn(api_1.api, 'sendBatch');
14
+ await database_1.database.reset();
21
15
  });
22
16
  it('should send a batch of items to the API if there are items in the database', async () => {
23
17
  const batch = {
@@ -27,21 +21,23 @@ describe('Cron', () => {
27
21
  original_created_at: new Date().toISOString(),
28
22
  type: types_1.EventLogType.EVENT,
29
23
  };
30
- database.setItem(batch, 'sandbox_1');
31
- await cron.sendBatch();
32
- expect(spy).toHaveBeenCalledWith([batch], 'sandbox_1');
24
+ database_1.database.setItem(batch);
25
+ await cron_1.cron.sendBatch();
26
+ expect(spy).toHaveBeenCalledWith([batch]);
33
27
  });
34
28
  it('should not send a batch of items to the API if there are no items in the database', async () => {
35
- await cron.sendBatch();
29
+ await cron_1.cron.sendBatch();
36
30
  expect(spy).not.toHaveBeenCalled();
37
31
  });
38
32
  it('should stop the interval when stop is called', () => {
39
33
  jest.useFakeTimers();
40
- cron.stop();
41
- expect(cron.getInterval()).toBeUndefined();
34
+ cron_1.cron.init(TIME_INTERVAL);
35
+ cron_1.cron.stop();
36
+ expect(cron_1.cron.getInterval()).toBeUndefined();
42
37
  });
43
38
  it('should start the interval when init is called', () => {
44
39
  jest.useFakeTimers();
45
- expect(cron.getInterval()).toBeDefined();
40
+ cron_1.cron.init(TIME_INTERVAL);
41
+ expect(cron_1.cron.getInterval()).toBeDefined();
46
42
  });
47
43
  });
@@ -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
  }