@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.
Files changed (61) hide show
  1. package/@types/amedia-frontend-rpc.d.ts +11 -0
  2. package/@types/browserid.d.ts +4 -0
  3. package/checkVersion.js +12 -0
  4. package/dist/actions/authorization.d.ts +5 -5
  5. package/dist/autologin/index.d.ts +4 -0
  6. package/dist/clients/europa.d.ts +3 -7
  7. package/dist/clients/mercuryLog.d.ts +5 -4
  8. package/dist/index.d.ts +78 -35
  9. package/dist/index.esm.js +1 -1
  10. package/dist/index.esm.js.map +3 -3
  11. package/dist/index.js +1 -1
  12. package/dist/index.js.map +3 -3
  13. package/dist/janitor/jobs/daxsubCookie.d.ts +2 -1
  14. package/dist/lib/PaywallUnlockRequest.d.ts +1 -1
  15. package/dist/lib/UserDataRequest.d.ts +1 -1
  16. package/dist/lib/rpc.d.ts +5 -5
  17. package/dist/utils/customEventWrapper.d.ts +5 -3
  18. package/dist/utils/debouncer.d.ts +26 -0
  19. package/dist/utils/debouncer.spec.d.ts +1 -0
  20. package/dist/utils/deepEquals.d.ts +0 -3
  21. package/dist/utils/log.d.ts +4 -4
  22. package/dist/utils/queue.d.ts +2 -2
  23. package/dist/utils/type.d.ts +6 -0
  24. package/eik.json +1 -1
  25. package/package.json +4 -3
  26. package/src/actions/authorization.spec.ts +13 -9
  27. package/src/actions/authorization.ts +7 -5
  28. package/src/autologin/index.spec.ts +3 -3
  29. package/src/autologin/index.ts +6 -1
  30. package/src/cache/index.spec.ts +2 -2
  31. package/src/cache/index.ts +1 -2
  32. package/src/clients/europa.spec.ts +11 -16
  33. package/src/clients/europa.ts +47 -41
  34. package/src/clients/mercuryLog.ts +22 -12
  35. package/src/clients/pluto.spec.ts +4 -3
  36. package/src/eventHandlers/state.spec.ts +2 -2
  37. package/src/eventHandlers/storage.spec.ts +2 -2
  38. package/src/eventHandlers/userAttributes.spec.ts +2 -2
  39. package/src/index-authentcation.spec.ts +1 -1
  40. package/src/index-storage.spec.ts +3 -3
  41. package/src/index-subscription.spec.ts +1 -1
  42. package/src/janitor/jobs/accessCookieRefresher.spec.ts +8 -2
  43. package/src/janitor/jobs/daxsubCookie.spec.ts +8 -2
  44. package/src/janitor/jobs/daxsubCookie.ts +4 -3
  45. package/src/lib/PaywallUnlockRequest.spec.ts +12 -12
  46. package/src/lib/PaywallUnlockRequest.ts +6 -3
  47. package/src/lib/UserDataRequest.spec.ts +3 -2
  48. package/src/lib/UserDataRequest.ts +4 -4
  49. package/src/lib/rpc.ts +7 -9
  50. package/src/store/index.ts +2 -0
  51. package/src/utils/customEventWrapper.ts +6 -6
  52. package/src/utils/debouncer.spec.ts +257 -0
  53. package/src/utils/debouncer.ts +103 -0
  54. package/src/utils/deepEquals.ts +12 -11
  55. package/src/utils/getOrUpdate.ts +1 -1
  56. package/src/utils/log.spec.ts +4 -4
  57. package/src/utils/log.ts +24 -13
  58. package/src/utils/queue.ts +4 -4
  59. package/src/utils/type.ts +19 -0
  60. package/tsconfig.eslint.json +2 -0
  61. package/tsconfig.json +2 -1
@@ -2,8 +2,9 @@ import fetch from 'jest-fetch-mock';
2
2
  import cache from '../cache';
3
3
  import store from '../store';
4
4
  import { getCurrentSession, removeSession } from './pluto';
5
- import { setLogger } from '../utils/log';
5
+ import { Logger, setLogger } from '../utils/log';
6
6
  import restoreAllMocks = jest.restoreAllMocks;
7
+ import { UserAttributes } from '../types';
7
8
 
