@amedia/user 0.7.1 → 0.7.3
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/preferences.d.ts +1 -1
- package/dist/clients/europa.d.ts +12 -7
- package/dist/clients/jupiter.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/index.js.LICENSE.txt +0 -9
- package/dist/types.d.ts +3 -0
- package/dist/utils/deepEquals.d.ts +4 -0
- package/dist/utils/getOrUpdate.d.ts +1 -1
- package/package.json +1 -2
- package/src/actions/authentication.ts +6 -5
- package/src/actions/authorization.ts +1 -44
- package/src/actions/preferences.spec.ts +2 -12
- package/src/actions/preferences.ts +26 -54
- package/src/clients/europa.spec.ts +51 -17
- package/src/clients/europa.ts +70 -53
- package/src/clients/jupiter.spec.ts +3 -1
- package/src/clients/jupiter.ts +3 -2
- package/src/index.ts +2 -3
- package/src/types.ts +1 -0
- package/src/utils/deepEquals.ts +41 -0
- package/src/utils/getOrUpdate.spec.ts +168 -0
- package/src/utils/getOrUpdate.ts +27 -3
- package/dist/index-userdata.spec.d.ts +0 -4
- package/dist/services/europa.d.ts +0 -12
- package/dist/services/jupiter.d.ts +0 -29
- package/dist/services/jupiter.spec.d.ts +0 -1
- package/dist/services/mercury.d.ts +0 -18
- package/dist/services/mercury.spec.d.ts +0 -4
- package/dist/services/nebula.d.ts +0 -22
- package/dist/services/nebula.spec.d.ts +0 -1
- package/dist/services/pluto.d.ts +0 -8
- package/dist/services/pluto.spec.d.ts +0 -1
- package/dist/store/store.d.ts +0 -13
- package/dist/store/store.spec.d.ts +0 -1
- /package/dist/{services/europa.spec.d.ts → utils/getOrUpdate.spec.d.ts} +0 -0
|
@@ -12,12 +12,3 @@
|
|
|
12
12
|
* Copyright 2006, 2015 Klaus Hartl & Fagner Brack
|
|
13
13
|
* Released under the MIT license
|
|
14
14
|
*/
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @license
|
|
18
|
-
* Lodash <https://lodash.com/>
|
|
19
|
-
* Copyright OpenJS Foundation and other contributors <https://openjsf.org/>
|
|
20
|
-
* Released under MIT license <https://lodash.com/license>
|
|
21
|
-
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
|
|
22
|
-
* Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
|
|
23
|
-
*/
|
package/dist/types.d.ts
CHANGED
|
@@ -3,4 +3,4 @@ export declare type DataFetcherWithCacheTTL<T extends Runtype> = Promise<{
|
|
|
3
3
|
ttl: number;
|
|
4
4
|
data: Static<T>;
|
|
5
5
|
}>;
|
|
6
|
-
export default function getOrUpdate<T extends Runtype>(key: string, schema: T, fetch: () => DataFetcherWithCacheTTL<T>): Promise<Static<T>>;
|
|
6
|
+
export default function getOrUpdate<T extends Runtype>(key: string, schema: T, fetch: () => DataFetcherWithCacheTTL<T>, cacheQualifier?: (cacheData: Static<T>) => boolean): Promise<Static<T>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amedia/user",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
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)",
|
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
"@amedia/browserid": "0.1.6",
|
|
15
15
|
"@amedia/frontend-rpc": "0.0.12",
|
|
16
16
|
"dexie": "3.0.3",
|
|
17
|
-
"lodash": "4.17.21",
|
|
18
17
|
"regenerator-runtime": "0.13.7",
|
|
19
18
|
"runtypes": "6.2.1"
|
|
20
19
|
},
|
|
@@ -2,16 +2,17 @@ import { getCurrentSession, removeSession } from '../clients/pluto';
|
|
|
2
2
|
import cache from '../cache';
|
|
3
3
|
import { getRPCClient } from '../lib/rpc';
|
|
4
4
|
import { removeCookie } from '../lib/cookie';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
getCurrentEnvironment,
|
|
7
|
+
getCurrentPublicationDomain,
|
|
8
|
+
} from '../utils/url';
|
|
6
9
|
import log from '../utils/log';
|
|
7
10
|
import { UserAttributes } from '../types';
|
|
8
11
|
|
|
9
12
|
export const login = (requestedUrl: string) => {
|
|
10
13
|
const environment = getCurrentEnvironment();
|
|
11
14
|
const url = new URL(
|
|
12
|
-
`https://aid.no${
|
|
13
|
-
environment && `.${environment}.localhost.api.no`
|
|
14
|
-
}/aid/logg_inn/`
|
|
15
|
+
`https://aid.no${environment && `.${environment}.api.no`}/aid/logg_inn/`
|
|
15
16
|
);
|
|
16
17
|
url.searchParams.set('requestedUrl', requestedUrl || window.location.href);
|
|
17
18
|
window.location.href = url.href;
|
|
@@ -26,7 +27,7 @@ function clearCookies() {
|
|
|
26
27
|
|
|
27
28
|
export const logout = async () =>
|
|
28
29
|
// TODO: It now clears the entire cache. Might be too aggressive.
|
|
29
|
-
Promise.all([removeSession(
|
|
30
|
+
Promise.all([removeSession(getCurrentPublicationDomain()), cache.clear()])
|
|
30
31
|
.then(() => {
|
|
31
32
|
// TODO This should be separated from logout and instead be setup to listen on state changes on storage
|
|
32
33
|
getRPCClient().userAuthenticated(false);
|
|
@@ -1,54 +1,11 @@
|
|
|
1
|
-
import { Record, Array, String } from 'runtypes';
|
|
2
1
|
import { checkForAccess } from '../clients/jupiter';
|
|
3
|
-
import cache from '../cache';
|
|
4
|
-
import queue from '../utils/queue';
|
|
5
|
-
import log from '../utils/log';
|
|
6
2
|
import { getCurrentPublicationDomain } from '../utils/url';
|
|
7
3
|
import { UserAttributes } from '../types';
|
|
8
4
|
import { activateSubscription, UserDeactivatedError } from '../clients/mercury';
|
|
9
5
|
import { logout } from './authentication';
|
|
10
6
|
|
|
11
|
-
const EXPIRES_OFFSET = 10000; // 10 seconds
|
|
12
|
-
const GET_ACCESS_CACHE_KEY = 'authorization.getAccess';
|
|
13
|
-
const GET_ACCESS_QUEUE_KEY = 'authorization.getAccess';
|
|
14
|
-
|
|
15
|
-
const AccessSchema = Record({
|
|
16
|
-
access: Array(String),
|
|
17
|
-
});
|
|
18
|
-
|
|
19
7
|
export const getAccess = (): Promise<Pick<UserAttributes, 'access'>> =>
|
|
20
|
-
|
|
21
|
-
queue.add(GET_ACCESS_QUEUE_KEY, { resolve, reject });
|
|
22
|
-
if (queue.peekQueueSize(GET_ACCESS_QUEUE_KEY) > 1) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
cache.get(GET_ACCESS_CACHE_KEY, AccessSchema).then((cacheResponse) => {
|
|
27
|
-
if (cacheResponse) {
|
|
28
|
-
queue.getAll(GET_ACCESS_QUEUE_KEY).forEach((promise) => {
|
|
29
|
-
promise.resolve(cacheResponse);
|
|
30
|
-
});
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
checkForAccess(getCurrentPublicationDomain(), ['newspaper'])
|
|
34
|
-
.then((accessState) => {
|
|
35
|
-
cache.set(
|
|
36
|
-
GET_ACCESS_CACHE_KEY,
|
|
37
|
-
accessState.access_features,
|
|
38
|
-
EXPIRES_OFFSET
|
|
39
|
-
);
|
|
40
|
-
queue.getAll(GET_ACCESS_QUEUE_KEY).forEach((promise) => {
|
|
41
|
-
promise.resolve({ access: accessState.access_features });
|
|
42
|
-
});
|
|
43
|
-
})
|
|
44
|
-
.catch((error) => {
|
|
45
|
-
log.error('Could not get access', error);
|
|
46
|
-
queue.getAll(GET_ACCESS_QUEUE_KEY).forEach((promise) => {
|
|
47
|
-
promise.reject();
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
});
|
|
8
|
+
checkForAccess(getCurrentPublicationDomain(), ['newspaper']);
|
|
52
9
|
|
|
53
10
|
export const activate = (accessFeature: string): Promise<boolean> => {
|
|
54
11
|
try {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import fetch from 'jest-fetch-mock';
|
|
2
2
|
import cache from '../cache';
|
|
3
|
-
import queue from '../utils/queue';
|
|
4
3
|
import { getPrivacyPreferences } from './preferences';
|
|
5
4
|
|
|
6
5
|
fetch.enableMocks();
|
|
@@ -15,7 +14,7 @@ const europaResponse = {
|
|
|
15
14
|
privacy_preferences: {
|
|
16
15
|
allow_research_usage: 'true',
|
|
17
16
|
connect_with_surveys: true,
|
|
18
|
-
|
|
17
|
+
personalizedAds: 'false',
|
|
19
18
|
personalized_content: false,
|
|
20
19
|
},
|
|
21
20
|
},
|
|
@@ -28,15 +27,6 @@ describe('privacyPreferences', () => {
|
|
|
28
27
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
29
28
|
});
|
|
30
29
|
|
|
31
|
-
it('simultanious call should return same preferences', async () => {
|
|
32
|
-
fetch.mockResponse(JSON.stringify(europaResponse));
|
|
33
|
-
|
|
34
|
-
jest.spyOn(queue, 'add');
|
|
35
|
-
await Promise.all([getPrivacyPreferences(), getPrivacyPreferences()]);
|
|
36
|
-
expect(queue.add).toHaveBeenCalledTimes(2);
|
|
37
|
-
expect(fetch).toHaveBeenCalledTimes(1);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
30
|
it('should use cache if available', async () => {
|
|
41
31
|
fetch.mockResponse(JSON.stringify(europaResponse));
|
|
42
32
|
|
|
@@ -44,7 +34,7 @@ describe('privacyPreferences', () => {
|
|
|
44
34
|
Promise.resolve({
|
|
45
35
|
allowResearchUsage: true,
|
|
46
36
|
connectWithSurveys: true,
|
|
47
|
-
|
|
37
|
+
personalizedAds: false,
|
|
48
38
|
personalizedContent: false,
|
|
49
39
|
})
|
|
50
40
|
);
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { Record, Boolean } from 'runtypes';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import log from '../utils/log';
|
|
6
|
-
import queue from '../utils/queue';
|
|
2
|
+
import { getNamespaces } from '../clients/europa';
|
|
3
|
+
import { UserAttributes } from '../types';
|
|
4
|
+
import getOrUpdate from '../utils/getOrUpdate';
|
|
7
5
|
|
|
8
6
|
const CACHE_DURATION = 5 * 60000; // 5 minutes
|
|
9
|
-
const PRIVACY_PREFERENCES_QUEUE_KEY = 'privacy_preferences_queue';
|
|
10
7
|
const PRIVACY_PREFERENCES_CACHE_KEY = 'privacyPreferences';
|
|
11
8
|
|
|
12
9
|
export const PrivacyPreferencesSchema = Record({
|
|
@@ -18,52 +15,27 @@ export const PrivacyPreferencesSchema = Record({
|
|
|
18
15
|
|
|
19
16
|
const toBoolean = (val: string | boolean) => val === 'true' || val === true;
|
|
20
17
|
|
|
21
|
-
export const getPrivacyPreferences = async (): Promise<
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
export const getPrivacyPreferences = async (): Promise<
|
|
19
|
+
Pick<UserAttributes, 'privacyPreferences'>
|
|
20
|
+
> =>
|
|
21
|
+
getOrUpdate(PRIVACY_PREFERENCES_CACHE_KEY, PrivacyPreferencesSchema, () =>
|
|
22
|
+
getNamespaces(['privacy_preferences']).then((user) => {
|
|
23
|
+
const privacyPreferences: any = user.storage?.privacy_preferences || {};
|
|
27
24
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
personalizedContent: toBoolean(
|
|
46
|
-
namespaceData.personalized_content
|
|
47
|
-
),
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
cache.set(
|
|
51
|
-
PRIVACY_PREFERENCES_CACHE_KEY,
|
|
52
|
-
mappedPrivacy,
|
|
53
|
-
CACHE_DURATION
|
|
54
|
-
);
|
|
55
|
-
queue.getAll(PRIVACY_PREFERENCES_QUEUE_KEY).forEach((promise) => {
|
|
56
|
-
promise.resolve({ privacyPreferences: mappedPrivacy });
|
|
57
|
-
});
|
|
58
|
-
})
|
|
59
|
-
.catch((error) => {
|
|
60
|
-
log.error(
|
|
61
|
-
`Could not fetch data for namespace: ${namespace}`,
|
|
62
|
-
error
|
|
63
|
-
);
|
|
64
|
-
queue.getAll(PRIVACY_PREFERENCES_QUEUE_KEY).forEach((promise) => {
|
|
65
|
-
promise.reject();
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
});
|
|
25
|
+
return {
|
|
26
|
+
ttl: CACHE_DURATION,
|
|
27
|
+
data: {
|
|
28
|
+
allowResearchUsage: toBoolean(
|
|
29
|
+
privacyPreferences.allow_research_usage
|
|
30
|
+
),
|
|
31
|
+
connectWithSurveys: toBoolean(
|
|
32
|
+
privacyPreferences.connect_with_surveys
|
|
33
|
+
),
|
|
34
|
+
personalizedAds: toBoolean(privacyPreferences.personalized_ads),
|
|
35
|
+
personalizedContent: toBoolean(
|
|
36
|
+
privacyPreferences.personalized_content
|
|
37
|
+
),
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
})
|
|
41
|
+
).then((data) => ({ privacyPreferences: data }));
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
clearLocalCache,
|
|
7
7
|
saveNamespace,
|
|
8
8
|
deleteNamespaceKey,
|
|
9
|
-
|
|
9
|
+
getNamespaces,
|
|
10
10
|
getAllNamespaces,
|
|
11
11
|
} from './europa';
|
|
12
12
|
|
|
@@ -24,7 +24,10 @@ describe('Europa', () => {
|
|
|
24
24
|
fetch.mockOnce(JSON.stringify({ namespaces: { user_info: userInfo } })); // data retrieved 'GET'
|
|
25
25
|
|
|
26
26
|
const namespaceData = await saveNamespace('user_info', userInfo);
|
|
27
|
-
expect(
|
|
27
|
+
expect(fetch).toHaveBeenCalledTimes(2);
|
|
28
|
+
expect(namespaceData).toMatchObject({
|
|
29
|
+
storage: { user_info: userInfo },
|
|
30
|
+
});
|
|
28
31
|
});
|
|
29
32
|
|
|
30
33
|
it('updateNamespace should update namespace', async () => {
|
|
@@ -37,7 +40,7 @@ describe('Europa', () => {
|
|
|
37
40
|
|
|
38
41
|
const old = await saveNamespace('user_info', oldUserInfo);
|
|
39
42
|
expect(fetch).toHaveBeenCalledTimes(2);
|
|
40
|
-
expect(old).toMatchObject(oldUserInfo);
|
|
43
|
+
expect(old).toMatchObject({ storage: { user_info: oldUserInfo } });
|
|
41
44
|
|
|
42
45
|
const updated = await updateNamespace('user_info', newUserInfo).then(() =>
|
|
43
46
|
cache.get('europa', EuropaSchema)
|
|
@@ -65,29 +68,60 @@ describe('Europa', () => {
|
|
|
65
68
|
.mockOnce(JSON.stringify({}), { status: 200 }) // 'DELETE'
|
|
66
69
|
.mockOnce(JSON.stringify({ namespaces: { euucp: { y: 'y' } } })); // 'PUT'
|
|
67
70
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
71
|
+
const data = { x: 'x', y: 'y' };
|
|
72
|
+
|
|
73
|
+
await saveNamespace('euucp', data);
|
|
74
|
+
// expect x and y
|
|
75
|
+
expect(await cache.get('europa', EuropaSchema)).toEqual(
|
|
76
|
+
expect.objectContaining({
|
|
77
|
+
namespaces: { euucp: data },
|
|
78
|
+
})
|
|
79
|
+
);
|
|
72
80
|
|
|
73
81
|
await deleteNamespaceKey('euucp', 'x');
|
|
74
|
-
expect
|
|
75
|
-
|
|
76
|
-
|
|
82
|
+
// expect only y
|
|
83
|
+
expect(await cache.get('europa', EuropaSchema)).toEqual(
|
|
84
|
+
expect.objectContaining({
|
|
85
|
+
namespaces: { euucp: { y: 'y' } },
|
|
86
|
+
})
|
|
87
|
+
);
|
|
77
88
|
});
|
|
78
89
|
|
|
79
|
-
it('getAllNamespaces should return all europa namespaces', () => {
|
|
80
|
-
const
|
|
90
|
+
it('getAllNamespaces should return all europa namespaces', async () => {
|
|
91
|
+
const data = {
|
|
81
92
|
euucp: { x: 'x' },
|
|
82
93
|
user_info: { gender: 'M', post_code: 'U' },
|
|
83
94
|
};
|
|
84
95
|
|
|
85
|
-
fetch.mockResponse(JSON.stringify({ namespaces:
|
|
86
|
-
expect(getAllNamespaces()).
|
|
96
|
+
fetch.mockResponse(JSON.stringify({ namespaces: data }));
|
|
97
|
+
expect(await getAllNamespaces()).toMatchObject({ namespaces: data });
|
|
87
98
|
});
|
|
99
|
+
});
|
|
88
100
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
101
|
+
describe('getNamespaces', () => {
|
|
102
|
+
it('should return namespace', async () => {
|
|
103
|
+
const data = { euucp: { x: 'x' } };
|
|
104
|
+
fetch.mockResponse(JSON.stringify({ namespaces: data }));
|
|
105
|
+
|
|
106
|
+
const namespaceData = await getNamespaces(['euucp']);
|
|
107
|
+
expect(namespaceData).toEqual(expect.objectContaining({ storage: data }));
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('should return multiple namespaces', async () => {
|
|
111
|
+
const data = { euucp: { x: 'x' }, user_info: { a: 'a' } };
|
|
112
|
+
fetch.mockResponse(JSON.stringify({ namespaces: data }));
|
|
113
|
+
|
|
114
|
+
const namespaceData = await getNamespaces(['euucp', 'user_info']);
|
|
115
|
+
expect(namespaceData).toEqual(
|
|
116
|
+
expect.objectContaining({
|
|
117
|
+
storage: data,
|
|
118
|
+
})
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should require at least one namespace', async () => {
|
|
123
|
+
await expect(getNamespaces([])).rejects.toThrowError(
|
|
124
|
+
new Error('namespaces array cannot be of length 0')
|
|
125
|
+
);
|
|
92
126
|
});
|
|
93
127
|
});
|
package/src/clients/europa.ts
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
|
-
import { Record,
|
|
1
|
+
import { Record, Unknown, Dictionary, Static, String } from 'runtypes';
|
|
2
2
|
|
|
3
3
|
import cache from '../cache';
|
|
4
|
+
import { UserAttributes } from '../types';
|
|
5
|
+
import getOrUpdate from '../utils/getOrUpdate';
|
|
4
6
|
import log from '../utils/log';
|
|
5
7
|
|
|
6
8
|
const CACHE_DURATION = 5 * 60000; // 5 minutes
|
|
7
9
|
|
|
8
|
-
export const EuropaSchema = Record({
|
|
9
|
-
namespaces: Partial({}),
|
|
10
|
-
});
|
|
10
|
+
export const EuropaSchema = Record({ namespaces: Dictionary(Unknown, String) });
|
|
11
11
|
|
|
12
12
|
export const clearLocalCache = () => cache.remove('europa');
|
|
13
13
|
|
|
14
|
-
const
|
|
14
|
+
const mapEuropaToUserData = (
|
|
15
|
+
data: Static<typeof EuropaSchema>
|
|
16
|
+
): Pick<UserAttributes, 'storage'> => ({
|
|
17
|
+
storage: data.namespaces,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const doFetch = (url: string, method: string = 'GET', body?: Object) =>
|
|
15
21
|
fetch(url, {
|
|
16
|
-
method
|
|
22
|
+
method,
|
|
17
23
|
body: body && JSON.stringify(body),
|
|
18
24
|
headers: {
|
|
19
25
|
'Content-Type': 'application/json',
|
|
@@ -45,37 +51,36 @@ export const updateNamespace = async (namespace: string, data: Object) => {
|
|
|
45
51
|
return updateNamespaceInCache(namespace, data);
|
|
46
52
|
};
|
|
47
53
|
|
|
48
|
-
export const
|
|
49
|
-
|
|
54
|
+
export const getNamespaces = async (
|
|
55
|
+
namespaces: string[],
|
|
50
56
|
options: { ignoreCache: boolean } = { ignoreCache: false }
|
|
51
|
-
) => {
|
|
52
|
-
if (
|
|
53
|
-
|
|
54
|
-
if (cacheData && cacheData.namespaces[namespace]) {
|
|
55
|
-
return cacheData.namespaces[namespace];
|
|
56
|
-
}
|
|
57
|
+
): Promise<Pick<UserAttributes, 'storage'>> => {
|
|
58
|
+
if (namespaces.length === 0) {
|
|
59
|
+
throw new Error('namespaces array cannot be of length 0');
|
|
57
60
|
}
|
|
58
61
|
|
|
59
|
-
return
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
62
|
+
return getOrUpdate(
|
|
63
|
+
'europa',
|
|
64
|
+
EuropaSchema,
|
|
65
|
+
() =>
|
|
66
|
+
doFetch(`/api/europa/v1/me/${namespaces.join(',')}`)
|
|
67
|
+
.then((res) => {
|
|
68
|
+
if (!res.ok) {
|
|
69
|
+
log.error(
|
|
70
|
+
`Could not get namespaces for: ${namespaces}, status: ${res.status}, statusText: ${res.statusText}`,
|
|
71
|
+
res
|
|
72
|
+
);
|
|
73
|
+
return { namespaces: {} };
|
|
74
|
+
}
|
|
75
|
+
return res.json();
|
|
76
|
+
})
|
|
77
|
+
.then((data) => ({ ttl: CACHE_DURATION, data })),
|
|
78
|
+
(cacheData) =>
|
|
79
|
+
!options.ignoreCache ||
|
|
80
|
+
Object.keys(cacheData.namespaces).filter(
|
|
81
|
+
(namespace) => !Object.keys(namespaces).includes(namespace)
|
|
82
|
+
).length === 0
|
|
83
|
+
).then(mapEuropaToUserData);
|
|
79
84
|
};
|
|
80
85
|
|
|
81
86
|
export const saveNamespace = (namespace: string, data: Object) =>
|
|
@@ -89,26 +94,38 @@ export const saveNamespace = (namespace: string, data: Object) =>
|
|
|
89
94
|
}
|
|
90
95
|
return {};
|
|
91
96
|
})
|
|
92
|
-
.then(() =>
|
|
97
|
+
.then(() => getNamespaces([namespace], { ignoreCache: true })); // updates cache
|
|
93
98
|
|
|
94
99
|
export const getAllNamespaces = () =>
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
100
|
+
getOrUpdate(
|
|
101
|
+
'europa',
|
|
102
|
+
EuropaSchema,
|
|
103
|
+
() =>
|
|
104
|
+
doFetch(`/api/europa/v1/me`)
|
|
105
|
+
.then((res) => {
|
|
106
|
+
if (!res.ok) {
|
|
107
|
+
log.error(
|
|
108
|
+
`Could not get all namespaces, status: ${res.status}, statusText: ${res.statusText}`,
|
|
109
|
+
res
|
|
110
|
+
);
|
|
111
|
+
return {};
|
|
112
|
+
}
|
|
113
|
+
return res.json();
|
|
114
|
+
})
|
|
115
|
+
.then((data) => {
|
|
116
|
+
const europaValidData = EuropaSchema.check(data);
|
|
117
|
+
try {
|
|
118
|
+
return {
|
|
119
|
+
ttl: CACHE_DURATION,
|
|
120
|
+
data: europaValidData,
|
|
121
|
+
};
|
|
122
|
+
} catch (error) {
|
|
123
|
+
log.error('Invalid schema', error);
|
|
124
|
+
throw error;
|
|
125
|
+
}
|
|
126
|
+
}),
|
|
127
|
+
() => false
|
|
128
|
+
);
|
|
112
129
|
export const deleteNamespaceKey = (namespace: string, key: string) =>
|
|
113
130
|
doFetch(`/api/europa/v1/me/${namespace}/${key}`, 'DELETE')
|
|
114
131
|
.then((res) => {
|
|
@@ -119,4 +136,4 @@ export const deleteNamespaceKey = (namespace: string, key: string) =>
|
|
|
119
136
|
);
|
|
120
137
|
}
|
|
121
138
|
})
|
|
122
|
-
.then(() =>
|
|
139
|
+
.then(() => getNamespaces([namespace], { ignoreCache: true })); // updates cache
|
|
@@ -36,7 +36,9 @@ describe('jupiter service', () => {
|
|
|
36
36
|
|
|
37
37
|
await expect(
|
|
38
38
|
checkForAccess(publicationDomain, accessFeatures)
|
|
39
|
-
).resolves.toEqual(
|
|
39
|
+
).resolves.toEqual({
|
|
40
|
+
access: ['amedia_staff', 'newspaper'],
|
|
41
|
+
});
|
|
40
42
|
|
|
41
43
|
expect(fetch).toHaveBeenCalledWith(
|
|
42
44
|
`/api/jupiter/v1/check_access_for_paper/${publicationDomain}/?require=any&access_features=${accessFeatures.join(
|
package/src/clients/jupiter.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
} from 'runtypes';
|
|
10
10
|
import log from '../utils/log';
|
|
11
11
|
import getOrUpdate from '../utils/getOrUpdate';
|
|
12
|
+
import { UserAttributes } from '../types';
|
|
12
13
|
|
|
13
14
|
export type AccessFeatures = string[];
|
|
14
15
|
|
|
@@ -45,7 +46,7 @@ export type GetNonUserAccess = Static<typeof GetNonUserAccessSchema>;
|
|
|
45
46
|
export const checkForAccess = (
|
|
46
47
|
publicationDomain: string,
|
|
47
48
|
accessFeatures: AccessFeatures
|
|
48
|
-
): Promise<
|
|
49
|
+
): Promise<Pick<UserAttributes, 'access'>> =>
|
|
49
50
|
getOrUpdate('checkForAccess', CheckAccessForPaperSchema, () =>
|
|
50
51
|
fetch(
|
|
51
52
|
`/api/jupiter/v1/check_access_for_paper/${publicationDomain}/?require=any&access_features=${accessFeatures.join(
|
|
@@ -72,7 +73,7 @@ export const checkForAccess = (
|
|
|
72
73
|
throw error;
|
|
73
74
|
}
|
|
74
75
|
})
|
|
75
|
-
);
|
|
76
|
+
).then((data) => ({ access: data.access_features }));
|
|
76
77
|
|
|
77
78
|
export const getVstoken = (publicationDomain: string): Promise<GetVstoken> =>
|
|
78
79
|
fetch(`/api/jupiter/v1/get_vstoken/${publicationDomain}`, { method: 'post' })
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import 'regenerator-runtime';
|
|
2
|
-
import _ from 'lodash';
|
|
3
2
|
import { AmediaUserEvents } from './eventHandlers/userAttributes';
|
|
4
3
|
import { ActivationEvents } from './eventHandlers/activation';
|
|
5
4
|
import { dispatch, listenForAllEvents } from './utils/customEventWrapper';
|
|
6
5
|
import { State, UserAttributes } from './types';
|
|
7
6
|
import * as authentication from './actions/authentication';
|
|
7
|
+
import { deepEquals } from './utils/deepEquals';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Enables you to subscribe to the user attributes you care about and
|
|
@@ -33,8 +33,7 @@ export const subscribeToUserAttributes = <
|
|
|
33
33
|
return map;
|
|
34
34
|
}, {} as Pick<UserAttributes, T[number]>);
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
if (_.isEqual(previousUserAttributes, responseAttributes)) {
|
|
36
|
+
if (deepEquals(previousUserAttributes, responseAttributes)) {
|
|
38
37
|
return;
|
|
39
38
|
}
|
|
40
39
|
onUserUpdated(responseAttributes);
|
package/src/types.ts
CHANGED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @see https://dev.to/sanderdebr/deep-equality-checking-of-objects-in-vanilla-javascript-5592
|
|
3
|
+
*/
|
|
4
|
+
export const deepEquals = (a: unknown, b: unknown): boolean => {
|
|
5
|
+
if (a === b) {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (
|
|
10
|
+
typeof a !== 'object' ||
|
|
11
|
+
typeof b !== 'object' ||
|
|
12
|
+
a === null ||
|
|
13
|
+
b === null
|
|
14
|
+
) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const keysA = Object.keys(a);
|
|
19
|
+
const keysB = Object.keys(b);
|
|
20
|
+
|
|
21
|
+
if (keysA.length !== keysB.length) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
26
|
+
for (const key of keysA) {
|
|
27
|
+
if (!keysB.includes(key)) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (typeof a[key] === 'function' || typeof b[key] === 'function') {
|
|
32
|
+
if (a[key].toString() !== b[key].toString()) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
} else if (!deepEquals(a[key], b[key])) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return true;
|
|
41
|
+
};
|