@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.
Files changed (120) hide show
  1. package/@types/amedia-frontend-rpc.d.ts +11 -0
  2. package/@types/browserid.d.ts +4 -0
  3. package/dist/actions/authorization.d.ts +5 -5
  4. package/dist/autologin/index.d.ts +4 -0
  5. package/dist/clients/mercuryLog.d.ts +5 -4
  6. package/dist/index.d.ts +43 -29
  7. package/dist/index.esm.js +1 -1
  8. package/dist/index.esm.js.map +3 -3
  9. package/dist/index.js +1 -1
  10. package/dist/index.js.map +3 -3
  11. package/dist/janitor/jobs/daxsubCookie.d.ts +2 -1
  12. package/dist/lib/PaywallUnlockRequest.d.ts +1 -1
  13. package/dist/lib/UserDataRequest.d.ts +1 -1
  14. package/dist/lib/rpc.d.ts +5 -5
  15. package/dist/utils/customEventWrapper.d.ts +5 -3
  16. package/dist/utils/deepEquals.d.ts +0 -3
  17. package/dist/utils/log.d.ts +4 -4
  18. package/dist/utils/queue.d.ts +2 -2
  19. package/dist/utils/type.d.ts +6 -0
  20. package/eik.json +1 -1
  21. package/package.json +1 -2
  22. package/src/actions/authorization.spec.ts +13 -9
  23. package/src/actions/authorization.ts +7 -5
  24. package/src/autologin/index.spec.ts +3 -3
  25. package/src/autologin/index.ts +6 -1
  26. package/src/cache/index.spec.ts +2 -2
  27. package/src/cache/index.ts +1 -2
  28. package/src/clients/europa.spec.ts +3 -3
  29. package/src/clients/europa.ts +9 -4
  30. package/src/clients/mercuryLog.ts +19 -11
  31. package/src/clients/pluto.spec.ts +4 -3
  32. package/src/eventHandlers/state.spec.ts +2 -2
  33. package/src/eventHandlers/storage.spec.ts +2 -2
  34. package/src/eventHandlers/userAttributes.spec.ts +2 -2
  35. package/src/index-authentcation.spec.ts +1 -1
  36. package/src/index-storage.spec.ts +3 -3
  37. package/src/index-subscription.spec.ts +1 -1
  38. package/src/index.ts +4 -8
  39. package/src/janitor/jobs/accessCookieRefresher.spec.ts +8 -2
  40. package/src/janitor/jobs/daxsubCookie.spec.ts +8 -2
  41. package/src/janitor/jobs/daxsubCookie.ts +4 -3
  42. package/src/lib/PaywallUnlockRequest.spec.ts +12 -12
  43. package/src/lib/PaywallUnlockRequest.ts +6 -3
  44. package/src/lib/UserDataRequest.spec.ts +3 -2
  45. package/src/lib/UserDataRequest.ts +4 -4
  46. package/src/lib/rpc.ts +7 -9
  47. package/src/store/index.ts +2 -0
  48. package/src/utils/customEventWrapper.ts +6 -6
  49. package/src/utils/deepEquals.ts +12 -11
  50. package/src/utils/getOrUpdate.ts +1 -1
  51. package/src/utils/log.spec.ts +4 -4
  52. package/src/utils/log.ts +24 -13
  53. package/src/utils/queue.ts +4 -4
  54. package/src/utils/type.ts +19 -0
  55. package/tsconfig.eslint.json +2 -0
  56. package/tsconfig.json +2 -1
  57. package/amedia-user-js.iml +0 -9
  58. package/dist/cache/engines/indexeddb.d.ts +0 -7
  59. package/dist/index.js.LICENSE.txt +0 -14
  60. package/dist/janitor/index.spec.d.ts +0 -4
  61. package/dist/lib/PaywallUnlocker.d.ts +0 -16
  62. package/dist/lib/PaywallUnlocker.spec.d.ts +0 -4
  63. package/dist/lib/User.d.ts +0 -21
  64. package/dist/utils/logValidationError.d.ts +0 -1
  65. package/esm/actions/authentication.d.ts +0 -5
  66. package/esm/actions/authentication.spec.d.ts +0 -4
  67. package/esm/actions/authorization.d.ts +0 -9
  68. package/esm/actions/preferences.d.ts +0 -9
  69. package/esm/actions/preferences.spec.d.ts +0 -1
  70. package/esm/actions/storage.d.ts +0 -4
  71. package/esm/actions/userData.d.ts +0 -2
  72. package/esm/autologin/index.d.ts +0 -1
  73. package/esm/autologin/thirdPartyCookie.d.ts +0 -3
  74. package/esm/autologin/thirdPartyCookie.spec.d.ts +0 -1
  75. package/esm/cache/engines/global.d.ts +0 -21
  76. package/esm/cache/engines/global.spec.d.ts +0 -1
  77. package/esm/cache/engines/indexeddb.d.ts +0 -7
  78. package/esm/cache/index.d.ts +0 -11
  79. package/esm/cache/index.spec.d.ts +0 -4
  80. package/esm/cache/types.d.ts +0 -11
  81. package/esm/clients/europa.d.ts +0 -28
  82. package/esm/clients/europa.spec.d.ts +0 -1
  83. package/esm/clients/jupiter.d.ts +0 -30
  84. package/esm/clients/jupiter.spec.d.ts +0 -1
  85. package/esm/clients/mercury.d.ts +0 -18
  86. package/esm/clients/mercury.spec.d.ts +0 -4
  87. package/esm/clients/nebula.d.ts +0 -2
  88. package/esm/clients/nebula.spec.d.ts +0 -1
  89. package/esm/clients/pluto.d.ts +0 -3
  90. package/esm/clients/pluto.spec.d.ts +0 -1
  91. package/esm/eventHandlers/activation.d.ts +0 -15
  92. package/esm/eventHandlers/authentication.d.ts +0 -17
  93. package/esm/eventHandlers/userAttributes.d.ts +0 -24
  94. package/esm/exceptions/PaywallError.d.ts +0 -7
  95. package/esm/index-authentcation.spec.d.ts +0 -4
  96. package/esm/index-storage.spec.d.ts +0 -4
  97. package/esm/index-subscription.spec.d.ts +0 -4
  98. package/esm/index.d.ts +0 -26
  99. package/esm/janitor/index.d.ts +0 -10
  100. package/esm/janitor/jobs/accessCookieRefresher.d.ts +0 -1
  101. package/esm/janitor/jobs/accessCookieRefresher.spec.d.ts +0 -4
  102. package/esm/lib/PaywallUnlocker.d.ts +0 -16
  103. package/esm/lib/PaywallUnlocker.spec.d.ts +0 -4
  104. package/esm/lib/User.d.ts +0 -21
  105. package/esm/lib/cookie.d.ts +0 -5
  106. package/esm/lib/rpc.d.ts +0 -6
  107. package/esm/store/index.d.ts +0 -22
  108. package/esm/store/index.spec.d.ts +0 -1
  109. package/esm/types.d.ts +0 -22
  110. package/esm/user.js +0 -2
  111. package/esm/user.js.map +0 -1
  112. package/esm/utils/customEventWrapper.d.ts +0 -17
  113. package/esm/utils/deepEquals.d.ts +0 -4
  114. package/esm/utils/getOrUpdate.d.ts +0 -6
  115. package/esm/utils/getOrUpdate.spec.d.ts +0 -1
  116. package/esm/utils/location.d.ts +0 -3
  117. package/esm/utils/log.d.ts +0 -26
  118. package/esm/utils/queue.d.ts +0 -12
  119. package/esm/utils/url.d.ts +0 -6
  120. package/esm/utils/url.spec.d.ts +0 -1