8
9
  beforeEach(async () => {
9
10
  restoreAllMocks();
@@ -24,7 +25,7 @@ const plutoLoggedInResponse = {
24
25
  tracking_key: userTrackingKey,
25
26
  };
26
27
 
27
- let log;
28
+ let log: Logger;
28
29
  beforeEach(() => {
29
30
  log = {
30
31
  debug: jest.fn(),
@@ -49,7 +50,7 @@ describe('Pluto service', () => {
49
50
  beforeEach(() => {
50
51
  jest.spyOn(store, 'merge');
51
52
  });
52
- let currentSession;
53
+ let currentSession: UserAttributes;
53
54
  function itShouldReturnSessionData() {
54
55
  it('should return session data', async () => {
55
56
  expect(currentSession).toEqual({
@@ -13,13 +13,13 @@ import { listenForStateRequests } from './state';
13
13
  import restoreAllMocks = jest.restoreAllMocks;
14
14
 
15
15
  beforeEach(() => {
16
- setRootElement(new Document());
16
+ setRootElement(new Document().createElement('div'));
17
17
  restoreAllMocks();
18
18
  store.clear();
19
19
  });
20
20
 
21
21
  describe('listenForUserAttributeRequests', () => {
22
- let currentState;
22
+ let currentState: any;
23
23
  beforeEach(() => {
24
24
  listenForAllEvents(AmediaUserEvents.ON_STATE_UPDATED, (evt) => {
25
25
  currentState = evt.detail;
@@ -15,13 +15,13 @@ import { AmediaUserEvents } from './eventMap';
15
15
  import restoreAllMocks = jest.restoreAllMocks;
16
16
 
17
17
  beforeEach(() => {
18
- setRootElement(new Document());
18
+ setRootElement(new Document().createElement('div'));
19
19
  restoreAllMocks();
20
20
  store.clear();
21
21
  });
22
22
 
23
23
  describe('listenForStorageRequests', () => {
24
- let currentStorage;
24
+ let currentStorage: any;
25
25
  beforeEach(() => {
26
26
  listenForAllEvents(AmediaUserEvents.ON_STORAGE_UPDATED, (evt) => {
27
27
  currentStorage = evt.detail;
@@ -14,13 +14,13 @@ import { AmediaUserEvents } from './eventMap';
14
14
  import restoreAllMocks = jest.restoreAllMocks;
15
15
 
16
16
  beforeEach(() => {
17
- setRootElement(new Document());
17
+ setRootElement(new Document().createElement('div'));
18
18
  restoreAllMocks();
19
19
  store.clear();
20
20
  });
21
21
 
22
22
  describe('listenForUserAttributeRequests', () => {
23
- let currentAttributes;
23
+ let currentAttributes: any;
24
24
  beforeEach(() => {
25
25
  listenForAllEvents(AmediaUserEvents.ON_ATTRIBUTES_UPDATED, (evt) => {
26
26
  currentAttributes = evt.detail;
@@ -13,7 +13,7 @@ beforeEach(() => {
13
13
 
14
14
  // We cannot use window as our event root during testing
15
15
  // since tests are executed in parallel.
16
- setRootElement(new Document());
16
+ setRootElement(new Document().createElement('div'));
17
17
 
18
18
  // Also, since we keep creating new root elements, we also
19
19
  // need to attach to these new roots.
@@ -16,7 +16,7 @@ beforeEach(() => {
16
16
 
17
17
  // We cannot use window as our event root during testing
18
18
  // since tests are executed in parallel.
19
- setRootElement(new Document());
19
+ setRootElement(new Document().createElement('div'));
20
20
 
21
21
  // Store must be cleared before we re-listen for user attribute requests
22
22
  store.clear();
@@ -53,7 +53,7 @@ describe('Storage', () => {
53
53
  });
54
54
 
55
55
  describe('when getting storage namespaces', () => {
56
- let onUpdate;
56
+ let onUpdate: any;
57
57
  beforeEach(() => {
58
58
  store.set('storage', {
59
59
  one: { key1: 'value1' },
@@ -102,7 +102,7 @@ describe('Storage', () => {
102
102
  });
103
103
 
104
104
  describe('when getting storage namespaces', () => {
105
- let onUpdate;
105
+ let onUpdate: any;
106
106
  beforeEach(() => {
107
107
  store.set('storage', {
108
108
  one: { key1: 'value1' },
@@ -15,7 +15,7 @@ beforeEach(() => {
15
15
 
16
16
  // We cannot use window as our event root during testing
17
17
  // since tests are executed in parallel.
18
- setRootElement(new Document());
18
+ setRootElement(new Document().createElement('div'));
19
19
 
20
20
  // Store must be cleared before we re-listen for user attribute requests
21
21
  store.clear();
@@ -11,11 +11,12 @@ import { setRootElement } from '../../utils/customEventWrapper';
11
11
  import store from '../../store';
12
12
  import restoreAllMocks = jest.restoreAllMocks;
13
13
  import spyOn = jest.spyOn;
14
+ import { AccessFeatures } from '../../clients/jupiter';
14
15
 
15
16
  const mockUuid = '3-1-33-7';
16
17
 
17
18
  beforeEach(() => {
18
- setRootElement(new Document());
19
+ setRootElement(new Document().createElement('div'));
19
20
  restoreAllMocks();
20
21
  spyOn(url, 'getCurrentPublicationDomain').mockReturnValue('www.avisnavn.no');
21
22
  url.setCurrentHostname('www.avisnavn.no');
@@ -31,7 +32,12 @@ beforeEach(() => {
31
32
  );
32
33
  });
33
34
 
34
- function mockAccessCookie(validHours, uuid, accessFeatures, domain) {
35
+ function mockAccessCookie(
36
+ validHours: number,
37
+ uuid: string,
38
+ accessFeatures: AccessFeatures,
39
+ domain: string
40
+ ) {
35
41
  const validTo = new Date();
36
42
  validTo.setHours(validTo.getHours() + validHours);
37
43
  return `signature:${Math.round(
@@ -22,7 +22,7 @@ describe('daxSubCookieHandler', () => {
22
22
 
23
23
  // We cannot use window as our event root during testing
24
24
  // since tests are executed in parallel.
25
- setRootElement(new Document());
25
+ setRootElement(new Document().createElement('div'));
26
26
 
27
27
  // Also, since we keep creating new root elements, we also
28
28
  // need to attach to these new roots.
@@ -50,7 +50,11 @@ describe('daxSubCookieHandler', () => {
50
50
  describe('when user is not logged in', () => {
51
51
  beforeEach(async () => {
52
52
  store.set('state', { isLoggedIn: false });
53
- store.set('userAttributes', { extraData: {} });
53
+ store.set('userAttributes', {
54
+ uuid: null,
55
+ trackingKey: null,
56
+ extraData: {},
57
+ });
54
58
  await daxSubCookieHandler();
55
59
  });
56
60
 
@@ -71,6 +75,7 @@ describe('daxSubCookieHandler', () => {
71
75
  describe('when has subscription', () => {
72
76
  beforeEach(async () => {
73
77
  store.set('userAttributes', {
78
+ uuid: 'uuid',
74
79
  extraData: {
75
80
  customer_keys: 'someString',
76
81
  },
@@ -95,6 +100,7 @@ describe('daxSubCookieHandler', () => {
95
100
  describe('when has no subscription', () => {
96
101
  beforeEach(async () => {
97
102
  store.set('userAttributes', {
103
+ uuid: 'uuid',
98
104
  extraData: {},
99
105
  trackingKey: '148d24b2-f28e-4b1c-9958-8abd5066fc59',
100
106
  });
@@ -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.customer_keys
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(requestedAccessFeatures) {
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 {} = { state: State }
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
- const previousData: SubscribeResponse = {
191
+ let previousData: SubscribeResponse = {
192
192
  state: {},
193
193
  };
194
194
  if (this.attributes && this.attributes.length > 0) {
195
- previousData['attributes'] = {};
195
+ previousData = { ...previousData, attributes: {} };
196
196
  }
197
197
 
198
198
  if (this.namespaces && this.namespaces.length > 0) {
199
- previousData['storage'] = {};
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
- let rpcSessionClient: {
4
- addMethod: (methodName: string) => void;
5
- userAuthenticated: (boolean) => void;
6
- };
7
- export const getRPCClient = (): Pick<
8
- typeof rpcSessionClient,
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
  };
@@ -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: Window, ev: WindowEventMap[T]) => void
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: Window, ev: WindowEventMap[T]) => void
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