@amedia/user 0.13.29 → 0.13.31

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.
@@ -1,4 +1,4 @@
1
- declare let rootElement: Window | Element;
1
+ declare let rootElement: Window | Element | typeof globalThis;
2
2
  export declare const setRootElement: (element: typeof rootElement) => void;
3
3
  /**
4
4
  * This listener MUST be used by all listeners in ../eventHandlers
package/eik.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
- "name": "user",
3
- "version": "0.13.29",
2
+ "name": "@amedia/user",
3
+ "version": "0.13.31",
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.29",
3
+ "version": "0.13.31",
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)",
@@ -25,7 +25,7 @@
25
25
  "devDependencies": {
26
26
  "@amedia/eslint-config-base": "^4.0.7",
27
27
  "@amedia/eslint-config-typescript": "^4.0.10",
28
- "@eik/cli": "^1.8.41",
28
+ "@eik/cli": "^1.8.42",
29
29
  "@types/convict": "^6.1.1",
30
30
  "@types/dexie": "^1.3.1",
31
31
  "@types/jest": "^26.0.24",
@@ -1,4 +1,8 @@
1
- import { getNamespaces, updateNamespace } from '../clients/europa';
1
+ import {
2
+ getNamespaces,
3
+ saveNamespace,
4
+ updateNamespace,
5
+ } from '../clients/europa';
2
6
  import store from '../store';
3
7
 
4
8
  export const requestStorage = (namespaces: string[]) => {
@@ -11,3 +15,8 @@ export const updateStorage = (storage: { [namespace: string]: object }) =>
11
15
  Object.keys(storage).forEach((namespace) =>
12
16
  updateNamespace(namespace, storage[namespace])
13
17
  );
18
+
19
+ export const saveStorage = (storage: { [namespace: string]: object }) =>
20
+ Object.keys(storage).forEach((namespace) =>
21
+ saveNamespace(namespace, storage[namespace])
22
+ );
@@ -36,8 +36,8 @@ function hasAlreadyAttemptedAutoLogin() {
36
36
  }
37
37
 
38
38
  const attemptAutologin = async (strategies: Strategies) => {
39
- if (inIframe()) {
40
- // Avoid auto-login on preview for dr edition.
39
+ if (typeof window === 'undefined' || inIframe()) {
40
+ // Avoid auto-login on preview for dr edition and in workers.
41
41
  return;
42
42
  }
43
43
 
@@ -1,19 +1,7 @@
1
1
  import { CacheEntry, Engine } from '../types';
2
2
 
3
- declare global {
4
- interface Window {
5
- amediaUserCache?: { [index: string]: CacheEntry };
6
- }
7
-
8
- namespace NodeJS {
9
- interface Global {
10
- amediaUserCache?: { [index: string]: CacheEntry };
11
- }
12
- }
13
- }
14
-
15
3
  const getStorage = () => {
16
- const rootObject = typeof window !== 'undefined' ? window : global;
4
+ const rootObject = globalThis;
17
5
  // eslint-disable-next-line no-return-assign
18
6
  return rootObject.amediaUserCache || (rootObject.amediaUserCache = {});
19
7
  };
@@ -38,7 +26,7 @@ export const remove = (key: string): Promise<void> => {
38
26
  };
39
27
 
40
28
  export const clear = (): Promise<void> => {
41
- (typeof window !== 'undefined' ? window : global).amediaUserCache = {};
29
+ globalThis.amediaUserCache = {};
42
30
  return Promise.resolve();
43
31
  };
44
32
 
@@ -18,6 +18,7 @@ function postLog(url: string, query: { [index: string]: any }) {
18
18
  };
19
19
  fetch(url, options).then((res) => {
20
20
  if (!res.ok) {
21
+ // eslint-disable-next-line no-console
21
22
  console.error(
22
23
  `Failed to send log to backend: Error, status: ${res.status}, text: ${res.statusText}`
23
24
  );
@@ -106,7 +106,7 @@ export const removeSession = async (domain: string) =>
106
106
  method: 'DELETE',
107
107
  }).then((res) => {
108
108
  if (!res.ok) {
109
- if (res.status === 403) {
109
+ if (res.status === 403 || res.status === 404) {
110
110
  // No session to log out. Ignoring
111
111
  return;
112
112
  }
@@ -4,7 +4,8 @@ export enum AmediaUserEvents {
4
4
  REQUEST_STATE = 'amedia-user.request-state',
5
5
  REQUEST_USER_ATTRIBUTES = 'amedia-user.request-user-attributes',
6
6
  REQUEST_STORAGE_NAMESPACES = 'amedia-user.request-storage-namespaces',
7
- UPDATE_STORAGE = 'amedua-user.update-storage-namespaces',
7
+ SAVE_STORAGE = 'amedia-user.save-storage-namespaces',
8
+ UPDATE_STORAGE = 'amedia-user.update-storage-namespaces',
8
9
  ON_ATTRIBUTES_UPDATED = 'amedia-user.on-user-attributes-updated',
9
10
  ON_STATE_UPDATED = 'amedia-user.on-state-updated',
10
11
  ON_STORAGE_UPDATED = 'amedia-user.on-storage-updated',
@@ -17,6 +18,9 @@ declare global {
17
18
  Array<keyof UserAttributes>
18
19
  >;
19
20
  [AmediaUserEvents.REQUEST_STORAGE_NAMESPACES]: CustomEvent<Array<string>>;
21
+ [AmediaUserEvents.SAVE_STORAGE]: CustomEvent<{
22
+ [namespace: string]: object;
23
+ }>;
20
24
  [AmediaUserEvents.UPDATE_STORAGE]: CustomEvent<{
21
25
  [namespace: string]: object;
22
26
  }>;
@@ -1,7 +1,7 @@
1
1
  import store from '../store';
2
2
  import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
3
3
  import { AmediaUserEvents } from './eventMap';
4
- import { requestStorage, updateStorage } from '../actions/storage';
4
+ import { requestStorage, saveStorage, updateStorage } from '../actions/storage';
5
5
  import { whenIsLoggedIn } from '../utils/loginState';
6
6
 
7
7
  export const listenForStorageRequests = () => {
@@ -36,4 +36,10 @@ export const listenForStorageRequests = () => {
36
36
  updateStorage(evt.detail);
37
37
  });
38
38
  });
39
+
40
+ listenWithExclusivity(AmediaUserEvents.SAVE_STORAGE, (evt) => {
41
+ whenIsLoggedIn().then(() => {
42
+ saveStorage(evt.detail);
43
+ });
44
+ });
39
45
  };
@@ -3,7 +3,7 @@
3
3
  */
