@yuno-payments/sdk-event-log 0.2.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.
Files changed (93) hide show
  1. package/dist/cjs/api/__tests__/api.test.d.ts +1 -0
  2. package/dist/cjs/api/__tests__/api.test.js +49 -0
  3. package/dist/cjs/api/__tests__/api.utils.test.d.ts +1 -0
  4. package/dist/cjs/api/__tests__/api.utils.test.js +35 -0
  5. package/dist/cjs/api/api.d.ts +10 -0
  6. package/dist/cjs/api/api.js +41 -0
  7. package/dist/cjs/api/api.utils.d.ts +1 -0
  8. package/dist/cjs/api/api.utils.js +18 -0
  9. package/dist/cjs/api/index.d.ts +1 -0
  10. package/dist/cjs/api/index.js +17 -0
  11. package/dist/cjs/cron/cron.d.ts +10 -0
  12. package/dist/cjs/cron/cron.js +46 -0
  13. package/dist/cjs/cron/cron.test.d.ts +1 -0
  14. package/dist/cjs/cron/cron.test.js +52 -0
  15. package/dist/cjs/cron/index.d.ts +1 -0
  16. package/dist/cjs/cron/index.js +17 -0
  17. package/dist/cjs/database/database.d.ts +3 -0
  18. package/dist/cjs/database/database.js +31 -0
  19. package/dist/cjs/database/database.test.d.ts +1 -0
  20. package/dist/cjs/database/database.test.js +25 -0
  21. package/dist/cjs/database/database.types.d.ts +8 -0
  22. package/dist/cjs/database/database.types.js +2 -0
  23. package/dist/cjs/database/index.d.ts +1 -0
  24. package/dist/cjs/database/index.js +17 -0
  25. package/dist/cjs/database/local-storage/index.d.ts +1 -0
  26. package/dist/cjs/database/local-storage/index.js +17 -0
  27. package/dist/cjs/database/local-storage/local-storage.d.ts +11 -0
  28. package/dist/cjs/database/local-storage/local-storage.js +65 -0
  29. package/dist/cjs/database/local-storage/local-storage.test.d.ts +1 -0
  30. package/dist/cjs/database/local-storage/local-storage.test.js +39 -0
  31. package/dist/cjs/database/memory-storage/index.d.ts +1 -0
  32. package/dist/cjs/database/memory-storage/index.js +17 -0
  33. package/dist/cjs/database/memory-storage/memory-storage.d.ts +11 -0
  34. package/dist/cjs/database/memory-storage/memory-storage.js +51 -0
  35. package/dist/cjs/database/memory-storage/memory-storage.test.d.ts +1 -0
  36. package/dist/cjs/database/memory-storage/memory-storage.test.js +34 -0
  37. package/dist/cjs/index.d.ts +15 -0
  38. package/dist/cjs/index.js +52 -0
  39. package/dist/cjs/index.test.d.ts +1 -0
  40. package/dist/cjs/index.test.js +113 -0
  41. package/dist/cjs/index.types.d.ts +7 -0
  42. package/dist/cjs/index.types.js +2 -0
  43. package/dist/cjs/index.utils.d.ts +2 -0
  44. package/dist/cjs/index.utils.js +12 -0
  45. package/dist/cjs/types/index.d.ts +41 -0
  46. package/dist/cjs/types/index.js +14 -0
  47. package/dist/esm/api/__tests__/api.test.d.ts +1 -0
  48. package/dist/esm/api/__tests__/api.test.js +40 -0
  49. package/dist/esm/api/__tests__/api.utils.test.d.ts +1 -0
  50. package/dist/esm/api/__tests__/api.utils.test.js +35 -0
  51. package/dist/esm/api/api.d.ts +10 -0
  52. package/dist/esm/api/api.js +41 -0
  53. package/dist/esm/api/api.utils.d.ts +1 -0
  54. package/dist/esm/api/api.utils.js +18 -0
  55. package/dist/esm/api/index.d.ts +1 -0
  56. package/dist/esm/api/index.js +17 -0
  57. package/dist/esm/cron/cron.d.ts +10 -0
  58. package/dist/esm/cron/cron.js +46 -0
  59. package/dist/esm/cron/cron.test.d.ts +1 -0
  60. package/dist/esm/cron/cron.test.js +43 -0
  61. package/dist/esm/cron/index.d.ts +1 -0
  62. package/dist/esm/cron/index.js +17 -0
  63. package/dist/esm/database/database.d.ts +3 -0
  64. package/dist/esm/database/database.js +31 -0
  65. package/dist/esm/database/database.test.d.ts +1 -0
  66. package/dist/esm/database/database.test.js +25 -0
  67. package/dist/esm/database/database.types.d.ts +8 -0
  68. package/dist/esm/database/database.types.js +2 -0
  69. package/dist/esm/database/index.d.ts +1 -0
  70. package/dist/esm/database/index.js +17 -0
  71. package/dist/esm/database/local-storage/index.d.ts +1 -0
  72. package/dist/esm/database/local-storage/index.js +17 -0
  73. package/dist/esm/database/local-storage/local-storage.d.ts +11 -0
  74. package/dist/esm/database/local-storage/local-storage.js +52 -0
  75. package/dist/esm/database/local-storage/local-storage.test.d.ts +1 -0
  76. package/dist/esm/database/local-storage/local-storage.test.js +30 -0
  77. package/dist/esm/database/memory-storage/index.d.ts +1 -0
  78. package/dist/esm/database/memory-storage/index.js +17 -0
  79. package/dist/esm/database/memory-storage/memory-storage.d.ts +11 -0
  80. package/dist/esm/database/memory-storage/memory-storage.js +38 -0
  81. package/dist/esm/database/memory-storage/memory-storage.test.d.ts +1 -0
  82. package/dist/esm/database/memory-storage/memory-storage.test.js +25 -0
  83. package/dist/esm/index.d.ts +15 -0
  84. package/dist/esm/index.js +52 -0
  85. package/dist/esm/index.test.d.ts +1 -0
  86. package/dist/esm/index.test.js +104 -0
  87. package/dist/esm/index.types.d.ts +7 -0
  88. package/dist/esm/index.types.js +2 -0
  89. package/dist/esm/index.utils.d.ts +2 -0
  90. package/dist/esm/index.utils.js +12 -0
  91. package/dist/esm/types/index.d.ts +41 -0
  92. package/dist/esm/types/index.js +14 -0
  93. package/package.json +51 -0
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,49 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const types_1 = require("../../types");
13
+ const api_1 = require("../api");
14
+ const PUBLIC_API_KEY = 'staging_gAAAAABj-NC2WEf3XA4lHnN6RfGbjETf6qL27jlU7c1tm01ERV-Y-E6ddee1QPyV_CA8cxjVlGy-qDNA2-mqa9msQlTJit-Fmbvv6f2AZdfbFd_CUo1stvixnF4_8MzM_IYVWYz7KK86VfEBvxNpjRCyp7BDef-QzrT1-yqa8HAYTuA1GU3Hjjo=';
15
+ const publicApiKey = PUBLIC_API_KEY;
16
+ const platform = 'web';
17
+ const STATUS_CREATED = 201;
18
+ describe('Api', () => {
19
+ beforeEach(() => {
20
+ // Reset the instance before each test
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();
25
+ });
26
+ it('should initialize the instance with the correct headers', () => {
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);
30
+ expect(instance === null || instance === void 0 ? void 0 : instance.defaults.headers['X-Platform']).toBe(platform);
31
+ });
32
+ it('should send a batch of events to the server', () => __awaiter(void 0, void 0, void 0, function* () {
33
+ const batches = [
34
+ {
35
+ source: 'test',
36
+ event: 'testEvent',
37
+ description: 'Test event',
38
+ original_created_at: new Date().toISOString(),
39
+ type: types_1.EventLogType.EVENT,
40
+ },
41
+ ];
42
+ const response = yield api_1.api.sendBatch(batches);
43
+ expect(response === null || response === void 0 ? void 0 : response.status).toBe(STATUS_CREATED);
44
+ }));
45
+ it('should return the instance', () => {
46
+ const instance = api_1.api.getInstances();
47
+ expect(instance).not.toBeNull();
48
+ });
49
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const api_utils_1 = require("../api.utils");
4
+ describe('getBaseUrl', () => {
5
+ it('should return the correct base URL for a dev environment', () => {
6
+ const publicApiKey = 'dev_1234567890';
7
+ const expectedUrl = 'https://api-dev.y.uno/v1';
8
+ const result = (0, api_utils_1.getBaseUrl)(publicApiKey);
9
+ expect(result).toBe(expectedUrl);
10
+ });
11
+ it('should return the correct base URL for a staging environment', () => {
12
+ const publicApiKey = 'staging_1234567890';
13
+ const expectedUrl = 'https://api-staging.y.uno/v1';
14
+ const result = (0, api_utils_1.getBaseUrl)(publicApiKey);
15
+ expect(result).toBe(expectedUrl);
16
+ });
17
+ it('should return the correct base URL for a sandbox environment', () => {
18
+ const publicApiKey = 'sandbox_1234567890';
19
+ const expectedUrl = 'https://api-sandbox.y.uno/v1';
20
+ const result = (0, api_utils_1.getBaseUrl)(publicApiKey);
21
+ expect(result).toBe(expectedUrl);
22
+ });
23
+ it('should return the correct base URL for a prod environment', () => {
24
+ const publicApiKey = 'prod_1234567890';
25
+ const expectedUrl = 'https://api.y.uno/v1';
26
+ const result = (0, api_utils_1.getBaseUrl)(publicApiKey);
27
+ expect(result).toBe(expectedUrl);
28
+ });
29
+ it('should throw an error for an invalid public API key', () => {
30
+ const publicApiKey = 'invalidApiKey';
31
+ expect(() => {
32
+ (0, api_utils_1.getBaseUrl)(publicApiKey);
33
+ }).toThrowError('Not environment found publicApiKey: invalidApiKey, apiKeyPrefix: invalidApiKey, environmentSuffix: undefined');
34
+ });
35
+ });
@@ -0,0 +1,10 @@
1
+ import { AxiosInstance } from 'axios';
2
+ import { Batch } from '../types';
3
+ declare class Api {
4
+ #private;
5
+ init(publicApiKey: string, platform: string): void;
6
+ sendBatch(batches: Batch[]): Promise<import("axios").AxiosResponse<any, any>> | undefined;
7
+ getInstances(): AxiosInstance | null;
8
+ }
9
+ export declare const api: Api;
10
+ export {};
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ var _Api_instance;
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.api = void 0;
19
+ const axios_1 = __importDefault(require("axios"));
20
+ const api_utils_1 = require("./api.utils");
21
+ class Api {
22
+ constructor() {
23
+ _Api_instance.set(this, null);
24
+ }
25
+ init(publicApiKey, platform) {
26
+ __classPrivateFieldSet(this, _Api_instance, axios_1.default.create({
27
+ baseURL: (0, api_utils_1.getBaseUrl)(publicApiKey),
28
+ // eslint-disable-next-line @typescript-eslint/naming-convention
29
+ headers: { 'public-api-key': publicApiKey, 'X-Platform': platform },
30
+ }), "f");
31
+ }
32
+ sendBatch(batches) {
33
+ var _a;
34
+ return (_a = __classPrivateFieldGet(this, _Api_instance, "f")) === null || _a === void 0 ? void 0 : _a.post('/sdk-event-log/publish', { batch: batches });
35
+ }
36
+ getInstances() {
37
+ return __classPrivateFieldGet(this, _Api_instance, "f");
38
+ }
39
+ }
40
+ _Api_instance = new WeakMap();
41
+ exports.api = new Api();
@@ -0,0 +1 @@
1
+ export declare const getBaseUrl: (publicApiKey: string) => string;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getBaseUrl = void 0;
4
+ const ApiKeyPrefixToEnvironmentSuffix = {
5
+ dev: '-dev',
6
+ staging: '-staging',
7
+ sandbox: '-sandbox',
8
+ prod: '',
9
+ };
10
+ const getBaseUrl = (publicApiKey) => {
11
+ const [apiKeyPrefix] = publicApiKey.split('_');
12
+ const environmentSuffix = ApiKeyPrefixToEnvironmentSuffix[apiKeyPrefix];
13
+ if (environmentSuffix === undefined) {
14
+ throw new Error(`Not environment found publicApiKey: ${publicApiKey}, apiKeyPrefix: ${apiKeyPrefix}, environmentSuffix: ${environmentSuffix}`);
15
+ }
16
+ return 'https://api_ENVIRONMENT_.y.uno/v1'.replace('_ENVIRONMENT_', environmentSuffix);
17
+ };
18
+ exports.getBaseUrl = getBaseUrl;
@@ -0,0 +1 @@
1
+ export * from './api';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./api"), exports);
@@ -0,0 +1,10 @@
1
+ /// <reference types="node" />
2
+ declare class Cron {
3
+ #private;
4
+ init(batchTime: number): void;
5
+ sendBatch(): void;
6
+ stop(): void;
7
+ getInterval(): NodeJS.Timeout | undefined;
8
+ }
9
+ export declare const cron: Cron;
10
+ export {};
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _Cron_interval;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.cron = void 0;
16
+ const api_1 = require("../api");
17
+ const database_1 = require("../database");
18
+ const NONE_ITEMS = 0;
19
+ class Cron {
20
+ constructor() {
21
+ _Cron_interval.set(this, void 0);
22
+ }
23
+ init(batchTime) {
24
+ this.stop();
25
+ __classPrivateFieldSet(this, _Cron_interval, setInterval(this.sendBatch, batchTime), "f");
26
+ }
27
+ sendBatch() {
28
+ database_1.database.getAllItems().then((items) => {
29
+ var _a;
30
+ if (items.length > NONE_ITEMS) {
31
+ (_a = api_1.api.sendBatch(items)) === null || _a === void 0 ? void 0 : _a.then(() => database_1.database.reset());
32
+ }
33
+ });
34
+ }
35
+ stop() {
36
+ if (__classPrivateFieldGet(this, _Cron_interval, "f")) {
37
+ clearInterval(__classPrivateFieldGet(this, _Cron_interval, "f"));
38
+ __classPrivateFieldSet(this, _Cron_interval, undefined, "f");
39
+ }
40
+ }
41
+ getInterval() {
42
+ return __classPrivateFieldGet(this, _Cron_interval, "f");
43
+ }
44
+ }
45
+ _Cron_interval = new WeakMap();
46
+ exports.cron = new Cron();
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,52 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const cron_1 = require("./cron");
13
+ const api_1 = require("../api");
14
+ const database_1 = require("../database");
15
+ const types_1 = require("../types");
16
+ const TIME_INTERVAL = 1000;
17
+ describe('Cron', () => {
18
+ let spy;
19
+ beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
20
+ // restore the spy created with spyOn
21
+ jest.restoreAllMocks();
22
+ spy = jest.spyOn(api_1.api, 'sendBatch');
23
+ yield database_1.database.reset();
24
+ }));
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* () {
26
+ const batch = {
27
+ source: 'test',
28
+ event: 'testEvent',
29
+ description: 'Test event',
30
+ original_created_at: new Date().toISOString(),
31
+ type: types_1.EventLogType.EVENT,
32
+ };
33
+ database_1.database.setItem(batch);
34
+ yield cron_1.cron.sendBatch();
35
+ expect(spy).toHaveBeenCalledWith([batch]);
36
+ }));
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* () {
38
+ yield cron_1.cron.sendBatch();
39
+ expect(spy).not.toHaveBeenCalled();
40
+ }));
41
+ it('should stop the interval when stop is called', () => {
42
+ jest.useFakeTimers();
43
+ cron_1.cron.init(TIME_INTERVAL);
44
+ cron_1.cron.stop();
45
+ expect(cron_1.cron.getInterval()).toBeUndefined();
46
+ });
47
+ it('should start the interval when init is called', () => {
48
+ jest.useFakeTimers();
49
+ cron_1.cron.init(TIME_INTERVAL);
50
+ expect(cron_1.cron.getInterval()).toBeDefined();
51
+ });
52
+ });
@@ -0,0 +1 @@
1
+ export * from './cron';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./cron"), exports);
@@ -0,0 +1,3 @@
1
+ import { Database } from './database.types';
2
+ export declare function databaseFactory(): Database;
3
+ export declare const database: Database;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.database = exports.databaseFactory = void 0;
4
+ const local_storage_1 = require("./local-storage");
5
+ const memory_storage_1 = require("./memory-storage");
6
+ function databaseFactory() {
7
+ const databaseType = isLocalStorageEnabled()
8
+ ? 'local-storage'
9
+ : 'memory-storage';
10
+ switch (databaseType) {
11
+ case 'local-storage':
12
+ return new local_storage_1.LocalStorage();
13
+ case 'memory-storage':
14
+ return new memory_storage_1.MemoryStorage();
15
+ default:
16
+ throw new Error(`Database type ${databaseType} not supported`);
17
+ }
18
+ }
19
+ 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();
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const database_1 = require("./database");
4
+ const local_storage_1 = require("./local-storage");
5
+ const memory_storage_1 = require("./memory-storage");
6
+ describe('databaseFactory', () => {
7
+ beforeEach(() => {
8
+ // restore the spy created with spyOn
9
+ jest.restoreAllMocks();
10
+ });
11
+ it('should return a LocalStorage instance if local storage is enabled', () => {
12
+ jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => { });
13
+ jest.spyOn(Storage.prototype, 'removeItem').mockImplementation(() => { });
14
+ const result = (0, database_1.databaseFactory)();
15
+ expect(result).toBeInstanceOf(local_storage_1.LocalStorage);
16
+ });
17
+ it('should return a MemoryStorage instance if local storage is not enabled', () => {
18
+ jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
19
+ throw new Error('Local storage not available');
20
+ });
21
+ jest.spyOn(Storage.prototype, 'removeItem').mockImplementation(() => { });
22
+ const result = (0, database_1.databaseFactory)();
23
+ expect(result).toBeInstanceOf(memory_storage_1.MemoryStorage);
24
+ });
25
+ });
@@ -0,0 +1,8 @@
1
+ import { Batch } from '../types';
2
+ export interface Database {
3
+ setItem(item: Batch): Promise<void>;
4
+ getItem(id: string): Promise<Batch>;
5
+ removeItem(id: string): Promise<void>;
6
+ getAllItems(): Promise<Batch[]>;
7
+ reset(): Promise<void>;
8
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export * from './database';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./database"), exports);
@@ -0,0 +1 @@
1
+ export * from './local-storage';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./local-storage"), exports);
@@ -0,0 +1,11 @@
1
+ import { Batch } from '../../types';
2
+ import { Database } from '../database.types';
3
+ export declare class LocalStorage implements Database {
4
+ #private;
5
+ constructor();
6
+ reset(): Promise<void>;
7
+ setItem(item: Batch): Promise<void>;
8
+ getItem(id: string): Promise<Batch>;
9
+ removeItem(id: string): Promise<void>;
10
+ getAllItems(): Promise<Batch[]>;
11
+ }
@@ -0,0 +1,65 @@
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
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
+ if (kind === "m") throw new TypeError("Private method is not writable");
13
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
14
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
15
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
16
+ };
17
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
18
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
+ };
22
+ var _LocalStorage_instances, _LocalStorage_localStorage, _LocalStorage_databaseName, _LocalStorage_getDatabase;
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.LocalStorage = void 0;
25
+ class LocalStorage {
26
+ constructor() {
27
+ _LocalStorage_instances.add(this);
28
+ _LocalStorage_localStorage.set(this, void 0);
29
+ _LocalStorage_databaseName.set(this, 'YUNO_EVENT_LOGS');
30
+ __classPrivateFieldSet(this, _LocalStorage_localStorage, window.localStorage, "f");
31
+ const database = __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").getItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"));
32
+ if (database === null || database === undefined) {
33
+ __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), JSON.stringify([]));
34
+ }
35
+ }
36
+ reset() {
37
+ return __awaiter(this, void 0, void 0, function* () {
38
+ __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), JSON.stringify([]));
39
+ });
40
+ }
41
+ setItem(item) {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ const database = __classPrivateFieldGet(this, _LocalStorage_instances, "m", _LocalStorage_getDatabase).call(this);
44
+ __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), JSON.stringify([...database, item]));
45
+ });
46
+ }
47
+ getItem(id) {
48
+ throw new Error('Method not implemented.' + id);
49
+ }
50
+ removeItem(id) {
51
+ throw new Error('Method not implemented.' + id);
52
+ }
53
+ getAllItems() {
54
+ return Promise.resolve(__classPrivateFieldGet(this, _LocalStorage_instances, "m", _LocalStorage_getDatabase).call(this));
55
+ }
56
+ }
57
+ exports.LocalStorage = LocalStorage;
58
+ _LocalStorage_localStorage = new WeakMap(), _LocalStorage_databaseName = new WeakMap(), _LocalStorage_instances = new WeakSet(), _LocalStorage_getDatabase = function _LocalStorage_getDatabase() {
59
+ let database = __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").getItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"));
60
+ if (database === null || database === undefined) {
61
+ database = JSON.stringify([]);
62
+ __classPrivateFieldGet(this, _LocalStorage_localStorage, "f").setItem(__classPrivateFieldGet(this, _LocalStorage_databaseName, "f"), database);
63
+ }
64
+ return JSON.parse(database);
65
+ };
@@ -0,0 +1,39 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const types_1 = require("../../types");
13
+ const local_storage_1 = require("./local-storage");
14
+ const batch = {
15
+ source: 'test',
16
+ event: 'testEvent',
17
+ description: 'Test event',
18
+ original_created_at: new Date().toISOString(),
19
+ type: types_1.EventLogType.EVENT,
20
+ };
21
+ describe('LocalStorageDatabase', () => {
22
+ let localStorageDatabase;
23
+ beforeEach(() => {
24
+ localStorageDatabase = new local_storage_1.LocalStorage();
25
+ });
26
+ afterEach(() => {
27
+ localStorage.clear();
28
+ });
29
+ it('should set an item in the database', () => __awaiter(void 0, void 0, void 0, function* () {
30
+ yield localStorageDatabase.setItem(batch);
31
+ const database = JSON.parse(localStorage.getItem('YUNO_EVENT_LOGS') || '[]');
32
+ expect(database).toContainEqual(batch);
33
+ }));
34
+ it('should get an item from the database', () => __awaiter(void 0, void 0, void 0, function* () {
35
+ localStorage.setItem('YUNO_EVENT_LOGS', JSON.stringify([batch]));
36
+ const result = yield localStorageDatabase.getAllItems();
37
+ expect(result).toEqual([batch]);
38
+ }));
39
+ });
@@ -0,0 +1 @@
1
+ export * from './memory-storage';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./memory-storage"), exports);
@@ -0,0 +1,11 @@
1
+ import { Batch } from '../../types';
2
+ import { Database } from '../database.types';
3
+ export declare class MemoryStorage implements Database {
4
+ #private;
5
+ constructor();
6
+ reset(): Promise<void>;
7
+ setItem(item: Batch): Promise<void>;
8
+ getItem(id: string): Promise<Batch>;
9
+ removeItem(id: string): Promise<void>;
10
+ getAllItems(): Promise<Batch[]>;
11
+ }