@amedia/user 0.5.3 → 0.6.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/README.md +2 -2
- package/cloudbuild.json +29 -0
- package/cop.json +8 -0
- package/dist/actions/authentication.d.ts +1 -1
- package/dist/actions/preferences.d.ts +9 -3
- package/dist/cache/index.d.ts +3 -2
- package/dist/clients/europa.d.ts +12 -0
- package/dist/clients/europa.spec.d.ts +1 -0
- package/dist/clients/jupiter.d.ts +29 -0
- package/dist/clients/jupiter.spec.d.ts +1 -0
- package/dist/clients/mercury.d.ts +18 -0
- package/dist/clients/mercury.spec.d.ts +4 -0
- package/dist/clients/nebula.d.ts +22 -0
- package/dist/clients/nebula.spec.d.ts +1 -0
- package/dist/clients/pluto.d.ts +8 -0
- package/dist/clients/pluto.spec.d.ts +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/lib/rpc.d.ts +6 -1
- package/dist/store/store.d.ts +13 -0
- package/dist/store/store.spec.d.ts +1 -0
- package/dist/types.d.ts +7 -0
- package/dist/utils/getOrUpdate.d.ts +3 -0
- package/package.json +4 -3
- package/src/actions/authentication.ts +2 -2
- package/src/actions/authorization.ts +4 -7
- package/src/actions/preferences.spec.ts +4 -5
- package/src/actions/preferences.ts +49 -47
- package/src/actions/userData.ts +2 -0
- package/src/cache/index.spec.ts +7 -2
- package/src/cache/index.ts +4 -3
- package/src/{services → clients}/europa.spec.ts +19 -20
- package/src/{services → clients}/europa.ts +8 -12
- package/src/{services → clients}/mercury.ts +0 -1
- package/src/index-authentcation.spec.ts +2 -2
- package/src/index-userdata.spec.ts +11 -11
- package/src/index.ts +4 -3
- package/src/lib/rpc.ts +9 -3
- package/src/store/store.spec.ts +34 -0
- package/src/store/store.ts +46 -0
- package/src/types.ts +8 -0
- package/src/utils/getOrUpdate.ts +41 -0
- package/src/utils/log.ts +2 -1
- /package/src/{services → clients}/jupiter.spec.ts +0 -0
- /package/src/{services → clients}/jupiter.ts +0 -0
- /package/src/{services → clients}/mercury.spec.ts +0 -0
- /package/src/{services → clients}/nebula.spec.ts +0 -0
- /package/src/{services → clients}/nebula.ts +0 -0
- /package/src/{services → clients}/pluto.spec.ts +0 -0
- /package/src/{services → clients}/pluto.ts +0 -0
package/dist/lib/rpc.d.ts
CHANGED
|
@@ -1 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
declare let rpcSessionClient: {
|
|
2
|
+
addMethod: (methodName: string) => void;
|
|
3
|
+
userAuthenticated: (boolean: any) => void;
|
|
4
|
+
};
|
|
5
|
+
export declare const getRPCClient: () => Pick<typeof rpcSessionClient, 'userAuthenticated'>;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { State, UserAttributes } from '../types';
|
|
2
|
+
declare type StoreDataMap = {
|
|
3
|
+
userAttributes: UserAttributes;
|
|
4
|
+
state: State;
|
|
5
|
+
};
|
|
6
|
+
declare type Listener<T> = (data: T) => void;
|
|
7
|
+
declare const _default: {
|
|
8
|
+
set: (key: keyof StoreDataMap, value: UserAttributes | State) => void;
|
|
9
|
+
get: (key: keyof StoreDataMap) => UserAttributes | State | undefined;
|
|
10
|
+
on: (key: keyof StoreDataMap, listener: Listener<UserAttributes | State>) => void;
|
|
11
|
+
clear: () => void;
|
|
12
|
+
};
|
|
13
|
+
export default _default;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/types.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export declare type UserAttributes = {
|
|
|
5
5
|
trackingKey?: string;
|
|
6
6
|
extraData?: [];
|
|
7
7
|
access?: string[];
|
|
8
|
+
privacyPreferences?: PrivacyPreferences;
|
|
8
9
|
};
|
|
9
10
|
export declare type State = {
|
|
10
11
|
isLoggedIn: boolean;
|
|
@@ -13,3 +14,9 @@ export declare type State = {
|
|
|
13
14
|
export declare type AnonymousAttributes = {
|
|
14
15
|
age: number;
|
|
15
16
|
};
|
|
17
|
+
export declare type PrivacyPreferences = {
|
|
18
|
+
allowResearchUsage: Boolean;
|
|
19
|
+
connectWithSurveys: Boolean;
|
|
20
|
+
personalizedAds: Boolean;
|
|
21
|
+
personalizedContent: Boolean;
|
|
22
|
+
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amedia/user",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
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)",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"scripts": {
|
|
9
|
-
"test": "jest",
|
|
10
|
-
"lint": "eslint
|
|
9
|
+
"test": "jest --coverage",
|
|
10
|
+
"lint": "eslint ./src --ext .ts",
|
|
11
11
|
"build": "webpack --mode=production"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"eslint-plugin-prettier": "^3.4.0",
|
|
36
36
|
"jest": "^26.6.3",
|
|
37
37
|
"jest-fetch-mock": "^3.0.3",
|
|
38
|
+
"mockdate": "3.0.5",
|
|
38
39
|
"npm-run-all": "4.1.5",
|
|
39
40
|
"prettier": "2.2.1",
|
|
40
41
|
"source-map-loader": "2.0.1",
|
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
InternalSessionSchema,
|
|
4
4
|
removeSession,
|
|
5
5
|
Session,
|
|
6
|
-
} from '../
|
|
6
|
+
} from '../clients/pluto';
|
|
7
7
|
import cache from '../cache';
|
|
8
8
|
import { getRPCClient } from '../lib/rpc';
|
|
9
9
|
import { removeCookie } from '../lib/cookie';
|
|
@@ -68,7 +68,7 @@ export const getSession = async (): Promise<Session | null> => {
|
|
|
68
68
|
|
|
69
69
|
// Actually perform a request against backend for the session
|
|
70
70
|
const session = await getCurrentSession();
|
|
71
|
-
cache.set(IS_LOGGED_IN_CACHE_KEY, session,
|
|
71
|
+
cache.set(IS_LOGGED_IN_CACHE_KEY, session, EXPIRES_10_SECONDS);
|
|
72
72
|
isFetchingSession = false;
|
|
73
73
|
return session;
|
|
74
74
|
};
|
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import { Record, Array, String } from 'runtypes';
|
|
2
|
-
import { checkForAccess } from '../
|
|
2
|
+
import { checkForAccess } from '../clients/jupiter';
|
|
3
3
|
import cache from '../cache';
|
|
4
4
|
import queue from '../utils/queue';
|
|
5
5
|
import log from '../utils/log';
|
|
6
6
|
import { getCurrentPublicationDomain } from '../utils/url';
|
|
7
7
|
import { UserAttributes } from '../types';
|
|
8
|
-
import {
|
|
9
|
-
activateSubscription,
|
|
10
|
-
UserDeactivatedError,
|
|
11
|
-
} from '../services/mercury';
|
|
8
|
+
import { activateSubscription, UserDeactivatedError } from '../clients/mercury';
|
|
12
9
|
import { logout } from './authentication';
|
|
13
10
|
|
|
14
11
|
const EXPIRES_OFFSET = 10000; // 10 seconds
|
|
@@ -19,7 +16,7 @@ const AccessSchema = Record({
|
|
|
19
16
|
access: Array(String),
|
|
20
17
|
});
|
|
21
18
|
|
|
22
|
-
export const getAccess =
|
|
19
|
+
export const getAccess = (): Promise<Pick<UserAttributes, 'access'>> =>
|
|
23
20
|
new Promise((resolve, reject) => {
|
|
24
21
|
queue.add(GET_ACCESS_QUEUE_KEY, { resolve, reject });
|
|
25
22
|
if (queue.peekQueueSize(GET_ACCESS_QUEUE_KEY) > 1) {
|
|
@@ -38,7 +35,7 @@ export const getAccess = async (): Promise<Pick<UserAttributes, 'access'>> =>
|
|
|
38
35
|
cache.set(
|
|
39
36
|
GET_ACCESS_CACHE_KEY,
|
|
40
37
|
accessState.access_features,
|
|
41
|
-
|
|
38
|
+
EXPIRES_OFFSET
|
|
42
39
|
);
|
|
43
40
|
queue.getAll(GET_ACCESS_QUEUE_KEY).forEach((promise) => {
|
|
44
41
|
promise.resolve({ access: accessState.access_features });
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import fetch from 'jest-fetch-mock';
|
|
2
2
|
import cache from '../cache';
|
|
3
3
|
import queue from '../utils/queue';
|
|
4
|
-
import
|
|
4
|
+
import { getPrivacyPreferences } from './preferences';
|
|
5
5
|
|
|
6
6
|
fetch.enableMocks();
|
|
7
7
|
|
|
@@ -24,7 +24,7 @@ const europaResponse = {
|
|
|
24
24
|
describe('privacyPreferences', () => {
|
|
25
25
|
it('should return preferences', async () => {
|
|
26
26
|
fetch.mockResponse(JSON.stringify(europaResponse));
|
|
27
|
-
await
|
|
27
|
+
await getPrivacyPreferences();
|
|
28
28
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
29
29
|
});
|
|
30
30
|
|
|
@@ -32,8 +32,7 @@ describe('privacyPreferences', () => {
|
|
|
32
32
|
fetch.mockResponse(JSON.stringify(europaResponse));
|
|
33
33
|
|
|
34
34
|
jest.spyOn(queue, 'add');
|
|
35
|
-
|
|
36
|
-
await privacyPreferences();
|
|
35
|
+
await Promise.all([getPrivacyPreferences(), getPrivacyPreferences()]);
|
|
37
36
|
expect(queue.add).toHaveBeenCalledTimes(2);
|
|
38
37
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
39
38
|
});
|
|
@@ -49,7 +48,7 @@ describe('privacyPreferences', () => {
|
|
|
49
48
|
personalizedContent: false,
|
|
50
49
|
})
|
|
51
50
|
);
|
|
52
|
-
await Promise.all([
|
|
51
|
+
await Promise.all([getPrivacyPreferences(), getPrivacyPreferences()]);
|
|
53
52
|
expect(fetch).toHaveBeenCalledTimes(0);
|
|
54
53
|
});
|
|
55
54
|
});
|
|
@@ -1,67 +1,69 @@
|
|
|
1
|
-
import { Record, Boolean
|
|
1
|
+
import { Record, Boolean } from 'runtypes';
|
|
2
2
|
import cache from '../cache';
|
|
3
|
-
import { getNamespace } from '../
|
|
3
|
+
import { getNamespace } from '../clients/europa';
|
|
4
|
+
import { UserAttributes, PrivacyPreferences } from '../types';
|
|
4
5
|
import log from '../utils/log';
|
|
5
6
|
import queue from '../utils/queue';
|
|
6
7
|
|
|
7
8
|
const CACHE_DURATION = 5 * 60000; // 5 minutes
|
|
8
|
-
const
|
|
9
|
+
const PRIVACY_PREFERENCES_QUEUE_KEY = 'privacy_preferences_queue';
|
|
10
|
+
const PRIVACY_PREFERENCES_CACHE_KEY = 'privacyPreferences';
|
|
9
11
|
|
|
10
|
-
const PrivacyPreferencesSchema = Record({
|
|
12
|
+
export const PrivacyPreferencesSchema = Record({
|
|
11
13
|
allowResearchUsage: Boolean,
|
|
12
14
|
connectWithSurveys: Boolean,
|
|
13
15
|
personalizedAds: Boolean,
|
|
14
16
|
personalizedContent: Boolean,
|
|
15
17
|
});
|
|
16
18
|
|
|
17
|
-
type PrivacyPreferences = Static<typeof PrivacyPreferencesSchema>;
|
|
18
|
-
|
|
19
19
|
const toBoolean = (val: string | boolean) => val === 'true' || val === true;
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const privacyPreferences = () => {
|
|
26
|
-
return new Promise(async (resolve, reject) => {
|
|
27
|
-
queue.add(PRIVACY_PREFERENCES_QUEUE, { resolve, reject });
|
|
28
|
-
if (queue.peekQueueSize(PRIVACY_PREFERENCES_QUEUE) > 1) {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const cachedData = await cache.get(
|
|
33
|
-
'privacyPreferences',
|
|
34
|
-
PrivacyPreferencesSchema
|
|
35
|
-
);
|
|
36
|
-
if (cachedData) {
|
|
37
|
-
queue.getAll(PRIVACY_PREFERENCES_QUEUE).forEach((promise) => {
|
|
38
|
-
promise.resolve(cachedData);
|
|
39
|
-
});
|
|
21
|
+
export const getPrivacyPreferences = async (): Promise<UserAttributes> =>
|
|
22
|
+
new Promise((resolve, reject) => {
|
|
23
|
+
queue.add(PRIVACY_PREFERENCES_QUEUE_KEY, { resolve, reject });
|
|
24
|
+
if (queue.peekQueueSize(PRIVACY_PREFERENCES_QUEUE_KEY) > 1) {
|
|
40
25
|
return;
|
|
41
26
|
}
|
|
42
27
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
28
|
+
cache
|
|
29
|
+
.get(PRIVACY_PREFERENCES_CACHE_KEY, PrivacyPreferencesSchema)
|
|
30
|
+
.then((cacheResponse) => {
|
|
31
|
+
if (cacheResponse) {
|
|
32
|
+
queue.getAll(PRIVACY_PREFERENCES_QUEUE_KEY).forEach((promise) => {
|
|
33
|
+
promise.resolve({ privacyPreferences: cacheResponse });
|
|
34
|
+
});
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
52
37
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
38
|
+
const namespace = 'privacy_preferences';
|
|
39
|
+
getNamespace(namespace)
|
|
40
|
+
.then((namespaceData) => {
|
|
41
|
+
const mappedPrivacy: PrivacyPreferences = {
|
|
42
|
+
allowResearchUsage: toBoolean(namespaceData.allow_research_usage),
|
|
43
|
+
connectWithSurveys: toBoolean(namespaceData.connect_with_surveys),
|
|
44
|
+
personalizedAds: toBoolean(namespaceData.personalized_ads),
|
|
45
|
+
personalizedContent: toBoolean(
|
|
46
|
+
namespaceData.personalized_content
|
|
47
|
+
),
|
|
48
|
+
};
|
|
59
49
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
+
});
|
|
64
69
|
});
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export default privacyPreferences;
|
package/src/actions/userData.ts
CHANGED
|
@@ -2,6 +2,7 @@ import log from '../utils/log';
|
|
|
2
2
|
import { UserAttributes } from '../types';
|
|
3
3
|
import { getSession } from './authentication';
|
|
4
4
|
import { getAccess } from './authorization';
|
|
5
|
+
import { getPrivacyPreferences } from './preferences';
|
|
5
6
|
|
|
6
7
|
// TODO We may want actions to tell us what they can deliver and not have this "organizer" (wohoo-bad-name) tell the actions..
|
|
7
8
|
const dataServiceMap: {
|
|
@@ -13,6 +14,7 @@ const dataServiceMap: {
|
|
|
13
14
|
// @ts-ignore
|
|
14
15
|
trackingKey: getSession,
|
|
15
16
|
access: getAccess,
|
|
17
|
+
privacyPreferences: getPrivacyPreferences,
|
|
16
18
|
};
|
|
17
19
|
|
|
18
20
|
let currentUserData: UserAttributes = {};
|
package/src/cache/index.spec.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import { Record, String } from 'runtypes';
|
|
6
|
+
import * as MockDate from 'mockdate';
|
|
6
7
|
import cache, { setCacheEngine } from '.';
|
|
7
8
|
import log from '../utils/log';
|
|
8
9
|
import resetAllMocks = jest.resetAllMocks;
|
|
@@ -14,6 +15,7 @@ const TestDataSchema = Record({
|
|
|
14
15
|
let mockEngine;
|
|
15
16
|
beforeEach(() => {
|
|
16
17
|
resetAllMocks();
|
|
18
|
+
MockDate.reset();
|
|
17
19
|
// @ts-ignore
|
|
18
20
|
mockEngine = {
|
|
19
21
|
get: jest.fn(),
|
|
@@ -86,11 +88,14 @@ describe('Cache', () => {
|
|
|
86
88
|
|
|
87
89
|
describe('set', () => {
|
|
88
90
|
it('should set', () => {
|
|
89
|
-
|
|
91
|
+
const EXPIRES = 987654321;
|
|
92
|
+
const MOCK_NOW_TIMESTAMP = 123456789;
|
|
93
|
+
MockDate.set(MOCK_NOW_TIMESTAMP);
|
|
94
|
+
cache.set('test-key', { test: 'data' }, EXPIRES);
|
|
90
95
|
expect(mockEngine.set).toHaveBeenCalledWith(
|
|
91
96
|
'test-key',
|
|
92
97
|
{ test: 'data' },
|
|
93
|
-
|
|
98
|
+
MOCK_NOW_TIMESTAMP + EXPIRES
|
|
94
99
|
);
|
|
95
100
|
});
|
|
96
101
|
});
|
package/src/cache/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Static, ValidationError } from 'runtypes';
|
|
2
|
+
import { RuntypeBase } from 'runtypes/lib/runtype';
|
|
2
3
|
import globalCache from './engines/global';
|
|
3
4
|
import { Engine } from './types';
|
|
4
5
|
import log from '../utils/log';
|
|
@@ -12,11 +13,11 @@ export const setCacheEngine = (engine: Engine) => {
|
|
|
12
13
|
const getCacheEngine = (): Engine => cacheEngine || globalCache;
|
|
13
14
|
|
|
14
15
|
const set = (key: string, value: unknown, expires: number) =>
|
|
15
|
-
getCacheEngine().set(key, value, expires);
|
|
16
|
+
getCacheEngine().set(key, value, Date.now() + expires);
|
|
16
17
|
|
|
17
18
|
const remove = (key: string): Promise<void> => getCacheEngine().remove(key);
|
|
18
19
|
|
|
19
|
-
const get = async <T extends
|
|
20
|
+
const get = async <T extends RuntypeBase>(
|
|
20
21
|
key: string,
|
|
21
22
|
validator: T
|
|
22
23
|
): Promise<Static<T> | void> => {
|
|
@@ -39,8 +39,8 @@ describe('Europa', () => {
|
|
|
39
39
|
expect(fetch).toHaveBeenCalledTimes(2);
|
|
40
40
|
expect(old).toMatchObject(oldUserInfo);
|
|
41
41
|
|
|
42
|
-
const updated = await updateNamespace('user_info', newUserInfo).then(
|
|
43
|
-
|
|
42
|
+
const updated = await updateNamespace('user_info', newUserInfo).then(() =>
|
|
43
|
+
cache.get('europa', EuropaSchema)
|
|
44
44
|
);
|
|
45
45
|
expect(updated).toMatchObject({ namespaces: { user_info: newUserInfo } });
|
|
46
46
|
expect(fetch).toHaveBeenCalledTimes(3);
|
|
@@ -50,45 +50,44 @@ describe('Europa', () => {
|
|
|
50
50
|
fetch.mockResponse(JSON.stringify({ namespaces: { euucp: { x: 'x' } } }));
|
|
51
51
|
|
|
52
52
|
await saveNamespace('euucp', { x: 'x' });
|
|
53
|
-
|
|
54
|
-
|
|
53
|
+
expect(cache.get('europa', EuropaSchema)).resolves.toMatchObject({
|
|
54
|
+
namespaces: { euucp: { x: 'x' } },
|
|
55
|
+
}); // expect something in cache
|
|
55
56
|
|
|
56
57
|
await clearLocalCache();
|
|
57
|
-
expect(
|
|
58
|
+
expect(cache.get('europa', EuropaSchema)).resolves.toBeUndefined(); // expect cache to be empty
|
|
58
59
|
});
|
|
59
60
|
|
|
60
61
|
it('deleteNamespaceKey should remove key from europa', async () => {
|
|
61
|
-
fetch
|
|
62
|
-
|
|
63
|
-
JSON.stringify({ namespaces: { euucp: { x: 'x', y: 'y' } } })
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
fetch.mockOnce(JSON.stringify({ namespaces: { euucp: { y: 'y' } } })); // 'PUT'
|
|
62
|
+
fetch
|
|
63
|
+
.mockOnce(JSON.stringify({})) // 'GET'
|
|
64
|
+
.mockOnce(JSON.stringify({ namespaces: { euucp: { x: 'x', y: 'y' } } })) // 'PUT'
|
|
65
|
+
.mockOnce(JSON.stringify({}), { status: 200 }) // 'DELETE'
|
|
66
|
+
.mockOnce(JSON.stringify({ namespaces: { euucp: { y: 'y' } } })); // 'PUT'
|
|
67
67
|
|
|
68
68
|
await saveNamespace('euucp', { x: 'x' });
|
|
69
|
-
|
|
70
|
-
expect(beforeDelete).toMatchObject({
|
|
69
|
+
expect(cache.get('europa', EuropaSchema)).resolves.toMatchObject({
|
|
71
70
|
namespaces: { euucp: { x: 'x', y: 'y' } },
|
|
72
71
|
}); // expect x and y
|
|
73
72
|
|
|
74
73
|
await deleteNamespaceKey('euucp', 'x');
|
|
75
|
-
|
|
76
|
-
|
|
74
|
+
expect(cache.get('europa', EuropaSchema)).resolves.toMatchObject({
|
|
75
|
+
namespaces: { euucp: { y: 'y' } },
|
|
76
|
+
}); // expect only y
|
|
77
77
|
});
|
|
78
78
|
|
|
79
|
-
it('getAllNamespaces should return all europa namespaces',
|
|
79
|
+
it('getAllNamespaces should return all europa namespaces', () => {
|
|
80
80
|
const namespaceData = {
|
|
81
81
|
euucp: { x: 'x' },
|
|
82
82
|
user_info: { gender: 'M', post_code: 'U' },
|
|
83
83
|
};
|
|
84
84
|
|
|
85
85
|
fetch.mockResponse(JSON.stringify({ namespaces: namespaceData }));
|
|
86
|
-
|
|
87
|
-
expect(retrieved).toMatchObject(namespaceData);
|
|
86
|
+
expect(getAllNamespaces()).resolves.toMatchObject(namespaceData);
|
|
88
87
|
});
|
|
89
88
|
|
|
90
|
-
it('getNamespace should return europa namespace',
|
|
89
|
+
it('getNamespace should return europa namespace', () => {
|
|
91
90
|
fetch.mockResponse(JSON.stringify({ namespaces: { euucp: { x: 'x' } } }));
|
|
92
|
-
expect(
|
|
91
|
+
expect(getNamespace('euucp')).resolves.toMatchObject({ x: 'x' });
|
|
93
92
|
});
|
|
94
93
|
});
|
|
@@ -34,11 +34,7 @@ const updateNamespaceInCache = async (namespace: string, data: Object) => {
|
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
export const updateNamespace = async (namespace: string, data: Object) => {
|
|
37
|
-
const res = await doFetch(
|
|
38
|
-
`//www.aid.no/api/europa/v1/me/${namespace}`,
|
|
39
|
-
'PATCH',
|
|
40
|
-
data
|
|
41
|
-
);
|
|
37
|
+
const res = await doFetch(`/api/europa/v1/me/${namespace}`, 'PATCH', data);
|
|
42
38
|
if (!res.ok) {
|
|
43
39
|
log.error(
|
|
44
40
|
`Could not get namespace: ${namespace}, status: ${res.status}, statusText: ${res.statusText}`,
|
|
@@ -46,7 +42,7 @@ export const updateNamespace = async (namespace: string, data: Object) => {
|
|
|
46
42
|
);
|
|
47
43
|
return {};
|
|
48
44
|
}
|
|
49
|
-
return
|
|
45
|
+
return updateNamespaceInCache(namespace, data);
|
|
50
46
|
};
|
|
51
47
|
|
|
52
48
|
export const getNamespace = async (
|
|
@@ -60,7 +56,7 @@ export const getNamespace = async (
|
|
|
60
56
|
}
|
|
61
57
|
}
|
|
62
58
|
|
|
63
|
-
return doFetch(
|
|
59
|
+
return doFetch(`/api/europa/v1/me/${namespace}`, 'GET')
|
|
64
60
|
.then((res) => {
|
|
65
61
|
if (!res.ok) {
|
|
66
62
|
log.error(
|
|
@@ -83,20 +79,20 @@ export const getNamespace = async (
|
|
|
83
79
|
};
|
|
84
80
|
|
|
85
81
|
export const saveNamespace = (namespace: string, data: Object) =>
|
|
86
|
-
doFetch(
|
|
82
|
+
doFetch(`/api/europa/v1/me/${namespace}`, 'PUT', data)
|
|
87
83
|
.then((res) => {
|
|
88
84
|
if (!res.ok) {
|
|
89
85
|
log.error(
|
|
90
86
|
`Could not get save namespace, namespace:${namespace} status: ${res.status}, statusText: ${res.statusText}`,
|
|
91
87
|
res
|
|
92
88
|
);
|
|
93
|
-
return {};
|
|
94
89
|
}
|
|
90
|
+
return {};
|
|
95
91
|
})
|
|
96
92
|
.then(() => getNamespace(namespace, { ignoreCache: true })); // updates cache
|
|
97
93
|
|
|
98
94
|
export const getAllNamespaces = () =>
|
|
99
|
-
doFetch(
|
|
95
|
+
doFetch(`/api/europa/v1/me`)
|
|
100
96
|
.then((res) => {
|
|
101
97
|
if (!res.ok) {
|
|
102
98
|
log.error(
|
|
@@ -109,12 +105,12 @@ export const getAllNamespaces = () =>
|
|
|
109
105
|
})
|
|
110
106
|
.then((data) => {
|
|
111
107
|
const europaValidData = EuropaSchema.check(data);
|
|
112
|
-
cache.set('europa', europaValidData,
|
|
108
|
+
cache.set('europa', europaValidData, CACHE_DURATION);
|
|
113
109
|
return europaValidData.namespaces;
|
|
114
110
|
});
|
|
115
111
|
|
|
116
112
|
export const deleteNamespaceKey = (namespace: string, key: string) =>
|
|
117
|
-
doFetch(
|
|
113
|
+
doFetch(`/api/europa/v1/me/${namespace}/${key}`, 'DELETE')
|
|
118
114
|
.then((res) => {
|
|
119
115
|
if (!res.ok) {
|
|
120
116
|
log.error(
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @jest-environment jsdom
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { goToLoginPage, logout } from './index';
|
|
6
6
|
import { setRootElement } from './utils/customEventWrapper';
|
|
7
7
|
import * as authentication from './actions/authentication';
|
|
8
8
|
import { listenForAuthenticationRequests } from './eventHandlers/authentication';
|
|
@@ -31,7 +31,7 @@ describe('Public endpoint - authentication', () => {
|
|
|
31
31
|
|
|
32
32
|
it('should login', () => {
|
|
33
33
|
jest.spyOn(authentication, 'login').mockImplementation();
|
|
34
|
-
|
|
34
|
+
goToLoginPage('https://www.avisnavn.no');
|
|
35
35
|
expect(authentication.login).toHaveBeenCalledWith(
|
|
36
36
|
'https://www.avisnavn.no'
|
|
37
37
|
);
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @jest-environment jsdom
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
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';
|
|
@@ -30,7 +30,7 @@ describe('Public endpoint - userdata', () => {
|
|
|
30
30
|
});
|
|
31
31
|
|
|
32
32
|
const onUserUpdate = jest.fn();
|
|
33
|
-
|
|
33
|
+
subscribeToUserAttributes(['name'], onUserUpdate);
|
|
34
34
|
|
|
35
35
|
expect(onUserUpdate).toHaveBeenCalledWith({
|
|
36
36
|
name: 'Donald Duck',
|
|
@@ -47,9 +47,9 @@ describe('Public endpoint - userdata', () => {
|
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
const onUserUpdate1 = jest.fn();
|
|
50
|
-
|
|
50
|
+
subscribeToUserAttributes(['name'], onUserUpdate1);
|
|
51
51
|
const onUserUpdate2 = jest.fn();
|
|
52
|
-
|
|
52
|
+
subscribeToUserAttributes(['name'], onUserUpdate2);
|
|
53
53
|
|
|
54
54
|
expect(onUserUpdate1).toHaveBeenCalledTimes(1);
|
|
55
55
|
expect(onUserUpdate1).toHaveBeenCalledWith({
|
|
@@ -77,10 +77,10 @@ describe('Public endpoint - userdata', () => {
|
|
|
77
77
|
});
|
|
78
78
|
|
|
79
79
|
const firstOnUserUpdate = jest.fn();
|
|
80
|
-
|
|
80
|
+
subscribeToUserAttributes(['name'], firstOnUserUpdate);
|
|
81
81
|
|
|
82
82
|
const secondOnUserUpdate = jest.fn();
|
|
83
|
-
|
|
83
|
+
subscribeToUserAttributes(['name'], secondOnUserUpdate);
|
|
84
84
|
|
|
85
85
|
expect(firstOnUserUpdate).toHaveBeenCalledTimes(2);
|
|
86
86
|
expect(firstOnUserUpdate).nthCalledWith(1, {});
|
|
@@ -105,7 +105,7 @@ describe('Public endpoint - userdata', () => {
|
|
|
105
105
|
});
|
|
106
106
|
|
|
107
107
|
const onUserUpdate = jest.fn();
|
|
108
|
-
|
|
108
|
+
subscribeToUserAttributes(['name'], onUserUpdate);
|
|
109
109
|
|
|
110
110
|
expect(onUserUpdate).toHaveBeenCalledWith({
|
|
111
111
|
name: 'Donald Duck',
|
|
@@ -124,7 +124,7 @@ describe('Public endpoint - userdata', () => {
|
|
|
124
124
|
});
|
|
125
125
|
|
|
126
126
|
const onUserUpdate = jest.fn();
|
|
127
|
-
|
|
127
|
+
subscribeToUserAttributes(['name', 'email'], onUserUpdate);
|
|
128
128
|
|
|
129
129
|
expect(onUserUpdate).toHaveBeenCalledWith({
|
|
130
130
|
name: 'Donald Duck',
|
|
@@ -143,7 +143,7 @@ describe('Public endpoint - userdata', () => {
|
|
|
143
143
|
});
|
|
144
144
|
|
|
145
145
|
const onUserUpdate = jest.fn();
|
|
146
|
-
expect(() =>
|
|
146
|
+
expect(() => subscribeToUserAttributes([], onUserUpdate)).toThrowError(
|
|
147
147
|
new Error('You must subscribe to at least one attribute')
|
|
148
148
|
);
|
|
149
149
|
});
|
|
@@ -159,10 +159,10 @@ describe('Public endpoint - userdata', () => {
|
|
|
159
159
|
});
|
|
160
160
|
|
|
161
161
|
const firstOnUserUpdate = jest.fn();
|
|
162
|
-
|
|
162
|
+
subscribeToUserAttributes(['name', 'email'], firstOnUserUpdate);
|
|
163
163
|
|
|
164
164
|
const secondOnUserUpdate = jest.fn();
|
|
165
|
-
|
|
165
|
+
subscribeToUserAttributes(['email'], secondOnUserUpdate);
|
|
166
166
|
|
|
167
167
|
expect(firstOnUserUpdate).toHaveBeenCalledTimes(1);
|
|
168
168
|
expect(firstOnUserUpdate).nthCalledWith(1, {
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import 'regenerator-runtime';
|
|
2
2
|
import _ from 'lodash';
|
|
3
|
-
import { AuthenticationEvents } from './eventHandlers/authentication';
|
|
4
3
|
import { AmediaUserEvents } from './eventHandlers/userAttributes';
|
|
5
4
|
import { ActivationEvents } from './eventHandlers/activation';
|
|
6
5
|
import { dispatch, listenForAllEvents } from './utils/customEventWrapper';
|
|
@@ -11,7 +10,9 @@ import * as authentication from './actions/authentication';
|
|
|
11
10
|
* Enables you to subscribe to the user attributes you care about and
|
|
12
11
|
* get notified when something changes
|
|
13
12
|
*/
|
|
14
|
-
export const
|
|
13
|
+
export const subscribeToUserAttributes = <
|
|
14
|
+
T extends Array<keyof UserAttributes>
|
|
15
|
+
>(
|
|
15
16
|
attributes: T,
|
|
16
17
|
onUserUpdated: (attributes: Pick<UserAttributes, T[number]>) => void,
|
|
17
18
|
onStateUpdated?: (attributes: State) => void
|
|
@@ -56,7 +57,7 @@ export const subscribeToUserUpdates = <T extends Array<keyof UserAttributes>>(
|
|
|
56
57
|
* to the requestedUrl if supplied or the current url before redirecting away if not.
|
|
57
58
|
* @param requestedUrl
|
|
58
59
|
*/
|
|
59
|
-
export const
|
|
60
|
+
export const goToLoginPage = (requestedUrl?: string) =>
|
|
60
61
|
authentication.login(requestedUrl || window.location.href);
|
|
61
62
|
export const logout = () => authentication.logout();
|
|
62
63
|
export const tryActivateSubscriptionAccess = (
|
package/src/lib/rpc.ts
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { RPCclient } from '@amedia/frontend-rpc';
|
|
2
2
|
|
|
3
|
-
let rpcSessionClient
|
|
4
|
-
|
|
3
|
+
let rpcSessionClient: {
|
|
4
|
+
addMethod: (methodName: string) => void;
|
|
5
|
+
userAuthenticated: (boolean) => void;
|
|
6
|
+
};
|
|
7
|
+
export const getRPCClient = (): Pick<
|
|
8
|
+
typeof rpcSessionClient,
|
|
9
|
+
'userAuthenticated'
|
|
10
|
+
> => {
|
|
5
11
|
if (rpcSessionClient) {
|
|
6
12
|
return rpcSessionClient;
|
|
7
13
|
}
|
|
8
14
|
rpcSessionClient = new RPCclient({
|
|
9
15
|
version: '1.0.0',
|
|
10
16
|
component: `userdata-session`,
|
|
17
|
+
methods: ['userAuthenticated'],
|
|
11
18
|
});
|
|
12
19
|
|
|
13
|
-
rpcSessionClient.addMethod('userAuthenticated');
|
|
14
20
|
return rpcSessionClient;
|
|
15
21
|
};
|