@amedia/user 0.13.24 → 0.13.26
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/dist/actions/authorization.d.ts +5 -5
- package/dist/autologin/index.d.ts +4 -0
- package/dist/clients/mercuryLog.d.ts +5 -4
- package/dist/index.d.ts +43 -29
- 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/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 +1 -2
- 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 +3 -3
- package/src/clients/europa.ts +9 -4
- package/src/clients/mercuryLog.ts +19 -11
- 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/index.ts +4 -8
- 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/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
- package/amedia-user-js.iml +0 -9
- package/dist/cache/engines/indexeddb.d.ts +0 -7
- package/dist/index.js.LICENSE.txt +0 -14
- package/dist/janitor/index.spec.d.ts +0 -4
- package/dist/lib/PaywallUnlocker.d.ts +0 -16
- package/dist/lib/PaywallUnlocker.spec.d.ts +0 -4
- package/dist/lib/User.d.ts +0 -21
- package/dist/utils/logValidationError.d.ts +0 -1
- package/esm/actions/authentication.d.ts +0 -5
- package/esm/actions/authentication.spec.d.ts +0 -4
- package/esm/actions/authorization.d.ts +0 -9
- package/esm/actions/preferences.d.ts +0 -9
- package/esm/actions/preferences.spec.d.ts +0 -1
- package/esm/actions/storage.d.ts +0 -4
- package/esm/actions/userData.d.ts +0 -2
- package/esm/autologin/index.d.ts +0 -1
- package/esm/autologin/thirdPartyCookie.d.ts +0 -3
- package/esm/autologin/thirdPartyCookie.spec.d.ts +0 -1
- package/esm/cache/engines/global.d.ts +0 -21
- package/esm/cache/engines/global.spec.d.ts +0 -1
- package/esm/cache/engines/indexeddb.d.ts +0 -7
- package/esm/cache/index.d.ts +0 -11
- package/esm/cache/index.spec.d.ts +0 -4
- package/esm/cache/types.d.ts +0 -11
- package/esm/clients/europa.d.ts +0 -28
- package/esm/clients/europa.spec.d.ts +0 -1
- package/esm/clients/jupiter.d.ts +0 -30
- package/esm/clients/jupiter.spec.d.ts +0 -1
- package/esm/clients/mercury.d.ts +0 -18
- package/esm/clients/mercury.spec.d.ts +0 -4
- package/esm/clients/nebula.d.ts +0 -2
- package/esm/clients/nebula.spec.d.ts +0 -1
- package/esm/clients/pluto.d.ts +0 -3
- package/esm/clients/pluto.spec.d.ts +0 -1
- package/esm/eventHandlers/activation.d.ts +0 -15
- package/esm/eventHandlers/authentication.d.ts +0 -17
- package/esm/eventHandlers/userAttributes.d.ts +0 -24
- package/esm/exceptions/PaywallError.d.ts +0 -7
- package/esm/index-authentcation.spec.d.ts +0 -4
- package/esm/index-storage.spec.d.ts +0 -4
- package/esm/index-subscription.spec.d.ts +0 -4
- package/esm/index.d.ts +0 -26
- package/esm/janitor/index.d.ts +0 -10
- package/esm/janitor/jobs/accessCookieRefresher.d.ts +0 -1
- package/esm/janitor/jobs/accessCookieRefresher.spec.d.ts +0 -4
- package/esm/lib/PaywallUnlocker.d.ts +0 -16
- package/esm/lib/PaywallUnlocker.spec.d.ts +0 -4
- package/esm/lib/User.d.ts +0 -21
- package/esm/lib/cookie.d.ts +0 -5
- package/esm/lib/rpc.d.ts +0 -6
- package/esm/store/index.d.ts +0 -22
- package/esm/store/index.spec.d.ts +0 -1
- package/esm/types.d.ts +0 -22
- package/esm/user.js +0 -2
- package/esm/user.js.map +0 -1
- package/esm/utils/customEventWrapper.d.ts +0 -17
- package/esm/utils/deepEquals.d.ts +0 -4
- package/esm/utils/getOrUpdate.d.ts +0 -6
- package/esm/utils/getOrUpdate.spec.d.ts +0 -1
- package/esm/utils/location.d.ts +0 -3
- package/esm/utils/log.d.ts +0 -26
- package/esm/utils/queue.d.ts +0 -12
- package/esm/utils/url.d.ts +0 -6
- package/esm/utils/url.spec.d.ts +0 -1
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { setCookie, removeCookie } from '../../lib/cookie';
|
|
2
2
|
import UserDataRequest from '../../lib/UserDataRequest';
|
|
3
3
|
import log from '../../utils/log';
|
|
4
|
+
import { UserAttributes } from '../../types';
|
|
4
5
|
|
|
5
|
-
export function getCookieValue(attributes) {
|
|
6
|
+
export function getCookieValue(attributes: UserAttributes) {
|
|
6
7
|
// If user has the customer_keys field set, assume its a paying subscriber and set subscriptionStatus = active.
|
|
7
|
-
const subscriptionStatus = attributes.extraData
|
|
8
|
+
const subscriptionStatus = attributes.extraData?.customer_keys
|
|
8
9
|
? 'active'
|
|
9
10
|
: 'null';
|
|
10
11
|
|
|
@@ -12,7 +13,7 @@ export function getCookieValue(attributes) {
|
|
|
12
13
|
return cookieValue;
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
function setDaxsubCookie(attributes) {
|
|
16
|
+
function setDaxsubCookie(attributes: UserAttributes) {
|
|
16
17
|
const name = 'daxsub';
|
|
17
18
|
const cookieValue = getCookieValue(attributes);
|
|
18
19
|
const cookieValueEncoded = encodeURIComponent(cookieValue);
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import fetch from 'jest-fetch-mock';
|
|
6
|
-
import PaywallUnlockRequest from './PaywallUnlockRequest';
|
|
6
|
+
import PaywallUnlockRequest, { UnlockResponse } from './PaywallUnlockRequest';
|
|
7
7
|
import * as authorization from '../actions/authorization';
|
|
8
8
|
import * as location from '../utils/location';
|
|
9
9
|
import * as autologin from '../autologin';
|
|
@@ -23,7 +23,7 @@ beforeEach(() => {
|
|
|
23
23
|
|
|
24
24
|
// We cannot use window as our event root during testing
|
|
25
25
|
// since tests are executed in parallel.
|
|
26
|
-
setRootElement(new Document());
|
|
26
|
+
setRootElement(new Document().createElement('div'));
|
|
27
27
|
removeAllCookies();
|
|
28
28
|
|
|
29
29
|
// Also, since we keep creating new root elements, we also
|
|
@@ -66,10 +66,10 @@ describe('PaywallUnlocker', () => {
|
|
|
66
66
|
});
|
|
67
67
|
|
|
68
68
|
describe('When not logged in', () => {
|
|
69
|
-
let unlockResponse;
|
|
69
|
+
let unlockResponse: UnlockResponse;
|
|
70
70
|
beforeEach(async () => {
|
|
71
71
|
store.set('state', { isLoggedIn: false });
|
|
72
|
-
store.set('userAttributes', { name: 'Donald Duck' });
|
|
72
|
+
store.set('userAttributes', { uuid: 'uuid', name: 'Donald Duck' });
|
|
73
73
|
unlockResponse = await new PaywallUnlockRequest().tryUnlock();
|
|
74
74
|
});
|
|
75
75
|
|
|
@@ -92,7 +92,7 @@ describe('PaywallUnlocker', () => {
|
|
|
92
92
|
describe('When loggen in', () => {
|
|
93
93
|
beforeEach(() => {
|
|
94
94
|
store.set('state', { isLoggedIn: true });
|
|
95
|
-
store.set('userAttributes', { name: 'Donald Duck' });
|
|
95
|
+
store.set('userAttributes', { uuid: 'uuid', name: 'Donald Duck' });
|
|
96
96
|
});
|
|
97
97
|
|
|
98
98
|
it('should not attempt autologin', () => {
|
|
@@ -103,7 +103,7 @@ describe('PaywallUnlocker', () => {
|
|
|
103
103
|
itShouldNotReload();
|
|
104
104
|
|
|
105
105
|
describe('when has access', () => {
|
|
106
|
-
let unlockResponse;
|
|
106
|
+
let unlockResponse: UnlockResponse;
|
|
107
107
|
beforeEach(async () => {
|
|
108
108
|
store.merge('userAttributes', { access: ['newspaper'] });
|
|
109
109
|
spyOn(authorization, 'setAccessCookies').mockResolvedValue({
|
|
@@ -115,7 +115,7 @@ describe('PaywallUnlocker', () => {
|
|
|
115
115
|
});
|
|
116
116
|
|
|
117
117
|
describe('When called with reload and already tried to reload', () => {
|
|
118
|
-
let error;
|
|
118
|
+
let error: Error | undefined;
|
|
119
119
|
beforeEach(async () => {
|
|
120
120
|
spyOn(
|
|
121
121
|
authorization,
|
|
@@ -159,7 +159,7 @@ describe('PaywallUnlocker', () => {
|
|
|
159
159
|
|
|
160
160
|
describe('when no access', () => {
|
|
161
161
|
describe('and activation give access', () => {
|
|
162
|
-
let unlockResponse;
|
|
162
|
+
let unlockResponse: UnlockResponse;
|
|
163
163
|
beforeEach(() => {
|
|
164
164
|
spyOn(authorization, 'setAccessCookies')
|
|
165
165
|
.mockResolvedValueOnce({
|
|
@@ -241,7 +241,7 @@ describe('PaywallUnlocker', () => {
|
|
|
241
241
|
});
|
|
242
242
|
|
|
243
243
|
describe('and activation does not give access', () => {
|
|
244
|
-
let unlockResponse;
|
|
244
|
+
let unlockResponse: UnlockResponse;
|
|
245
245
|
beforeEach(async () => {
|
|
246
246
|
spyOn(authorization, 'setAccessCookies').mockResolvedValueOnce({
|
|
247
247
|
success: true,
|
|
@@ -265,7 +265,7 @@ describe('PaywallUnlocker', () => {
|
|
|
265
265
|
});
|
|
266
266
|
|
|
267
267
|
describe("and activation don't give enough access", () => {
|
|
268
|
-
let unlockResponse;
|
|
268
|
+
let unlockResponse: UnlockResponse;
|
|
269
269
|
beforeEach(async () => {
|
|
270
270
|
spyOn(authorization, 'setAccessCookies').mockResolvedValueOnce({
|
|
271
271
|
success: true,
|
|
@@ -292,7 +292,7 @@ describe('PaywallUnlocker', () => {
|
|
|
292
292
|
});
|
|
293
293
|
|
|
294
294
|
describe('when setting access cookies fails', () => {
|
|
295
|
-
let paywallError;
|
|
295
|
+
let paywallError: any;
|
|
296
296
|
beforeEach(async () => {
|
|
297
297
|
spyOn(authorization, 'setAccessCookies').mockResolvedValue({
|
|
298
298
|
success: false,
|
|
@@ -320,7 +320,7 @@ describe('PaywallUnlocker', () => {
|
|
|
320
320
|
});
|
|
321
321
|
|
|
322
322
|
describe('when internal system disagree about users access', () => {
|
|
323
|
-
let paywallError;
|
|
323
|
+
let paywallError: any;
|
|
324
324
|
beforeEach(async () => {
|
|
325
325
|
spyOn(authorization, 'setAccessCookies')
|
|
326
326
|
.mockResolvedValueOnce({
|
|
@@ -10,8 +10,9 @@ import * as location from '../utils/location';
|
|
|
10
10
|
import PaywallError from '../exceptions/PaywallError';
|
|
11
11
|
import UserDataRequest from './UserDataRequest';
|
|
12
12
|
import { attemptPaywallAutoLogin } from '../autologin';
|
|
13
|
+
import { AccessFeatures } from '../clients/jupiter';
|
|
13
14
|
|
|
14
|
-
type UnlockResponse = {
|
|
15
|
+
export type UnlockResponse = {
|
|
15
16
|
isLoggedIn: boolean;
|
|
16
17
|
hasAccess: boolean;
|
|
17
18
|
accessFeatures: string[];
|
|
@@ -103,7 +104,9 @@ class PaywallUnlockRequest {
|
|
|
103
104
|
}
|
|
104
105
|
|
|
105
106
|
// eslint-disable-next-line class-methods-use-this
|
|
106
|
-
private setAccessCookiesAndGetAccessFeatures(
|
|
107
|
+
private setAccessCookiesAndGetAccessFeatures(
|
|
108
|
+
requestedAccessFeatures: AccessFeatures
|
|
109
|
+
) {
|
|
107
110
|
return setAccessCookies(getCurrentPublicationDomain()).then((response) => {
|
|
108
111
|
const accessDenied = !hasAccess(
|
|
109
112
|
response.accessFeatures,
|
|
@@ -122,7 +125,7 @@ class PaywallUnlockRequest {
|
|
|
122
125
|
});
|
|
123
126
|
}
|
|
124
127
|
|
|
125
|
-
private async activateAccess(requestedAccessFeatures) {
|
|
128
|
+
private async activateAccess(requestedAccessFeatures: AccessFeatures) {
|
|
126
129
|
const accessFeatures = await activate(this.requestedAccessFeatures);
|
|
127
130
|
return {
|
|
128
131
|
denied: !hasAccess(accessFeatures, requestedAccessFeatures),
|
|
@@ -12,7 +12,7 @@ import * as europa from '../clients/europa';
|
|
|
12
12
|
beforeEach(() => {
|
|
13
13
|
// We cannot use window as our event root during testing
|
|
14
14
|
// since tests are executed in parallel.
|
|
15
|
-
setRootElement(new Document());
|
|
15
|
+
setRootElement(new Document().createElement('div'));
|
|
16
16
|
|
|
17
17
|
store.clear();
|
|
18
18
|
|
|
@@ -29,12 +29,13 @@ describe('UserDataRequest', () => {
|
|
|
29
29
|
beforeEach(() => {
|
|
30
30
|
store.set('state', { isLoggedIn: true });
|
|
31
31
|
store.set('userAttributes', {
|
|
32
|
+
uuid: 'uuid',
|
|
32
33
|
name: 'Donald Duck',
|
|
33
34
|
});
|
|
34
35
|
});
|
|
35
36
|
|
|
36
37
|
describe('and already have all requested attributes', () => {
|
|
37
|
-
let response;
|
|
38
|
+
let response: any;
|
|
38
39
|
beforeEach(async () => {
|
|
39
40
|
response = await new UserDataRequest()
|
|
40
41
|
.withAttributes(['name'])
|
|
@@ -21,7 +21,7 @@ class UserDataRequest<
|
|
|
21
21
|
S extends { [index: string]: unknown },
|
|
22
22
|
Attributes extends Array<keyof UA>,
|
|
23
23
|
Namespaces extends Array<string>,
|
|
24
|
-
SubscribeResponse extends
|
|
24
|
+
SubscribeResponse extends Record<string, object> = { state: State }
|
|
25
25
|
> {
|
|
26
26
|
constructor(
|
|
27
27
|
private attributes?: Attributes,
|
|
@@ -188,15 +188,15 @@ class UserDataRequest<
|
|
|
188
188
|
|
|
189
189
|
private setupLocalCache() {
|
|
190
190
|
// @ts-ignore
|
|
191
|
-
|
|
191
|
+
let previousData: SubscribeResponse = {
|
|
192
192
|
state: {},
|
|
193
193
|
};
|
|
194
194
|
if (this.attributes && this.attributes.length > 0) {
|
|
195
|
-
previousData
|
|
195
|
+
previousData = { ...previousData, attributes: {} };
|
|
196
196
|
}
|
|
197
197
|
|
|
198
198
|
if (this.namespaces && this.namespaces.length > 0) {
|
|
199
|
-
previousData
|
|
199
|
+
previousData = { ...previousData, storage: {} };
|
|
200
200
|
}
|
|
201
201
|
return previousData;
|
|
202
202
|
}
|
package/src/lib/rpc.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { RPCclient } from '@amedia/frontend-rpc';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
'userAuthenticated'
|
|
10
|
-
> => {
|
|
3
|
+
interface LocalRPCclient extends RPCclient {
|
|
4
|
+
userAuthenticated: (isLoggedIn: boolean) => void;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
let rpcSessionClient: LocalRPCclient;
|
|
8
|
+
export const getRPCClient = (): LocalRPCclient => {
|
|
11
9
|
if (rpcSessionClient) {
|
|
12
10
|
return rpcSessionClient;
|
|
13
11
|
}
|
|
@@ -15,7 +13,7 @@ export const getRPCClient = (): Pick<
|
|
|
15
13
|
version: '1.0.0',
|
|
16
14
|
component: `userdata-session`,
|
|
17
15
|
methods: ['userAuthenticated'],
|
|
18
|
-
});
|
|
16
|
+
}) as LocalRPCclient;
|
|
19
17
|
|
|
20
18
|
return rpcSessionClient;
|
|
21
19
|
};
|
package/src/store/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ const store: { [index in keyof StoreDataMap]?: StoreDataMap[index] } = {};
|
|
|
16
16
|
const listeners: Listeners = {};
|
|
17
17
|
|
|
18
18
|
const emit = (key: keyof StoreDataMap): void => {
|
|
19
|
+
// @ts-ignore
|
|
19
20
|
listeners[key]?.forEach((listener) => listener(store[key]));
|
|
20
21
|
};
|
|
21
22
|
|
|
@@ -55,6 +56,7 @@ const clear = () => {
|
|
|
55
56
|
store.userAttributes = undefined;
|
|
56
57
|
store.storage = undefined;
|
|
57
58
|
Object.keys(listeners).forEach((key) => {
|
|
59
|
+
// @ts-ignore
|
|
58
60
|
listeners[key] = [];
|
|
59
61
|
});
|
|
60
62
|
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import log from './log';
|
|
2
2
|
|
|
3
|
-
let rootElement;
|
|
3
|
+
let rootElement: Window | Element;
|
|
4
4
|
|
|
5
|
-
export const setRootElement = (element) => {
|
|
5
|
+
export const setRootElement = (element: typeof rootElement) => {
|
|
6
6
|
rootElement = element;
|
|
7
7
|
};
|
|
8
8
|
|
|
@@ -19,7 +19,7 @@ if (typeof window !== 'undefined') {
|
|
|
19
19
|
*/
|
|
20
20
|
export const listenWithExclusivity = <T extends keyof WindowEventMap>(
|
|
21
21
|
type: T,
|
|
22
|
-
listener: (this:
|
|
22
|
+
listener: (this: typeof rootElement, ev: WindowEventMap[T]) => void
|
|
23
23
|
) => {
|
|
24
24
|
if (!rootElement) {
|
|
25
25
|
log.error('No root element to bind event to!');
|
|
@@ -28,7 +28,7 @@ export const listenWithExclusivity = <T extends keyof WindowEventMap>(
|
|
|
28
28
|
rootElement.addEventListener(type, (evt) => {
|
|
29
29
|
// Since this lib might me loaded multiple times, we need to prevent all other processing of this event.
|
|
30
30
|
evt.stopImmediatePropagation();
|
|
31
|
-
listener.apply(rootElement, [evt]);
|
|
31
|
+
listener.apply(rootElement, [evt as WindowEventMap[T]]);
|
|
32
32
|
});
|
|
33
33
|
};
|
|
34
34
|
|
|
@@ -40,14 +40,14 @@ export const listenWithExclusivity = <T extends keyof WindowEventMap>(
|
|
|
40
40
|
*/
|
|
41
41
|
export const listenForAllEvents = <T extends keyof WindowEventMap>(
|
|
42
42
|
type: T,
|
|
43
|
-
listener: (this:
|
|
43
|
+
listener: (this: typeof rootElement, ev: WindowEventMap[T]) => void
|
|
44
44
|
) => {
|
|
45
45
|
if (!rootElement) {
|
|
46
46
|
log.error('No root element to bind event to!');
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
49
|
rootElement.addEventListener(type, (evt) => {
|
|
50
|
-
listener.apply(rootElement, [evt]);
|
|
50
|
+
listener.apply(rootElement, [evt as WindowEventMap[T]]);
|
|
51
51
|
});
|
|
52
52
|
};
|
|
53
53
|
|
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
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,7 +0,0 @@
|
|
|
1
|
-
import { CacheEntry, Engine } from '../types';
|
|
2
|
-
export declare const get: (key: string) => Promise<CacheEntry>;
|
|
3
|
-
export declare const set: (key: string, data: unknown, expires: number) => Promise<void>;
|
|
4
|
-
export declare const remove: (key: string) => Promise<void>;
|
|
5
|
-
export declare const clear: () => Promise<void>;
|
|
6
|
-
declare const _default: Engine;
|
|
7
|
-
export default _default;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
localForage -- Offline Storage, Improved
|
|
3
|
-
Version 1.9.0
|
|
4
|
-
https://localforage.github.io/localForage
|
|
5
|
-
(c) 2013-2017 Mozilla, Apache License 2.0
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/*!
|
|
9
|
-
* JavaScript Cookie v2.2.1
|
|
10
|
-
* https://github.com/js-cookie/js-cookie
|
|
11
|
-
*
|
|
12
|
-
* Copyright 2006, 2015 Klaus Hartl & Fagner Brack
|
|
13
|
-
* Released under the MIT license
|
|
14
|
-
*/
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
declare class PaywallUnlocker {
|
|
2
|
-
private requestedAccessFeatures;
|
|
3
|
-
private reload;
|
|
4
|
-
private requestedUrl;
|
|
5
|
-
withAccessFeatures(accessFeatures: string[]): this;
|
|
6
|
-
reloadOnAccess(): this;
|
|
7
|
-
redirectOnAccess(requestedUrl: string): this;
|
|
8
|
-
tryUnlock(): Promise<{
|
|
9
|
-
isLoggedIn: boolean;
|
|
10
|
-
hasAccess: boolean;
|
|
11
|
-
accessFeatures: string[];
|
|
12
|
-
}>;
|
|
13
|
-
private resolveActualAccessFeatures;
|
|
14
|
-
private setAccessCookiesAndPerformRequestedActions;
|
|
15
|
-
}
|
|
16
|
-
export default PaywallUnlocker;
|
package/dist/lib/User.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { State, UserAttributes } from '../types';
|
|
2
|
-
declare class User<UA extends UserAttributes, S extends {
|
|
3
|
-
[index: string]: unknown;
|
|
4
|
-
}, Attributes extends Array<keyof UA>, Namespaces extends Array<string>, SubscribeResponse extends {} = {
|
|
5
|
-
state: State;
|
|
6
|
-
}> {
|
|
7
|
-
private attributes?;
|
|
8
|
-
private namespaces?;
|
|
9
|
-
constructor(attributes?: Attributes | undefined, namespaces?: Namespaces | undefined);
|
|
10
|
-
withAttributes<NewAttributes extends Array<keyof UA>>(attributes: NewAttributes): User<UA, S, (Attributes[number] & NewAttributes[number])[], Namespaces, SubscribeResponse & {
|
|
11
|
-
attributes: Pick<UA, Attributes[number] & NewAttributes[number]>;
|
|
12
|
-
}>;
|
|
13
|
-
withStorage<NewNamespaces extends Array<string>>(...namespace: NewNamespaces): User<UA, S, Attributes, (Namespaces[number] & NewNamespaces[number])[], SubscribeResponse & {
|
|
14
|
-
storage: Pick<S, Namespaces[number] & NewNamespaces[number]>;
|
|
15
|
-
}>;
|
|
16
|
-
subscribe(onData: (data: SubscribeResponse) => void): void;
|
|
17
|
-
private processUserAttributesEvent;
|
|
18
|
-
private processStorageEvent;
|
|
19
|
-
private setupLocalCache;
|
|
20
|
-
}
|
|
21
|
-
export default User;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default function logValidationError(name: string, error: Error, extra?: Object): void;
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { UserAttributes } from '../types';
|
|
2
|
-
export declare const login: (requestedUrl: URL | string) => void;
|
|
3
|
-
export declare const logout: () => Promise<void>;
|
|
4
|
-
export declare const getSession: () => Promise<UserAttributes>;
|
|
5
|
-
export declare const isLoggedIn: () => boolean;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { UserAttributes } from '../types';
|
|
2
|
-
export declare const getAccess: (accessFeatures?: string[]) => Promise<Pick<UserAttributes, 'access'>>;
|
|
3
|
-
export declare const activate: (accessFeatures: string[]) => Promise<boolean>;
|
|
4
|
-
export declare const hasAccess: (currentAccessFeatures: string[], requestedAccessFeatures: string[]) => boolean;
|
|
5
|
-
export declare const setUserAccessCookie: (publicationDomain: any) => Promise<boolean>;
|
|
6
|
-
export declare const setNonUserAccessCookie: (publicationDomain: any) => Promise<boolean>;
|
|
7
|
-
export declare const hasRecentlyTriedToUnlockPaywall: () => boolean;
|
|
8
|
-
export declare const setAccessCookies: (publicationDomain: any) => Promise<boolean>;
|
|
9
|
-
export declare const setHasTriedToUnlockPaywall: () => void;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Record, Boolean } from 'runtypes';
|
|
2
|
-
import { UserAttributes } from '../types';
|
|
3
|
-
export declare const PrivacyPreferencesSchema: Record<{
|
|
4
|
-
allowResearchUsage: Boolean;
|
|
5
|
-
connectWithSurveys: Boolean;
|
|
6
|
-
personalizedAds: Boolean;
|
|
7
|
-
personalizedContent: Boolean;
|
|
8
|
-
}, false>;
|
|
9
|
-
export declare const getPrivacyPreferences: () => Promise<Pick<UserAttributes, 'privacyPreferences'>>;
|