@amedia/user 0.8.1 → 0.9.0
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/dist/actions/storage.d.ts +1 -0
- package/dist/clients/europa.d.ts +16 -5
- package/dist/eventHandlers/userAttributes.d.ts +7 -1
- package/dist/index.d.ts +6 -10
- package/dist/index.js +1 -1
- package/dist/lib/User.d.ts +21 -0
- package/dist/store/index.d.ts +10 -1
- package/dist/types.d.ts +4 -10
- package/package.json +1 -1
- package/src/actions/preferences.ts +2 -2
- package/src/actions/storage.ts +8 -0
- package/src/clients/europa.spec.ts +11 -20
- package/src/clients/europa.ts +8 -12
- package/src/eventHandlers/userAttributes.ts +15 -0
- package/src/index-subscription.spec.ts +115 -44
- package/src/index.ts +4 -67
- package/src/lib/User.ts +163 -0
- package/src/store/index.ts +2 -0
- package/src/types.ts +4 -9
- package/src/utils/customEventWrapper.ts +2 -2
|
@@ -18,14 +18,13 @@ beforeEach(() => {
|
|
|
18
18
|
describe('Europa', () => {
|
|
19
19
|
it('saveNamespace should set namespace and return the new value', async () => {
|
|
20
20
|
const userInfo = { gender: 'U', post_code: 'U' };
|
|
21
|
-
fetch
|
|
22
|
-
|
|
21
|
+
fetch
|
|
22
|
+
.mockOnce(JSON.stringify(userInfo)) // data to save 'PUT'
|
|
23
|
+
.mockOnce(JSON.stringify({ namespaces: { user_info: userInfo } })); // data retrieved 'GET'
|
|
23
24
|
|
|
24
25
|
const namespaceData = await saveNamespace('user_info', userInfo);
|
|
25
26
|
expect(fetch).toHaveBeenCalledTimes(2);
|
|
26
|
-
expect(namespaceData).toMatchObject({
|
|
27
|
-
storage: { user_info: userInfo },
|
|
28
|
-
});
|
|
27
|
+
expect(namespaceData).toMatchObject({ user_info: userInfo });
|
|
29
28
|
});
|
|
30
29
|
|
|
31
30
|
it('updateNamespace should update namespace', async () => {
|
|
@@ -38,7 +37,7 @@ describe('Europa', () => {
|
|
|
38
37
|
|
|
39
38
|
const old = await saveNamespace('user_info', oldUserInfo);
|
|
40
39
|
expect(fetch).toHaveBeenCalledTimes(2);
|
|
41
|
-
expect(old).
|
|
40
|
+
expect(old).toEqual({ user_info: oldUserInfo });
|
|
42
41
|
|
|
43
42
|
const updated = await updateNamespace('user_info', newUserInfo).then(() =>
|
|
44
43
|
cache.get('europa', EuropaSchema)
|
|
@@ -97,28 +96,20 @@ describe('Europa', () => {
|
|
|
97
96
|
});
|
|
98
97
|
|
|
99
98
|
describe('getNamespaces', () => {
|
|
100
|
-
it('should return namespace',
|
|
99
|
+
it('should return namespace', () => {
|
|
101
100
|
const data = { euucp: { x: 'x' } };
|
|
102
101
|
fetch.mockResponse(JSON.stringify({ namespaces: data }));
|
|
103
|
-
|
|
104
|
-
const namespaceData = await getNamespaces(['euucp']);
|
|
105
|
-
expect(namespaceData).toEqual(expect.objectContaining({ storage: data }));
|
|
102
|
+
expect(getNamespaces(['euucp'])).resolves.toEqual(data);
|
|
106
103
|
});
|
|
107
104
|
|
|
108
|
-
it('should return multiple namespaces',
|
|
105
|
+
it('should return multiple namespaces', () => {
|
|
109
106
|
const data = { euucp: { x: 'x' }, user_info: { a: 'a' } };
|
|
110
107
|
fetch.mockResponse(JSON.stringify({ namespaces: data }));
|
|
111
|
-
|
|
112
|
-
const namespaceData = await getNamespaces(['euucp', 'user_info']);
|
|
113
|
-
expect(namespaceData).toEqual(
|
|
114
|
-
expect.objectContaining({
|
|
115
|
-
storage: data,
|
|
116
|
-
})
|
|
117
|
-
);
|
|
108
|
+
expect(getNamespaces(['euucp', 'user_info'])).resolves.toEqual(data);
|
|
118
109
|
});
|
|
119
110
|
|
|
120
|
-
it('should require at least one namespace',
|
|
121
|
-
|
|
111
|
+
it('should require at least one namespace', () => {
|
|
112
|
+
expect(getNamespaces([])).rejects.toThrowError(
|
|
122
113
|
new Error('namespaces array cannot be of length 0')
|
|
123
114
|
);
|
|
124
115
|
});
|
package/src/clients/europa.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { Record, Unknown, Dictionary,
|
|
1
|
+
import { Record, Unknown, Dictionary, String } from 'runtypes';
|
|
2
2
|
|
|
3
3
|
import cache from '../cache';
|
|
4
|
-
import { UserAttributes } from '../types';
|
|
5
4
|
import getOrUpdate from '../utils/getOrUpdate';
|
|
6
5
|
import log from '../utils/log';
|
|
7
6
|
|
|
@@ -11,12 +10,6 @@ export const EuropaSchema = Record({ namespaces: Dictionary(Unknown, String) });
|
|
|
11
10
|
|
|
12
11
|
export const clearLocalCache = () => cache.remove('europa');
|
|
13
12
|
|
|
14
|
-
const mapEuropaToUserData = (
|
|
15
|
-
data: Static<typeof EuropaSchema>
|
|
16
|
-
): Pick<UserAttributes, 'storage'> => ({
|
|
17
|
-
storage: data.namespaces,
|
|
18
|
-
});
|
|
19
|
-
|
|
20
13
|
const doFetch = (url: string, method: string = 'GET', body?: Object) =>
|
|
21
14
|
fetch(url, {
|
|
22
15
|
method,
|
|
@@ -51,10 +44,13 @@ export const updateNamespace = async (namespace: string, data: Object) => {
|
|
|
51
44
|
return updateNamespaceInCache(namespace, data);
|
|
52
45
|
};
|
|
53
46
|
|
|
54
|
-
export const getNamespaces = async
|
|
55
|
-
|
|
47
|
+
export const getNamespaces = async <
|
|
48
|
+
S extends { [namespace: string]: { [index: string]: unknown } },
|
|
49
|
+
Namespaces extends Array<keyof S>
|
|
50
|
+
>(
|
|
51
|
+
namespaces: Namespaces,
|
|
56
52
|
options: { ignoreCache: boolean } = { ignoreCache: false }
|
|
57
|
-
): Promise<Pick<
|
|
53
|
+
): Promise<Pick<S, Namespaces[number]>> => {
|
|
58
54
|
if (namespaces.length === 0) {
|
|
59
55
|
throw new Error('namespaces array cannot be of length 0');
|
|
60
56
|
}
|
|
@@ -80,7 +76,7 @@ export const getNamespaces = async (
|
|
|
80
76
|
Object.keys(cacheData.namespaces).filter(
|
|
81
77
|
(namespace) => !Object.keys(namespaces).includes(namespace)
|
|
82
78
|
).length === 0
|
|
83
|
-
).then(
|
|
79
|
+
).then((d) => d.namespaces as Pick<S, Namespaces[number]>);
|
|
84
80
|
};
|
|
85
81
|
|
|
86
82
|
export const saveNamespace = (namespace: string, data: Object) =>
|
|
@@ -2,11 +2,14 @@ import { requestAttributes } from '../actions/userData';
|
|
|
2
2
|
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
3
3
|
import { State, UserAttributes } from '../types';
|
|
4
4
|
import store from '../store';
|
|
5
|
+
import { requestStorage } from '../actions/storage';
|
|
5
6
|
|
|
6
7
|
export enum AmediaUserEvents {
|
|
7
8
|
REQUEST_USER_ATTRIBUTES = 'amedia-user.request-user-attributes',
|
|
9
|
+
REQUEST_STORAGE_NAMESPACES = 'amedia-user.request-storage-namespaces',
|
|
8
10
|
ON_ATTRIBUTES_UPDATED = 'amedia-user.on-user-attributes-updated',
|
|
9
11
|
ON_STATE_UPDATED = 'amedia-user.on-state-updated',
|
|
12
|
+
ON_STORAGE_UPDATED = 'amedia-user.on-storage-updated',
|
|
10
13
|
}
|
|
11
14
|
|
|
12
15
|
declare global {
|
|
@@ -14,8 +17,12 @@ declare global {
|
|
|
14
17
|
[AmediaUserEvents.REQUEST_USER_ATTRIBUTES]: CustomEvent<
|
|
15
18
|
Array<keyof UserAttributes>
|
|
16
19
|
>;
|
|
20
|
+
[AmediaUserEvents.REQUEST_STORAGE_NAMESPACES]: CustomEvent<Array<string>>;
|
|
17
21
|
[AmediaUserEvents.ON_ATTRIBUTES_UPDATED]: CustomEvent<UserAttributes>;
|
|
18
22
|
[AmediaUserEvents.ON_STATE_UPDATED]: CustomEvent<State>;
|
|
23
|
+
[AmediaUserEvents.ON_STORAGE_UPDATED]: CustomEvent<{
|
|
24
|
+
[index: string]: unknown;
|
|
25
|
+
}>;
|
|
19
26
|
}
|
|
20
27
|
}
|
|
21
28
|
|
|
@@ -32,9 +39,17 @@ export const listenForUserAttributeRequests = () => {
|
|
|
32
39
|
new CustomEvent(AmediaUserEvents.ON_STATE_UPDATED, { detail: state })
|
|
33
40
|
);
|
|
34
41
|
});
|
|
42
|
+
store.on('storage', (storage) => {
|
|
43
|
+
dispatch(
|
|
44
|
+
new CustomEvent(AmediaUserEvents.ON_STORAGE_UPDATED, { detail: storage })
|
|
45
|
+
);
|
|
46
|
+
});
|
|
35
47
|
listenWithExclusivity(AmediaUserEvents.REQUEST_USER_ATTRIBUTES, (evt) => {
|
|
36
48
|
requestAttributes(evt.detail);
|
|
37
49
|
});
|
|
50
|
+
listenWithExclusivity(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, (evt) => {
|
|
51
|
+
requestStorage(evt.detail);
|
|
52
|
+
});
|
|
38
53
|
};
|
|
39
54
|
|
|
40
55
|
listenForUserAttributeRequests();
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
* @jest-environment jsdom
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import User from './lib/User';
|
|
6
6
|
import * as userData from './actions/userData';
|
|
7
|
+
import * as storage from './actions/storage';
|
|
7
8
|
import { setRootElement } from './utils/customEventWrapper';
|
|
8
9
|
import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
|
|
9
10
|
import store from './store';
|
|
@@ -31,10 +32,13 @@ describe('Public endpoint - userdata', () => {
|
|
|
31
32
|
});
|
|
32
33
|
|
|
33
34
|
const onUserUpdate = jest.fn();
|
|
34
|
-
new User().withAttributes(
|
|
35
|
+
new User().withAttributes('name').subscribe(onUserUpdate);
|
|
35
36
|
|
|
36
37
|
expect(onUserUpdate).toHaveBeenCalledWith({
|
|
37
|
-
|
|
38
|
+
attributes: {
|
|
39
|
+
name: 'Donald Duck',
|
|
40
|
+
},
|
|
41
|
+
state: {},
|
|
38
42
|
});
|
|
39
43
|
});
|
|
40
44
|
|
|
@@ -43,52 +47,60 @@ describe('Public endpoint - userdata', () => {
|
|
|
43
47
|
store.set('userAttributes', { name: 'Donald Duck' });
|
|
44
48
|
});
|
|
45
49
|
|
|
46
|
-
const userUpdates = new User();
|
|
47
|
-
|
|
48
50
|
const onUserUpdate1 = jest.fn();
|
|
49
|
-
|
|
51
|
+
new User().withAttributes('name').subscribe(onUserUpdate1);
|
|
50
52
|
|
|
51
53
|
const onUserUpdate2 = jest.fn();
|
|
52
|
-
|
|
54
|
+
new User().withAttributes('name').subscribe(onUserUpdate2);
|
|
53
55
|
|
|
54
56
|
expect(onUserUpdate1).toHaveBeenCalledTimes(1);
|
|
55
57
|
expect(onUserUpdate1).toHaveBeenCalledWith({
|
|
56
|
-
|
|
58
|
+
attributes: {
|
|
59
|
+
name: 'Donald Duck',
|
|
60
|
+
},
|
|
61
|
+
state: {},
|
|
57
62
|
});
|
|
58
63
|
|
|
59
64
|
expect(onUserUpdate2).toHaveBeenCalledTimes(1);
|
|
60
65
|
expect(onUserUpdate2).toHaveBeenCalledWith({
|
|
61
|
-
|
|
66
|
+
attributes: {
|
|
67
|
+
name: 'Donald Duck',
|
|
68
|
+
},
|
|
69
|
+
state: {},
|
|
62
70
|
});
|
|
63
71
|
});
|
|
64
72
|
|
|
65
73
|
it('should alert when data changes', () => {
|
|
66
74
|
jest
|
|
67
75
|
.spyOn(userData, 'requestAttributes')
|
|
68
|
-
.mockImplementationOnce(() =>
|
|
69
|
-
store.set('userAttributes', { name: undefined })
|
|
70
|
-
)
|
|
71
|
-
.mockImplementationOnce(() =>
|
|
72
|
-
store.set('userAttributes', { name: 'Donald Duck' })
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
const userUpdates = new User();
|
|
76
|
+
.mockImplementationOnce(() => {
|
|
77
|
+
store.set('userAttributes', { name: undefined });
|
|
78
|
+
})
|
|
79
|
+
.mockImplementationOnce(() => {
|
|
80
|
+
store.set('userAttributes', { name: 'Donald Duck' });
|
|
81
|
+
});
|
|
76
82
|
|
|
77
83
|
const firstOnUserUpdate = jest.fn();
|
|
78
|
-
|
|
84
|
+
new User().withAttributes('name').subscribe(firstOnUserUpdate);
|
|
79
85
|
|
|
80
86
|
const secondOnUserUpdate = jest.fn();
|
|
81
|
-
|
|
87
|
+
new User().withAttributes('name').subscribe(secondOnUserUpdate);
|
|
82
88
|
|
|
83
89
|
expect(firstOnUserUpdate).toHaveBeenCalledTimes(2);
|
|
84
|
-
expect(firstOnUserUpdate).nthCalledWith(1, {});
|
|
90
|
+
expect(firstOnUserUpdate).nthCalledWith(1, { attributes: {}, state: {} });
|
|
85
91
|
expect(firstOnUserUpdate).nthCalledWith(2, {
|
|
86
|
-
|
|
92
|
+
attributes: {
|
|
93
|
+
name: 'Donald Duck',
|
|
94
|
+
},
|
|
95
|
+
state: {},
|
|
87
96
|
});
|
|
88
97
|
|
|
89
98
|
expect(secondOnUserUpdate).toHaveBeenCalledTimes(1);
|
|
90
99
|
expect(secondOnUserUpdate).toHaveBeenCalledWith({
|
|
91
|
-
|
|
100
|
+
attributes: {
|
|
101
|
+
name: 'Donald Duck',
|
|
102
|
+
},
|
|
103
|
+
state: {},
|
|
92
104
|
});
|
|
93
105
|
});
|
|
94
106
|
|
|
@@ -101,10 +113,13 @@ describe('Public endpoint - userdata', () => {
|
|
|
101
113
|
});
|
|
102
114
|
|
|
103
115
|
const onUserUpdate = jest.fn();
|
|
104
|
-
new User().withAttributes(
|
|
116
|
+
new User().withAttributes('name').subscribe(onUserUpdate);
|
|
105
117
|
|
|
106
118
|
expect(onUserUpdate).toHaveBeenCalledWith({
|
|
107
|
-
|
|
119
|
+
attributes: {
|
|
120
|
+
name: 'Donald Duck',
|
|
121
|
+
},
|
|
122
|
+
state: {},
|
|
108
123
|
});
|
|
109
124
|
});
|
|
110
125
|
|
|
@@ -117,21 +132,17 @@ describe('Public endpoint - userdata', () => {
|
|
|
117
132
|
});
|
|
118
133
|
|
|
119
134
|
const onUserUpdate = jest.fn();
|
|
120
|
-
new User().withAttributes(
|
|
135
|
+
new User().withAttributes('name', 'email').subscribe(onUserUpdate);
|
|
121
136
|
|
|
122
137
|
expect(onUserUpdate).toHaveBeenCalledWith({
|
|
123
|
-
|
|
124
|
-
|
|
138
|
+
attributes: {
|
|
139
|
+
name: 'Donald Duck',
|
|
140
|
+
email: 'donald.duck@andeby.no',
|
|
141
|
+
},
|
|
142
|
+
state: {},
|
|
125
143
|
});
|
|
126
144
|
});
|
|
127
145
|
|
|
128
|
-
it('should require at least one attribute', () => {
|
|
129
|
-
const onUserUpdate = jest.fn();
|
|
130
|
-
expect(() => new User().subscribe(onUserUpdate)).toThrowError(
|
|
131
|
-
new Error('You must subscribe to at least one attribute')
|
|
132
|
-
);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
146
|
it('should support requesting different attributes', () => {
|
|
136
147
|
jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
|
|
137
148
|
store.set('userAttributes', {
|
|
@@ -141,20 +152,26 @@ describe('Public endpoint - userdata', () => {
|
|
|
141
152
|
});
|
|
142
153
|
|
|
143
154
|
const firstOnUserUpdate = jest.fn();
|
|
144
|
-
new User().withAttributes(
|
|
155
|
+
new User().withAttributes('name', 'email').subscribe(firstOnUserUpdate);
|
|
145
156
|
|
|
146
157
|
const secondOnUserUpdate = jest.fn();
|
|
147
|
-
new User().withAttributes(
|
|
158
|
+
new User().withAttributes('email').subscribe(secondOnUserUpdate);
|
|
148
159
|
|
|
149
160
|
expect(firstOnUserUpdate).toHaveBeenCalledTimes(1);
|
|
150
161
|
expect(firstOnUserUpdate).nthCalledWith(1, {
|
|
151
|
-
|
|
152
|
-
|
|
162
|
+
attributes: {
|
|
163
|
+
name: 'Donald Duck',
|
|
164
|
+
email: 'donald.duck@andeby.no',
|
|
165
|
+
},
|
|
166
|
+
state: {},
|
|
153
167
|
});
|
|
154
168
|
|
|
155
169
|
expect(secondOnUserUpdate).toHaveBeenCalledTimes(1);
|
|
156
170
|
expect(secondOnUserUpdate).nthCalledWith(1, {
|
|
157
|
-
|
|
171
|
+
attributes: {
|
|
172
|
+
email: 'donald.duck@andeby.no',
|
|
173
|
+
},
|
|
174
|
+
state: {},
|
|
158
175
|
});
|
|
159
176
|
});
|
|
160
177
|
|
|
@@ -166,12 +183,66 @@ describe('Public endpoint - userdata', () => {
|
|
|
166
183
|
store.set('state', { isLoggedIn: true });
|
|
167
184
|
});
|
|
168
185
|
|
|
169
|
-
const
|
|
170
|
-
|
|
186
|
+
const onUpdate = jest.fn();
|
|
187
|
+
|
|
188
|
+
new User().withAttributes('name').subscribe(onUpdate);
|
|
189
|
+
|
|
190
|
+
expect(onUpdate).toHaveBeenCalledWith({
|
|
191
|
+
attributes: { name: 'Donald Duck' },
|
|
192
|
+
state: { isLoggedIn: true },
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('should return storage', () => {
|
|
197
|
+
jest.spyOn(storage, 'requestStorage').mockImplementation(() => {
|
|
198
|
+
store.set('storage', {
|
|
199
|
+
namespace: {
|
|
200
|
+
key: 'value',
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const onUpdate = jest.fn();
|
|
206
|
+
new User().withStorage('namespace').subscribe(onUpdate);
|
|
207
|
+
|
|
208
|
+
expect(onUpdate).toHaveBeenCalledWith({
|
|
209
|
+
storage: {
|
|
210
|
+
namespace: {
|
|
211
|
+
key: 'value',
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
state: {},
|
|
215
|
+
});
|
|
216
|
+
});
|
|
171
217
|
|
|
172
|
-
|
|
218
|
+
it('should return multiple namespaces if requested (but only requested)', () => {
|
|
219
|
+
jest.spyOn(storage, 'requestStorage').mockImplementation(() => {
|
|
220
|
+
store.set('storage', {
|
|
221
|
+
one: {
|
|
222
|
+
key1: 'value1',
|
|
223
|
+
},
|
|
224
|
+
two: {
|
|
225
|
+
key2: 'value2',
|
|
226
|
+
},
|
|
227
|
+
three: {
|
|
228
|
+
key3: 'value3',
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
});
|
|
173
232
|
|
|
174
|
-
|
|
175
|
-
|
|
233
|
+
const onUpdate = jest.fn();
|
|
234
|
+
new User().withStorage('one', 'three').subscribe(onUpdate);
|
|
235
|
+
|
|
236
|
+
expect(onUpdate).toHaveBeenCalledWith({
|
|
237
|
+
storage: {
|
|
238
|
+
one: {
|
|
239
|
+
key1: 'value1',
|
|
240
|
+
},
|
|
241
|
+
three: {
|
|
242
|
+
key3: 'value3',
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
state: {},
|
|
246
|
+
});
|
|
176
247
|
});
|
|
177
248
|
});
|
package/src/index.ts
CHANGED
|
@@ -1,79 +1,14 @@
|
|
|
1
1
|
import 'regenerator-runtime';
|
|
2
|
-
import { AmediaUserEvents } from './eventHandlers/userAttributes';
|
|
3
2
|
import { ActivationEvents } from './eventHandlers/activation';
|
|
4
3
|
import { dispatch, listenForAllEvents } from './utils/customEventWrapper';
|
|
5
|
-
import { State, UserAttributes } from './types';
|
|
6
4
|
import * as authentication from './actions/authentication';
|
|
7
|
-
import
|
|
5
|
+
import User from './lib/User';
|
|
8
6
|
|
|
9
7
|
/**
|
|
10
8
|
* Enables you to subscribe to the user attributes you care about and
|
|
11
9
|
* get notified when something changes
|
|
12
10
|
*/
|
|
13
|
-
|
|
14
|
-
export class User {
|
|
15
|
-
private attributes: Array<keyof UserAttributes> = [];
|
|
16
|
-
|
|
17
|
-
private namespaces: Array<string> = [];
|
|
18
|
-
|
|
19
|
-
private extraData?: Array<string> = [];
|
|
20
|
-
|
|
21
|
-
withAttributes(attributes: Array<keyof UserAttributes>): this {
|
|
22
|
-
this.attributes = attributes;
|
|
23
|
-
return this;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
withStorage(namespaces: Array<string>): this {
|
|
27
|
-
this.namespaces = namespaces;
|
|
28
|
-
return this;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
withExtraData(extraData?: Array<string>): this {
|
|
32
|
-
this.extraData = extraData;
|
|
33
|
-
return this;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
subscribe(
|
|
37
|
-
onUserUpdated: (attributes: UserAttributes) => void,
|
|
38
|
-
onStateUpdated?: (state: State) => void
|
|
39
|
-
): void {
|
|
40
|
-
if (!this.attributes || this.attributes.length === 0) {
|
|
41
|
-
throw new Error('You must subscribe to at least one attribute');
|
|
42
|
-
}
|
|
43
|
-
let previousUserAttributes: UserAttributes;
|
|
44
|
-
listenForAllEvents(AmediaUserEvents.ON_ATTRIBUTES_UPDATED, (evt) => {
|
|
45
|
-
const userAttributes = evt.detail;
|
|
46
|
-
|
|
47
|
-
// Reduce responseAttributes to match requested attributes
|
|
48
|
-
const responseAttributes = this.attributes.reduce((map, attribute) => {
|
|
49
|
-
if (userAttributes[attribute] !== undefined) {
|
|
50
|
-
// @ts-ignore
|
|
51
|
-
// eslint-disable-next-line no-param-reassign
|
|
52
|
-
map[attribute] = userAttributes[attribute];
|
|
53
|
-
}
|
|
54
|
-
return map;
|
|
55
|
-
}, {} as UserAttributes);
|
|
56
|
-
|
|
57
|
-
if (deepEquals(previousUserAttributes, responseAttributes)) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
onUserUpdated(responseAttributes);
|
|
62
|
-
previousUserAttributes = responseAttributes;
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
if (onStateUpdated) {
|
|
66
|
-
listenForAllEvents(AmediaUserEvents.ON_STATE_UPDATED, (evt) => {
|
|
67
|
-
onStateUpdated(evt.detail);
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
dispatch(
|
|
71
|
-
new CustomEvent(AmediaUserEvents.REQUEST_USER_ATTRIBUTES, {
|
|
72
|
-
detail: this.attributes,
|
|
73
|
-
})
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
11
|
+
export const user = () => new User();
|
|
77
12
|
|
|
78
13
|
/**
|
|
79
14
|
* This will, if necessary, trigger a redirect to www.aid.no and return
|
|
@@ -82,7 +17,9 @@ export class User {
|
|
|
82
17
|
*/
|
|
83
18
|
export const goToLoginPage = (requestedUrl?: string) =>
|
|
84
19
|
authentication.login(requestedUrl || window.location.href);
|
|
20
|
+
|
|
85
21
|
export const logout = () => authentication.logout();
|
|
22
|
+
|
|
86
23
|
export const tryActivateSubscriptionAccess = (
|
|
87
24
|
accessFeatures: string[]
|
|
88
25
|
): Promise<{ hasAccess: boolean }> =>
|
package/src/lib/User.ts
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { State, UserAttributes } from '../types';
|
|
2
|
+
import { dispatch, listenForAllEvents } from '../utils/customEventWrapper';
|
|
3
|
+
import { AmediaUserEvents } from '../eventHandlers/userAttributes';
|
|
4
|
+
import { deepEquals } from '../utils/deepEquals';
|
|
5
|
+
|
|
6
|
+
class User<
|
|
7
|
+
UA extends UserAttributes,
|
|
8
|
+
S extends { [index: string]: unknown },
|
|
9
|
+
Attributes extends Array<keyof UA>,
|
|
10
|
+
Namespaces extends Array<string>,
|
|
11
|
+
SubscribeResponse extends {} = { state: State }
|
|
12
|
+
> {
|
|
13
|
+
constructor(
|
|
14
|
+
private attributes?: Attributes,
|
|
15
|
+
private namespaces?: Namespaces
|
|
16
|
+
) {}
|
|
17
|
+
|
|
18
|
+
withAttributes<NewAttributes extends Array<keyof UA>>(
|
|
19
|
+
...attribute: NewAttributes
|
|
20
|
+
) {
|
|
21
|
+
return new User<
|
|
22
|
+
UA,
|
|
23
|
+
S,
|
|
24
|
+
Array<Attributes[number] & NewAttributes[number]>,
|
|
25
|
+
Namespaces,
|
|
26
|
+
SubscribeResponse & {
|
|
27
|
+
attributes: Pick<UA, Attributes[number] & NewAttributes[number]>;
|
|
28
|
+
}
|
|
29
|
+
>(
|
|
30
|
+
[...((this.attributes || ([] as unknown)) as Attributes), ...attribute],
|
|
31
|
+
this.namespaces
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
withStorage<NewNamespaces extends Array<string>>(
|
|
36
|
+
...namespace: NewNamespaces
|
|
37
|
+
) {
|
|
38
|
+
return new User<
|
|
39
|
+
UA,
|
|
40
|
+
S,
|
|
41
|
+
Attributes,
|
|
42
|
+
Array<Namespaces[number] & NewNamespaces[number]>,
|
|
43
|
+
SubscribeResponse & {
|
|
44
|
+
storage: Pick<S, Namespaces[number] & NewNamespaces[number]>;
|
|
45
|
+
}
|
|
46
|
+
>(this.attributes, [
|
|
47
|
+
...((this.namespaces || ([] as unknown)) as Namespaces),
|
|
48
|
+
...namespace,
|
|
49
|
+
]);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
subscribe(onData: (data: SubscribeResponse) => void): void {
|
|
53
|
+
let previousData = this.setupLocalCache();
|
|
54
|
+
|
|
55
|
+
listenForAllEvents(AmediaUserEvents.ON_STATE_UPDATED, (evt) => {
|
|
56
|
+
const state = evt.detail;
|
|
57
|
+
|
|
58
|
+
if (
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
60
|
+
Object.keys(previousData['state']).length > 0 &&
|
|
61
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
62
|
+
deepEquals(previousData['state'], state)
|
|
63
|
+
) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
previousData = { ...previousData, state };
|
|
68
|
+
onData(previousData);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
if (Array.isArray(this.attributes) && this.attributes.length > 0) {
|
|
72
|
+
listenForAllEvents(AmediaUserEvents.ON_ATTRIBUTES_UPDATED, (evt) => {
|
|
73
|
+
const responseAttributes = this.processUserAttributesEvent(evt);
|
|
74
|
+
|
|
75
|
+
if (
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
77
|
+
Object.keys(previousData['attributes']).length > 0 &&
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
79
|
+
deepEquals(previousData['attributes'], responseAttributes)
|
|
80
|
+
) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
previousData = { ...previousData, attributes: responseAttributes };
|
|
85
|
+
onData(previousData);
|
|
86
|
+
});
|
|
87
|
+
dispatch(
|
|
88
|
+
new CustomEvent(AmediaUserEvents.REQUEST_USER_ATTRIBUTES, {
|
|
89
|
+
detail: this.attributes,
|
|
90
|
+
})
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (Array.isArray(this.namespaces) && this.namespaces.length > 0) {
|
|
95
|
+
listenForAllEvents(AmediaUserEvents.ON_STORAGE_UPDATED, (evt) => {
|
|
96
|
+
const responseStorage = this.processStorageEvent(evt);
|
|
97
|
+
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
99
|
+
if (deepEquals(previousData['storage'], responseStorage)) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
previousData = { ...previousData, storage: responseStorage };
|
|
104
|
+
onData(previousData);
|
|
105
|
+
});
|
|
106
|
+
dispatch(
|
|
107
|
+
new CustomEvent(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, {
|
|
108
|
+
detail: this.namespaces,
|
|
109
|
+
})
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private processUserAttributesEvent(evt: CustomEvent<UserAttributes>) {
|
|
115
|
+
const userAttributes = evt.detail as UA;
|
|
116
|
+
|
|
117
|
+
// Reduce responseAttributes to match requested attributes
|
|
118
|
+
// @ts-ignore Cannot be undefined. Guarded above
|
|
119
|
+
const responseAttributes = this.attributes.reduce((map, attribute) => {
|
|
120
|
+
if (userAttributes[attribute] !== undefined) {
|
|
121
|
+
// @ts-ignore
|
|
122
|
+
// eslint-disable-next-line no-param-reassign
|
|
123
|
+
map[attribute] = userAttributes[attribute];
|
|
124
|
+
}
|
|
125
|
+
return map;
|
|
126
|
+
}, {} as UA);
|
|
127
|
+
return responseAttributes;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private processStorageEvent(evt: CustomEvent<{ [index: string]: unknown }>) {
|
|
131
|
+
const storage = evt.detail as S;
|
|
132
|
+
|
|
133
|
+
// Reduce responseAttributes to match requested attributes
|
|
134
|
+
// @ts-ignore Cannot be undefined. Guarded above
|
|
135
|
+
return this.namespaces.reduce((map, namespace) => {
|
|
136
|
+
if (storage[namespace] !== undefined) {
|
|
137
|
+
// @ts-ignore
|
|
138
|
+
// eslint-disable-next-line no-param-reassign
|
|
139
|
+
map[namespace] = storage[namespace];
|
|
140
|
+
}
|
|
141
|
+
return map;
|
|
142
|
+
}, {} as S);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private setupLocalCache() {
|
|
146
|
+
// @ts-ignore
|
|
147
|
+
const previousData: SubscribeResponse = {
|
|
148
|
+
state: {},
|
|
149
|
+
};
|
|
150
|
+
if (this.attributes && this.attributes.length > 0) {
|
|
151
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
152
|
+
previousData['attributes'] = {};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (this.namespaces && this.namespaces.length > 0) {
|
|
156
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
157
|
+
previousData['storage'] = {};
|
|
158
|
+
}
|
|
159
|
+
return previousData;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export default User;
|
package/src/store/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { State, UserAttributes } from '../types';
|
|
|
3
3
|
type StoreDataMap = {
|
|
4
4
|
userAttributes: UserAttributes;
|
|
5
5
|
state: State;
|
|
6
|
+
storage: { [namespace: string]: { [index: string]: unknown } };
|
|
6
7
|
};
|
|
7
8
|
|
|
8
9
|
type Listener<T> = (data: T) => void;
|
|
@@ -43,6 +44,7 @@ const get = <K extends keyof StoreDataMap, T = StoreDataMap[K]>(
|
|
|
43
44
|
const clear = () => {
|
|
44
45
|
store.state = undefined;
|
|
45
46
|
store.userAttributes = undefined;
|
|
47
|
+
store.storage = undefined;
|
|
46
48
|
Object.keys(listeners).forEach((key) => {
|
|
47
49
|
listeners[key] = [];
|
|
48
50
|
});
|