@amedia/user 0.12.33 → 0.13.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/README.md +54 -2
- package/dist/actions/authorization.d.ts +1 -1
- package/dist/clients/mercury.d.ts +5 -13
- package/dist/eventHandlers/activation.d.ts +1 -1
- package/dist/index.d.ts +5 -13
- package/dist/index.js +1 -1
- package/dist/lib/PaywallUnlockRequest.d.ts +1 -0
- package/dist/lib/UserDataRequest.spec.d.ts +4 -0
- package/dist/utils/log.d.ts +5 -10
- package/dist/utils/log.spec.d.ts +1 -0
- package/eik.json +1 -1
- package/package.json +1 -1
- package/src/actions/authorization.ts +29 -5
- package/src/actions/userData.ts +1 -1
- package/src/autologin/thirdPartyCookie.ts +1 -2
- package/src/cache/index.spec.ts +21 -6
- package/src/cache/index.ts +2 -2
- package/src/clients/europa.ts +1 -2
- package/src/clients/jupiter.ts +4 -4
- package/src/clients/mercury.spec.ts +33 -13
- package/src/clients/mercury.ts +13 -15
- package/src/clients/nebula.ts +2 -2
- package/src/clients/pluto.ts +1 -2
- package/src/eventHandlers/activation.ts +3 -3
- package/src/index.ts +13 -14
- package/src/lib/PaywallUnlockRequest.spec.ts +7 -4
- package/src/lib/PaywallUnlockRequest.ts +36 -26
- package/src/utils/log.spec.ts +153 -0
- package/src/utils/log.ts +66 -17
- package/amedia-user-js.iml +0 -9
- package/src/utils/logValidationError.ts +0 -22
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import log, { setLogger } from './log';
|
|
2
|
+
import restoreAllMocks = jest.restoreAllMocks;
|
|
3
|
+
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
restoreAllMocks();
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
describe('Log', () => {
|
|
9
|
+
let logger;
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
logger = {
|
|
12
|
+
debug: jest.fn(),
|
|
13
|
+
info: jest.fn(),
|
|
14
|
+
warn: jest.fn(),
|
|
15
|
+
error: jest.fn(),
|
|
16
|
+
};
|
|
17
|
+
setLogger(logger);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
function itShouldLogMessage(message: string, level: string = 'error') {
|
|
21
|
+
it('should log message', () => {
|
|
22
|
+
expect(logger[level]).toHaveBeenCalledWith(message, expect.anything());
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function itShouldLogContext(context: Object, level: string = 'error') {
|
|
27
|
+
it('should log context', () => {
|
|
28
|
+
expect(logger[level]).toHaveBeenCalledWith(expect.anything(), {
|
|
29
|
+
...context,
|
|
30
|
+
origin: '@amedia/user',
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function expectRewrittenError(message: string) {
|
|
36
|
+
const matcher = new RegExp(`Error: ${message}\n(^\\s+at .+$\\n){3,}`, 'm');
|
|
37
|
+
return {
|
|
38
|
+
message,
|
|
39
|
+
stack: expect.stringMatching(matcher),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe('with stringed message', () => {
|
|
44
|
+
beforeEach(() => {
|
|
45
|
+
log.error('Epic fail');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
itShouldLogMessage('Epic fail');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('with all error levels', () => {
|
|
52
|
+
beforeEach(() => {
|
|
53
|
+
log.debug('debug');
|
|
54
|
+
log.info('info');
|
|
55
|
+
log.warn('warn');
|
|
56
|
+
log.error('error');
|
|
57
|
+
});
|
|
58
|
+
itShouldLogMessage('debug', 'debug');
|
|
59
|
+
itShouldLogMessage('info', 'info');
|
|
60
|
+
itShouldLogMessage('warn', 'warn');
|
|
61
|
+
itShouldLogMessage('error', 'error');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('with stringed message and context', () => {
|
|
65
|
+
beforeEach(() => {
|
|
66
|
+
log.error('Epic fail', {
|
|
67
|
+
my: 'context',
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
itShouldLogMessage('Epic fail');
|
|
71
|
+
itShouldLogContext({
|
|
72
|
+
my: 'context',
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('with stringed message and complex context', () => {
|
|
77
|
+
beforeEach(() => {
|
|
78
|
+
log.error('Epic fail', {
|
|
79
|
+
my: 'context',
|
|
80
|
+
more: {
|
|
81
|
+
nested: 'context',
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
itShouldLogMessage('Epic fail');
|
|
86
|
+
itShouldLogContext({
|
|
87
|
+
my: 'context',
|
|
88
|
+
more: {
|
|
89
|
+
nested: 'context',
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe('with Error as message', () => {
|
|
95
|
+
beforeEach(() => {
|
|
96
|
+
log.error(new Error('Epic fail'));
|
|
97
|
+
});
|
|
98
|
+
itShouldLogMessage('Epic fail');
|
|
99
|
+
itShouldLogContext({
|
|
100
|
+
error: expectRewrittenError('Epic fail'),
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe('with Error as message and context', () => {
|
|
105
|
+
beforeEach(() => {
|
|
106
|
+
log.error(new Error('Epic fail'), {
|
|
107
|
+
my: 'context',
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
itShouldLogMessage('Epic fail');
|
|
111
|
+
itShouldLogContext({
|
|
112
|
+
my: 'context',
|
|
113
|
+
error: expectRewrittenError('Epic fail'),
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('with message as string and Errors in context', () => {
|
|
118
|
+
beforeEach(() => {
|
|
119
|
+
log.error('Epic fail', {
|
|
120
|
+
failError: new Error('Much fail'),
|
|
121
|
+
boomError: new Error('Very boom'),
|
|
122
|
+
my: 'context',
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
itShouldLogMessage('Epic fail');
|
|
126
|
+
itShouldLogContext({
|
|
127
|
+
my: 'context',
|
|
128
|
+
failError: expectRewrittenError('Much fail'),
|
|
129
|
+
boomError: expectRewrittenError('Very boom'),
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('with Error directly as context', () => {
|
|
134
|
+
beforeEach(() => {
|
|
135
|
+
log.error('Epic fail', new Error('Downhill from here'));
|
|
136
|
+
});
|
|
137
|
+
itShouldLogMessage('Epic fail');
|
|
138
|
+
itShouldLogContext({
|
|
139
|
+
error: expectRewrittenError('Downhill from here'),
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe('with Error both at message and context', () => {
|
|
144
|
+
beforeEach(() => {
|
|
145
|
+
log.error(new Error('Epic fail'), new Error('Downhill from here'));
|
|
146
|
+
});
|
|
147
|
+
itShouldLogMessage('Epic fail');
|
|
148
|
+
itShouldLogContext({
|
|
149
|
+
error: expectRewrittenError('Epic fail'),
|
|
150
|
+
contextError: expectRewrittenError('Downhill from here'),
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
});
|
package/src/utils/log.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import mercury from '../clients/mercury';
|
|
2
2
|
|
|
3
3
|
export type Logger = {
|
|
4
|
-
debug: (message: string |
|
|
5
|
-
info: (message: string |
|
|
6
|
-
warn: (message: string |
|
|
7
|
-
error: (message: string |
|
|
4
|
+
debug: (message: string | Error, context?: Object) => void;
|
|
5
|
+
info: (message: string | Error, context?: Object) => void;
|
|
6
|
+
warn: (message: string | Error, context?: Object) => void;
|
|
7
|
+
error: (message: string | Error, context?: Object) => void;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
let logger: Logger;
|
|
@@ -15,17 +15,66 @@ export const setLogger = (engine: Logger) => {
|
|
|
15
15
|
|
|
16
16
|
const getLogger = () => logger || mercury;
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
18
|
+
const rewriteErrorObject = (
|
|
19
|
+
error: Error
|
|
20
|
+
): { [key: string]: unknown } & { message: string } => {
|
|
21
|
+
const rewrittenError = {
|
|
22
|
+
message: error.message,
|
|
23
|
+
};
|
|
24
|
+
Object.getOwnPropertyNames(error).forEach((key) => {
|
|
25
|
+
rewrittenError[key] = error[key];
|
|
26
|
+
});
|
|
27
|
+
return rewrittenError;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const processIncomingLogData = (
|
|
31
|
+
message: string | Error,
|
|
32
|
+
context: Object = {}
|
|
33
|
+
): Promise<{ message: string; context: Object }> => {
|
|
34
|
+
let internalMessage = message;
|
|
35
|
+
let internalContext = context || {};
|
|
36
|
+
if (internalMessage instanceof Error) {
|
|
37
|
+
const rewrittenErrorObject = rewriteErrorObject(internalMessage);
|
|
38
|
+
internalMessage = rewrittenErrorObject.message;
|
|
39
|
+
internalContext = { ...internalContext, error: rewrittenErrorObject };
|
|
40
|
+
}
|
|
41
|
+
internalContext = { ...internalContext, origin: '@amedia/user' };
|
|
42
|
+
if (context instanceof Error) {
|
|
43
|
+
const parsedContextError = rewriteErrorObject(context);
|
|
44
|
+
internalContext = {
|
|
45
|
+
...internalContext,
|
|
46
|
+
...{
|
|
47
|
+
[Object.keys(internalContext).includes('error')
|
|
48
|
+
? 'contextError'
|
|
49
|
+
: 'error']: parsedContextError,
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
} else {
|
|
53
|
+
Object.keys(internalContext).forEach((key) => {
|
|
54
|
+
if (internalContext[key] instanceof Error) {
|
|
55
|
+
internalContext[key] = rewriteErrorObject(internalContext[key]);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
return Promise.resolve({
|
|
60
|
+
message: internalMessage,
|
|
61
|
+
context: internalContext,
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const processAndLog = (
|
|
66
|
+
level: keyof Logger,
|
|
67
|
+
message: string | Error,
|
|
68
|
+
context?: Object
|
|
69
|
+
) => {
|
|
70
|
+
processIncomingLogData(message, context).then(({ message: m, context: c }) =>
|
|
71
|
+
getLogger()[level](m, c)
|
|
72
|
+
);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export default <Logger>{
|
|
76
|
+
debug: (message, context) => processAndLog('debug', message, context),
|
|
77
|
+
info: (message, context) => processAndLog('info', message, context),
|
|
78
|
+
warn: (message, context) => processAndLog('warn', message, context),
|
|
79
|
+
error: (message, context) => processAndLog('error', message, context),
|
|
31
80
|
};
|
package/amedia-user-js.iml
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
-
<module type="WEB_MODULE" version="4">
|
|
3
|
-
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
|
4
|
-
<exclude-output />
|
|
5
|
-
<content url="file://$MODULE_DIR$" />
|
|
6
|
-
<orderEntry type="inheritedJdk" />
|
|
7
|
-
<orderEntry type="sourceFolder" forTests="false" />
|
|
8
|
-
</component>
|
|
9
|
-
</module>
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { ValidationError } from 'runtypes';
|
|
2
|
-
import log from './log';
|
|
3
|
-
|
|
4
|
-
export default function logValidationError(
|
|
5
|
-
name: string,
|
|
6
|
-
error: Error,
|
|
7
|
-
extra?: Object
|
|
8
|
-
) {
|
|
9
|
-
let message: string;
|
|
10
|
-
const args: any[] = [];
|
|
11
|
-
if (error instanceof ValidationError) {
|
|
12
|
-
message = `Invalid ${name}`;
|
|
13
|
-
args.push(error.details);
|
|
14
|
-
} else {
|
|
15
|
-
message = `Unknown error after validating ${name}: (${error.message}`;
|
|
16
|
-
args.push(error);
|
|
17
|
-
}
|
|
18
|
-
if (extra) {
|
|
19
|
-
args.push(extra);
|
|
20
|
-
}
|
|
21
|
-
log.error(message, ...args);
|
|
22
|
-
}
|