@yuno-payments/sdk-event-log 0.3.0-beta.8 → 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/api/__tests__/api.test.js +5 -9
- package/dist/cjs/api/api.d.ts +3 -5
- package/dist/cjs/api/api.js +12 -10
- package/dist/cjs/cron/cron.d.ts +5 -5
- package/dist/cjs/cron/cron.js +26 -16
- package/dist/cjs/cron/cron.test.js +15 -11
- package/dist/cjs/database/database.d.ts +1 -2
- package/dist/cjs/database/database.js +5 -16
- 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 +17 -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 +7 -9
- package/dist/cjs/index.js +43 -21
- package/dist/cjs/index.test.js +168 -86
- package/dist/cjs/index.types.d.ts +4 -4
- package/dist/cjs/index.utils.d.ts +5 -2
- package/dist/cjs/index.utils.js +12 -4
- package/dist/cjs/types/index.d.ts +11 -1
- package/dist/cjs/utils/__tests__/check-environment.test.d.ts +1 -0
- package/dist/cjs/utils/__tests__/check-environment.test.js +30 -0
- package/dist/cjs/utils/__tests__/client-detection.test.d.ts +1 -0
- package/dist/cjs/utils/__tests__/client-detection.test.js +40 -0
- package/dist/cjs/utils/check-environment.d.ts +2 -0
- package/dist/cjs/utils/check-environment.js +17 -0
- package/dist/cjs/utils/client-detection.d.ts +8 -0
- package/dist/cjs/utils/client-detection.js +216 -0
- package/dist/cjs/utils/index.d.ts +3 -0
- package/dist/cjs/utils/index.js +19 -0
- package/dist/cjs/utils/stringyfy.d.ts +1 -0
- package/dist/cjs/utils/stringyfy.js +20 -0
- package/dist/esm/api/__tests__/api.test.js +5 -9
- package/dist/esm/api/api.d.ts +3 -5
- package/dist/esm/api/api.js +12 -10
- package/dist/esm/cron/cron.d.ts +5 -5
- package/dist/esm/cron/cron.js +17 -18
- package/dist/esm/cron/cron.test.js +15 -11
- package/dist/esm/database/database.d.ts +1 -2
- package/dist/esm/database/database.js +5 -16
- 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 +17 -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 +7 -9
- package/dist/esm/index.js +36 -21
- package/dist/esm/index.test.js +199 -77
- package/dist/esm/index.types.d.ts +4 -4
- package/dist/esm/index.utils.d.ts +5 -2
- package/dist/esm/index.utils.js +21 -4
- package/dist/esm/types/index.d.ts +11 -1
- package/dist/esm/utils/__tests__/check-environment.test.d.ts +1 -0
- package/dist/esm/utils/__tests__/check-environment.test.js +30 -0
- package/dist/esm/utils/__tests__/client-detection.test.d.ts +1 -0
- package/dist/esm/utils/__tests__/client-detection.test.js +31 -0
- package/dist/esm/utils/check-environment.d.ts +2 -0
- package/dist/esm/utils/check-environment.js +17 -0
- package/dist/esm/utils/client-detection.d.ts +8 -0
- package/dist/esm/utils/client-detection.js +205 -0
- package/dist/esm/utils/index.d.ts +3 -0
- package/dist/esm/utils/index.js +19 -0
- package/dist/esm/utils/stringyfy.d.ts +1 -0
- package/dist/esm/utils/stringyfy.js +20 -0
- package/package.json +4 -4
|
@@ -16,17 +16,13 @@ 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.
|
|
22
|
-
});
|
|
23
|
-
it('should thorw an error if the instance is not initialized well', () => {
|
|
24
|
-
expect(() => api_1.api.init('', '')).toThrowError();
|
|
22
|
+
api = new api_1.Api(platform, true);
|
|
25
23
|
});
|
|
26
24
|
it('should initialize the instance with the correct headers', () => {
|
|
27
|
-
const instance =
|
|
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);
|
|
25
|
+
const instance = api.getInstances();
|
|
30
26
|
expect(instance === null || instance === void 0 ? void 0 : instance.defaults.headers['X-Platform']).toBe(platform);
|
|
31
27
|
});
|
|
32
28
|
it('should send a batch of events to the server', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
@@ -39,11 +35,11 @@ describe('Api', () => {
|
|
|
39
35
|
type: types_1.EventLogType.EVENT,
|
|
40
36
|
},
|
|
41
37
|
];
|
|
42
|
-
const response = yield
|
|
38
|
+
const response = yield api.sendBatch(batches, publicApiKey);
|
|
43
39
|
expect(response === null || response === void 0 ? void 0 : response.status).toBe(STATUS_CREATED);
|
|
44
40
|
}));
|
|
45
41
|
it('should return the instance', () => {
|
|
46
|
-
const instance =
|
|
42
|
+
const instance = api.getInstances();
|
|
47
43
|
expect(instance).not.toBeNull();
|
|
48
44
|
});
|
|
49
45
|
});
|
package/dist/cjs/api/api.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
}
|
|
9
|
-
export declare const api: Api;
|
|
10
|
-
export {};
|
package/dist/cjs/api/api.js
CHANGED
|
@@ -15,32 +15,34 @@ 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.
|
|
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(platform, debug = false) {
|
|
23
23
|
_Api_instance.set(this, null);
|
|
24
24
|
_Api_debug.set(this, false);
|
|
25
|
-
}
|
|
26
|
-
init(publicApiKey, platform, debug = false) {
|
|
27
25
|
__classPrivateFieldSet(this, _Api_debug, debug, "f");
|
|
28
26
|
__classPrivateFieldSet(this, _Api_instance, axios_1.default.create({
|
|
29
|
-
baseURL: (0, api_utils_1.getBaseUrl)(publicApiKey),
|
|
30
27
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
31
|
-
headers: { '
|
|
28
|
+
headers: { 'X-Platform': platform },
|
|
32
29
|
}), "f");
|
|
33
30
|
}
|
|
34
|
-
sendBatch(batches) {
|
|
31
|
+
sendBatch(batches, publicApiKey) {
|
|
35
32
|
var _a;
|
|
36
|
-
if (
|
|
33
|
+
if (__classPrivateFieldGet(this, _Api_debug, "f")) {
|
|
37
34
|
console.dir(batches, { depth: null });
|
|
35
|
+
console.log(publicApiKey);
|
|
38
36
|
}
|
|
39
|
-
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
|
+
});
|
|
40
42
|
}
|
|
41
43
|
getInstances() {
|
|
42
44
|
return __classPrivateFieldGet(this, _Api_instance, "f");
|
|
43
45
|
}
|
|
44
46
|
}
|
|
47
|
+
exports.Api = Api;
|
|
45
48
|
_Api_instance = new WeakMap(), _Api_debug = new WeakMap();
|
|
46
|
-
exports.api = new Api();
|
package/dist/cjs/cron/cron.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
|
|
2
|
+
import { Api } from '../api';
|
|
3
|
+
import { Database } from '../database';
|
|
4
|
+
export declare class Cron {
|
|
3
5
|
#private;
|
|
4
|
-
|
|
5
|
-
sendBatch(): void
|
|
6
|
+
constructor(batchTime: number, api: Api, database: Database);
|
|
7
|
+
sendBatch(): Promise<void>;
|
|
6
8
|
stop(): void;
|
|
7
9
|
getInterval(): NodeJS.Timeout | undefined;
|
|
8
10
|
}
|
|
9
|
-
export declare const cron: Cron;
|
|
10
|
-
export {};
|
package/dist/cjs/cron/cron.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
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
|
+
};
|
|
2
11
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
12
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
13
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
@@ -10,27 +19,28 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
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");
|
|
11
20
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
21
|
};
|
|
13
|
-
var _Cron_interval;
|
|
22
|
+
var _Cron_interval, _Cron_api, _Cron_database;
|
|
14
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.
|
|
16
|
-
const api_1 = require("../api");
|
|
17
|
-
const database_1 = require("../database");
|
|
24
|
+
exports.Cron = void 0;
|
|
18
25
|
const NONE_ITEMS = 0;
|
|
19
26
|
class Cron {
|
|
20
|
-
constructor() {
|
|
27
|
+
constructor(batchTime, api, database) {
|
|
21
28
|
_Cron_interval.set(this, void 0);
|
|
22
|
-
|
|
23
|
-
|
|
29
|
+
_Cron_api.set(this, void 0);
|
|
30
|
+
_Cron_database.set(this, void 0);
|
|
24
31
|
this.stop();
|
|
25
|
-
__classPrivateFieldSet(this,
|
|
26
|
-
|
|
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");
|
|
27
35
|
}
|
|
28
36
|
sendBatch() {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (
|
|
33
|
-
|
|
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
|
+
}
|
|
34
44
|
}
|
|
35
45
|
});
|
|
36
46
|
}
|
|
@@ -44,5 +54,5 @@ class Cron {
|
|
|
44
54
|
return __classPrivateFieldGet(this, _Cron_interval, "f");
|
|
45
55
|
}
|
|
46
56
|
}
|
|
47
|
-
|
|
48
|
-
|
|
57
|
+
exports.Cron = Cron;
|
|
58
|
+
_Cron_interval = new WeakMap(), _Cron_api = new WeakMap(), _Cron_database = new WeakMap();
|
|
@@ -16,11 +16,17 @@ 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;
|
|
19
22
|
beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
23
|
// restore the spy created with spyOn
|
|
21
24
|
jest.restoreAllMocks();
|
|
22
|
-
|
|
23
|
-
|
|
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();
|
|
24
30
|
}));
|
|
25
31
|
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* () {
|
|
26
32
|
const batch = {
|
|
@@ -30,23 +36,21 @@ describe('Cron', () => {
|
|
|
30
36
|
original_created_at: new Date().toISOString(),
|
|
31
37
|
type: types_1.EventLogType.EVENT,
|
|
32
38
|
};
|
|
33
|
-
|
|
34
|
-
yield
|
|
35
|
-
expect(spy).toHaveBeenCalledWith([batch]);
|
|
39
|
+
database.setItem(batch, 'sandbox_1');
|
|
40
|
+
yield cron.sendBatch();
|
|
41
|
+
expect(spy).toHaveBeenCalledWith([batch], 'sandbox_1');
|
|
36
42
|
}));
|
|
37
43
|
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
|
|
44
|
+
yield cron.sendBatch();
|
|
39
45
|
expect(spy).not.toHaveBeenCalled();
|
|
40
46
|
}));
|
|
41
47
|
it('should stop the interval when stop is called', () => {
|
|
42
48
|
jest.useFakeTimers();
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
expect(cron_1.cron.getInterval()).toBeUndefined();
|
|
49
|
+
cron.stop();
|
|
50
|
+
expect(cron.getInterval()).toBeUndefined();
|
|
46
51
|
});
|
|
47
52
|
it('should start the interval when init is called', () => {
|
|
48
53
|
jest.useFakeTimers();
|
|
49
|
-
|
|
50
|
-
expect(cron_1.cron.getInterval()).toBeDefined();
|
|
54
|
+
expect(cron.getInterval()).toBeDefined();
|
|
51
55
|
});
|
|
52
56
|
});
|
|
@@ -1,15 +1,16 @@
|
|
|
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
|
-
|
|
7
|
-
|
|
6
|
+
const utils_1 = require("../utils");
|
|
7
|
+
function databaseFactory(organizationName) {
|
|
8
|
+
const databaseType = (0, utils_1.isLocalStorageEnabled)()
|
|
8
9
|
? 'local-storage'
|
|
9
10
|
: 'memory-storage';
|
|
10
11
|
switch (databaseType) {
|
|
11
12
|
case 'local-storage':
|
|
12
|
-
return new local_storage_1.LocalStorage();
|
|
13
|
+
return new local_storage_1.LocalStorage(organizationName);
|
|
13
14
|
case 'memory-storage':
|
|
14
15
|
return new memory_storage_1.MemoryStorage();
|
|
15
16
|
default:
|
|
@@ -17,15 +18,3 @@ function databaseFactory() {
|
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
exports.databaseFactory = databaseFactory;
|
|
20
|
-
function isLocalStorageEnabled() {
|
|
21
|
-
try {
|
|
22
|
-
const key = '__yuno_storage__test';
|
|
23
|
-
window.localStorage.setItem(key, 'test_value');
|
|
24
|
-
window.localStorage.removeItem(key);
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
catch (e) {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
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
|
}
|
|
@@ -22,26 +22,33 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
22
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 utils_1 = require("../../utils");
|
|
26
|
+
const DATABASE_NAME_SPACE = 'YUNO_EVENT_LOGS';
|
|
25
27
|
class LocalStorage {
|
|
26
|
-
constructor() {
|
|
28
|
+
constructor(organizationName) {
|
|
27
29
|
_LocalStorage_instances.add(this);
|
|
28
30
|
_LocalStorage_localStorage.set(this, void 0);
|
|
29
|
-
_LocalStorage_databaseName.set(this,
|
|
31
|
+
_LocalStorage_databaseName.set(this, void 0);
|
|
30
32
|
__classPrivateFieldSet(this, _LocalStorage_localStorage, window.localStorage, "f");
|
|
33
|
+
__classPrivateFieldSet(this, _LocalStorage_databaseName, `${DATABASE_NAME_SPACE}_${organizationName}`, "f");
|
|
31
34
|
const database = __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").getItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"));
|
|
32
35
|
if (database === null || database === undefined) {
|
|
33
|
-
|
|
36
|
+
this.reset();
|
|
34
37
|
}
|
|
35
38
|
}
|
|
36
39
|
reset() {
|
|
37
40
|
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
-
__classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), JSON.stringify(
|
|
41
|
+
__classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), JSON.stringify({}));
|
|
39
42
|
});
|
|
40
43
|
}
|
|
41
|
-
setItem(item) {
|
|
44
|
+
setItem(item, publicApiKey) {
|
|
42
45
|
return __awaiter(this, void 0, void 0, function* () {
|
|
43
46
|
const database = __classPrivateFieldGet(this, _LocalStorage_instances, "m", _LocalStorage_getDatabase).call(this);
|
|
44
|
-
|
|
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"), (0, utils_1.stringify)(database));
|
|
45
52
|
});
|
|
46
53
|
}
|
|
47
54
|
getItem(id) {
|
|
@@ -51,14 +58,16 @@ class LocalStorage {
|
|
|
51
58
|
throw new Error('Method not implemented.' + id);
|
|
52
59
|
}
|
|
53
60
|
getAllItems() {
|
|
54
|
-
|
|
61
|
+
const database = __classPrivateFieldGet(this, _LocalStorage_instances, "m", _LocalStorage_getDatabase).call(this);
|
|
62
|
+
this.reset();
|
|
63
|
+
return Promise.resolve(database);
|
|
55
64
|
}
|
|
56
65
|
}
|
|
57
66
|
exports.LocalStorage = LocalStorage;
|
|
58
67
|
_LocalStorage_localStorage = new WeakMap(), _LocalStorage_databaseName = new WeakMap(), _LocalStorage_instances = new WeakSet(), _LocalStorage_getDatabase = function _LocalStorage_getDatabase() {
|
|
59
68
|
let database = __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").getItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"));
|
|
60
69
|
if (database === null || database === undefined) {
|
|
61
|
-
database = JSON.stringify(
|
|
70
|
+
database = JSON.stringify({});
|
|
62
71
|
__classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), database);
|
|
63
72
|
}
|
|
64
73
|
return JSON.parse(database);
|
|
@@ -21,19 +21,20 @@ const batch = {
|
|
|
21
21
|
describe('LocalStorageDatabase', () => {
|
|
22
22
|
let localStorageDatabase;
|
|
23
23
|
beforeEach(() => {
|
|
24
|
-
localStorageDatabase = new local_storage_1.LocalStorage();
|
|
24
|
+
localStorageDatabase = new local_storage_1.LocalStorage('test');
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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] });
|
|
33
34
|
}));
|
|
34
35
|
it('should get an item from the database', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
35
|
-
localStorage.setItem('
|
|
36
|
+
localStorage.setItem('YUNO_EVENT_LOGS_test', JSON.stringify({ testKey: [batch] }));
|
|
36
37
|
const result = yield localStorageDatabase.getAllItems();
|
|
37
|
-
expect(result).toEqual([batch]);
|
|
38
|
+
expect(result).toEqual({ testKey: [batch] });
|
|
38
39
|
}));
|
|
39
40
|
});
|
|
@@ -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
|
}
|
|
@@ -25,16 +25,19 @@ exports.MemoryStorage = void 0;
|
|
|
25
25
|
class MemoryStorage {
|
|
26
26
|
constructor() {
|
|
27
27
|
_MemoryStorage_items.set(this, void 0);
|
|
28
|
-
__classPrivateFieldSet(this, _MemoryStorage_items,
|
|
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,
|
|
32
|
+
__classPrivateFieldSet(this, _MemoryStorage_items, {}, "f");
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
|
-
setItem(item) {
|
|
35
|
+
setItem(item, publicApiKey) {
|
|
36
36
|
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
-
__classPrivateFieldGet(this, _MemoryStorage_items, "f")
|
|
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);
|
|
38
41
|
});
|
|
39
42
|
}
|
|
40
43
|
getItem(id) {
|
|
@@ -44,7 +47,9 @@ class MemoryStorage {
|
|
|
44
47
|
throw new Error('Method not implemented.' + id);
|
|
45
48
|
}
|
|
46
49
|
getAllItems() {
|
|
47
|
-
|
|
50
|
+
const items = Object.assign({}, __classPrivateFieldGet(this, _MemoryStorage_items, "f"));
|
|
51
|
+
this.reset();
|
|
52
|
+
return Promise.resolve(items);
|
|
48
53
|
}
|
|
49
54
|
}
|
|
50
55
|
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);
|
|
30
|
+
yield memoryStorage.setItem(batch, 'testKey');
|
|
31
31
|
const database = yield memoryStorage.getAllItems();
|
|
32
|
-
expect(database).toEqual([batch]);
|
|
32
|
+
expect(database).toEqual({ testKey: [batch] });
|
|
33
33
|
}));
|
|
34
34
|
});
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import { InitArgs } from './index.types';
|
|
2
|
-
import './database';
|
|
3
2
|
import { Event, Log } from './types';
|
|
4
|
-
declare class EventLog {
|
|
3
|
+
export declare class EventLog {
|
|
5
4
|
#private;
|
|
6
|
-
|
|
7
|
-
event(eventArgs: Omit<Event, 'type'
|
|
5
|
+
constructor({ organizationName, platform, batchTime, enableEvents, enableLogs, debug, }: InitArgs);
|
|
6
|
+
event(eventArgs: Omit<Event, 'type'>, publicApiKey: string): Promise<void> | undefined;
|
|
8
7
|
logger: {
|
|
9
|
-
error: (errorArgs: Omit<Log, 'level' | 'type'
|
|
10
|
-
debug: (debugArgs: Omit<Log, 'level' | 'type'
|
|
11
|
-
info: (infoArgs: Omit<Log, 'level' | 'type'
|
|
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;
|
|
12
11
|
};
|
|
12
|
+
sendBatch(): Promise<void>;
|
|
13
13
|
}
|
|
14
|
-
export declare const eventLog: EventLog;
|
|
15
|
-
export {};
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
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
|
+
};
|
|
2
11
|
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
3
12
|
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
4
13
|
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
@@ -10,43 +19,56 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
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");
|
|
11
20
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
21
|
};
|
|
13
|
-
var _EventLog_enableEvents, _EventLog_enableLogs;
|
|
22
|
+
var _EventLog_enableEvents, _EventLog_enableLogs, _EventLog_api, _EventLog_database;
|
|
14
23
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.
|
|
24
|
+
exports.EventLog = void 0;
|
|
16
25
|
const api_1 = require("./api");
|
|
17
|
-
require("./database");
|
|
18
26
|
const cron_1 = require("./cron");
|
|
19
27
|
const types_1 = require("./types");
|
|
20
28
|
const database_1 = require("./database");
|
|
21
29
|
const index_utils_1 = require("./index.utils");
|
|
22
30
|
class EventLog {
|
|
23
|
-
constructor() {
|
|
24
|
-
_EventLog_enableEvents.set(this,
|
|
25
|
-
_EventLog_enableLogs.set(this,
|
|
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);
|
|
26
36
|
this.logger = {
|
|
27
|
-
error: (errorArgs) => {
|
|
28
|
-
return (0, index_utils_1.setLog)(errorArgs, types_1.Level.ERROR, __classPrivateFieldGet(this, _EventLog_enableLogs, "f"));
|
|
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, index_utils_1.browserClientInfo);
|
|
29
39
|
},
|
|
30
|
-
debug: (debugArgs) => {
|
|
31
|
-
return (0, index_utils_1.setLog)(debugArgs, types_1.Level.DEBUG, __classPrivateFieldGet(this, _EventLog_enableLogs, "f"));
|
|
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, index_utils_1.browserClientInfo);
|
|
42
|
+
},
|
|
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, index_utils_1.browserClientInfo);
|
|
32
45
|
},
|
|
33
|
-
info: (infoArgs) => {
|
|
34
|
-
return (0, index_utils_1.setLog)(infoArgs, types_1.Level.INFO, __classPrivateFieldGet(this, _EventLog_enableLogs, "f"));
|
|
35
|
-
}
|
|
36
46
|
};
|
|
37
|
-
}
|
|
38
|
-
init({ publicApiKey, platform = 'web', batchTime, enableEvents, enableLogs, debug = false }) {
|
|
39
47
|
__classPrivateFieldSet(this, _EventLog_enableEvents, enableEvents, "f");
|
|
40
48
|
__classPrivateFieldSet(this, _EventLog_enableLogs, enableLogs, "f");
|
|
41
|
-
api_1.
|
|
42
|
-
|
|
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
|
+
}
|
|
43
54
|
}
|
|
44
|
-
event(eventArgs) {
|
|
55
|
+
event(eventArgs, publicApiKey) {
|
|
45
56
|
if (!__classPrivateFieldGet(this, _EventLog_enableEvents, "f")) {
|
|
46
57
|
return;
|
|
47
58
|
}
|
|
48
|
-
return
|
|
59
|
+
return __classPrivateFieldGet(this, _EventLog_database, "f").setItem(Object.assign(Object.assign({ os: index_utils_1.browserClientInfo === null || index_utils_1.browserClientInfo === void 0 ? void 0 : index_utils_1.browserClientInfo.os }, 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
|
+
});
|
|
49
71
|
}
|
|
50
72
|
}
|
|
51
|
-
|
|
52
|
-
|
|
73
|
+
exports.EventLog = EventLog;
|
|
74
|
+
_EventLog_enableEvents = new WeakMap(), _EventLog_enableLogs = new WeakMap(), _EventLog_api = new WeakMap(), _EventLog_database = new WeakMap();
|