@yuno-payments/sdk-event-log 0.3.0-beta.10 → 0.3.0-beta.11
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/dist/cjs/api/__tests__/api.test.js +2 -7
- package/dist/cjs/api/api.d.ts +2 -2
- package/dist/cjs/api/api.js +9 -5
- package/dist/cjs/cron/cron.d.ts +3 -2
- package/dist/cjs/cron/cron.js +21 -8
- package/dist/cjs/cron/cron.test.js +7 -6
- package/dist/cjs/database/database.d.ts +1 -2
- package/dist/cjs/database/database.js +3 -4
- package/dist/cjs/database/database.test.js +2 -2
- package/dist/cjs/database/database.types.d.ts +2 -2
- package/dist/cjs/database/index.d.ts +1 -0
- package/dist/cjs/database/index.js +1 -0
- package/dist/cjs/database/local-storage/local-storage.d.ts +3 -3
- package/dist/cjs/database/local-storage/local-storage.js +16 -8
- package/dist/cjs/database/local-storage/local-storage.test.js +7 -6
- package/dist/cjs/database/memory-storage/memory-storage.d.ts +4 -2
- package/dist/cjs/database/memory-storage/memory-storage.js +10 -5
- package/dist/cjs/database/memory-storage/memory-storage.test.js +2 -2
- package/dist/cjs/index.d.ts +6 -8
- package/dist/cjs/index.js +35 -25
- package/dist/cjs/index.test.js +146 -84
- package/dist/cjs/index.types.d.ts +3 -3
- package/dist/cjs/index.utils.d.ts +2 -1
- package/dist/cjs/index.utils.js +2 -3
- package/dist/esm/api/__tests__/api.test.js +2 -7
- package/dist/esm/api/api.d.ts +2 -2
- package/dist/esm/api/api.js +9 -5
- package/dist/esm/cron/cron.d.ts +3 -2
- package/dist/esm/cron/cron.js +12 -10
- package/dist/esm/cron/cron.test.js +7 -6
- package/dist/esm/database/database.d.ts +1 -2
- package/dist/esm/database/database.js +3 -4
- package/dist/esm/database/database.test.js +2 -2
- package/dist/esm/database/database.types.d.ts +2 -2
- package/dist/esm/database/index.d.ts +1 -0
- package/dist/esm/database/index.js +1 -0
- package/dist/esm/database/local-storage/local-storage.d.ts +3 -3
- package/dist/esm/database/local-storage/local-storage.js +16 -8
- package/dist/esm/database/local-storage/local-storage.test.js +7 -6
- package/dist/esm/database/memory-storage/memory-storage.d.ts +4 -2
- package/dist/esm/database/memory-storage/memory-storage.js +10 -5
- package/dist/esm/database/memory-storage/memory-storage.test.js +2 -2
- package/dist/esm/index.d.ts +6 -8
- package/dist/esm/index.js +26 -27
- package/dist/esm/index.test.js +137 -75
- package/dist/esm/index.types.d.ts +3 -3
- package/dist/esm/index.utils.d.ts +2 -1
- package/dist/esm/index.utils.js +2 -3
- package/package.json +2 -1
package/dist/cjs/index.test.js
CHANGED
|
@@ -10,8 +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
|
|
14
|
-
const
|
|
13
|
+
const types_1 = require("./types");
|
|
14
|
+
const node_1 = require("msw/node");
|
|
15
|
+
const msw_1 = require("msw");
|
|
15
16
|
const eventArgs = {
|
|
16
17
|
source: 'test',
|
|
17
18
|
event: 'testEvent',
|
|
@@ -23,91 +24,152 @@ const logArgs = {
|
|
|
23
24
|
url: '',
|
|
24
25
|
method: '',
|
|
25
26
|
country: '',
|
|
26
|
-
original_created_at: new Date().toISOString()
|
|
27
|
+
original_created_at: new Date().toISOString(),
|
|
27
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
|
+
}));
|
|
28
37
|
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());
|
|
29
48
|
beforeEach(() => {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
publicApiKey: PUBLIC_API_KEY,
|
|
34
|
-
batchTime: 500,
|
|
35
|
-
enableEvents: true,
|
|
36
|
-
enableLogs: true,
|
|
49
|
+
eventLog = new index_1.EventLog({
|
|
50
|
+
organizationName: 'testOrg',
|
|
51
|
+
debug: false,
|
|
37
52
|
});
|
|
38
|
-
database_1.database.reset();
|
|
39
53
|
});
|
|
40
|
-
afterEach(() =>
|
|
41
|
-
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
publicApiKey
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
+
});
|
|
113
175
|
});
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
+
import { Database } from './database';
|
|
1
2
|
import { Level, Log } from './types';
|
|
2
|
-
export declare function setLog(logArgs: Omit<Log, 'level' | 'type'>, level: Level, enableLog: boolean): Promise<void> | undefined;
|
|
3
|
+
export declare function setLog(logArgs: Omit<Log, 'level' | 'type'>, level: Level, enableLog: boolean, database: Database, publicApiKey: string): Promise<void> | undefined;
|
package/dist/cjs/index.utils.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.setLog = void 0;
|
|
4
|
-
const database_1 = require("./database");
|
|
5
4
|
const types_1 = require("./types");
|
|
6
|
-
function setLog(logArgs, level, enableLog) {
|
|
5
|
+
function setLog(logArgs, level, enableLog, database, publicApiKey) {
|
|
7
6
|
if (!enableLog) {
|
|
8
7
|
return;
|
|
9
8
|
}
|
|
10
|
-
return
|
|
9
|
+
return database.setItem(Object.assign(Object.assign({}, logArgs), { level, type: types_1.EventLogType.LOG }), publicApiKey);
|
|
11
10
|
}
|
|
12
11
|
exports.setLog = setLog;
|
|
@@ -10,15 +10,10 @@ describe('Api', () => {
|
|
|
10
10
|
let api;
|
|
11
11
|
beforeEach(() => {
|
|
12
12
|
// Reset the instance before each test
|
|
13
|
-
api = new api_1.Api(
|
|
14
|
-
});
|
|
15
|
-
it('should thorw an error if the instance is not initialized well', () => {
|
|
16
|
-
expect(() => new api_1.Api('', '')).toThrowError();
|
|
13
|
+
api = new api_1.Api(platform, true);
|
|
17
14
|
});
|
|
18
15
|
it('should initialize the instance with the correct headers', () => {
|
|
19
16
|
const instance = api.getInstances();
|
|
20
|
-
expect(instance === null || instance === void 0 ? void 0 : instance.defaults.baseURL).toBe('https://api-staging.y.uno/v1');
|
|
21
|
-
expect(instance === null || instance === void 0 ? void 0 : instance.defaults.headers['public-api-key']).toBe(publicApiKey);
|
|
22
17
|
expect(instance === null || instance === void 0 ? void 0 : instance.defaults.headers['X-Platform']).toBe(platform);
|
|
23
18
|
});
|
|
24
19
|
it('should send a batch of events to the server', async () => {
|
|
@@ -31,7 +26,7 @@ describe('Api', () => {
|
|
|
31
26
|
type: types_1.EventLogType.EVENT,
|
|
32
27
|
},
|
|
33
28
|
];
|
|
34
|
-
const response = await api.sendBatch(batches);
|
|
29
|
+
const response = await api.sendBatch(batches, publicApiKey);
|
|
35
30
|
expect(response === null || response === void 0 ? void 0 : response.status).toBe(STATUS_CREATED);
|
|
36
31
|
});
|
|
37
32
|
it('should return the instance', () => {
|
package/dist/esm/api/api.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { AxiosInstance } from 'axios';
|
|
|
2
2
|
import { Batch } from '../types';
|
|
3
3
|
export declare class Api {
|
|
4
4
|
#private;
|
|
5
|
-
constructor(
|
|
6
|
-
sendBatch(batches: Batch[]): Promise<import("axios").AxiosResponse<any, any>> | undefined;
|
|
5
|
+
constructor(platform: string, debug?: boolean);
|
|
6
|
+
sendBatch(batches: Batch[], publicApiKey: string): Promise<import("axios").AxiosResponse<any, any>> | undefined;
|
|
7
7
|
getInstances(): AxiosInstance | null;
|
|
8
8
|
}
|
package/dist/esm/api/api.js
CHANGED
|
@@ -19,22 +19,26 @@ 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(platform, debug = false) {
|
|
23
23
|
_Api_instance.set(this, null);
|
|
24
24
|
_Api_debug.set(this, false);
|
|
25
25
|
__classPrivateFieldSet(this, _Api_debug, debug, "f");
|
|
26
26
|
__classPrivateFieldSet(this, _Api_instance, axios_1.default.create({
|
|
27
|
-
baseURL: (0, api_utils_1.getBaseUrl)(publicApiKey),
|
|
28
27
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
29
|
-
headers: { '
|
|
28
|
+
headers: { 'X-Platform': platform },
|
|
30
29
|
}), "f");
|
|
31
30
|
}
|
|
32
|
-
sendBatch(batches) {
|
|
31
|
+
sendBatch(batches, publicApiKey) {
|
|
33
32
|
var _a;
|
|
34
33
|
if (__classPrivateFieldGet(this, _Api_debug, "f")) {
|
|
35
34
|
console.dir(batches, { depth: null });
|
|
35
|
+
console.log(publicApiKey);
|
|
36
36
|
}
|
|
37
|
-
return (_a = __classPrivateFieldGet(this, _Api_instance, "f")) === null || _a === void 0 ? void 0 : _a.post('/sdk/event-log/publish', { batch: batches }
|
|
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
|
+
});
|
|
38
42
|
}
|
|
39
43
|
getInstances() {
|
|
40
44
|
return __classPrivateFieldGet(this, _Api_instance, "f");
|
package/dist/esm/cron/cron.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { Api } from '../api';
|
|
3
|
+
import { Database } from '../database';
|
|
3
4
|
export declare class Cron {
|
|
4
5
|
#private;
|
|
5
|
-
constructor(batchTime: number, api: Api);
|
|
6
|
-
sendBatch(): void
|
|
6
|
+
constructor(batchTime: number, api: Api, database: Database);
|
|
7
|
+
sendBatch(): Promise<void>;
|
|
7
8
|
stop(): void;
|
|
8
9
|
getInterval(): NodeJS.Timeout | undefined;
|
|
9
10
|
}
|
package/dist/esm/cron/cron.js
CHANGED
|
@@ -10,26 +10,28 @@ 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;
|
|
13
|
+
var _Cron_interval, _Cron_api, _Cron_database;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.Cron = void 0;
|
|
16
|
-
const database_1 = require("../database");
|
|
17
16
|
const NONE_ITEMS = 0;
|
|
18
17
|
class Cron {
|
|
19
|
-
constructor(batchTime, api) {
|
|
18
|
+
constructor(batchTime, api, database) {
|
|
20
19
|
_Cron_interval.set(this, void 0);
|
|
21
20
|
_Cron_api.set(this, void 0);
|
|
21
|
+
_Cron_database.set(this, void 0);
|
|
22
22
|
this.stop();
|
|
23
23
|
__classPrivateFieldSet(this, _Cron_api, api, "f");
|
|
24
|
+
__classPrivateFieldSet(this, _Cron_database, database, "f");
|
|
24
25
|
__classPrivateFieldSet(this, _Cron_interval, setInterval(this.sendBatch.bind(this), batchTime), "f");
|
|
25
26
|
}
|
|
26
|
-
sendBatch() {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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);
|
|
31
33
|
}
|
|
32
|
-
}
|
|
34
|
+
}
|
|
33
35
|
}
|
|
34
36
|
stop() {
|
|
35
37
|
if (__classPrivateFieldGet(this, _Cron_interval, "f")) {
|
|
@@ -42,4 +44,4 @@ class Cron {
|
|
|
42
44
|
}
|
|
43
45
|
}
|
|
44
46
|
exports.Cron = Cron;
|
|
45
|
-
_Cron_interval = new WeakMap(), _Cron_api = new WeakMap();
|
|
47
|
+
_Cron_interval = new WeakMap(), _Cron_api = new WeakMap(), _Cron_database = new WeakMap();
|
|
@@ -4,19 +4,20 @@ 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=';
|
|
8
7
|
const TIME_INTERVAL = 1000;
|
|
9
8
|
describe('Cron', () => {
|
|
10
9
|
let spy;
|
|
11
10
|
let cron;
|
|
12
11
|
let api;
|
|
12
|
+
let database;
|
|
13
13
|
beforeEach(async () => {
|
|
14
14
|
// restore the spy created with spyOn
|
|
15
15
|
jest.restoreAllMocks();
|
|
16
|
-
api = new api_1.Api(
|
|
17
|
-
|
|
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);
|
|
18
19
|
spy = jest.spyOn(api, 'sendBatch');
|
|
19
|
-
await
|
|
20
|
+
await database.reset();
|
|
20
21
|
});
|
|
21
22
|
it('should send a batch of items to the API if there are items in the database', async () => {
|
|
22
23
|
const batch = {
|
|
@@ -26,9 +27,9 @@ describe('Cron', () => {
|
|
|
26
27
|
original_created_at: new Date().toISOString(),
|
|
27
28
|
type: types_1.EventLogType.EVENT,
|
|
28
29
|
};
|
|
29
|
-
|
|
30
|
+
database.setItem(batch, 'sandbox_1');
|
|
30
31
|
await cron.sendBatch();
|
|
31
|
-
expect(spy).toHaveBeenCalledWith([batch]);
|
|
32
|
+
expect(spy).toHaveBeenCalledWith([batch], 'sandbox_1');
|
|
32
33
|
});
|
|
33
34
|
it('should not send a batch of items to the API if there are no items in the database', async () => {
|
|
34
35
|
await cron.sendBatch();
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
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() {
|
|
6
|
+
function databaseFactory(organizationName) {
|
|
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();
|
|
12
|
+
return new local_storage_1.LocalStorage(organizationName);
|
|
13
13
|
case 'memory-storage':
|
|
14
14
|
return new memory_storage_1.MemoryStorage();
|
|
15
15
|
default:
|
|
@@ -28,4 +28,3 @@ 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)();
|
|
14
|
+
const result = (0, database_1.databaseFactory)('testOrganization');
|
|
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)();
|
|
22
|
+
const result = (0, database_1.databaseFactory)('testOrganization');
|
|
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): Promise<void>;
|
|
3
|
+
setItem(item: Batch, publicApiKey: string): Promise<void>;
|
|
4
4
|
getItem(id: string): Promise<Batch>;
|
|
5
5
|
removeItem(id: string): Promise<void>;
|
|
6
|
-
getAllItems(): Promise<Batch[]
|
|
6
|
+
getAllItems(): Promise<Record<string, Batch[]>>;
|
|
7
7
|
reset(): Promise<void>;
|
|
8
8
|
}
|
|
@@ -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();
|
|
5
|
+
constructor(organizationName: string);
|
|
6
6
|
reset(): Promise<void>;
|
|
7
|
-
setItem(item: Batch): Promise<void>;
|
|
7
|
+
setItem(item: Batch, publicApiKey: string): Promise<void>;
|
|
8
8
|
getItem(id: string): Promise<Batch>;
|
|
9
9
|
removeItem(id: string): Promise<void>;
|
|
10
|
-
getAllItems(): Promise<Batch[]
|
|
10
|
+
getAllItems(): Promise<Record<string, Batch[]>>;
|
|
11
11
|
}
|
|
@@ -13,23 +13,29 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
13
13
|
var _LocalStorage_instances, _LocalStorage_localStorage, _LocalStorage_databaseName, _LocalStorage_getDatabase;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.LocalStorage = void 0;
|
|
16
|
+
const DATABASE_NAME_SPACE = 'YUNO_EVENT_LOGS';
|
|
16
17
|
class LocalStorage {
|
|
17
|
-
constructor() {
|
|
18
|
+
constructor(organizationName) {
|
|
18
19
|
_LocalStorage_instances.add(this);
|
|
19
20
|
_LocalStorage_localStorage.set(this, void 0);
|
|
20
|
-
_LocalStorage_databaseName.set(this,
|
|
21
|
+
_LocalStorage_databaseName.set(this, void 0);
|
|
21
22
|
__classPrivateFieldSet(this, _LocalStorage_localStorage, window.localStorage, "f");
|
|
23
|
+
__classPrivateFieldSet(this, _LocalStorage_databaseName, `${DATABASE_NAME_SPACE}_${organizationName}`, "f");
|
|
22
24
|
const database = __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").getItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"));
|
|
23
25
|
if (database === null || database === undefined) {
|
|
24
|
-
|
|
26
|
+
this.reset();
|
|
25
27
|
}
|
|
26
28
|
}
|
|
27
29
|
async reset() {
|
|
28
|
-
__classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), JSON.stringify(
|
|
30
|
+
__classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), JSON.stringify({}));
|
|
29
31
|
}
|
|
30
|
-
async setItem(item) {
|
|
32
|
+
async setItem(item, publicApiKey) {
|
|
31
33
|
const database = __classPrivateFieldGet(this, _LocalStorage_instances, "m", _LocalStorage_getDatabase).call(this);
|
|
32
|
-
|
|
34
|
+
if (database[publicApiKey] === undefined) {
|
|
35
|
+
database[publicApiKey] = [];
|
|
36
|
+
}
|
|
37
|
+
database[publicApiKey].push(item);
|
|
38
|
+
__classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), JSON.stringify(database));
|
|
33
39
|
}
|
|
34
40
|
getItem(id) {
|
|
35
41
|
throw new Error('Method not implemented.' + id);
|
|
@@ -38,14 +44,16 @@ class LocalStorage {
|
|
|
38
44
|
throw new Error('Method not implemented.' + id);
|
|
39
45
|
}
|
|
40
46
|
getAllItems() {
|
|
41
|
-
|
|
47
|
+
const database = __classPrivateFieldGet(this, _LocalStorage_instances, "m", _LocalStorage_getDatabase).call(this);
|
|
48
|
+
this.reset();
|
|
49
|
+
return Promise.resolve(database);
|
|
42
50
|
}
|
|
43
51
|
}
|
|
44
52
|
exports.LocalStorage = LocalStorage;
|
|
45
53
|
_LocalStorage_localStorage = new WeakMap(), _LocalStorage_databaseName = new WeakMap(), _LocalStorage_instances = new WeakSet(), _LocalStorage_getDatabase = function _LocalStorage_getDatabase() {
|
|
46
54
|
let database = __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").getItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"));
|
|
47
55
|
if (database === null || database === undefined) {
|
|
48
|
-
database = JSON.stringify(
|
|
56
|
+
database = JSON.stringify({});
|
|
49
57
|
__classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), database);
|
|
50
58
|
}
|
|
51
59
|
return JSON.parse(database);
|
|
@@ -12,19 +12,20 @@ const batch = {
|
|
|
12
12
|
describe('LocalStorageDatabase', () => {
|
|
13
13
|
let localStorageDatabase;
|
|
14
14
|
beforeEach(() => {
|
|
15
|
-
localStorageDatabase = new local_storage_1.LocalStorage();
|
|
15
|
+
localStorageDatabase = new local_storage_1.LocalStorage('test');
|
|
16
16
|
});
|
|
17
17
|
afterEach(() => {
|
|
18
18
|
localStorage.clear();
|
|
19
19
|
});
|
|
20
20
|
it('should set an item in the database', async () => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
const testKey = 'testKey';
|
|
22
|
+
await localStorageDatabase.setItem(batch, testKey);
|
|
23
|
+
const database = JSON.parse(localStorage.getItem('YUNO_EVENT_LOGS_test') || '[]');
|
|
24
|
+
expect(database).toEqual({ [testKey]: [batch] });
|
|
24
25
|
});
|
|
25
26
|
it('should get an item from the database', async () => {
|
|
26
|
-
localStorage.setItem('
|
|
27
|
+
localStorage.setItem('YUNO_EVENT_LOGS_test', JSON.stringify({ testKey: [batch] }));
|
|
27
28
|
const result = await localStorageDatabase.getAllItems();
|
|
28
|
-
expect(result).toEqual([batch]);
|
|
29
|
+
expect(result).toEqual({ testKey: [batch] });
|
|
29
30
|
});
|
|
30
31
|
});
|
|
@@ -4,8 +4,10 @@ export declare class MemoryStorage implements Database {
|
|
|
4
4
|
#private;
|
|
5
5
|
constructor();
|
|
6
6
|
reset(): Promise<void>;
|
|
7
|
-
setItem(item: Batch): Promise<void>;
|
|
7
|
+
setItem(item: Batch, publicApiKey: string): Promise<void>;
|
|
8
8
|
getItem(id: string): Promise<Batch>;
|
|
9
9
|
removeItem(id: string): Promise<void>;
|
|
10
|
-
getAllItems(): Promise<
|
|
10
|
+
getAllItems(): Promise<{
|
|
11
|
+
[x: string]: Batch[];
|
|
12
|
+
}>;
|
|
11
13
|
}
|