@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
package/dist/cjs/index.test.js
CHANGED
|
@@ -9,10 +9,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
/* eslint-disable no-magic-numbers */
|
|
12
13
|
const index_1 = require("./index");
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
14
|
+
const types_1 = require("./types");
|
|
15
|
+
const node_1 = require("msw/node");
|
|
16
|
+
const msw_1 = require("msw");
|
|
16
17
|
const eventArgs = {
|
|
17
18
|
source: 'test',
|
|
18
19
|
event: 'testEvent',
|
|
@@ -24,92 +25,173 @@ const logArgs = {
|
|
|
24
25
|
url: '',
|
|
25
26
|
method: '',
|
|
26
27
|
country: '',
|
|
27
|
-
original_created_at: new Date().toISOString()
|
|
28
|
+
original_created_at: new Date().toISOString(),
|
|
29
|
+
};
|
|
30
|
+
const browserClientInfo = {
|
|
31
|
+
browser: 'jsdom',
|
|
32
|
+
browserMajorVersion: 20,
|
|
33
|
+
browserVersion: '20.0.3',
|
|
34
|
+
cookies: true,
|
|
35
|
+
mobile: false,
|
|
36
|
+
os: '-',
|
|
37
|
+
osVersion: '-',
|
|
38
|
+
screen: '',
|
|
28
39
|
};
|
|
40
|
+
const metadata = {
|
|
41
|
+
browser_info: browserClientInfo,
|
|
42
|
+
};
|
|
43
|
+
const publicApiKey = 'prod_1';
|
|
44
|
+
let mockInterceptRequest = jest.fn();
|
|
45
|
+
const server = (0, node_1.setupServer)(
|
|
46
|
+
// Describe the requests to mock.
|
|
47
|
+
msw_1.rest.post('https://api.y.uno/v1/sdk/event-log/publish', (req, res, ctx) => {
|
|
48
|
+
mockInterceptRequest(req.body);
|
|
49
|
+
return res(ctx.status(200));
|
|
50
|
+
}));
|
|
29
51
|
describe('EventLog', () => {
|
|
52
|
+
let eventLog;
|
|
53
|
+
beforeAll(() => {
|
|
54
|
+
mockInterceptRequest = jest.fn();
|
|
55
|
+
server.listen();
|
|
56
|
+
});
|
|
57
|
+
// Reset any request handlers that we may add during the tests,
|
|
58
|
+
// so they don't affect other tests.
|
|
59
|
+
afterEach(() => server.resetHandlers());
|
|
60
|
+
// Clean up after the tests are finished.
|
|
61
|
+
afterAll(() => server.close());
|
|
30
62
|
beforeEach(() => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
publicApiKey: PUBLIC_API_KEY,
|
|
35
|
-
batchTime: 500,
|
|
36
|
-
enableEvents: true,
|
|
37
|
-
enableLogs: true,
|
|
63
|
+
eventLog = new index_1.EventLog({
|
|
64
|
+
organizationName: 'testOrg',
|
|
65
|
+
debug: false,
|
|
38
66
|
});
|
|
39
|
-
database_1.database.reset();
|
|
40
67
|
});
|
|
41
|
-
afterEach(() =>
|
|
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
|
-
|
|
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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
68
|
+
afterEach(() => {
|
|
69
|
+
jest.clearAllMocks();
|
|
70
|
+
});
|
|
71
|
+
describe('event', () => {
|
|
72
|
+
it('should add an event to the database if events are enabled', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
73
|
+
eventLog.event(eventArgs, publicApiKey);
|
|
74
|
+
eventLog.sendBatch();
|
|
75
|
+
yield new Promise(process.nextTick);
|
|
76
|
+
expect(mockInterceptRequest).toHaveBeenCalledWith({
|
|
77
|
+
batch: [
|
|
78
|
+
Object.assign(Object.assign({}, eventArgs), { os: browserClientInfo.os, type: types_1.EventLogType.EVENT }),
|
|
79
|
+
],
|
|
80
|
+
});
|
|
81
|
+
}));
|
|
82
|
+
it('should not add an event to the database if events are disabled', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
83
|
+
const publicApiKey = 'testApiKey';
|
|
84
|
+
eventLog = new index_1.EventLog({
|
|
85
|
+
organizationName: 'testOrg',
|
|
86
|
+
enableEvents: false,
|
|
87
|
+
});
|
|
88
|
+
eventLog.event(eventArgs, publicApiKey);
|
|
89
|
+
eventLog.sendBatch();
|
|
90
|
+
yield new Promise(process.nextTick);
|
|
91
|
+
expect(mockInterceptRequest).not.toHaveBeenCalled();
|
|
92
|
+
}));
|
|
93
|
+
});
|
|
94
|
+
describe('logger', () => {
|
|
95
|
+
it('should add an error log to the database if logs are enabled', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
96
|
+
eventLog.logger.error(logArgs, publicApiKey);
|
|
97
|
+
eventLog.sendBatch();
|
|
98
|
+
yield new Promise(process.nextTick);
|
|
99
|
+
expect(mockInterceptRequest).toHaveBeenCalledWith({
|
|
100
|
+
batch: [
|
|
101
|
+
Object.assign(Object.assign({}, logArgs), { os: browserClientInfo.os, metadata, type: types_1.EventLogType.LOG, level: types_1.Level.ERROR }),
|
|
102
|
+
],
|
|
103
|
+
});
|
|
104
|
+
}));
|
|
105
|
+
it('should not add an error log to the database if logs are disabled', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
106
|
+
const publicApiKey = 'testApiKey';
|
|
107
|
+
eventLog = new index_1.EventLog({
|
|
108
|
+
organizationName: 'testOrg',
|
|
109
|
+
enableEvents: true,
|
|
110
|
+
enableLogs: false,
|
|
111
|
+
debug: false,
|
|
112
|
+
});
|
|
113
|
+
eventLog.logger.error(logArgs, publicApiKey);
|
|
114
|
+
eventLog.sendBatch();
|
|
115
|
+
yield new Promise(process.nextTick);
|
|
116
|
+
expect(mockInterceptRequest).not.toHaveBeenCalled();
|
|
117
|
+
}));
|
|
118
|
+
it('should add a debug log to the database if logs are enabled', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
119
|
+
eventLog.logger.debug(logArgs, publicApiKey);
|
|
120
|
+
eventLog.sendBatch();
|
|
121
|
+
yield new Promise(process.nextTick);
|
|
122
|
+
expect(mockInterceptRequest).toHaveBeenCalledWith({
|
|
123
|
+
batch: [
|
|
124
|
+
Object.assign(Object.assign({}, logArgs), { os: browserClientInfo.os, metadata, type: types_1.EventLogType.LOG, level: types_1.Level.DEBUG }),
|
|
125
|
+
],
|
|
126
|
+
});
|
|
127
|
+
}));
|
|
128
|
+
it('should not add a debug log to the database if logs are disabled', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
129
|
+
const publicApiKey = 'testApiKey';
|
|
130
|
+
eventLog = new index_1.EventLog({
|
|
131
|
+
organizationName: 'testOrg',
|
|
132
|
+
platform: 'web',
|
|
133
|
+
enableEvents: true,
|
|
134
|
+
enableLogs: false,
|
|
135
|
+
debug: false,
|
|
136
|
+
});
|
|
137
|
+
eventLog.logger.debug(logArgs, publicApiKey);
|
|
138
|
+
eventLog.sendBatch();
|
|
139
|
+
yield new Promise(process.nextTick);
|
|
140
|
+
expect(mockInterceptRequest).not.toHaveBeenCalled();
|
|
141
|
+
}));
|
|
142
|
+
it('should add an info log to the database if logs are enabled', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
143
|
+
eventLog.logger.info(logArgs, publicApiKey);
|
|
144
|
+
eventLog.sendBatch();
|
|
145
|
+
yield new Promise(process.nextTick);
|
|
146
|
+
expect(mockInterceptRequest).toHaveBeenCalledWith({
|
|
147
|
+
batch: [
|
|
148
|
+
Object.assign(Object.assign({}, logArgs), { os: browserClientInfo.os, metadata, type: types_1.EventLogType.LOG, level: types_1.Level.INFO }),
|
|
149
|
+
],
|
|
150
|
+
});
|
|
151
|
+
}));
|
|
152
|
+
it('should not add an info log to the database if logs are disabled', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
153
|
+
eventLog = new index_1.EventLog({
|
|
154
|
+
organizationName: 'testOrg',
|
|
155
|
+
platform: 'web',
|
|
156
|
+
enableEvents: true,
|
|
157
|
+
enableLogs: false,
|
|
158
|
+
debug: false,
|
|
159
|
+
});
|
|
160
|
+
eventLog.sendBatch();
|
|
161
|
+
yield new Promise(process.nextTick);
|
|
162
|
+
expect(mockInterceptRequest).not.toHaveBeenCalled();
|
|
163
|
+
}));
|
|
164
|
+
});
|
|
165
|
+
describe('sendBatch', () => {
|
|
166
|
+
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* () {
|
|
167
|
+
yield eventLog.sendBatch();
|
|
168
|
+
yield new Promise(process.nextTick);
|
|
169
|
+
expect(mockInterceptRequest).not.toHaveBeenCalled();
|
|
170
|
+
}));
|
|
171
|
+
});
|
|
172
|
+
describe('Cron', () => {
|
|
173
|
+
it('should send a batch of items to the API', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
174
|
+
eventLog = new index_1.EventLog({
|
|
175
|
+
organizationName: 'testOrg',
|
|
176
|
+
platform: 'web',
|
|
177
|
+
batchTime: 1000,
|
|
178
|
+
enableEvents: true,
|
|
179
|
+
enableLogs: true,
|
|
180
|
+
debug: false,
|
|
181
|
+
});
|
|
182
|
+
yield eventLog.event(eventArgs, publicApiKey);
|
|
183
|
+
yield eventLog.logger.error(logArgs, publicApiKey);
|
|
184
|
+
yield eventLog.logger.debug(logArgs, publicApiKey);
|
|
185
|
+
yield eventLog.logger.info(logArgs, publicApiKey);
|
|
186
|
+
yield new Promise((resolve) => setTimeout(resolve, 1200));
|
|
187
|
+
expect(mockInterceptRequest).toHaveBeenCalledWith({
|
|
188
|
+
batch: [
|
|
189
|
+
Object.assign(Object.assign({}, eventArgs), { os: browserClientInfo.os, type: types_1.EventLogType.EVENT }),
|
|
190
|
+
Object.assign(Object.assign({}, logArgs), { os: browserClientInfo.os, metadata, type: types_1.EventLogType.LOG, level: types_1.Level.ERROR }),
|
|
191
|
+
Object.assign(Object.assign({}, logArgs), { os: browserClientInfo.os, metadata, type: types_1.EventLogType.LOG, level: types_1.Level.DEBUG }),
|
|
192
|
+
Object.assign(Object.assign({}, logArgs), { os: browserClientInfo.os, metadata, type: types_1.EventLogType.LOG, level: types_1.Level.INFO }),
|
|
193
|
+
],
|
|
194
|
+
});
|
|
195
|
+
}));
|
|
196
|
+
});
|
|
115
197
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export type InitArgs = {
|
|
2
|
-
|
|
3
|
-
batchTime
|
|
4
|
-
enableEvents
|
|
5
|
-
enableLogs
|
|
2
|
+
organizationName: string;
|
|
3
|
+
batchTime?: number;
|
|
4
|
+
enableEvents?: boolean;
|
|
5
|
+
enableLogs?: boolean;
|
|
6
6
|
platform?: string;
|
|
7
7
|
debug?: boolean;
|
|
8
8
|
};
|
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { Database } from './database';
|
|
2
|
+
import { BrowserClientInfo, Level, Log } from './types';
|
|
3
|
+
export declare function setLog(logArgs: Omit<Log, 'level' | 'type'>, level: Level, enableLog: boolean, database: Database, publicApiKey: string, browserInfo: BrowserClientInfo | null): Promise<void> | undefined;
|
|
4
|
+
declare let browserClientInfo: BrowserClientInfo | null;
|
|
5
|
+
export { browserClientInfo };
|
package/dist/cjs/index.utils.js
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setLog = void 0;
|
|
4
|
-
const database_1 = require("./database");
|
|
3
|
+
exports.browserClientInfo = exports.setLog = void 0;
|
|
5
4
|
const types_1 = require("./types");
|
|
6
|
-
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
const utils_2 = require("./utils");
|
|
7
|
+
function setLog(logArgs, level, enableLog, database, publicApiKey, browserInfo) {
|
|
7
8
|
if (!enableLog) {
|
|
8
9
|
return;
|
|
9
10
|
}
|
|
10
|
-
return
|
|
11
|
+
return database.setItem(Object.assign(Object.assign({ os: browserInfo === null || browserInfo === void 0 ? void 0 : browserInfo.os }, logArgs), { metadata: Object.assign(Object.assign({}, logArgs.metadata), { browser_info: browserInfo }), level, type: types_1.EventLogType.LOG }), publicApiKey);
|
|
11
12
|
}
|
|
12
13
|
exports.setLog = setLog;
|
|
14
|
+
let browserClientInfo = null;
|
|
15
|
+
exports.browserClientInfo = browserClientInfo;
|
|
16
|
+
if ((0, utils_1.isBrowser)()) {
|
|
17
|
+
(0, utils_2.clientDetection)().then((clientInfo) => {
|
|
18
|
+
exports.browserClientInfo = browserClientInfo = clientInfo;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
@@ -23,7 +23,7 @@ export type Log = BaseEventLog & {
|
|
|
23
23
|
response?: object | null;
|
|
24
24
|
status_code?: string | null;
|
|
25
25
|
trace_id?: string | null;
|
|
26
|
-
metadata?:
|
|
26
|
+
metadata?: object | null;
|
|
27
27
|
country?: string | null;
|
|
28
28
|
organization_name?: string | null;
|
|
29
29
|
level: Level;
|
|
@@ -38,4 +38,14 @@ export type Event = BaseEventLog & {
|
|
|
38
38
|
headers?: object;
|
|
39
39
|
};
|
|
40
40
|
export type Batch = Log | Event;
|
|
41
|
+
export type BrowserClientInfo = {
|
|
42
|
+
screen: string;
|
|
43
|
+
browser: string;
|
|
44
|
+
browserVersion: string;
|
|
45
|
+
browserMajorVersion: number;
|
|
46
|
+
mobile: boolean;
|
|
47
|
+
os: string;
|
|
48
|
+
osVersion: string | number | undefined;
|
|
49
|
+
cookies: boolean;
|
|
50
|
+
};
|
|
41
51
|
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const check_environment_1 = require("../check-environment");
|
|
4
|
+
describe('isLocalStorageEnabled', () => {
|
|
5
|
+
it('should return true if local storage is enabled', () => {
|
|
6
|
+
expect((0, check_environment_1.isLocalStorageEnabled)()).toBe(true);
|
|
7
|
+
});
|
|
8
|
+
it('should return false if local storage is disabled', () => {
|
|
9
|
+
const localStorageMock = {
|
|
10
|
+
getItem: jest.fn(() => null),
|
|
11
|
+
setItem: jest.fn(() => {
|
|
12
|
+
throw new Error('Local storage is disabled');
|
|
13
|
+
}),
|
|
14
|
+
removeItem: jest.fn(() => null),
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(window, 'localStorage', {
|
|
17
|
+
value: localStorageMock,
|
|
18
|
+
});
|
|
19
|
+
expect((0, check_environment_1.isLocalStorageEnabled)()).toBe(false);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
describe('isBrowser', () => {
|
|
23
|
+
it('should return true if running in a browser environment', () => {
|
|
24
|
+
expect((0, check_environment_1.isBrowser)()).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
it('should return false if running in a non-browser environment', () => {
|
|
27
|
+
const isBrowserMock = new Function('try {return this===window;}catch(e){ return false;}').bind({});
|
|
28
|
+
expect(isBrowserMock()).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
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
|
+
/* eslint-disable no-magic-numbers */
|
|
13
|
+
const client_detection_1 = require("../client-detection");
|
|
14
|
+
describe('clientDetection', () => {
|
|
15
|
+
it('should return an object with screen, browser, browserVersion, browserMajorVersion, mobile, os, osVersion, and cookies properties', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
const result = yield (0, client_detection_1.clientDetection)();
|
|
17
|
+
expect(result).toHaveProperty('screen');
|
|
18
|
+
expect(result).toHaveProperty('browser');
|
|
19
|
+
expect(result).toHaveProperty('browserVersion');
|
|
20
|
+
expect(result).toHaveProperty('browserMajorVersion');
|
|
21
|
+
expect(result).toHaveProperty('mobile');
|
|
22
|
+
expect(result).toHaveProperty('os');
|
|
23
|
+
expect(result).toHaveProperty('osVersion');
|
|
24
|
+
expect(result).toHaveProperty('cookies');
|
|
25
|
+
}));
|
|
26
|
+
it('should correctly detect the browser and browser version', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
|
+
const result = yield (0, client_detection_1.clientDetection)();
|
|
28
|
+
expect(result.browser).toBe('jsdom');
|
|
29
|
+
expect(result.browserVersion).toBe('20.0.3');
|
|
30
|
+
expect(result.browserMajorVersion).toBeGreaterThanOrEqual(20);
|
|
31
|
+
}));
|
|
32
|
+
it('should correctly detect whether the client is mobile', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
33
|
+
const result = yield (0, client_detection_1.clientDetection)();
|
|
34
|
+
expect(result.mobile).toBe(false);
|
|
35
|
+
}));
|
|
36
|
+
it('should correctly detect whether cookies are enabled', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
37
|
+
const result = yield (0, client_detection_1.clientDetection)();
|
|
38
|
+
expect(result.cookies).toBe(true);
|
|
39
|
+
}));
|
|
40
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isBrowser = exports.isLocalStorageEnabled = void 0;
|
|
4
|
+
function isLocalStorageEnabled() {
|
|
5
|
+
try {
|
|
6
|
+
const key = '__yuno_storage__test';
|
|
7
|
+
window.localStorage.setItem(key, 'test_value');
|
|
8
|
+
window.localStorage.removeItem(key);
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
catch (e) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.isLocalStorageEnabled = isLocalStorageEnabled;
|
|
16
|
+
// https://stackoverflow.com/questions/17575790/environment-detection-node-js-or-browser
|
|
17
|
+
exports.isBrowser = new Function('try {return this===window;}catch(e){ return false;}');
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BrowserClientInfo } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* JavaScript Client Detection
|
|
4
|
+
* (C) viazenetti GmbH (Christian Ludwig)
|
|
5
|
+
* https://stackoverflow.com/questions/9514179/how-to-find-the-operating-system-details-using-javascript
|
|
6
|
+
* this package is better but too weighty: https://www.npmjs.com/package/ua-parser-js
|
|
7
|
+
*/
|
|
8
|
+
export declare function clientDetection(): Promise<BrowserClientInfo>;
|