@amedia/user 0.13.35 → 0.13.37
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/user.cjs +1 -1
- package/dist/user.cjs.map +3 -3
- package/dist/user.d.ts +15 -1
- package/dist/user.js +1 -1
- package/dist/user.js.map +3 -3
- package/package.json +5 -2
- package/.codeclimate.yml +0 -8
- package/.eslintignore +0 -2
- package/.eslintrc +0 -11
- package/.prettierrc.json +0 -1
- package/@types/amedia-frontend-rpc.d.ts +0 -11
- package/@types/browserid.d.ts +0 -4
- package/build.js +0 -28
- package/checkVersion.cjs +0 -12
- package/cloudbuild.json +0 -29
- package/cop.json +0 -8
- package/dist/actions/authentication.d.ts +0 -4
- package/dist/actions/authentication.spec.d.ts +0 -1
- package/dist/actions/authorization.d.ts +0 -14
- package/dist/actions/authorization.spec.d.ts +0 -4
- package/dist/actions/preferences.d.ts +0 -9
- package/dist/actions/preferences.spec.d.ts +0 -1
- package/dist/actions/storage.d.ts +0 -7
- package/dist/actions/userData.d.ts +0 -2
- package/dist/actions/userData.spec.d.ts +0 -1
- package/dist/autologin/index.d.ts +0 -7
- package/dist/autologin/index.spec.d.ts +0 -1
- package/dist/autologin/thirdPartyCookie.d.ts +0 -3
- package/dist/autologin/thirdPartyCookie.spec.d.ts +0 -1
- package/dist/cache/engines/global.d.ts +0 -24
- package/dist/cache/engines/global.spec.d.ts +0 -1
- package/dist/cache/index.d.ts +0 -11
- package/dist/cache/index.spec.d.ts +0 -1
- package/dist/cache/types.d.ts +0 -11
- package/dist/clients/europa.d.ts +0 -24
- package/dist/clients/europa.spec.d.ts +0 -1
- package/dist/clients/jupiter.d.ts +0 -30
- package/dist/clients/jupiter.spec.d.ts +0 -1
- package/dist/clients/mercury.d.ts +0 -3
- package/dist/clients/mercury.spec.d.ts +0 -1
- package/dist/clients/mercuryLog.d.ts +0 -8
- package/dist/clients/mercuryLog.spec.d.ts +0 -1
- package/dist/clients/nebula.d.ts +0 -2
- package/dist/clients/nebula.spec.d.ts +0 -1
- package/dist/clients/pluto.d.ts +0 -4
- package/dist/clients/pluto.spec.d.ts +0 -1
- package/dist/eventHandlers/activation.d.ts +0 -15
- package/dist/eventHandlers/authentication.d.ts +0 -17
- package/dist/eventHandlers/eventMap.d.ts +0 -29
- package/dist/eventHandlers/state.d.ts +0 -1
- package/dist/eventHandlers/state.spec.d.ts +0 -1
- package/dist/eventHandlers/storage.d.ts +0 -1
- package/dist/eventHandlers/storage.spec.d.ts +0 -1
- package/dist/eventHandlers/userAttributes.d.ts +0 -1
- package/dist/eventHandlers/userAttributes.spec.d.ts +0 -1
- package/dist/exceptions/PaywallError.d.ts +0 -8
- package/dist/index-authentcation.spec.d.ts +0 -1
- package/dist/index-storage.spec.d.ts +0 -1
- package/dist/index-subscription.spec.d.ts +0 -1
- package/dist/index.d.ts +0 -20
- package/dist/janitor/index.d.ts +0 -10
- package/dist/janitor/index.spec.d.ts +0 -1
- package/dist/janitor/jobs/accessCookieRefresher.d.ts +0 -1
- package/dist/janitor/jobs/accessCookieRefresher.spec.d.ts +0 -1
- package/dist/janitor/jobs/daxsubCookie.d.ts +0 -3
- package/dist/janitor/jobs/daxsubCookie.spec.d.ts +0 -1
- package/dist/janitor/jobs/removeUserStorage.d.ts +0 -1
- package/dist/janitor/jobs/removeUserStorage.spec.d.ts +0 -1
- package/dist/lib/PaywallUnlockRequest.d.ts +0 -17
- package/dist/lib/PaywallUnlockRequest.spec.d.ts +0 -1
- package/dist/lib/UserDataRequest.d.ts +0 -22
- package/dist/lib/UserDataRequest.spec.d.ts +0 -1
- package/dist/lib/cookie.d.ts +0 -9
- package/dist/lib/rpc.d.ts +0 -6
- package/dist/store/index.d.ts +0 -23
- package/dist/store/index.spec.d.ts +0 -1
- package/dist/types.d.ts +0 -22
- package/dist/utils/customEventWrapper.d.ts +0 -19
- package/dist/utils/debouncer.d.ts +0 -26
- package/dist/utils/debouncer.spec.d.ts +0 -1
- package/dist/utils/deepEquals.d.ts +0 -1
- package/dist/utils/expectBootLogs.d.ts +0 -2
- package/dist/utils/getOrUpdate.d.ts +0 -6
- package/dist/utils/getOrUpdate.spec.d.ts +0 -1
- package/dist/utils/localhostCheck.d.ts +0 -1
- package/dist/utils/location.d.ts +0 -3
- package/dist/utils/log.d.ts +0 -9
- package/dist/utils/log.spec.d.ts +0 -1
- package/dist/utils/loginState.d.ts +0 -2
- package/dist/utils/networkError.d.ts +0 -1
- package/dist/utils/queue.d.ts +0 -12
- package/dist/utils/type.d.ts +0 -6
- package/dist/utils/url.d.ts +0 -6
- package/dist/utils/url.spec.d.ts +0 -1
- package/eik.json +0 -10
- package/global.d.ts +0 -1
- package/jest.config.js +0 -7
- package/setupJest.js +0 -1
- package/src/actions/authentication.spec.ts +0 -51
- package/src/actions/authentication.ts +0 -43
- package/src/actions/authorization.spec.ts +0 -369
- package/src/actions/authorization.ts +0 -117
- package/src/actions/preferences.spec.ts +0 -41
- package/src/actions/preferences.ts +0 -41
- package/src/actions/storage.ts +0 -22
- package/src/actions/userData.spec.ts +0 -133
- package/src/actions/userData.ts +0 -68
- package/src/autologin/index.spec.ts +0 -239
- package/src/autologin/index.ts +0 -92
- package/src/autologin/thirdPartyCookie.spec.ts +0 -95
- package/src/autologin/thirdPartyCookie.ts +0 -55
- package/src/cache/engines/global.spec.ts +0 -62
- package/src/cache/engines/global.ts +0 -48
- package/src/cache/index.spec.ts +0 -130
- package/src/cache/index.ts +0 -48
- package/src/cache/types.ts +0 -12
- package/src/clients/europa.spec.ts +0 -209
- package/src/clients/europa.ts +0 -183
- package/src/clients/jupiter.spec.ts +0 -179
- package/src/clients/jupiter.ts +0 -119
- package/src/clients/mercury.spec.ts +0 -59
- package/src/clients/mercury.ts +0 -48
- package/src/clients/mercuryLog.spec.ts +0 -20
- package/src/clients/mercuryLog.ts +0 -67
- package/src/clients/nebula.spec.ts +0 -70
- package/src/clients/nebula.ts +0 -83
- package/src/clients/pluto.spec.ts +0 -165
- package/src/clients/pluto.ts +0 -118
- package/src/eventHandlers/activation.ts +0 -35
- package/src/eventHandlers/authentication.ts +0 -34
- package/src/eventHandlers/eventMap.ts +0 -34
- package/src/eventHandlers/state.spec.ts +0 -48
- package/src/eventHandlers/state.ts +0 -23
- package/src/eventHandlers/storage.spec.ts +0 -114
- package/src/eventHandlers/storage.ts +0 -45
- package/src/eventHandlers/userAttributes.spec.ts +0 -93
- package/src/eventHandlers/userAttributes.ts +0 -31
- package/src/exceptions/PaywallError.ts +0 -16
- package/src/index-authentcation.spec.ts +0 -26
- package/src/index-storage.spec.ts +0 -144
- package/src/index-subscription.spec.ts +0 -213
- package/src/index.ts +0 -59
- package/src/janitor/index.spec.ts +0 -18
- package/src/janitor/index.ts +0 -59
- package/src/janitor/jobs/accessCookieRefresher.spec.ts +0 -213
- package/src/janitor/jobs/accessCookieRefresher.ts +0 -98
- package/src/janitor/jobs/daxsubCookie.spec.ts +0 -109
- package/src/janitor/jobs/daxsubCookie.ts +0 -38
- package/src/janitor/jobs/removeUserStorage.spec.ts +0 -34
- package/src/janitor/jobs/removeUserStorage.ts +0 -12
- package/src/lib/PaywallUnlockRequest.spec.ts +0 -363
- package/src/lib/PaywallUnlockRequest.ts +0 -143
- package/src/lib/UserDataRequest.spec.ts +0 -77
- package/src/lib/UserDataRequest.ts +0 -213
- package/src/lib/cookie.ts +0 -121
- package/src/lib/rpc.ts +0 -19
- package/src/store/index.spec.ts +0 -49
- package/src/store/index.ts +0 -64
- package/src/types.ts +0 -22
- package/src/utils/customEventWrapper.ts +0 -58
- package/src/utils/debouncer.spec.ts +0 -252
- package/src/utils/debouncer.ts +0 -123
- package/src/utils/deepEquals.ts +0 -42
- package/src/utils/expectBootLogs.ts +0 -9
- package/src/utils/getOrUpdate.spec.ts +0 -166
- package/src/utils/getOrUpdate.ts +0 -66
- package/src/utils/localhostCheck.ts +0 -9
- package/src/utils/location.ts +0 -31
- package/src/utils/log.spec.ts +0 -148
- package/src/utils/log.ts +0 -124
- package/src/utils/loginState.ts +0 -24
- package/src/utils/networkError.ts +0 -11
- package/src/utils/queue.ts +0 -32
- package/src/utils/type.ts +0 -19
- package/src/utils/url.spec.ts +0 -33
- package/src/utils/url.ts +0 -35
- package/testSetup.js +0 -19
- package/tsconfig.eslint.json +0 -24
- package/tsconfig.json +0 -18
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import store from '../store';
|
|
2
|
-
import UserDataRequest from './UserDataRequest';
|
|
3
|
-
import { listenForUserAttributeRequests } from '../eventHandlers/userAttributes';
|
|
4
|
-
import { listenForStateRequests } from '../eventHandlers/state';
|
|
5
|
-
import { listenForStorageRequests } from '../eventHandlers/storage';
|
|
6
|
-
import * as europa from '../clients/europa';
|
|
7
|
-
import * as localhostCheck from '../utils/localhostCheck';
|
|
8
|
-
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
store.clear();
|
|
11
|
-
|
|
12
|
-
listenForUserAttributeRequests();
|
|
13
|
-
listenForStateRequests();
|
|
14
|
-
listenForStorageRequests();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
describe('UserDataRequest', () => {
|
|
18
|
-
describe('When running on localhost', () => {
|
|
19
|
-
beforeEach(() => {
|
|
20
|
-
jest.spyOn(localhostCheck, 'isRunningOnLocalhost').mockReturnValue(true);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should fail with expected error', async () => {
|
|
24
|
-
try {
|
|
25
|
-
await new UserDataRequest().withAttributes(['name']).fetch();
|
|
26
|
-
} catch (e) {
|
|
27
|
-
expect(e).toEqual(new Error('@amedia/user cannot run on localhost'));
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
describe('when fetching', () => {
|
|
33
|
-
describe('and is logged in', () => {
|
|
34
|
-
beforeEach(() => {
|
|
35
|
-
store.set('state', { isLoggedIn: true });
|
|
36
|
-
store.set('userAttributes', {
|
|
37
|
-
uuid: 'uuid',
|
|
38
|
-
name: 'Donald Duck',
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
describe('and already have all requested attributes', () => {
|
|
43
|
-
let response: any;
|
|
44
|
-
beforeEach(async () => {
|
|
45
|
-
response = await new UserDataRequest()
|
|
46
|
-
.withAttributes(['name'])
|
|
47
|
-
.fetch();
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
it('should have resolved to include name', () => {
|
|
51
|
-
expect(response.attributes).toEqual({ name: 'Donald Duck' });
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('should be logged in', () => {
|
|
55
|
-
expect(response.state).toEqual({ isLoggedIn: true });
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
describe('and not logged in', () => {
|
|
61
|
-
beforeEach(() => {
|
|
62
|
-
store.set('state', { isLoggedIn: false });
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
describe('when getting storage', () => {
|
|
66
|
-
beforeEach(async () => {
|
|
67
|
-
spyOn(europa, 'getNamespaces');
|
|
68
|
-
await new UserDataRequest().withStorage(['euucp']).fetch();
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('should not request data', () => {
|
|
72
|
-
expect(europa.getNamespaces).not.toHaveBeenCalled();
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
});
|
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/dot-notation */
|
|
2
|
-
import { State, UserAttributes } from '../types';
|
|
3
|
-
import { dispatch, listenForAllEvents } from '../utils/customEventWrapper';
|
|
4
|
-
import { AmediaUserEvents } from '../eventHandlers/eventMap';
|
|
5
|
-
import { deepEquals } from '../utils/deepEquals';
|
|
6
|
-
import { isRunningOnLocalhost } from '../utils/localhostCheck';
|
|
7
|
-
|
|
8
|
-
function isMissingData(requestedKeys?: string[], data?: Object) {
|
|
9
|
-
if (requestedKeys && requestedKeys.length > 0) {
|
|
10
|
-
if (data && Object.keys(data).length > 0) {
|
|
11
|
-
return (
|
|
12
|
-
requestedKeys.filter((a) => !Object.keys(data).includes(a)).length > 0
|
|
13
|
-
);
|
|
14
|
-
}
|
|
15
|
-
return true;
|
|
16
|
-
}
|
|
17
|
-
return false;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
class UserDataRequest<
|
|
21
|
-
UA extends UserAttributes,
|
|
22
|
-
S extends { [index: string]: unknown },
|
|
23
|
-
Attributes extends Array<keyof UA>,
|
|
24
|
-
Namespaces extends Array<string>,
|
|
25
|
-
SubscribeResponse extends Record<string, object> = { state: State }
|
|
26
|
-
> {
|
|
27
|
-
constructor(
|
|
28
|
-
private attributes?: Attributes,
|
|
29
|
-
private namespaces?: Namespaces
|
|
30
|
-
) {}
|
|
31
|
-
|
|
32
|
-
withAttributes<NewAttributes extends Array<keyof UA>>(
|
|
33
|
-
attributes: NewAttributes
|
|
34
|
-
) {
|
|
35
|
-
return new UserDataRequest<
|
|
36
|
-
UA,
|
|
37
|
-
S,
|
|
38
|
-
Array<Attributes[number] & NewAttributes[number]>,
|
|
39
|
-
Namespaces,
|
|
40
|
-
SubscribeResponse & {
|
|
41
|
-
attributes: Pick<UA, Attributes[number] & NewAttributes[number]>;
|
|
42
|
-
}
|
|
43
|
-
>(
|
|
44
|
-
[...((this.attributes || ([] as unknown)) as Attributes), ...attributes],
|
|
45
|
-
this.namespaces
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
withStorage<NewNamespaces extends Array<string>>(namespaces: NewNamespaces) {
|
|
50
|
-
return new UserDataRequest<
|
|
51
|
-
UA,
|
|
52
|
-
S,
|
|
53
|
-
Attributes,
|
|
54
|
-
Array<Namespaces[number] & NewNamespaces[number]>,
|
|
55
|
-
SubscribeResponse & {
|
|
56
|
-
storage: Pick<S, Namespaces[number] & NewNamespaces[number]>;
|
|
57
|
-
}
|
|
58
|
-
>(this.attributes, [
|
|
59
|
-
...((this.namespaces || ([] as unknown)) as Namespaces),
|
|
60
|
-
...namespaces,
|
|
61
|
-
]);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
subscribe(onData: (data: SubscribeResponse) => void): void {
|
|
65
|
-
let previousData = this.setupLocalCache();
|
|
66
|
-
if (isRunningOnLocalhost()) {
|
|
67
|
-
onData(previousData);
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
listenForAllEvents(AmediaUserEvents.ON_STATE_UPDATED, (evt) => {
|
|
71
|
-
const state = evt.detail;
|
|
72
|
-
if (
|
|
73
|
-
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
74
|
-
Object.keys(previousData['state']).length > 0 &&
|
|
75
|
-
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
76
|
-
deepEquals(previousData['state'], state)
|
|
77
|
-
) {
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
previousData = { ...previousData, state };
|
|
82
|
-
onData(previousData);
|
|
83
|
-
});
|
|
84
|
-
dispatch(new CustomEvent(AmediaUserEvents.REQUEST_STATE));
|
|
85
|
-
|
|
86
|
-
if (Array.isArray(this.attributes) && this.attributes.length > 0) {
|
|
87
|
-
listenForAllEvents(AmediaUserEvents.ON_ATTRIBUTES_UPDATED, (evt) => {
|
|
88
|
-
const responseAttributes = this.processUserAttributesEvent(evt);
|
|
89
|
-
|
|
90
|
-
if (
|
|
91
|
-
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
92
|
-
Object.keys(previousData['attributes']).length > 0 &&
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
94
|
-
deepEquals(previousData['attributes'], responseAttributes)
|
|
95
|
-
) {
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
previousData = { ...previousData, attributes: responseAttributes };
|
|
100
|
-
onData(previousData);
|
|
101
|
-
});
|
|
102
|
-
dispatch(
|
|
103
|
-
new CustomEvent(AmediaUserEvents.REQUEST_USER_ATTRIBUTES, {
|
|
104
|
-
detail: [...this.attributes, 'uuid'],
|
|
105
|
-
})
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
if (Array.isArray(this.namespaces) && this.namespaces.length > 0) {
|
|
110
|
-
listenForAllEvents(AmediaUserEvents.ON_STORAGE_UPDATED, (evt) => {
|
|
111
|
-
const responseStorage = this.processStorageEvent(evt);
|
|
112
|
-
|
|
113
|
-
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
114
|
-
if (deepEquals(previousData['storage'], responseStorage)) {
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
previousData = { ...previousData, storage: responseStorage };
|
|
119
|
-
onData(previousData);
|
|
120
|
-
});
|
|
121
|
-
dispatch(
|
|
122
|
-
new CustomEvent(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, {
|
|
123
|
-
detail: this.namespaces,
|
|
124
|
-
})
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
fetch(): Promise<SubscribeResponse> {
|
|
130
|
-
return new Promise<SubscribeResponse>((resolve, reject) => {
|
|
131
|
-
if (isRunningOnLocalhost()) {
|
|
132
|
-
reject(new Error('@amedia/user cannot run on localhost'));
|
|
133
|
-
}
|
|
134
|
-
this.subscribe((data: any) => {
|
|
135
|
-
// @ts-ignore
|
|
136
|
-
if (typeof data.state.isLoggedIn === 'undefined') {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
// @ts-ignore
|
|
140
|
-
if (data.state.isLoggedIn === false) {
|
|
141
|
-
if (
|
|
142
|
-
this.attributes?.includes('extraData') &&
|
|
143
|
-
!Object.keys(data.attributes).includes('extraData')
|
|
144
|
-
) {
|
|
145
|
-
return; // ExtraData is valid even for logged out users
|
|
146
|
-
}
|
|
147
|
-
resolve(data);
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
if (
|
|
152
|
-
// @ts-ignore
|
|
153
|
-
isMissingData(this.attributes, data.attributes) ||
|
|
154
|
-
isMissingData(this.namespaces, data.storage)
|
|
155
|
-
) {
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
resolve(data);
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
private processUserAttributesEvent(evt: CustomEvent<UserAttributes>) {
|
|
165
|
-
const userAttributes = evt.detail as UA;
|
|
166
|
-
|
|
167
|
-
// Reduce responseAttributes to match requested attributes
|
|
168
|
-
// @ts-ignore Cannot be undefined. Guarded above
|
|
169
|
-
const responseAttributes = this.attributes.reduce((map, attribute) => {
|
|
170
|
-
if (userAttributes[attribute] !== undefined) {
|
|
171
|
-
// @ts-ignore
|
|
172
|
-
// eslint-disable-next-line no-param-reassign
|
|
173
|
-
map[attribute] = userAttributes[attribute];
|
|
174
|
-
}
|
|
175
|
-
return map;
|
|
176
|
-
}, {} as UA);
|
|
177
|
-
return responseAttributes;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
private processStorageEvent(evt: CustomEvent<{ [index: string]: unknown }>) {
|
|
181
|
-
const storage = evt.detail as S;
|
|
182
|
-
|
|
183
|
-
// Reduce responseAttributes to match requested attributes
|
|
184
|
-
return (
|
|
185
|
-
this.namespaces &&
|
|
186
|
-
this.namespaces.reduce((map, namespace) => {
|
|
187
|
-
if (storage[namespace] !== undefined) {
|
|
188
|
-
// @ts-ignore
|
|
189
|
-
// eslint-disable-next-line no-param-reassign
|
|
190
|
-
map[namespace] = storage[namespace];
|
|
191
|
-
}
|
|
192
|
-
return map;
|
|
193
|
-
}, {} as S)
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
private setupLocalCache() {
|
|
198
|
-
// @ts-ignore
|
|
199
|
-
let previousData: SubscribeResponse = {
|
|
200
|
-
state: {},
|
|
201
|
-
};
|
|
202
|
-
if (this.attributes && this.attributes.length > 0) {
|
|
203
|
-
previousData = { ...previousData, attributes: {} };
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
if (this.namespaces && this.namespaces.length > 0) {
|
|
207
|
-
previousData = { ...previousData, storage: {} };
|
|
208
|
-
}
|
|
209
|
-
return previousData;
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
export default UserDataRequest;
|
package/src/lib/cookie.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
const getCookieValue = (
|
|
2
|
-
nameEQ: string,
|
|
3
|
-
c: string
|
|
4
|
-
): undefined | null | string | { [index: string]: unknown } => {
|
|
5
|
-
const value = c.substring(nameEQ.length, c.length);
|
|
6
|
-
|
|
7
|
-
if (value.substring(0, 1) === '{') {
|
|
8
|
-
return JSON.parse(value);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
if (value.substring(0, 3) === '%7B') {
|
|
12
|
-
return JSON.parse(decodeURIComponent(value));
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (value === 'undefined') {
|
|
16
|
-
return undefined;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (value === '' || value === 'null') {
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return value;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export const getCookie = (name: string) => {
|
|
27
|
-
const nameEQ = `${name}=`;
|
|
28
|
-
const ca = document.cookie.split(';');
|
|
29
|
-
|
|
30
|
-
for (let i = ca.length - 1; i >= 0; i -= 1) {
|
|
31
|
-
const c = ca[i].trim();
|
|
32
|
-
if (c.indexOf(nameEQ) === 0) {
|
|
33
|
-
return getCookieValue(nameEQ, c);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return null;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const setValidTime = (date: Date, validTimeDateOrDays?: Date | number) => {
|
|
40
|
-
if (!validTimeDateOrDays) {
|
|
41
|
-
return '';
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (validTimeDateOrDays instanceof Date) {
|
|
45
|
-
return `; expires=${validTimeDateOrDays.toUTCString()}`;
|
|
46
|
-
}
|
|
47
|
-
/* Assume validTime is given in days */
|
|
48
|
-
date.setTime(date.getTime() + validTimeDateOrDays * 24 * 60 * 60 * 1000);
|
|
49
|
-
return `; expires=${date.toUTCString()}`;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const setValue = (value: unknown): string => {
|
|
53
|
-
if (!value || value === '') {
|
|
54
|
-
return '';
|
|
55
|
-
}
|
|
56
|
-
if (value instanceof Object) {
|
|
57
|
-
return encodeURIComponent(JSON.stringify(value));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (typeof value === 'number') {
|
|
61
|
-
return value.toString(10);
|
|
62
|
-
}
|
|
63
|
-
return <string>value;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
export function setCookie(
|
|
67
|
-
name: string,
|
|
68
|
-
value?: unknown,
|
|
69
|
-
validTimeInDateOrDays: Date | number = Date.now(),
|
|
70
|
-
path = '/',
|
|
71
|
-
secure: boolean = false
|
|
72
|
-
) {
|
|
73
|
-
const date = new Date();
|
|
74
|
-
|
|
75
|
-
let cookiestring = `${name}=`;
|
|
76
|
-
|
|
77
|
-
/* If value is empty or not set, handle as a cookie delete */
|
|
78
|
-
if (!value || value === '') {
|
|
79
|
-
// eslint-disable-next-line no-param-reassign
|
|
80
|
-
validTimeInDateOrDays = -1;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
cookiestring += setValue(value);
|
|
84
|
-
cookiestring += setValidTime(date, validTimeInDateOrDays);
|
|
85
|
-
cookiestring += `; path=${path}`;
|
|
86
|
-
|
|
87
|
-
if (secure) {
|
|
88
|
-
cookiestring += '; secure';
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
document.cookie = cookiestring;
|
|
92
|
-
|
|
93
|
-
if (
|
|
94
|
-
validTimeInDateOrDays &&
|
|
95
|
-
validTimeInDateOrDays !== -1 &&
|
|
96
|
-
getCookie(name) === null
|
|
97
|
-
) {
|
|
98
|
-
/**
|
|
99
|
-
* IE has a mode where user can choose to only accept session cookies, if we were unable to set it with
|
|
100
|
-
* expires, we need to try setting it without expires to trigger setting it as session cookie for these users.
|
|
101
|
-
*/
|
|
102
|
-
document.cookie = cookiestring.replace(/; expires=[^;]*/, '');
|
|
103
|
-
}
|
|
104
|
-
return cookiestring;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function removeCookie(name: string): void {
|
|
108
|
-
setCookie(name);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* This function should only be used in tests, as it will remove all cookies a user has on the domain.
|
|
113
|
-
*/
|
|
114
|
-
export function removeAllCookies() {
|
|
115
|
-
const cookies = document.cookie.split(';');
|
|
116
|
-
|
|
117
|
-
cookies.forEach((cookieToDelete) => {
|
|
118
|
-
const eqPos = cookieToDelete.indexOf('=');
|
|
119
|
-
removeCookie(eqPos > -1 ? cookieToDelete.substr(0, eqPos) : cookieToDelete);
|
|
120
|
-
});
|
|
121
|
-
}
|
package/src/lib/rpc.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { RPCclient } from '@amedia/frontend-rpc';
|
|
2
|
-
|
|
3
|
-
interface LocalRPCclient extends RPCclient {
|
|
4
|
-
userAuthenticated: (isLoggedIn: boolean) => void;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
let rpcSessionClient: LocalRPCclient;
|
|
8
|
-
export const getRPCClient = (): LocalRPCclient => {
|
|
9
|
-
if (rpcSessionClient) {
|
|
10
|
-
return rpcSessionClient;
|
|
11
|
-
}
|
|
12
|
-
rpcSessionClient = new RPCclient({
|
|
13
|
-
version: '1.0.0',
|
|
14
|
-
component: `userdata-session`,
|
|
15
|
-
methods: ['userAuthenticated'],
|
|
16
|
-
}) as LocalRPCclient;
|
|
17
|
-
|
|
18
|
-
return rpcSessionClient;
|
|
19
|
-
};
|
package/src/store/index.spec.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import store from '.';
|
|
2
|
-
|
|
3
|
-
beforeEach(() => {
|
|
4
|
-
store.clear();
|
|
5
|
-
});
|
|
6
|
-
|
|
7
|
-
describe('Store', () => {
|
|
8
|
-
it('should set in store', () => {
|
|
9
|
-
store.merge('userAttributes', { access: ['newspaper'] });
|
|
10
|
-
expect(store.get('userAttributes')).toEqual({ access: ['newspaper'] });
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it('should append in store', () => {
|
|
14
|
-
store.merge('userAttributes', { access: ['newspaper'] });
|
|
15
|
-
store.merge('userAttributes', { name: 'foo' });
|
|
16
|
-
expect(store.get('userAttributes')).toEqual({
|
|
17
|
-
name: 'foo',
|
|
18
|
-
access: ['newspaper'],
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should replace in store', () => {
|
|
23
|
-
store.merge('userAttributes', { access: ['newspaper'] });
|
|
24
|
-
store.set('userAttributes', {});
|
|
25
|
-
expect(store.get('userAttributes')).toEqual({});
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('should emit on change', () => {
|
|
29
|
-
const listenerSpy = jest.fn();
|
|
30
|
-
store.on('userAttributes', listenerSpy);
|
|
31
|
-
expect(listenerSpy).not.toHaveBeenCalled();
|
|
32
|
-
store.merge('userAttributes', { name: 'Donald' });
|
|
33
|
-
expect(listenerSpy).toHaveBeenCalledWith({ name: 'Donald' });
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should emit to multiple relevant', () => {
|
|
37
|
-
const listenerSpy1 = jest.fn();
|
|
38
|
-
const listenerSpy2 = jest.fn();
|
|
39
|
-
const listenerSpy3 = jest.fn();
|
|
40
|
-
store.on('userAttributes', listenerSpy1);
|
|
41
|
-
store.on('userAttributes', listenerSpy2);
|
|
42
|
-
store.on('state', listenerSpy3);
|
|
43
|
-
|
|
44
|
-
store.merge('userAttributes', { name: 'Donald' });
|
|
45
|
-
expect(listenerSpy1).toHaveBeenCalledWith({ name: 'Donald' });
|
|
46
|
-
expect(listenerSpy2).toHaveBeenCalledWith({ name: 'Donald' });
|
|
47
|
-
expect(listenerSpy3).not.toHaveBeenCalled();
|
|
48
|
-
});
|
|
49
|
-
});
|
package/src/store/index.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import deepmerge from 'ts-deepmerge';
|
|
2
|
-
import { State, UserAttributes } from '../types';
|
|
3
|
-
|
|
4
|
-
type StoreDataMap = {
|
|
5
|
-
userAttributes: UserAttributes;
|
|
6
|
-
state: State;
|
|
7
|
-
storage: { [namespace: string]: { [index: string]: unknown } };
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
type Listener<T> = (data: T) => void;
|
|
11
|
-
type Listeners<T = StoreDataMap> = {
|
|
12
|
-
[key in keyof T]?: Array<Listener<T[key]>>;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const store: { [index in keyof StoreDataMap]?: StoreDataMap[index] } = {};
|
|
16
|
-
const listeners: Listeners = {};
|
|
17
|
-
|
|
18
|
-
const emit = (key: keyof StoreDataMap): void => {
|
|
19
|
-
// @ts-ignore
|
|
20
|
-
listeners[key]?.forEach((listener) => listener(store[key]));
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const on = <K extends keyof StoreDataMap, T = StoreDataMap[K]>(
|
|
24
|
-
key: K,
|
|
25
|
-
listener: Listener<T>
|
|
26
|
-
) => {
|
|
27
|
-
if (!listeners[key]) {
|
|
28
|
-
listeners[key] = [];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// @ts-ignore todo How can this possibly be undefined given the statement above?
|
|
32
|
-
listeners[key].push(listener);
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const set = (key: keyof StoreDataMap, value: StoreDataMap[typeof key]) => {
|
|
36
|
-
// @ts-ignore todo Not sure why this type fails.
|
|
37
|
-
store[key] = value;
|
|
38
|
-
emit(key);
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
const merge = <K extends keyof StoreDataMap>(
|
|
42
|
-
key: K,
|
|
43
|
-
value: Partial<StoreDataMap[K]>
|
|
44
|
-
) => {
|
|
45
|
-
// @ts-ignore
|
|
46
|
-
set(key, deepmerge(store[key] || {}, value));
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const get = <K extends keyof StoreDataMap, T = StoreDataMap[K]>(
|
|
50
|
-
key: K
|
|
51
|
-
): T | undefined =>
|
|
52
|
-
// @ts-ignore Not sure why this fails
|
|
53
|
-
store[key];
|
|
54
|
-
const clear = () => {
|
|
55
|
-
store.state = undefined;
|
|
56
|
-
store.userAttributes = undefined;
|
|
57
|
-
store.storage = undefined;
|
|
58
|
-
Object.keys(listeners).forEach((key) => {
|
|
59
|
-
// @ts-ignore
|
|
60
|
-
listeners[key] = [];
|
|
61
|
-
});
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
export default { merge, set, get, on, clear };
|
package/src/types.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export type UserAttributes = {
|
|
2
|
-
uuid?: string | null;
|
|
3
|
-
name?: string | null;
|
|
4
|
-
email?: string | null;
|
|
5
|
-
phone?: string | null;
|
|
6
|
-
trackingKey?: string | null;
|
|
7
|
-
extraData?: { [index: string]: unknown };
|
|
8
|
-
access?: string[];
|
|
9
|
-
privacyPreferences?: PrivacyPreferences | null;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export type State = {
|
|
13
|
-
isLoggedIn: boolean;
|
|
14
|
-
emergencyMode?: string[];
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export type PrivacyPreferences = {
|
|
18
|
-
allowResearchUsage: boolean;
|
|
19
|
-
connectWithSurveys: boolean;
|
|
20
|
-
personalizedAds: boolean;
|
|
21
|
-
personalizedContent: boolean;
|
|
22
|
-
};
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import log from './log';
|
|
2
|
-
|
|
3
|
-
let rootElement: Window | Element | typeof globalThis;
|
|
4
|
-
|
|
5
|
-
export const setRootElement = (element: typeof rootElement) => {
|
|
6
|
-
rootElement = element;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
setRootElement(globalThis);
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* This listener MUST be used by all listeners in ../eventHandlers
|
|
13
|
-
* We want to ensure that only _one_ instance of this package actually
|
|
14
|
-
* orchestrates data and performs requests against backends.
|
|
15
|
-
* @param type
|
|
16
|
-
* @param listener
|
|
17
|
-
*/
|
|
18
|
-
export const listenWithExclusivity = <T extends keyof WindowEventMap>(
|
|
19
|
-
type: T,
|
|
20
|
-
listener: (this: typeof rootElement, ev: WindowEventMap[T]) => void
|
|
21
|
-
) => {
|
|
22
|
-
if (!rootElement) {
|
|
23
|
-
log.error('No root element to bind event to!');
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
rootElement.addEventListener(type, (evt) => {
|
|
27
|
-
// Since this lib might me loaded multiple times, we need to prevent all other processing of this event.
|
|
28
|
-
evt.stopImmediatePropagation();
|
|
29
|
-
listener.apply(rootElement, [evt as WindowEventMap[T]]);
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Use this for response listeners. (../index.ts) where we want
|
|
35
|
-
* all updates with data - no matter who sent the actual request
|
|
36
|
-
* @param type
|
|
37
|
-
* @param listener
|
|
38
|
-
*/
|
|
39
|
-
export const listenForAllEvents = <T extends keyof WindowEventMap>(
|
|
40
|
-
type: T,
|
|
41
|
-
listener: (this: typeof rootElement, ev: WindowEventMap[T]) => void
|
|
42
|
-
) => {
|
|
43
|
-
if (!rootElement) {
|
|
44
|
-
log.error('No root element to bind event to!');
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
rootElement.addEventListener(type, (evt) => {
|
|
48
|
-
listener.apply(rootElement, [evt as WindowEventMap[T]]);
|
|
49
|
-
});
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
export const dispatch = <T extends Event>(event: T) => {
|
|
53
|
-
if (!rootElement) {
|
|
54
|
-
log.error('No root element to dispatch event on!');
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
rootElement.dispatchEvent(event);
|
|
58
|
-
};
|