@amedia/user 0.13.25 → 0.13.27
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/@types/amedia-frontend-rpc.d.ts +11 -0
- package/@types/browserid.d.ts +4 -0
- package/checkVersion.js +12 -0
- package/dist/actions/authorization.d.ts +5 -5
- package/dist/autologin/index.d.ts +4 -0
- package/dist/clients/europa.d.ts +3 -7
- package/dist/clients/mercuryLog.d.ts +5 -4
- package/dist/index.d.ts +78 -35
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +3 -3
- package/dist/index.js +1 -1
- package/dist/index.js.map +3 -3
- package/dist/janitor/jobs/daxsubCookie.d.ts +2 -1
- package/dist/lib/PaywallUnlockRequest.d.ts +1 -1
- package/dist/lib/UserDataRequest.d.ts +1 -1
- package/dist/lib/rpc.d.ts +5 -5
- package/dist/utils/customEventWrapper.d.ts +5 -3
- package/dist/utils/debouncer.d.ts +26 -0
- package/dist/utils/debouncer.spec.d.ts +1 -0
- package/dist/utils/deepEquals.d.ts +0 -3
- package/dist/utils/log.d.ts +4 -4
- package/dist/utils/queue.d.ts +2 -2
- package/dist/utils/type.d.ts +6 -0
- package/eik.json +1 -1
- package/package.json +4 -3
- package/src/actions/authorization.spec.ts +13 -9
- package/src/actions/authorization.ts +7 -5
- package/src/autologin/index.spec.ts +3 -3
- package/src/autologin/index.ts +6 -1
- package/src/cache/index.spec.ts +2 -2
- package/src/cache/index.ts +1 -2
- package/src/clients/europa.spec.ts +11 -16
- package/src/clients/europa.ts +47 -41
- package/src/clients/mercuryLog.ts +22 -12
- package/src/clients/pluto.spec.ts +4 -3
- package/src/eventHandlers/state.spec.ts +2 -2
- package/src/eventHandlers/storage.spec.ts +2 -2
- package/src/eventHandlers/userAttributes.spec.ts +2 -2
- package/src/index-authentcation.spec.ts +1 -1
- package/src/index-storage.spec.ts +3 -3
- package/src/index-subscription.spec.ts +1 -1
- package/src/janitor/jobs/accessCookieRefresher.spec.ts +8 -2
- package/src/janitor/jobs/daxsubCookie.spec.ts +8 -2
- package/src/janitor/jobs/daxsubCookie.ts +4 -3
- package/src/lib/PaywallUnlockRequest.spec.ts +12 -12
- package/src/lib/PaywallUnlockRequest.ts +6 -3
- package/src/lib/UserDataRequest.spec.ts +3 -2
- package/src/lib/UserDataRequest.ts +4 -4
- package/src/lib/rpc.ts +7 -9
- package/src/store/index.ts +2 -0
- package/src/utils/customEventWrapper.ts +6 -6
- package/src/utils/debouncer.spec.ts +257 -0
- package/src/utils/debouncer.ts +103 -0
- package/src/utils/deepEquals.ts +12 -11
- package/src/utils/getOrUpdate.ts +1 -1
- package/src/utils/log.spec.ts +4 -4
- package/src/utils/log.ts +24 -13
- package/src/utils/queue.ts +4 -4
- package/src/utils/type.ts +19 -0
- package/tsconfig.eslint.json +2 -0
- package/tsconfig.json +2 -1
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import restoreAllMocks = jest.restoreAllMocks;
|
|
2
|
+
import Debouncer from './debouncer';
|
|
3
|
+
|
|
4
|
+
beforeEach(async () => {
|
|
5
|
+
restoreAllMocks();
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
describe('Debouncer', () => {
|
|
9
|
+
describe('when params is list of arrays and response is key value object', () => {
|
|
10
|
+
let debouncer: Debouncer<string[], Record<string, string>>;
|
|
11
|
+
const params1 = ['foo', 'bar'];
|
|
12
|
+
const params2 = ['baz', '1337'];
|
|
13
|
+
describe('when workload succeeds', () => {
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
debouncer = new Debouncer<string[], Record<string, string>>(
|
|
16
|
+
10,
|
|
17
|
+
[],
|
|
18
|
+
Debouncer.stringListParamsAccumulator,
|
|
19
|
+
(params) =>
|
|
20
|
+
Promise.resolve(
|
|
21
|
+
params.reduce(
|
|
22
|
+
(map, param): Record<string, string> => ({
|
|
23
|
+
...map,
|
|
24
|
+
[param]: param.toUpperCase(),
|
|
25
|
+
}),
|
|
26
|
+
{} as Record<string, string>
|
|
27
|
+
)
|
|
28
|
+
)
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
describe('when called in quick succession', () => {
|
|
32
|
+
let result1: Record<string, string>;
|
|
33
|
+
let result2: Record<string, string>;
|
|
34
|
+
const expectedResult = {
|
|
35
|
+
foo: 'FOO',
|
|
36
|
+
bar: 'BAR',
|
|
37
|
+
baz: 'BAZ',
|
|
38
|
+
1337: '1337',
|
|
39
|
+
};
|
|
40
|
+
beforeEach(async () => {
|
|
41
|
+
const promise1 = debouncer.perform(params1);
|
|
42
|
+
const promise2 = debouncer.perform(params2);
|
|
43
|
+
result1 = await promise1;
|
|
44
|
+
result2 = await promise2;
|
|
45
|
+
});
|
|
46
|
+
it('Resolution to first promise is complete set', () => {
|
|
47
|
+
expect(result1).toEqual(expectedResult);
|
|
48
|
+
});
|
|
49
|
+
it('Resolution to second promise is complete set', () => {
|
|
50
|
+
expect(result2).toEqual(expectedResult);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
describe('when waiting for first resolution before second call', () => {
|
|
54
|
+
let result1: Record<string, string>;
|
|
55
|
+
let result2: Record<string, string>;
|
|
56
|
+
const expectedResult1 = {
|
|
57
|
+
foo: 'FOO',
|
|
58
|
+
bar: 'BAR',
|
|
59
|
+
};
|
|
60
|
+
const expectedResult2 = {
|
|
61
|
+
baz: 'BAZ',
|
|
62
|
+
1337: '1337',
|
|
63
|
+
};
|
|
64
|
+
beforeEach(async () => {
|
|
65
|
+
result1 = await debouncer.perform(params1);
|
|
66
|
+
result2 = await debouncer.perform(params2);
|
|
67
|
+
});
|
|
68
|
+
it('Resolution to first promise is only result of first set', () => {
|
|
69
|
+
expect(result1).toEqual(expectedResult1);
|
|
70
|
+
});
|
|
71
|
+
it('Resolution to second promise is only result of second set', () => {
|
|
72
|
+
expect(result2).toEqual(expectedResult2);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
describe('when workload fails', () => {
|
|
77
|
+
beforeEach(() => {
|
|
78
|
+
debouncer = new Debouncer<string[], Record<string, string>>(
|
|
79
|
+
10,
|
|
80
|
+
[],
|
|
81
|
+
Debouncer.stringListParamsAccumulator,
|
|
82
|
+
(params) =>
|
|
83
|
+
Promise.reject(new Error(`Cannot handle ${params.join(',')}`))
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
describe('when called in quick succession', () => {
|
|
87
|
+
let result1: string;
|
|
88
|
+
let result2: string;
|
|
89
|
+
beforeEach(async () => {
|
|
90
|
+
const promise1 = debouncer.perform(params1);
|
|
91
|
+
const promise2 = debouncer.perform(params2);
|
|
92
|
+
try {
|
|
93
|
+
await promise1;
|
|
94
|
+
} catch (e) {
|
|
95
|
+
result1 = e.message;
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
await promise2;
|
|
99
|
+
} catch (e) {
|
|
100
|
+
result2 = e.message;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
it('Resolution to first promise is error handling all params', () => {
|
|
104
|
+
expect(result1).toEqual('Cannot handle foo,bar,baz,1337');
|
|
105
|
+
});
|
|
106
|
+
it('Resolution to second promise is error handling all params', async () => {
|
|
107
|
+
expect(result2).toEqual('Cannot handle foo,bar,baz,1337');
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
describe('when waiting for first resolution before second call', () => {
|
|
111
|
+
let result1: string;
|
|
112
|
+
let result2: string;
|
|
113
|
+
beforeEach(async () => {
|
|
114
|
+
try {
|
|
115
|
+
await debouncer.perform(params1);
|
|
116
|
+
} catch (e) {
|
|
117
|
+
result1 = e.message;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
await debouncer.perform(params2);
|
|
121
|
+
} catch (e) {
|
|
122
|
+
result2 = e.message;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
it('Resolution to first promise is error handling first set', () => {
|
|
126
|
+
expect(result1).toEqual('Cannot handle foo,bar');
|
|
127
|
+
});
|
|
128
|
+
it('Resolution to second promise is error handling second set', () => {
|
|
129
|
+
expect(result2).toEqual('Cannot handle baz,1337');
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
describe('when params is key value object response is list of strings', () => {
|
|
135
|
+
let debouncer: Debouncer<Record<string, string>, string[]>;
|
|
136
|
+
const params1 = {
|
|
137
|
+
foo: 'FOO',
|
|
138
|
+
bar: 'BAR',
|
|
139
|
+
};
|
|
140
|
+
const params2 = {
|
|
141
|
+
baz: 'BAZ',
|
|
142
|
+
1337: '1337',
|
|
143
|
+
};
|
|
144
|
+
describe('when workload succeeds', () => {
|
|
145
|
+
beforeEach(() => {
|
|
146
|
+
debouncer = new Debouncer<Record<string, string>, string[]>(
|
|
147
|
+
10,
|
|
148
|
+
{},
|
|
149
|
+
Debouncer.stringMapParamsAccumulator,
|
|
150
|
+
(params) =>
|
|
151
|
+
Promise.resolve(Object.entries(params).map(([k, v]) => `${k}=${v}`))
|
|
152
|
+
);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe('when called in quick succession', () => {
|
|
156
|
+
let result1: string[];
|
|
157
|
+
let result2: string[];
|
|
158
|
+
const expectedResult = ['1337=1337', 'foo=FOO', 'bar=BAR', 'baz=BAZ'];
|
|
159
|
+
beforeEach(async () => {
|
|
160
|
+
const promise1 = debouncer.perform(params1);
|
|
161
|
+
const promise2 = debouncer.perform(params2);
|
|
162
|
+
result1 = await promise1;
|
|
163
|
+
result2 = await promise2;
|
|
164
|
+
});
|
|
165
|
+
it('Resolution to first promise is complete set', () => {
|
|
166
|
+
expect(result1).toEqual(expectedResult);
|
|
167
|
+
});
|
|
168
|
+
it('Resolution to second promise is complete set', () => {
|
|
169
|
+
expect(result2).toEqual(expectedResult);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
describe('when waiting for first resolution before second call', () => {
|
|
173
|
+
let result1: string[];
|
|
174
|
+
let result2: string[];
|
|
175
|
+
const expectedResult1 = ['foo=FOO', 'bar=BAR'];
|
|
176
|
+
const expectedResult2 = ['1337=1337', 'baz=BAZ'];
|
|
177
|
+
beforeEach(async () => {
|
|
178
|
+
result1 = await debouncer.perform(params1);
|
|
179
|
+
result2 = await debouncer.perform(params2);
|
|
180
|
+
});
|
|
181
|
+
it('Resolution to first promise is only result of first set', () => {
|
|
182
|
+
expect(result1).toEqual(expectedResult1);
|
|
183
|
+
});
|
|
184
|
+
it('Resolution to second promise is only result of second set', () => {
|
|
185
|
+
expect(result2).toEqual(expectedResult2);
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
describe('when workload fails', () => {
|
|
190
|
+
beforeEach(() => {
|
|
191
|
+
debouncer = new Debouncer<Record<string, string>, string[]>(
|
|
192
|
+
10,
|
|
193
|
+
{},
|
|
194
|
+
Debouncer.stringMapParamsAccumulator,
|
|
195
|
+
(params) =>
|
|
196
|
+
Promise.reject(
|
|
197
|
+
new Error(
|
|
198
|
+
`Cannot handle ${Object.entries(params)
|
|
199
|
+
.map(([k, v]) => `${k}=${v}`)
|
|
200
|
+
.join(',')}`
|
|
201
|
+
)
|
|
202
|
+
)
|
|
203
|
+
);
|
|
204
|
+
});
|
|
205
|
+
describe('when called in quick succession', () => {
|
|
206
|
+
let result1: string;
|
|
207
|
+
let result2: string;
|
|
208
|
+
beforeEach(async () => {
|
|
209
|
+
const promise1 = debouncer.perform(params1);
|
|
210
|
+
const promise2 = debouncer.perform(params2);
|
|
211
|
+
try {
|
|
212
|
+
await promise1;
|
|
213
|
+
} catch (e) {
|
|
214
|
+
result1 = e.message;
|
|
215
|
+
}
|
|
216
|
+
try {
|
|
217
|
+
await promise2;
|
|
218
|
+
} catch (e) {
|
|
219
|
+
result2 = e.message;
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
it('Resolution to first promise is error handling all params', () => {
|
|
223
|
+
expect(result1).toEqual(
|
|
224
|
+
'Cannot handle 1337=1337,foo=FOO,bar=BAR,baz=BAZ'
|
|
225
|
+
);
|
|
226
|
+
});
|
|
227
|
+
it('Resolution to second promise is error handling all params', async () => {
|
|
228
|
+
expect(result2).toEqual(
|
|
229
|
+
'Cannot handle 1337=1337,foo=FOO,bar=BAR,baz=BAZ'
|
|
230
|
+
);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
describe('when waiting for first resolution before second call', () => {
|
|
234
|
+
let result1: string;
|
|
235
|
+
let result2: string;
|
|
236
|
+
beforeEach(async () => {
|
|
237
|
+
try {
|
|
238
|
+
await debouncer.perform(params1);
|
|
239
|
+
} catch (e) {
|
|
240
|
+
result1 = e.message;
|
|
241
|
+
}
|
|
242
|
+
try {
|
|
243
|
+
await debouncer.perform(params2);
|
|
244
|
+
} catch (e) {
|
|
245
|
+
result2 = e.message;
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
it('Resolution to first promise is error handling first set', () => {
|
|
249
|
+
expect(result1).toEqual('Cannot handle foo=FOO,bar=BAR');
|
|
250
|
+
});
|
|
251
|
+
it('Resolution to second promise is error handling second set', () => {
|
|
252
|
+
expect(result2).toEqual('Cannot handle 1337=1337,baz=BAZ');
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { deepCopy } from 'deep-copy-ts';
|
|
2
|
+
import deepmerge from 'ts-deepmerge';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The Debouncer can be used when we want a clean way to say 'I want to fetch this and that, and once it's done,
|
|
6
|
+
* resolve my promise', but instead of the fetch being performed right away, we want to wait for more users to
|
|
7
|
+
* request other stuff. This will keep us from performing lots of individual fetches to the backend.
|
|
8
|
+
*
|
|
9
|
+
* Our main use case is a standard publication front page where one component on the page requests the euucp namespace
|
|
10
|
+
* from Europa, and then another component requests the privacy_preferences namespace. Instead of the Europa client
|
|
11
|
+
* calling two fetches to the server, one for euucp and another for privacy_preferences, we want the Debouncer to
|
|
12
|
+
* aggregate these calls into one call to the backend, that requests both euucp AND privacy_preferences, so the response
|
|
13
|
+
* to both components will be both namespaces (which the Europa client can filter before it actually reaches the
|
|
14
|
+
* component).
|
|
15
|
+
*/
|
|
16
|
+
class Debouncer<ParamsType, ResultType> {
|
|
17
|
+
private timer: ReturnType<typeof setTimeout> | undefined;
|
|
18
|
+
|
|
19
|
+
private params: ParamsType;
|
|
20
|
+
|
|
21
|
+
private queue: {
|
|
22
|
+
resolve: (arg0: any) => void;
|
|
23
|
+
reject: (reason?: any) => void;
|
|
24
|
+
}[] = [];
|
|
25
|
+
|
|
26
|
+
constructor(
|
|
27
|
+
// The number of milliseconds to wait for new calls to perform before actually performing workload
|
|
28
|
+
private timeoutMs: number,
|
|
29
|
+
|
|
30
|
+
// Default parameters if none provided, normally an empty object or list
|
|
31
|
+
private defaultParams: ParamsType,
|
|
32
|
+
|
|
33
|
+
// Function that can accumulate parameters from successive calls to perform, see examples below
|
|
34
|
+
private paramAccumulator: (
|
|
35
|
+
previousParams: ParamsType,
|
|
36
|
+
newParams: ParamsType
|
|
37
|
+
) => ParamsType,
|
|
38
|
+
|
|
39
|
+
// The workload we want to perform on accumulated parameters once timeout has been reached
|
|
40
|
+
private workload: (params: ParamsType) => Promise<ResultType>
|
|
41
|
+
) {
|
|
42
|
+
this.params = deepCopy(defaultParams);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Caller indicates that it wants to perform workload on a set of parameters. We will wait this.timeoutMs milliseconds
|
|
46
|
+
// and see if we get more calls to perform workload, if we do, we accumulate parameters and wait this.timeoutMs
|
|
47
|
+
// more milliseconds until we finally perform workload and resolve promises.
|
|
48
|
+
perform(newParams: ParamsType): Promise<ResultType> {
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
if (this.timer) {
|
|
51
|
+
clearTimeout(this.timer);
|
|
52
|
+
}
|
|
53
|
+
this.params = this.paramAccumulator(this.params, newParams);
|
|
54
|
+
this.queue.push({ resolve, reject });
|
|
55
|
+
this.timer = setTimeout(async () => {
|
|
56
|
+
// Copy current state to use for performing workload and resolving promises after workload completes
|
|
57
|
+
const params = deepCopy(this.params);
|
|
58
|
+
const promisesToKeep = [...this.queue];
|
|
59
|
+
|
|
60
|
+
// Setup new clean state for future calls to perform
|
|
61
|
+
this.params = this.defaultParams;
|
|
62
|
+
this.queue = [];
|
|
63
|
+
|
|
64
|
+
// Perform workload and resolve promises for all callers with result
|
|
65
|
+
this.workload(params)
|
|
66
|
+
.then((result) => {
|
|
67
|
+
promisesToKeep.forEach((promise) => {
|
|
68
|
+
promise.resolve(result);
|
|
69
|
+
});
|
|
70
|
+
})
|
|
71
|
+
.catch((e) => {
|
|
72
|
+
// If workload fails, we need to reject all promises so callers won't be left hanging
|
|
73
|
+
promisesToKeep.forEach((promise) => {
|
|
74
|
+
promise.reject(e);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}, this.timeoutMs);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Accumulate parameters as lists of strings
|
|
82
|
+
static stringListParamsAccumulator(
|
|
83
|
+
accumulatedParams: string[],
|
|
84
|
+
newParams: string[]
|
|
85
|
+
) {
|
|
86
|
+
newParams.forEach((param) => {
|
|
87
|
+
if (!accumulatedParams.includes(param)) {
|
|
88
|
+
accumulatedParams.push(param);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
return accumulatedParams;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Accumulate parameters as map with string keys
|
|
95
|
+
static stringMapParamsAccumulator(
|
|
96
|
+
previousParams: Record<string, any>,
|
|
97
|
+
newParams: Record<string, any>
|
|
98
|
+
) {
|
|
99
|
+
return deepmerge(previousParams, newParams);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export default Debouncer;
|
package/src/utils/deepEquals.ts
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @see https://dev.to/sanderdebr/deep-equality-checking-of-objects-in-vanilla-javascript-5592
|
|
3
3
|
*/
|
|
4
|
+
import { isFunction, isRecord } from './type';
|
|
5
|
+
|
|
4
6
|
export const deepEquals = (a: unknown, b: unknown): boolean => {
|
|
5
7
|
if (a === b) {
|
|
6
8
|
return true;
|
|
7
9
|
}
|
|
8
10
|
|
|
9
|
-
if (
|
|
10
|
-
typeof a !== 'object' ||
|
|
11
|
-
typeof b !== 'object' ||
|
|
12
|
-
a === null ||
|
|
13
|
-
b === null
|
|
14
|
-
) {
|
|
11
|
+
if (!isRecord(a) || !isRecord(b) || a === null || b === null) {
|
|
15
12
|
return false;
|
|
16
13
|
}
|
|
17
14
|
|
|
@@ -28,11 +25,15 @@ export const deepEquals = (a: unknown, b: unknown): boolean => {
|
|
|
28
25
|
return false;
|
|
29
26
|
}
|
|
30
27
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
28
|
+
const currentA = a[key];
|
|
29
|
+
const currentB = b[key];
|
|
30
|
+
if (isFunction(currentA) || isFunction(currentB)) {
|
|
31
|
+
return isFunction(currentA) && isFunction(currentB)
|
|
32
|
+
? currentA.toString() !== currentB.toString()
|
|
33
|
+
: false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (!deepEquals(currentA, currentB)) {
|
|
36
37
|
return false;
|
|
37
38
|
}
|
|
38
39
|
}
|
package/src/utils/getOrUpdate.ts
CHANGED
|
@@ -7,7 +7,7 @@ export type DataFetcherWithCacheTTL<T extends Runtype> = Promise<{
|
|
|
7
7
|
data: Static<T>;
|
|
8
8
|
}>;
|
|
9
9
|
|
|
10
|
-
const mergeCache = (oldCache:
|
|
10
|
+
const mergeCache = <T>(oldCache: T, newCache: T): T => {
|
|
11
11
|
// Since we cannot know how to deal with duplicates when newCache is array
|
|
12
12
|
// always return newCache
|
|
13
13
|
if (!oldCache || Array.isArray(newCache)) {
|
package/src/utils/log.spec.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import log, { setLogger } from './log';
|
|
1
|
+
import log, { Logger, setLogger } from './log';
|
|
2
2
|
import restoreAllMocks = jest.restoreAllMocks;
|
|
3
3
|
|
|
4
4
|
beforeEach(() => {
|
|
@@ -6,7 +6,7 @@ beforeEach(() => {
|
|
|
6
6
|
});
|
|
7
7
|
|
|
8
8
|
describe('Log', () => {
|
|
9
|
-
let logger;
|
|
9
|
+
let logger: Logger;
|
|
10
10
|
beforeEach(() => {
|
|
11
11
|
logger = {
|
|
12
12
|
debug: jest.fn(),
|
|
@@ -17,13 +17,13 @@ describe('Log', () => {
|
|
|
17
17
|
setLogger(logger);
|
|
18
18
|
});
|
|
19
19
|
|
|
20
|
-
function itShouldLogMessage(message: string, level:
|
|
20
|
+
function itShouldLogMessage(message: string, level: keyof Logger = 'error') {
|
|
21
21
|
it('should log message', () => {
|
|
22
22
|
expect(logger[level]).toHaveBeenCalledWith(message, expect.anything());
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
function itShouldLogContext(context: Object, level:
|
|
26
|
+
function itShouldLogContext(context: Object, level: keyof Logger = 'error') {
|
|
27
27
|
it('should log context', () => {
|
|
28
28
|
expect(logger[level]).toHaveBeenCalledWith(expect.anything(), {
|
|
29
29
|
...context,
|
package/src/utils/log.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import mercury from '../clients/mercuryLog';
|
|
2
2
|
|
|
3
3
|
export type Logger = {
|
|
4
|
-
debug: (message: string | Error, context?:
|
|
5
|
-
info: (message: string | Error, context?:
|
|
6
|
-
warn: (message: string | Error, context?:
|
|
7
|
-
error: (message: string | Error, context?:
|
|
4
|
+
debug: (message: string | Error, context?: any) => void;
|
|
5
|
+
info: (message: string | Error, context?: any) => void;
|
|
6
|
+
warn: (message: string | Error, context?: any) => void;
|
|
7
|
+
error: (message: string | Error, context?: any) => void;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
10
|
let logger: Logger;
|
|
@@ -18,10 +18,11 @@ const getLogger = () => logger || mercury;
|
|
|
18
18
|
const rewriteErrorObject = (
|
|
19
19
|
error: Error
|
|
20
20
|
): { [key: string]: unknown } & { message: string } => {
|
|
21
|
-
const rewrittenError = {
|
|
21
|
+
const rewrittenError: any = {
|
|
22
22
|
message: error.message,
|
|
23
23
|
};
|
|
24
24
|
Object.getOwnPropertyNames(error).forEach((key) => {
|
|
25
|
+
// @ts-ignore
|
|
25
26
|
rewrittenError[key] = error[key];
|
|
26
27
|
});
|
|
27
28
|
return rewrittenError;
|
|
@@ -40,22 +41,31 @@ const rewriteResponseObject = async (
|
|
|
40
41
|
const responseText = !response.bodyUsed
|
|
41
42
|
? await response.text()
|
|
42
43
|
: 'Response body not available for logging';
|
|
43
|
-
const rewrittenResponseObject = {
|
|
44
|
+
const rewrittenResponseObject: any = {
|
|
44
45
|
url: response.url,
|
|
45
46
|
status: response.status,
|
|
46
47
|
statusText: response.statusText,
|
|
47
48
|
body: responseText,
|
|
48
49
|
};
|
|
49
50
|
Object.getOwnPropertyNames(response).forEach((key) => {
|
|
51
|
+
// @ts-ignore
|
|
50
52
|
rewrittenResponseObject[key] = response[key];
|
|
51
53
|
});
|
|
52
54
|
return rewrittenResponseObject;
|
|
53
55
|
};
|
|
54
56
|
|
|
57
|
+
function isError(param: any): param is Error {
|
|
58
|
+
return param && param instanceof Error;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function isResponse(param: any): param is Response {
|
|
62
|
+
return param && param instanceof Response;
|
|
63
|
+
}
|
|
64
|
+
|
|
55
65
|
const processIncomingLogData = async (
|
|
56
66
|
message: string | Error,
|
|
57
|
-
context:
|
|
58
|
-
): Promise<{ message: string; context:
|
|
67
|
+
context: any = {}
|
|
68
|
+
): Promise<{ message: string; context: any }> => {
|
|
59
69
|
let internalMessage = message;
|
|
60
70
|
let internalContext = context || {};
|
|
61
71
|
if (internalMessage instanceof Error) {
|
|
@@ -80,11 +90,12 @@ const processIncomingLogData = async (
|
|
|
80
90
|
} else {
|
|
81
91
|
// eslint-disable-next-line no-restricted-syntax
|
|
82
92
|
for (const key of Object.keys(internalContext)) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
93
|
+
const currentInternalContext = internalContext[key];
|
|
94
|
+
if (isError(currentInternalContext)) {
|
|
95
|
+
internalContext[key] = rewriteErrorObject(currentInternalContext);
|
|
96
|
+
} else if (isResponse(currentInternalContext)) {
|
|
86
97
|
// eslint-disable-next-line no-await-in-loop
|
|
87
|
-
const rewritten = await rewriteResponseObject(
|
|
98
|
+
const rewritten = await rewriteResponseObject(currentInternalContext);
|
|
88
99
|
internalContext[key] = rewritten;
|
|
89
100
|
}
|
|
90
101
|
}
|
|
@@ -98,7 +109,7 @@ const processIncomingLogData = async (
|
|
|
98
109
|
const processAndLog = (
|
|
99
110
|
level: keyof Logger,
|
|
100
111
|
message: string | Error,
|
|
101
|
-
context?:
|
|
112
|
+
context?: any
|
|
102
113
|
) => {
|
|
103
114
|
processIncomingLogData(message, context).then(({ message: m, context: c }) =>
|
|
104
115
|
getLogger()[level](m, c)
|
package/src/utils/queue.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
const queue: {
|
|
2
2
|
[key: string]: {
|
|
3
|
-
resolve: (
|
|
4
|
-
reject: (reason
|
|
3
|
+
resolve: (arg0?: any) => void;
|
|
4
|
+
reject: (reason?: any) => void;
|
|
5
5
|
}[];
|
|
6
6
|
} = {};
|
|
7
7
|
|
|
8
8
|
const add = (
|
|
9
9
|
key: string,
|
|
10
10
|
promiseCallbacks: {
|
|
11
|
-
resolve: (
|
|
12
|
-
reject: (reason
|
|
11
|
+
resolve: (arg0?: any) => void;
|
|
12
|
+
reject: (reason?: any) => void;
|
|
13
13
|
}
|
|
14
14
|
): void => {
|
|
15
15
|
if (!queue[key]) {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const isFunction = (argument: unknown): argument is Function =>
|
|
2
|
+
!!argument && typeof argument === 'function';
|
|
3
|
+
|
|
4
|
+
export const isRecord = (
|
|
5
|
+
argument: unknown
|
|
6
|
+
): argument is Record<string, unknown> =>
|
|
7
|
+
!!argument && typeof argument === 'object';
|
|
8
|
+
|
|
9
|
+
export const isError = (argument: unknown): argument is Error =>
|
|
10
|
+
!!argument && argument instanceof Error;
|
|
11
|
+
|
|
12
|
+
export const isString = (argument: unknown): argument is string =>
|
|
13
|
+
!!argument && typeof argument === 'string';
|
|
14
|
+
|
|
15
|
+
export const isNumber = (argument: unknown): argument is number =>
|
|
16
|
+
!!argument && typeof argument === 'number';
|
|
17
|
+
|
|
18
|
+
export const isBoolean = (argument: unknown): argument is boolean =>
|
|
19
|
+
!!argument && typeof argument === 'boolean';
|
package/tsconfig.eslint.json
CHANGED
package/tsconfig.json
CHANGED