@@ -1,2 +1,3 @@
1
- export declare function getCookieValue(attributes: any): string;
1
+ import { UserAttributes } from '../../types';
2
+ export declare function getCookieValue(attributes: UserAttributes): string;
2
3
  export default function daxSubCookieHandler(): Promise<void>;
@@ -1,4 +1,4 @@
1
- declare type UnlockResponse = {
1
+ export declare type UnlockResponse = {
2
2
  isLoggedIn: boolean;
3
3
  hasAccess: boolean;
4
4
  accessFeatures: string[];
@@ -1,7 +1,7 @@
1
1
  import { State, UserAttributes } from '../types';
2
2
  declare class UserDataRequest<UA extends UserAttributes, S extends {
3
3
  [index: string]: unknown;
4
- }, Attributes extends Array<keyof UA>, Namespaces extends Array<string>, SubscribeResponse extends {} = {
4
+ }, Attributes extends Array<keyof UA>, Namespaces extends Array<string>, SubscribeResponse extends Record<string, object> = {
5
5
  state: State;
6
6
  }> {
7
7
  private attributes?;
package/dist/lib/rpc.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- declare let rpcSessionClient: {
2
- addMethod: (methodName: string) => void;
3
- userAuthenticated: (boolean: any) => void;
4
- };
5
- export declare const getRPCClient: () => Pick<typeof rpcSessionClient, 'userAuthenticated'>;
1
+ import { RPCclient } from '@amedia/frontend-rpc';
2
+ interface LocalRPCclient extends RPCclient {
3
+ userAuthenticated: (isLoggedIn: boolean) => void;
4
+ }
5
+ export declare const getRPCClient: () => LocalRPCclient;
6
6
  export {};
@@ -1,4 +1,5 @@
1
- export declare const setRootElement: (element: any) => void;
1
+ declare let rootElement: Window | Element;
2
+ export declare const setRootElement: (element: typeof rootElement) => void;
2
3
  /**
3
4
  * This listener MUST be used by all listeners in ../eventHandlers
4
5
  * We want to ensure that only _one_ instance of this package actually
@@ -6,12 +7,13 @@ export declare const setRootElement: (element: any) => void;
6
7
  * @param type
7
8
  * @param listener
8
9
  */
9
- export declare const listenWithExclusivity: <T extends keyof WindowEventMap>(type: T, listener: (this: Window, ev: WindowEventMap[T]) => void) => void;
10
+ export declare const listenWithExclusivity: <T extends keyof WindowEventMap>(type: T, listener: (this: typeof rootElement, ev: WindowEventMap[T]) => void) => void;
10
11
  /**
11
12
  * Use this for response listeners. (../index.ts) where we want
12
13
  * all updates with data - no matter who sent the actual request
13
14
  * @param type
14
15
  * @param listener
15
16
  */
16
- export declare const listenForAllEvents: <T extends keyof WindowEventMap>(type: T, listener: (this: Window, ev: WindowEventMap[T]) => void) => void;
17
+ export declare const listenForAllEvents: <T extends keyof WindowEventMap>(type: T, listener: (this: typeof rootElement, ev: WindowEventMap[T]) => void) => void;
17
18
  export declare const dispatch: <T extends Event>(event: T) => void;
19
+ export {};
@@ -1,4 +1 @@
1
- /**
2
- * @see https://dev.to/sanderdebr/deep-equality-checking-of-objects-in-vanilla-javascript-5592
3
- */
4
1
  export declare const deepEquals: (a: unknown, b: unknown) => boolean;
@@ -1,8 +1,8 @@
1
1
  export declare type Logger = {
2
- debug: (message: string | Error, context?: Object) => void;
3
- info: (message: string | Error, context?: Object) => void;
4
- warn: (message: string | Error, context?: Object) => void;
5
- error: (message: string | Error, context?: Object) => void;
2
+ debug: (message: string | Error, context?: any) => void;
3
+ info: (message: string | Error, context?: any) => void;
4
+ warn: (message: string | Error, context?: any) => void;
5
+ error: (message: string | Error, context?: any) => void;
6
6
  };
7
7
  export declare const setLogger: (engine: Logger) => void;
8
8
  declare const _default: Logger;
@@ -1,10 +1,10 @@
1
1
  declare const _default: {
2
2
  add: (key: string, promiseCallbacks: {
3
- resolve: (PromiseLike: any) => void;
3
+ resolve: (arg0?: any) => void;
4
4
  reject: (reason?: any) => void;
5
5
  }) => void;
6
6
  getAll: (key: string) => {
7
- resolve: (PromiseLike: any) => void;
7
+ resolve: (arg0?: any) => void;
8
8
  reject: (reason?: any) => void;
9
9
  }[];
10
10
  peekQueueSize: (key: string) => number;
@@ -0,0 +1,6 @@
1
+ export declare const isFunction: (argument: unknown) => argument is Function;
2
+ export declare const isRecord: (argument: unknown) => argument is Record<string, unknown>;
3
+ export declare const isError: (argument: unknown) => argument is Error;
4
+ export declare const isString: (argument: unknown) => argument is string;
5
+ export declare const isNumber: (argument: unknown) => argument is number;
6
+ export declare const isBoolean: (argument: unknown) => argument is boolean;
package/eik.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "user",
3
- "version": "0.13.16",
3
+ "version": "0.13.26",
4
4
  "server": "https://assets.acdn.no",
5
5
  "files": {
6
6
  "user.js": "./dist/index.esm.js",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amedia/user",
3
- "version": "0.13.24",
3
+ "version": "0.13.26",
4
4
  "description": "Client lib for working with aID user and associated data",
5
5
  "repository": "https://github.com/amedia/amedia-user-js",
6
6
  "author": "Amedia Utvikling (ID og data)",
@@ -28,7 +28,6 @@
28
28
  "@types/convict": "^6.1.1",
29
29
  "@types/dexie": "^1.3.1",
30
30
  "@types/jest": "^26.0.24",
31
- "@types/lodash": "^4.14.172",
32
31
  "@typescript-eslint/eslint-plugin": "^4.29.1",
33
32
  "esbuild": "^0.12.19",
34
33
  "eslint": "^7.32.0",
@@ -20,11 +20,12 @@ import * as authentication from './authentication';
20
20
  import { setRootElement } from '../utils/customEventWrapper';
21
21
  import { removeAllCookies } from '../lib/cookie';
22
22
  import restoreAllMocks = jest.restoreAllMocks;
23
+ import { AccessFeatures } from '../clients/jupiter';
23
24
 
24
25
  const publicationDomain = 'www.avisnavn.no';
25
26
 
26
27
  beforeEach(() => {
27
- setRootElement(new Document());
28
+ setRootElement(new Document().createElement('div'));
28
29
  restoreAllMocks();
29
30
  removeAllCookies();
30
31
  jest.spyOn(cookie, 'setCookie');
@@ -113,9 +114,9 @@ describe('hasAccess', () => {
113
114
  });
114
115
  });
115
116
 
116
- let cookieResponse;
117
+ let cookieResponse: any;
117
118
 
118
- function itReturnsCookieResponse(success, accessFeatures) {
119
+ function itReturnsCookieResponse(success: any, accessFeatures: AccessFeatures) {
119
120
  it(`returns cookie response with success = ${success}`, () => {
120
121
  expect(cookieResponse.success).toEqual(success);
121
122
  });
@@ -164,7 +165,7 @@ function itSetsVstoken2Cookie() {
164
165
  });
165
166
  }
166
167
 
167
- function mockGetUserAccess(status, accessFeatures) {
168
+ function mockGetUserAccess(status: any, accessFeatures: AccessFeatures) {
168
169
  beforeEach(async () => {
169
170
  jest.spyOn(jupiter, 'getUserAccess').mockResolvedValue({
170
171
  status,
@@ -174,15 +175,18 @@ function mockGetUserAccess(status, accessFeatures) {
174
175
  });
175
176
  }
176
177
 
177
- function getUserAccessSucceeds(accessFeatures) {
178
+ function getUserAccessSucceeds(accessFeatures: AccessFeatures) {
178
179
  mockGetUserAccess('success', accessFeatures);
179
180
  }
180
181
 
181
- function getUserAccessFails(accessFeatures) {
182
+ function getUserAccessFails(accessFeatures: AccessFeatures) {
182
183
  mockGetUserAccess('failure', accessFeatures);
183
184
  }
184
185
 
185
- function mockGetNonUserAccessSucceeds(status, accessFeatures) {
186
+ function mockGetNonUserAccessSucceeds(
187
+ status: any,
188
+ accessFeatures: AccessFeatures
189
+ ) {
186
190
  beforeEach(async () => {
187
191
  jest.spyOn(jupiter, 'getNonUserAccess').mockResolvedValue({
188
192
  status,
@@ -193,11 +197,11 @@ function mockGetNonUserAccessSucceeds(status, accessFeatures) {
193
197
  });
194
198
  }
195
199
 
196
- function getNonUserAccessSucceeds(accessFeatures) {
200
+ function getNonUserAccessSucceeds(accessFeatures: AccessFeatures) {
197
201
  mockGetNonUserAccessSucceeds('success', accessFeatures);
198
202
  }
199
203
 
200
- function getNonUserAccessFails(accessFeatures) {
204
+ function getNonUserAccessFails(accessFeatures: AccessFeatures) {
201
205
  mockGetNonUserAccessSucceeds('failure', accessFeatures);
202
206
  }
203
207
 
@@ -38,7 +38,7 @@ export const hasAccess = (
38
38
  ).length > 0;
39
39
 
40
40
  export const setUserAccessCookie = async (
41
- publicationDomain
41
+ publicationDomain: string
42
42
  ): Promise<SetCookieResponse> => {
43
43
  const vstoken = await getUserAccess(publicationDomain);
44
44
  if (vstoken.status === 'success') {
@@ -56,7 +56,7 @@ export const setUserAccessCookie = async (
56
56
  };
57
57
 
58
58
  export const setNonUserAccessCookie = async (
59
- publicationDomain
59
+ publicationDomain: string
60
60
  ): Promise<SetCookieResponse> => {
61
61
  const vstoken2 = await getNonUserAccess(publicationDomain);
62
62
  if (vstoken2.status === 'success') {
@@ -69,7 +69,7 @@ export const setNonUserAccessCookie = async (
69
69
  };
70
70
 
71
71
  export const setAccessCookies = async (
72
- publicationDomain
72
+ publicationDomain: string
73
73
  ): Promise<SetCookieResponse> => {
74
74
  const setters: Array<Promise<SetCookieResponse>> = [];
75
75
  setters.push(
@@ -99,7 +99,7 @@ export const setAccessCookies = async (
99
99
  );
100
100
  };
101
101
 
102
- export const setHasPerformedPaywallReload = (requestedUrl?): void => {
102
+ export const setHasPerformedPaywallReload = (requestedUrl?: string): void => {
103
103
  const expires = new Date();
104
104
  expires.setTime(expires.getTime() + 2 * 60 * 1000); // Add two minutes
105
105
 
@@ -110,6 +110,8 @@ export const setHasPerformedPaywallReload = (requestedUrl?): void => {
110
110
  );
111
111
  };
112
112
 
113
- export const hasRecentlyPerformedPaywallReload = (requestedUrl?): boolean =>
113
+ export const hasRecentlyPerformedPaywallReload = (
114
+ requestedUrl?: string
115
+ ): boolean =>
114
116
  getCookie('aid-paywall-reload-performed') ===
115
117
  (requestedUrl || getCurrentUrl());
@@ -31,7 +31,7 @@ function itShouldCallThirdPartyCookieStrategy() {
31
31
  });
32
32
  }
33
33
 
34
- function itShouldNavigateToAutoLogin(context) {
34
+ function itShouldNavigateToAutoLogin(context: string) {
35
35
  it(`should navigate to auto with context ${context}`, async () => {
36
36
  expect(location.navigateTo).toHaveBeenCalledWith(
37
37
  `https://www.aid.no/aid/logg_inn/auto?context=auto_login_${context}&requestedUrl=https%3A%2F%2Fwww.avisnavn.no%2Fabo%2F%3Fquery%3Dparam`
@@ -102,7 +102,7 @@ describe('attemptAutoLoginJob', () => {
102
102
  });
103
103
 
104
104
  describe('when in iframe (Dr. Edition)', () => {
105
- let originalTop;
105
+ let originalTop: Window;
106
106
  beforeEach(async () => {
107
107
  originalTop = global.top;
108
108
  // @ts-ignore
@@ -199,7 +199,7 @@ describe('attemptPaywallAutoLogin', () => {
199
199
  });
200
200
 
201
201
  describe('when in iframe (Dr. Edition)', () => {
202
- let originalTop;
202
+ let originalTop: Window;
203
203
  beforeEach(async () => {
204
204
  originalTop = global.top;
205
205
  // @ts-ignore
@@ -4,6 +4,11 @@ import { navigateTo } from '../utils/location';
4
4
  import { getCookie, removeCookie, setCookie } from '../lib/cookie';
5
5
  import UserDataRequest from '../lib/UserDataRequest';
6
6
 
7
+ export type Strategies = Array<{
8
+ name: string;
9
+ handler: () => Promise<boolean>;
10
+ }>;
11
+
7
12
  const AUTOLOGIN_COOKIE = 'has-tried-autologin';
8
13
  export const AUTOLOGIN_COOKIE_TTL = 60000; // One minute
9
14
 
@@ -30,7 +35,7 @@ function hasAlreadyAttemptedAutoLogin() {
30
35
  return false;
31
36
  }
32
37
 
33
- const attemptAutologin = async (strategies) => {
38
+ const attemptAutologin = async (strategies: Strategies) => {
34
39
  if (inIframe()) {
35
40
  // Avoid auto-login on preview for dr edition.
36
41
  return;
@@ -12,8 +12,8 @@ const TestDataSchema = Record({
12
12
  test: String,
13
13
  });
14
14
 
15
- let log;
16
- let mockEngine;
15
+ let log: any;
16
+ let mockEngine: any;
17
17
  beforeEach(() => {
18
18
  restoreAllMocks();
19
19
  MockDate.reset();
@@ -30,8 +30,7 @@ const get = async <T extends RuntypeBase>(
30
30
  return Promise.resolve();
31
31
  }
32
32
  try {
33
- const typedData = validator.check(cacheEntry.data);
34
- return typedData as T;
33
+ return validator.check(cacheEntry.data);
35
34
  } catch (error) {
36
35
  log.error(error, { cacheData: cacheEntry.data });
37
36
  remove(key);
@@ -98,7 +98,7 @@ describe('Europa', () => {
98
98
  });
99
99
 
100
100
  describe('getNamespaces', () => {
101
- let response;
101
+ let response: any;
102
102
  describe('with cold cache', () => {
103
103
  describe('with one namespace', () => {
104
104
  const data = { euucp: { x: 'x' } };
@@ -125,7 +125,7 @@ describe('getNamespaces', () => {
125
125
  describe('when doing sequence of calls for different namespaces', () => {
126
126
  const data = { euucp: { x: 'x' } };
127
127
  const data2 = { user_info: { a: 'a' } };
128
- let response2;
128
+ let response2: any;
129
129
  beforeEach(async () => {
130
130
  fetch.mockResponseOnce(JSON.stringify({ namespaces: data }));
131
131
  fetch.mockResponseOnce(JSON.stringify({ namespaces: data2 }));
@@ -141,7 +141,7 @@ describe('getNamespaces', () => {
141
141
  describe('when doing sequence of calls for different namespaces before first response returns', () => {
142
142
  const data = { euucp: { x: 'x' } };
143
143
  const data2 = { user_info: { a: 'a' } };
144
- let response2;
144
+ let response2: any;
145
145
  beforeEach(async () => {
146
146
  fetch.mockResponseOnce(JSON.stringify({ namespaces: data }));
147
147
  fetch.mockResponseOnce(JSON.stringify({ namespaces: data2 }));
@@ -1,4 +1,4 @@
1
- import { Record, Unknown, Dictionary, String } from 'runtypes';
1
+ import { Record, Unknown, Dictionary, String, Static } from 'runtypes';
2
2
 
3
3
  import cache from '../cache';
4
4
  import getOrUpdate from '../utils/getOrUpdate';
@@ -20,7 +20,10 @@ const doFetch = (url: string, method: string = 'GET', body?: Object) =>
20
20
  },
21
21
  });
22
22
 
23
- function mergeCache(cachedData, newData) {
23
+ function mergeCache(
24
+ cachedData: Static<typeof EuropaSchema>,
25
+ newData: Static<typeof EuropaSchema>
26
+ ) {
24
27
  return {
25
28
  namespaces: Object.entries(newData.namespaces).reduce(
26
29
  (mergedNamespaces, [namespace, data]) => ({
@@ -34,7 +37,7 @@ function mergeCache(cachedData, newData) {
34
37
 
35
38
  const updateNamespaceInCache = async (namespace: string, data: Object) => {
36
39
  const cachedData = await cache.get('europa', EuropaSchema);
37
- const updatedData = mergeCache(cachedData, {
40
+ const updatedData = mergeCache(cachedData || { namespaces: {} }, {
38
41
  namespaces: { [namespace]: data },
39
42
  });
40
43
  return cache.set('europa', updatedData, Date.now() + CACHE_DURATION);
@@ -64,7 +67,9 @@ export const getNamespaces = async <
64
67
  }
65
68
 
66
69
  let missingNamespaces = namespaces as string[];
67
- const cachedData = await cache.get('europa', EuropaSchema);
70
+ const cachedData = (await cache.get('europa', EuropaSchema)) || {
71
+ namespaces: {},
72
+ };
68
73
  if (!options.ignoreCache) {
69
74
  missingNamespaces = (
70
75
  cachedData
@@ -1,4 +1,7 @@
1
1
  import { getCurrentHostname, getCurrentUrl } from '../utils/url';
2
+ import { isBoolean, isNumber, isString } from '../utils/type';
3
+
4
+ type Context = Record<string, unknown>;
2
5
 
3
6
  /**
4
7
  * Extracted from mercury.tsx so other calls to mercury can log properly
@@ -20,17 +23,22 @@ function postLog(url: string, query: { [index: string]: any }) {
20
23
  });
21
24
  }
22
25
 
23
- const backendLog = (level: string, message: string, context: Object) => {
24
- const internalContext = {};
26
+ const backendLog = (level: string, message: string, context: Context) => {
27
+ const internalContext: Context = {};
25
28
  Object.keys(context).forEach((key) => {
26
- if (['string', 'number', 'boolean'].includes(typeof context[key])) {
27
- internalContext[key] = context[key];
29
+ const currentContext = context[key];
30
+ if (
31
+ isString(currentContext) ||
32
+ isNumber(currentContext) ||
33
+ isBoolean(currentContext)
34
+ ) {
35
+ internalContext[key] = currentContext;
28
36
  return;
29
37
  }
30
38
  try {
31
- internalContext[key] = JSON.stringify(context[key]);
32
- } catch (e) {
33
- internalContext[key] = context[key];
39
+ internalContext[key] = JSON.stringify(currentContext);
40
+ } catch {
41
+ internalContext[key] = currentContext;
34
42
  }
35
43
  });
36
44
  const extendedContext = {
@@ -43,13 +51,13 @@ const backendLog = (level: string, message: string, context: Object) => {
43
51
  };
44
52
 
45
53
  const log = {
46
- error: (message: string, context: Object) =>
54
+ error: (message: string, context: Context) =>
47
55
  backendLog('error', message, context),
48
- warn: (message: string, context: Object) =>
56
+ warn: (message: string, context: Context) =>
49
57
  backendLog('warn', message, context),
50
- info: (message: string, context: Object) =>
58
+ info: (message: string, context: Context) =>
51
59
  backendLog('info', message, context),
52
- debug: (message: string, context: Object) =>
60
+ debug: (message: string, context: Context) =>
53
61
  backendLog('debug', message, context),
54
62
  };
55
63
 
@@ -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();
package/src/index.ts CHANGED
@@ -46,11 +46,7 @@ export const updateStorage = (storage: { [index: string]: Object }) => {
46
46
  );
47
47
  };
48
48
 
49
- // TODO: It's seemingly impossible to mock this.
50
- // TODO: Excluding from test for now
51
- if (typeof process === 'undefined') {
52
- listenForStateRequests();
53
- listenForUserAttributeRequests();
54
- listenForStorageRequests();
55
- runJanitor();
56
- }
49
+ listenForStateRequests();
50
+ listenForUserAttributeRequests();
51
+ listenForStorageRequests();
52
+ runJanitor();
@@ -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
  });