@amedia/user 0.8.1 → 0.10.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/.codeclimate.yml +1 -0
- package/dist/actions/authentication.d.ts +2 -1
- package/dist/actions/authorization.d.ts +5 -1
- 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/exceptions/PaywallError.d.ts +7 -0
- package/dist/index.d.ts +8 -10
- package/dist/index.js +1 -1
- package/dist/lib/PaywallUnlocker.d.ts +16 -0
- package/dist/lib/PaywallUnlocker.spec.d.ts +4 -0
- package/dist/lib/User.d.ts +21 -0
- package/dist/lib/cookie.d.ts +1 -1
- package/dist/store/index.d.ts +10 -1
- package/dist/types.d.ts +4 -10
- package/dist/utils/location.d.ts +2 -0
- package/esm/user.js +6499 -0
- package/esm/user.js.map +1 -0
- package/package.json +1 -1
- package/src/actions/authentication.ts +10 -2
- package/src/actions/authorization.ts +58 -3
- package/src/actions/preferences.spec.ts +2 -2
- 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/clients/nebula.ts +1 -1
- package/src/eventHandlers/userAttributes.ts +15 -0
- package/src/exceptions/PaywallError.ts +13 -0
- package/src/index-subscription.spec.ts +110 -39
- package/src/index.ts +7 -67
- package/src/lib/PaywallUnlocker.spec.ts +196 -0
- package/src/lib/PaywallUnlocker.ts +113 -0
- package/src/lib/User.ts +163 -0
- package/src/lib/cookie.ts +1 -1
- package/src/store/index.ts +2 -0
- package/src/types.ts +4 -9
- package/src/utils/customEventWrapper.ts +2 -2
- package/src/utils/location.ts +6 -0
package/package.json
CHANGED
|
@@ -8,13 +8,19 @@ import {
|
|
|
8
8
|
} from '../utils/url';
|
|
9
9
|
import log from '../utils/log';
|
|
10
10
|
import { UserAttributes } from '../types';
|
|
11
|
+
import store from '../store';
|
|
11
12
|
|
|
12
|
-
export const login = (requestedUrl: string) => {
|
|
13
|
+
export const login = (requestedUrl: URL | string) => {
|
|
13
14
|
const environment = getCurrentEnvironment();
|
|
14
15
|
const url = new URL(
|
|
15
16
|
`https://aid.no${environment && `.${environment}.api.no`}/aid/logg_inn/`
|
|
16
17
|
);
|
|
17
|
-
url.searchParams.set(
|
|
18
|
+
url.searchParams.set(
|
|
19
|
+
'requestedUrl',
|
|
20
|
+
requestedUrl instanceof URL
|
|
21
|
+
? requestedUrl.href
|
|
22
|
+
: requestedUrl || window.location.href
|
|
23
|
+
);
|
|
18
24
|
window.location.href = url.href;
|
|
19
25
|
};
|
|
20
26
|
|
|
@@ -40,3 +46,5 @@ export const logout = async () =>
|
|
|
40
46
|
});
|
|
41
47
|
|
|
42
48
|
export const getSession = (): Promise<UserAttributes> => getCurrentSession();
|
|
49
|
+
|
|
50
|
+
export const isLoggedIn = (): boolean => !!store.get('state')?.isLoggedIn;
|
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
checkForAccess,
|
|
3
|
+
getNonUserAccess,
|
|
4
|
+
getVstoken,
|
|
5
|
+
} from '../clients/jupiter';
|
|
2
6
|
import { getCurrentPublicationDomain } from '../utils/url';
|
|
3
7
|
import { UserAttributes } from '../types';
|
|
4
8
|
import { activateSubscription, UserDeactivatedError } from '../clients/mercury';
|
|
5
9
|
import { logout } from './authentication';
|
|
10
|
+
import { getCookie, setCookie } from '../lib/cookie';
|
|
6
11
|
|
|
7
|
-
export const getAccess = (
|
|
8
|
-
|
|
12
|
+
export const getAccess = (
|
|
13
|
+
accessFeatures: string[] = ['newspaper']
|
|
14
|
+
): Promise<Pick<UserAttributes, 'access'>> =>
|
|
15
|
+
checkForAccess(getCurrentPublicationDomain(), accessFeatures);
|
|
9
16
|
|
|
10
17
|
export const activate = (accessFeatures: string[]): Promise<boolean> => {
|
|
11
18
|
try {
|
|
@@ -18,3 +25,51 @@ export const activate = (accessFeatures: string[]): Promise<boolean> => {
|
|
|
18
25
|
throw error;
|
|
19
26
|
}
|
|
20
27
|
};
|
|
28
|
+
|
|
29
|
+
export const hasAccess = (
|
|
30
|
+
currentAccessFeatures: string[],
|
|
31
|
+
requestedAccessFeatures: string[]
|
|
32
|
+
): boolean =>
|
|
33
|
+
requestedAccessFeatures.filter((requestedAccessFeature) =>
|
|
34
|
+
currentAccessFeatures.includes(requestedAccessFeature)
|
|
35
|
+
).length > 0;
|
|
36
|
+
|
|
37
|
+
export const setAccessCookies = async (publicationDomain): Promise<boolean> => {
|
|
38
|
+
const vstoken = await getVstoken(publicationDomain);
|
|
39
|
+
if (vstoken.status === 'success') {
|
|
40
|
+
/**
|
|
41
|
+
* The VSTOKEN cookie will live for 365 days, but a timestamp in the payload will ensure that it cannot
|
|
42
|
+
* be used to gain access for that long. The reason we want the cookie to live long is that it's also
|
|
43
|
+
* used to keep subscribers from seeing ads for buying the subscription they already have.
|
|
44
|
+
*/
|
|
45
|
+
setCookie('VSTOKEN', vstoken.cookie, 365, '/');
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
const vstoken2 = await getNonUserAccess(publicationDomain);
|
|
49
|
+
if (vstoken2.status === 'success') {
|
|
50
|
+
setCookie('VSTOKEN2', vstoken2.vstoken_cookie, 1, '/');
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const hasRecentlyTriedToUnlockPaywall = (): boolean => {
|
|
57
|
+
const cookie = getCookie('aid-paywall-check-performed');
|
|
58
|
+
if (!cookie) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
if (Array.isArray(cookie)) {
|
|
62
|
+
return cookie.filter((value) => value === window.location.href).length > 0;
|
|
63
|
+
}
|
|
64
|
+
return cookie === window.location.href;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const setHasTriedToUnlockPaywall = (): void => {
|
|
68
|
+
const url = new URL(window.location.href);
|
|
69
|
+
url.searchParams.set('access', 'granted');
|
|
70
|
+
|
|
71
|
+
const expires = new Date();
|
|
72
|
+
expires.setTime(expires.getTime() + 2 * 60 * 1000); // Add two minutes
|
|
73
|
+
|
|
74
|
+
setCookie('aid-paywall-check-performed', url.href, expires);
|
|
75
|
+
};
|
|
@@ -2,9 +2,9 @@ import fetch from 'jest-fetch-mock';
|
|
|
2
2
|
import cache from '../cache';
|
|
3
3
|
import { getPrivacyPreferences } from './preferences';
|
|
4
4
|
|
|
5
|
-
beforeEach(() => {
|
|
5
|
+
beforeEach(async () => {
|
|
6
6
|
fetch.resetMocks();
|
|
7
|
-
cache.clear();
|
|
7
|
+
await cache.clear();
|
|
8
8
|
});
|
|
9
9
|
|
|
10
10
|
const europaResponse = {
|
|
@@ -19,8 +19,8 @@ export const getPrivacyPreferences = async (): Promise<
|
|
|
19
19
|
Pick<UserAttributes, 'privacyPreferences'>
|
|
20
20
|
> =>
|
|
21
21
|
getOrUpdate(PRIVACY_PREFERENCES_CACHE_KEY, PrivacyPreferencesSchema, () =>
|
|
22
|
-
getNamespaces(['privacy_preferences']).then((
|
|
23
|
-
const privacyPreferences: any =
|
|
22
|
+
getNamespaces(['privacy_preferences']).then((namespaces) => {
|
|
23
|
+
const privacyPreferences: any = namespaces.privacy_preferences;
|
|
24
24
|
|
|
25
25
|
return {
|
|
26
26
|
ttl: CACHE_DURATION,
|
|
@@ -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) =>
|
package/src/clients/nebula.ts
CHANGED
|
@@ -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();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
class PaywallError extends Error {
|
|
2
|
+
static ERROR_RELOADED_NO_ACCESS = 'RELOADED_NO_ACCESS';
|
|
3
|
+
|
|
4
|
+
static ERROR_ENABLING_ACCESS_FAILED = 'ENABLING_ACCESS_FAILED';
|
|
5
|
+
|
|
6
|
+
public code: string;
|
|
7
|
+
|
|
8
|
+
constructor(message: string, code: string) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.code = code;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export default PaywallError;
|
|
@@ -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';
|
|
@@ -34,7 +35,10 @@ describe('Public endpoint - userdata', () => {
|
|
|
34
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
|
|
|
@@ -104,7 +116,10 @@ describe('Public endpoint - userdata', () => {
|
|
|
104
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
|
|
|
@@ -120,18 +135,14 @@ describe('Public endpoint - userdata', () => {
|
|
|
120
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', {
|
|
@@ -148,13 +159,19 @@ describe('Public endpoint - userdata', () => {
|
|
|
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,15 @@
|
|
|
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';
|
|
6
|
+
import PaywallUnlocker from './lib/PaywallUnlocker';
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* Enables you to subscribe to the user attributes you care about and
|
|
11
10
|
* get notified when something changes
|
|
12
11
|
*/
|
|
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
|
-
}
|
|
12
|
+
export const user = () => new User();
|
|
77
13
|
|
|
78
14
|
/**
|
|
79
15
|
* This will, if necessary, trigger a redirect to www.aid.no and return
|
|
@@ -82,7 +18,9 @@ export class User {
|
|
|
82
18
|
*/
|
|
83
19
|
export const goToLoginPage = (requestedUrl?: string) =>
|
|
84
20
|
authentication.login(requestedUrl || window.location.href);
|
|
21
|
+
|
|
85
22
|
export const logout = () => authentication.logout();
|
|
23
|
+
|
|
86
24
|
export const tryActivateSubscriptionAccess = (
|
|
87
25
|
accessFeatures: string[]
|
|
88
26
|
): Promise<{ hasAccess: boolean }> =>
|
|
@@ -96,3 +34,5 @@ export const tryActivateSubscriptionAccess = (
|
|
|
96
34
|
})
|
|
97
35
|
);
|
|
98
36
|
});
|
|
37
|
+
|
|
38
|
+
export const paywall = () => new PaywallUnlocker();
|