4
4
 
5
5
  import * as europa from './clients/europa';
6
- import { updateStorage } from './index';
6
+ import { saveStorage, updateStorage } from './index';
7
7
  import store from './store';
8
8
  import UserDataRequest from './lib/UserDataRequest';
9
9
  import { setRootElement } from './utils/customEventWrapper';
@@ -29,6 +29,7 @@ beforeEach(() => {
29
29
 
30
30
  describe('Storage', () => {
31
31
  beforeEach(() => {
32
+ spyOn(europa, 'saveNamespace');
32
33
  spyOn(europa, 'updateNamespace');
33
34
  });
34
35
 
@@ -37,6 +38,21 @@ describe('Storage', () => {
37
38
  store.set('state', { isLoggedIn: true });
38
39
  });
39
40
 
41
+ describe('when saving storage', () => {
42
+ beforeEach(() => {
43
+ saveStorage({
44
+ user_info: {
45
+ some: 'data',
46
+ },
47
+ });
48
+ });
49
+ it('should save storage', () => {
50
+ expect(europa.saveNamespace).toHaveBeenCalledWith('user_info', {
51
+ some: 'data',
52
+ });
53
+ });
54
+ });
55
+
40
56
  describe('when updating storage', () => {
41
57
  beforeEach(() => {
42
58
  updateStorage({
@@ -88,6 +104,19 @@ describe('Storage', () => {
88
104
  store.set('state', { isLoggedIn: false });
89
105
  });
90
106
 
107
+ describe('when saving storage', () => {
108
+ beforeEach(() => {
109
+ saveStorage({
110
+ user_info: {
111
+ some: 'data',
112
+ },
113
+ });
114
+ });
115
+ it('should not attempt storage save', () => {
116
+ expect(europa.saveNamespace).not.toHaveBeenCalled();
117
+ });
118
+ });
119
+
91
120
  describe('when updating storage', () => {
92
121
  beforeEach(() => {
93
122
  updateStorage({
package/src/index.ts CHANGED
@@ -46,6 +46,10 @@ export const updateStorage = (storage: { [index: string]: Object }) => {
46
46
  );
47
47
  };
48
48
 
49
+ export const saveStorage = (storage: { [index: string]: Object }) => {
50
+ dispatch(new CustomEvent(AmediaUserEvents.SAVE_STORAGE, { detail: storage }));
51
+ };
52
+
49
53
  listenForStateRequests();
50
54
  listenForUserAttributeRequests();
51
55
  listenForStorageRequests();
@@ -12,6 +12,10 @@ const jobs = [
12
12
  ];
13
13
 
14
14
  async function performJobs() {
15
+ if (typeof window === 'undefined') {
16
+ // None of our current jobs make any sense to run in a web worker or other settings without a window object.
17
+ return;
18
+ }
15
19
  // eslint-disable-next-line no-restricted-syntax
16
20
  for (const job of jobs) {
17
21
  // eslint-disable-next-line no-await-in-loop
@@ -1,14 +1,12 @@
1
1
  import log from './log';
2
2
 
3
- let rootElement: Window | Element;
3
+ let rootElement: Window | Element | typeof globalThis;
4
4
 
5
5
  export const setRootElement = (element: typeof rootElement) => {
6
6
  rootElement = element;
7
7
  };
8
8
 
9
- if (typeof window !== 'undefined') {
10
- setRootElement(window);
11
- }
9
+ setRootElement(globalThis);
12
10
 
13
11
  /**
14
12
  * This listener MUST be used by all listeners in ../eventHandlers
@@ -1,8 +1,31 @@
1
- export const reload = () => window.location.reload();
1
+ import log from './log';
2
2
 
3
- export const replace = (requestedUrl: string) =>
3
+ export const reload = () => {
4
+ if (typeof window === 'undefined') {
5
+ log.error(
6
+ 'location.reload used in a context without window. Be careful not to use paywall logic etc. in workers and other settings.'
7
+ );
8
+ return;
9
+ }
10
+ window.location.reload();
11
+ };
12
+
13
+ export const replace = (requestedUrl: string) => {
14
+ if (typeof window === 'undefined') {
15
+ log.error(
16
+ 'location.replace used in a context without window. Be careful not to use paywall logic etc. in workers and other settings.'
17
+ );
18
+ return;
19
+ }
4
20
  window.location.replace(requestedUrl);
21
+ };
5
22
 
6
23
  export const navigateTo = (url: string) => {
24
+ if (typeof window === 'undefined') {
25
+ log.error(
26
+ 'location.navigateTo used in a context without window. Be careful not to use interactive UX code in workers and other settings.'
27
+ );
28
+ return;
29
+ }
7
30
  window.location.href = url;
8
31
  };
package/src/utils/url.ts CHANGED
@@ -4,9 +4,9 @@ export const setCurrentHostname = (hostname: string) => {
4
4
  };
5
5
 
6
6
  export const getCurrentHostname = (): string =>
7
- overriddenHostname || window.location.hostname;
7
+ overriddenHostname || globalThis.location.hostname;
8
8
 
9
- export const getCurrentUrl = (): string => window.location.href;
9
+ export const getCurrentUrl = (): string => globalThis.location.href;
10
10
 
11
11
  export const getCurrentEnvironment = () => {
12
12
  const matchBed = /^bed-([^.]+)\.api\.no$/.exec(getCurrentHostname());