@amedia/user 0.6.1 → 0.7.1
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/.codeclimate.yml +7 -0
- package/cloudbuild.json +1 -1
- package/dist/actions/authentication.d.ts +2 -2
- package/dist/actions/userData.d.ts +1 -1
- package/dist/clients/nebula.d.ts +2 -22
- package/dist/clients/pluto.d.ts +2 -7
- package/dist/index-subscription.spec.d.ts +4 -0
- package/dist/index.js +1 -1
- package/dist/index.js.LICENSE.txt +15 -0
- package/dist/store/index.d.ts +13 -0
- package/dist/store/index.spec.d.ts +1 -0
- package/dist/types.d.ts +3 -1
- package/dist/utils/getOrUpdate.d.ts +5 -2
- package/package.json +2 -1
- package/src/actions/authentication.ts +5 -38
- package/src/actions/userData.ts +17 -19
- package/src/clients/jupiter.spec.ts +5 -5
- package/src/clients/jupiter.ts +28 -22
- package/src/clients/nebula.spec.ts +11 -11
- package/src/clients/nebula.ts +33 -10
- package/src/clients/pluto.spec.ts +6 -3
- package/src/clients/pluto.ts +26 -16
- package/src/eventHandlers/userAttributes.ts +15 -8
- package/src/{index-userdata.spec.ts → index-subscription.spec.ts} +49 -57
- package/src/store/{store.spec.ts → index.spec.ts} +1 -1
- package/src/store/{store.ts → index.ts} +11 -6
- package/src/types.ts +1 -1
- package/src/utils/getOrUpdate.ts +12 -11
|
@@ -6,6 +6,7 @@ import { subscribeToUserAttributes } from './index';
|
|
|
6
6
|
import * as userData from './actions/userData';
|
|
7
7
|
import { setRootElement } from './utils/customEventWrapper';
|
|
8
8
|
import { listenForUserAttributeRequests } from './eventHandlers/userAttributes';
|
|
9
|
+
import store from './store';
|
|
9
10
|
|
|
10
11
|
beforeEach(() => {
|
|
11
12
|
jest.resetAllMocks();
|
|
@@ -14,6 +15,9 @@ beforeEach(() => {
|
|
|
14
15
|
// since tests are executed in parallel.
|
|
15
16
|
setRootElement(new Document());
|
|
16
17
|
|
|
18
|
+
// Store must be cleared before we re-listen for user attribute requests
|
|
19
|
+
store.clear();
|
|
20
|
+
|
|
17
21
|
// Also, since we keep creating new root elements, we also
|
|
18
22
|
// need to attach to these new roots.
|
|
19
23
|
listenForUserAttributeRequests();
|
|
@@ -21,13 +25,9 @@ beforeEach(() => {
|
|
|
21
25
|
|
|
22
26
|
describe('Public endpoint - userdata', () => {
|
|
23
27
|
it('should alert about user data', () => {
|
|
24
|
-
jest
|
|
25
|
-
.
|
|
26
|
-
|
|
27
|
-
callback({
|
|
28
|
-
name: 'Donald Duck',
|
|
29
|
-
});
|
|
30
|
-
});
|
|
28
|
+
jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
|
|
29
|
+
store.set('userAttributes', { name: 'Donald Duck' });
|
|
30
|
+
});
|
|
31
31
|
|
|
32
32
|
const onUserUpdate = jest.fn();
|
|
33
33
|
subscribeToUserAttributes(['name'], onUserUpdate);
|
|
@@ -38,13 +38,9 @@ describe('Public endpoint - userdata', () => {
|
|
|
38
38
|
});
|
|
39
39
|
|
|
40
40
|
it('should not alert on same data', () => {
|
|
41
|
-
jest
|
|
42
|
-
.
|
|
43
|
-
|
|
44
|
-
callback({
|
|
45
|
-
name: 'Donald Duck',
|
|
46
|
-
});
|
|
47
|
-
});
|
|
41
|
+
jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
|
|
42
|
+
store.set('userAttributes', { name: 'Donald Duck' });
|
|
43
|
+
});
|
|
48
44
|
|
|
49
45
|
const onUserUpdate1 = jest.fn();
|
|
50
46
|
subscribeToUserAttributes(['name'], onUserUpdate1);
|
|
@@ -64,17 +60,13 @@ describe('Public endpoint - userdata', () => {
|
|
|
64
60
|
|
|
65
61
|
it('should alert when data changes', () => {
|
|
66
62
|
jest
|
|
67
|
-
.spyOn(userData, '
|
|
68
|
-
.mockImplementationOnce((
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
callback({
|
|
75
|
-
name: 'Donald Duck',
|
|
76
|
-
});
|
|
77
|
-
});
|
|
63
|
+
.spyOn(userData, 'requestAttributes')
|
|
64
|
+
.mockImplementationOnce(() =>
|
|
65
|
+
store.set('userAttributes', { name: undefined })
|
|
66
|
+
)
|
|
67
|
+
.mockImplementationOnce(() =>
|
|
68
|
+
store.set('userAttributes', { name: 'Donald Duck' })
|
|
69
|
+
);
|
|
78
70
|
|
|
79
71
|
const firstOnUserUpdate = jest.fn();
|
|
80
72
|
subscribeToUserAttributes(['name'], firstOnUserUpdate);
|
|
@@ -94,15 +86,13 @@ describe('Public endpoint - userdata', () => {
|
|
|
94
86
|
});
|
|
95
87
|
});
|
|
96
88
|
|
|
97
|
-
it('should not return more data that requested
|
|
98
|
-
jest
|
|
99
|
-
.
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
name: 'Donald Duck',
|
|
103
|
-
email: 'donald.duck@andeby.no',
|
|
104
|
-
});
|
|
89
|
+
it('should not return more data that requested', () => {
|
|
90
|
+
jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
|
|
91
|
+
store.set('userAttributes', {
|
|
92
|
+
name: 'Donald Duck',
|
|
93
|
+
email: 'donald.duck@andeby.no',
|
|
105
94
|
});
|
|
95
|
+
});
|
|
106
96
|
|
|
107
97
|
const onUserUpdate = jest.fn();
|
|
108
98
|
subscribeToUserAttributes(['name'], onUserUpdate);
|
|
@@ -113,15 +103,12 @@ describe('Public endpoint - userdata', () => {
|
|
|
113
103
|
});
|
|
114
104
|
|
|
115
105
|
it('should support returning multiple requested attributes', () => {
|
|
116
|
-
jest
|
|
117
|
-
.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
callback({
|
|
121
|
-
name: 'Donald Duck',
|
|
122
|
-
email: 'donald.duck@andeby.no',
|
|
123
|
-
});
|
|
106
|
+
jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
|
|
107
|
+
store.set('userAttributes', {
|
|
108
|
+
name: 'Donald Duck',
|
|
109
|
+
email: 'donald.duck@andeby.no',
|
|
124
110
|
});
|
|
111
|
+
});
|
|
125
112
|
|
|
126
113
|
const onUserUpdate = jest.fn();
|
|
127
114
|
subscribeToUserAttributes(['name', 'email'], onUserUpdate);
|
|
@@ -133,15 +120,6 @@ describe('Public endpoint - userdata', () => {
|
|
|
133
120
|
});
|
|
134
121
|
|
|
135
122
|
it('should require at least one attribute', () => {
|
|
136
|
-
jest
|
|
137
|
-
.spyOn(userData, 'fetchDataBasedOnAttributes')
|
|
138
|
-
.mockImplementationOnce((_, callback: Function) => {
|
|
139
|
-
callback({
|
|
140
|
-
name: 'Donald Duck',
|
|
141
|
-
email: 'donald.duck@andeby.no',
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
|
|
145
123
|
const onUserUpdate = jest.fn();
|
|
146
124
|
expect(() => subscribeToUserAttributes([], onUserUpdate)).toThrowError(
|
|
147
125
|
new Error('You must subscribe to at least one attribute')
|
|
@@ -149,14 +127,12 @@ describe('Public endpoint - userdata', () => {
|
|
|
149
127
|
});
|
|
150
128
|
|
|
151
129
|
it('should support requesting different attributes', () => {
|
|
152
|
-
jest
|
|
153
|
-
.
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
name: 'Donald Duck',
|
|
157
|
-
email: 'donald.duck@andeby.no',
|
|
158
|
-
});
|
|
130
|
+
jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
|
|
131
|
+
store.set('userAttributes', {
|
|
132
|
+
name: 'Donald Duck',
|
|
133
|
+
email: 'donald.duck@andeby.no',
|
|
159
134
|
});
|
|
135
|
+
});
|
|
160
136
|
|
|
161
137
|
const firstOnUserUpdate = jest.fn();
|
|
162
138
|
subscribeToUserAttributes(['name', 'email'], firstOnUserUpdate);
|
|
@@ -175,4 +151,20 @@ describe('Public endpoint - userdata', () => {
|
|
|
175
151
|
email: 'donald.duck@andeby.no',
|
|
176
152
|
});
|
|
177
153
|
});
|
|
154
|
+
|
|
155
|
+
it('should also report state changes', () => {
|
|
156
|
+
jest.spyOn(userData, 'requestAttributes').mockImplementation(() => {
|
|
157
|
+
store.set('userAttributes', {
|
|
158
|
+
name: 'Donald Duck',
|
|
159
|
+
});
|
|
160
|
+
store.set('state', { isLoggedIn: true });
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const onUserUpdate = jest.fn();
|
|
164
|
+
const onStateUpdate = jest.fn();
|
|
165
|
+
subscribeToUserAttributes(['name'], onUserUpdate, onStateUpdate);
|
|
166
|
+
|
|
167
|
+
expect(onUserUpdate).toHaveBeenCalledWith({ name: 'Donald Duck' });
|
|
168
|
+
expect(onStateUpdate).toHaveBeenCalledWith({ isLoggedIn: true });
|
|
169
|
+
});
|
|
178
170
|
});
|
|
@@ -17,15 +17,15 @@ const emit = (key: keyof StoreDataMap): void => {
|
|
|
17
17
|
listeners[key]?.forEach((listener) => listener(store[key]));
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
-
const on = (
|
|
21
|
-
key:
|
|
22
|
-
listener: Listener<
|
|
20
|
+
const on = <K extends keyof StoreDataMap, T = StoreDataMap[K]>(
|
|
21
|
+
key: K,
|
|
22
|
+
listener: Listener<T>
|
|
23
23
|
) => {
|
|
24
24
|
if (!listeners[key]) {
|
|
25
25
|
listeners[key] = [];
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
// @ts-ignore todo How can this possibly be undefined?
|
|
28
|
+
// @ts-ignore todo How can this possibly be undefined given the statement above?
|
|
29
29
|
listeners[key].push(listener);
|
|
30
30
|
};
|
|
31
31
|
|
|
@@ -35,12 +35,17 @@ const set = (key: keyof StoreDataMap, value: StoreDataMap[typeof key]) => {
|
|
|
35
35
|
emit(key);
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
-
const get =
|
|
39
|
-
|
|
38
|
+
const get = <K extends keyof StoreDataMap, T = StoreDataMap[K]>(
|
|
39
|
+
key: K
|
|
40
|
+
// @ts-ignore Not sure why this fails
|
|
41
|
+
): T | undefined => store[key];
|
|
40
42
|
|
|
41
43
|
const clear = () => {
|
|
42
44
|
store.state = undefined;
|
|
43
45
|
store.userAttributes = undefined;
|
|
46
|
+
Object.keys(listeners).forEach((key) => {
|
|
47
|
+
listeners[key] = [];
|
|
48
|
+
});
|
|
44
49
|
};
|
|
45
50
|
|
|
46
51
|
export default { set, get, on, clear };
|
package/src/types.ts
CHANGED
package/src/utils/getOrUpdate.ts
CHANGED
|
@@ -2,15 +2,17 @@ import { Runtype, Static } from 'runtypes';
|
|
|
2
2
|
import queue from './queue';
|
|
3
3
|
import cache from '../cache';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
export type DataFetcherWithCacheTTL<T extends Runtype> = Promise<{
|
|
6
|
+
ttl: number;
|
|
7
|
+
data: Static<T>;
|
|
8
|
+
}>;
|
|
7
9
|
|
|
8
|
-
|
|
10
|
+
export default function getOrUpdate<T extends Runtype>(
|
|
9
11
|
key: string,
|
|
10
12
|
schema: T,
|
|
11
|
-
|
|
12
|
-
): Promise<Static<T>>
|
|
13
|
-
new Promise((resolve, reject) => {
|
|
13
|
+
fetch: () => DataFetcherWithCacheTTL<T>
|
|
14
|
+
): Promise<Static<T>> {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
14
16
|
queue.add(key, { resolve, reject });
|
|
15
17
|
if (queue.peekQueueSize(key) > 1) {
|
|
16
18
|
return;
|
|
@@ -23,11 +25,11 @@ const getOrUpdate = <T extends Runtype>(
|
|
|
23
25
|
});
|
|
24
26
|
return;
|
|
25
27
|
}
|
|
26
|
-
|
|
28
|
+
fetch()
|
|
27
29
|
.then((res) => {
|
|
28
|
-
cache.set(key, res,
|
|
30
|
+
cache.set(key, res.data, res.ttl);
|
|
29
31
|
queue.getAll(key).forEach((promise) => {
|
|
30
|
-
promise.resolve(res);
|
|
32
|
+
promise.resolve(res.data);
|
|
31
33
|
});
|
|
32
34
|
})
|
|
33
35
|
.catch(() => {
|
|
@@ -37,5 +39,4 @@ const getOrUpdate = <T extends Runtype>(
|
|
|
37
39
|
});
|
|
38
40
|
});
|
|
39
41
|
});
|
|
40
|
-
|
|
41
|
-
export default getOrUpdate;
|
|
42
|
+
}
|