@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
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fetch from 'jest-fetch-mock';
|
|
6
|
+
import PaywallUnlocker from './PaywallUnlocker';
|
|
7
|
+
import * as authentication from '../actions/authentication';
|
|
8
|
+
import * as authorization from '../actions/authorization';
|
|
9
|
+
import * as pluto from '../clients/pluto';
|
|
10
|
+
import * as location from '../utils/location';
|
|
11
|
+
import store from '../store';
|
|
12
|
+
import PaywallError from '../exceptions/PaywallError';
|
|
13
|
+
import restoreAllMocks = jest.restoreAllMocks;
|
|
14
|
+
import spyOn = jest.spyOn;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
restoreAllMocks();
|
|
18
|
+
fetch.resetMocks();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('PaywallUnlocker', () => {
|
|
22
|
+
it('should fail if already tried to unlock paywall', () => {
|
|
23
|
+
spyOn(authorization, 'hasRecentlyTriedToUnlockPaywall').mockReturnValue(
|
|
24
|
+
true
|
|
25
|
+
);
|
|
26
|
+
expect(new PaywallUnlocker().tryUnlock()).rejects.toThrow(
|
|
27
|
+
new PaywallError(
|
|
28
|
+
'Unlock check already performed.',
|
|
29
|
+
PaywallError.ERROR_RELOADED_NO_ACCESS
|
|
30
|
+
)
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should return no access if not logged in', () => {
|
|
35
|
+
spyOn(store, 'get').mockImplementation((key) => {
|
|
36
|
+
if (key === 'state') {
|
|
37
|
+
return { isLoggedIn: false };
|
|
38
|
+
}
|
|
39
|
+
throw new Error('Mock not setup for this');
|
|
40
|
+
});
|
|
41
|
+
spyOn(pluto, 'getCurrentSession').mockResolvedValue({});
|
|
42
|
+
|
|
43
|
+
expect(new PaywallUnlocker().tryUnlock())
|
|
44
|
+
.resolves.toEqual({
|
|
45
|
+
isLoggedIn: false,
|
|
46
|
+
hasAccess: false,
|
|
47
|
+
accessFeatures: [],
|
|
48
|
+
})
|
|
49
|
+
.then(() => {
|
|
50
|
+
expect(pluto.getCurrentSession).toHaveBeenCalledTimes(1);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should return positive of has access', () => {
|
|
55
|
+
spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
|
|
56
|
+
spyOn(authorization, 'getAccess').mockResolvedValue({
|
|
57
|
+
access: ['newspaper'],
|
|
58
|
+
});
|
|
59
|
+
spyOn(authorization, 'setAccessCookies').mockResolvedValue(true);
|
|
60
|
+
spyOn(authorization, 'setHasTriedToUnlockPaywall');
|
|
61
|
+
|
|
62
|
+
expect(new PaywallUnlocker().tryUnlock())
|
|
63
|
+
.resolves.toEqual({
|
|
64
|
+
isLoggedIn: true,
|
|
65
|
+
hasAccess: true,
|
|
66
|
+
accessFeatures: ['newspaper'],
|
|
67
|
+
})
|
|
68
|
+
.then(() =>
|
|
69
|
+
expect(authorization.setHasTriedToUnlockPaywall).toHaveBeenCalled()
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('should try to activate if missing access', (done) => {
|
|
74
|
+
spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
|
|
75
|
+
spyOn(authorization, 'getAccess')
|
|
76
|
+
.mockResolvedValueOnce({ access: [] })
|
|
77
|
+
.mockResolvedValueOnce({ access: ['newspaper'] });
|
|
78
|
+
spyOn(authorization, 'setAccessCookies').mockResolvedValue(true);
|
|
79
|
+
spyOn(authorization, 'setHasTriedToUnlockPaywall');
|
|
80
|
+
spyOn(authorization, 'activate').mockResolvedValue(true);
|
|
81
|
+
|
|
82
|
+
expect(new PaywallUnlocker().tryUnlock())
|
|
83
|
+
.resolves.toEqual({
|
|
84
|
+
isLoggedIn: true,
|
|
85
|
+
hasAccess: true,
|
|
86
|
+
accessFeatures: ['newspaper'],
|
|
87
|
+
})
|
|
88
|
+
.then(() => {
|
|
89
|
+
expect(authorization.activate).toHaveBeenCalled();
|
|
90
|
+
done();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('should try to activate if missing access, but really has no access', (done) => {
|
|
95
|
+
spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
|
|
96
|
+
spyOn(authorization, 'getAccess').mockResolvedValueOnce({ access: [] });
|
|
97
|
+
spyOn(authorization, 'activate').mockResolvedValue(false);
|
|
98
|
+
|
|
99
|
+
expect(new PaywallUnlocker().tryUnlock())
|
|
100
|
+
.resolves.toEqual({
|
|
101
|
+
isLoggedIn: true,
|
|
102
|
+
hasAccess: false,
|
|
103
|
+
accessFeatures: [],
|
|
104
|
+
})
|
|
105
|
+
.then(() => {
|
|
106
|
+
expect(authorization.activate).toHaveBeenCalled();
|
|
107
|
+
done();
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("should return what the user has access to even if it's not enough", (done) => {
|
|
112
|
+
spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
|
|
113
|
+
spyOn(authorization, 'getAccess').mockResolvedValueOnce({
|
|
114
|
+
access: ['newspaper'],
|
|
115
|
+
});
|
|
116
|
+
spyOn(authorization, 'activate').mockResolvedValue(false);
|
|
117
|
+
|
|
118
|
+
expect(new PaywallUnlocker().withAccessFeatures(['plussalt']).tryUnlock())
|
|
119
|
+
.resolves.toEqual({
|
|
120
|
+
isLoggedIn: true,
|
|
121
|
+
hasAccess: false,
|
|
122
|
+
accessFeatures: ['newspaper'],
|
|
123
|
+
})
|
|
124
|
+
.then(() => {
|
|
125
|
+
expect(authorization.activate).toHaveBeenCalled();
|
|
126
|
+
done();
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('should throw error if failing to set access cookies', () => {
|
|
131
|
+
spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
|
|
132
|
+
spyOn(authorization, 'getAccess').mockResolvedValue({
|
|
133
|
+
access: ['newspaper'],
|
|
134
|
+
});
|
|
135
|
+
spyOn(authorization, 'setAccessCookies').mockResolvedValue(false);
|
|
136
|
+
spyOn(authorization, 'setHasTriedToUnlockPaywall');
|
|
137
|
+
|
|
138
|
+
expect(new PaywallUnlocker().tryUnlock())
|
|
139
|
+
.rejects.toThrow(
|
|
140
|
+
new PaywallError(
|
|
141
|
+
'Could not set access cookies',
|
|
142
|
+
PaywallError.ERROR_ENABLING_ACCESS_FAILED
|
|
143
|
+
)
|
|
144
|
+
)
|
|
145
|
+
.then(() =>
|
|
146
|
+
expect(authorization.setHasTriedToUnlockPaywall).not.toHaveBeenCalled()
|
|
147
|
+
);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('should perform reload if requested', (done) => {
|
|
151
|
+
spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
|
|
152
|
+
spyOn(authorization, 'getAccess').mockResolvedValue({
|
|
153
|
+
access: ['newspaper'],
|
|
154
|
+
});
|
|
155
|
+
spyOn(authorization, 'setAccessCookies').mockResolvedValue(true);
|
|
156
|
+
spyOn(location, 'reload').mockReturnValue();
|
|
157
|
+
spyOn(location, 'replace').mockReturnValue();
|
|
158
|
+
|
|
159
|
+
new PaywallUnlocker()
|
|
160
|
+
.reloadOnAccess()
|
|
161
|
+
.tryUnlock()
|
|
162
|
+
.then(() => {
|
|
163
|
+
expect(location.reload).toHaveBeenCalled();
|
|
164
|
+
expect(location.replace).not.toHaveBeenCalled();
|
|
165
|
+
done();
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('should perform redirect if requested', async () => {
|
|
170
|
+
spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
|
|
171
|
+
spyOn(authorization, 'getAccess').mockResolvedValue({
|
|
172
|
+
access: ['newspaper'],
|
|
173
|
+
});
|
|
174
|
+
spyOn(authorization, 'setAccessCookies').mockResolvedValue(true);
|
|
175
|
+
spyOn(location, 'reload').mockReturnValue();
|
|
176
|
+
spyOn(location, 'replace').mockReturnValue();
|
|
177
|
+
|
|
178
|
+
await new PaywallUnlocker().redirectOnAccess('/some/url').tryUnlock();
|
|
179
|
+
expect(location.reload).not.toHaveBeenCalled();
|
|
180
|
+
expect(location.replace).toHaveBeenCalledWith('/some/url');
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('should not perform redirect or reload if not requested', async () => {
|
|
184
|
+
spyOn(authentication, 'isLoggedIn').mockReturnValue(true);
|
|
185
|
+
spyOn(authorization, 'getAccess').mockResolvedValue({
|
|
186
|
+
access: ['newspaper'],
|
|
187
|
+
});
|
|
188
|
+
spyOn(authorization, 'setAccessCookies').mockResolvedValue(true);
|
|
189
|
+
spyOn(location, 'reload').mockReturnValue();
|
|
190
|
+
spyOn(location, 'replace').mockReturnValue();
|
|
191
|
+
|
|
192
|
+
await new PaywallUnlocker().tryUnlock();
|
|
193
|
+
expect(location.reload).not.toHaveBeenCalled();
|
|
194
|
+
expect(location.replace).not.toHaveBeenCalled();
|
|
195
|
+
});
|
|
196
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {
|
|
2
|
+
activate,
|
|
3
|
+
getAccess,
|
|
4
|
+
hasAccess,
|
|
5
|
+
hasRecentlyTriedToUnlockPaywall,
|
|
6
|
+
setAccessCookies,
|
|
7
|
+
setHasTriedToUnlockPaywall,
|
|
8
|
+
} from '../actions/authorization';
|
|
9
|
+
import { isLoggedIn } from '../actions/authentication';
|
|
10
|
+
import { getCurrentSession } from '../clients/pluto';
|
|
11
|
+
import { getCurrentPublicationDomain } from '../utils/url';
|
|
12
|
+
import * as location from '../utils/location';
|
|
13
|
+
import PaywallError from '../exceptions/PaywallError';
|
|
14
|
+
|
|
15
|
+
class PaywallUnlocker {
|
|
16
|
+
private requestedAccessFeatures: string[] = ['newspaper'];
|
|
17
|
+
|
|
18
|
+
private reload: boolean = false;
|
|
19
|
+
|
|
20
|
+
private requestedUrl: string | undefined;
|
|
21
|
+
|
|
22
|
+
public withAccessFeatures(accessFeatures: string[]): this {
|
|
23
|
+
this.requestedAccessFeatures = accessFeatures;
|
|
24
|
+
return this;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
public reloadOnAccess(): this {
|
|
28
|
+
this.reload = true;
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public redirectOnAccess(requestedUrl: string): this {
|
|
33
|
+
this.requestedUrl = requestedUrl;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public async tryUnlock(): Promise<{
|
|
38
|
+
isLoggedIn: boolean;
|
|
39
|
+
hasAccess: boolean;
|
|
40
|
+
accessFeatures: string[];
|
|
41
|
+
}> {
|
|
42
|
+
if (hasRecentlyTriedToUnlockPaywall()) {
|
|
43
|
+
throw new PaywallError(
|
|
44
|
+
'Unlock check already performed.',
|
|
45
|
+
PaywallError.ERROR_RELOADED_NO_ACCESS
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (
|
|
50
|
+
!isLoggedIn() &&
|
|
51
|
+
(await getCurrentSession().then(() => !isLoggedIn()))
|
|
52
|
+
) {
|
|
53
|
+
return {
|
|
54
|
+
isLoggedIn: false,
|
|
55
|
+
hasAccess: false,
|
|
56
|
+
accessFeatures: [],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return this.resolveActualAccessFeatures().then((accessFeatures) =>
|
|
61
|
+
hasAccess(accessFeatures, this.requestedAccessFeatures)
|
|
62
|
+
? this.setAccessCookiesAndPerformRequestedActions(accessFeatures)
|
|
63
|
+
: {
|
|
64
|
+
isLoggedIn: true,
|
|
65
|
+
hasAccess: false,
|
|
66
|
+
accessFeatures,
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private async resolveActualAccessFeatures() {
|
|
72
|
+
let accessFeatures = await getAccess(this.requestedAccessFeatures).then(
|
|
73
|
+
(a) => a.access || []
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
if (!hasAccess(accessFeatures, this.requestedAccessFeatures)) {
|
|
77
|
+
if (!(await activate(this.requestedAccessFeatures))) {
|
|
78
|
+
return accessFeatures;
|
|
79
|
+
}
|
|
80
|
+
accessFeatures = await getAccess(this.requestedAccessFeatures).then(
|
|
81
|
+
(a) => a.access || []
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
return accessFeatures;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
private setAccessCookiesAndPerformRequestedActions(
|
|
88
|
+
actualAccessFeatures: string[]
|
|
89
|
+
) {
|
|
90
|
+
return setAccessCookies(getCurrentPublicationDomain()).then((success) => {
|
|
91
|
+
if (!success) {
|
|
92
|
+
throw new PaywallError(
|
|
93
|
+
'Could not set access cookies',
|
|
94
|
+
PaywallError.ERROR_ENABLING_ACCESS_FAILED
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
setHasTriedToUnlockPaywall();
|
|
98
|
+
if (this.reload) {
|
|
99
|
+
location.reload();
|
|
100
|
+
}
|
|
101
|
+
if (this.requestedUrl) {
|
|
102
|
+
location.replace(this.requestedUrl);
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
isLoggedIn: true,
|
|
106
|
+
hasAccess: true,
|
|
107
|
+
accessFeatures: actualAccessFeatures,
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export default PaywallUnlocker;
|
package/src/lib/User.ts
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { State, UserAttributes } from '../types';
|
|
2
|
+
import { dispatch, listenForAllEvents } from '../utils/customEventWrapper';
|
|
3
|
+
import { AmediaUserEvents } from '../eventHandlers/userAttributes';
|
|
4
|
+
import { deepEquals } from '../utils/deepEquals';
|
|
5
|
+
|
|
6
|
+
class User<
|
|
7
|
+
UA extends UserAttributes,
|
|
8
|
+
S extends { [index: string]: unknown },
|
|
9
|
+
Attributes extends Array<keyof UA>,
|
|
10
|
+
Namespaces extends Array<string>,
|
|
11
|
+
SubscribeResponse extends {} = { state: State }
|
|
12
|
+
> {
|
|
13
|
+
constructor(
|
|
14
|
+
private attributes?: Attributes,
|
|
15
|
+
private namespaces?: Namespaces
|
|
16
|
+
) {}
|
|
17
|
+
|
|
18
|
+
withAttributes<NewAttributes extends Array<keyof UA>>(
|
|
19
|
+
attributes: NewAttributes
|
|
20
|
+
) {
|
|
21
|
+
return new User<
|
|
22
|
+
UA,
|
|
23
|
+
S,
|
|
24
|
+
Array<Attributes[number] & NewAttributes[number]>,
|
|
25
|
+
Namespaces,
|
|
26
|
+
SubscribeResponse & {
|
|
27
|
+
attributes: Pick<UA, Attributes[number] & NewAttributes[number]>;
|
|
28
|
+
}
|
|
29
|
+
>(
|
|
30
|
+
[...((this.attributes || ([] as unknown)) as Attributes), ...attributes],
|
|
31
|
+
this.namespaces
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
withStorage<NewNamespaces extends Array<string>>(
|
|
36
|
+
...namespace: NewNamespaces
|
|
37
|
+
) {
|
|
38
|
+
return new User<
|
|
39
|
+
UA,
|
|
40
|
+
S,
|
|
41
|
+
Attributes,
|
|
42
|
+
Array<Namespaces[number] & NewNamespaces[number]>,
|
|
43
|
+
SubscribeResponse & {
|
|
44
|
+
storage: Pick<S, Namespaces[number] & NewNamespaces[number]>;
|
|
45
|
+
}
|
|
46
|
+
>(this.attributes, [
|
|
47
|
+
...((this.namespaces || ([] as unknown)) as Namespaces),
|
|
48
|
+
...namespace,
|
|
49
|
+
]);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
subscribe(onData: (data: SubscribeResponse) => void): void {
|
|
53
|
+
let previousData = this.setupLocalCache();
|
|
54
|
+
|
|
55
|
+
listenForAllEvents(AmediaUserEvents.ON_STATE_UPDATED, (evt) => {
|
|
56
|
+
const state = evt.detail;
|
|
57
|
+
|
|
58
|
+
if (
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
60
|
+
Object.keys(previousData['state']).length > 0 &&
|
|
61
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
62
|
+
deepEquals(previousData['state'], state)
|
|
63
|
+
) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
previousData = { ...previousData, state };
|
|
68
|
+
onData(previousData);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
if (Array.isArray(this.attributes) && this.attributes.length > 0) {
|
|
72
|
+
listenForAllEvents(AmediaUserEvents.ON_ATTRIBUTES_UPDATED, (evt) => {
|
|
73
|
+
const responseAttributes = this.processUserAttributesEvent(evt);
|
|
74
|
+
|
|
75
|
+
if (
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
77
|
+
Object.keys(previousData['attributes']).length > 0 &&
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
79
|
+
deepEquals(previousData['attributes'], responseAttributes)
|
|
80
|
+
) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
previousData = { ...previousData, attributes: responseAttributes };
|
|
85
|
+
onData(previousData);
|
|
86
|
+
});
|
|
87
|
+
dispatch(
|
|
88
|
+
new CustomEvent(AmediaUserEvents.REQUEST_USER_ATTRIBUTES, {
|
|
89
|
+
detail: this.attributes,
|
|
90
|
+
})
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (Array.isArray(this.namespaces) && this.namespaces.length > 0) {
|
|
95
|
+
listenForAllEvents(AmediaUserEvents.ON_STORAGE_UPDATED, (evt) => {
|
|
96
|
+
const responseStorage = this.processStorageEvent(evt);
|
|
97
|
+
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
99
|
+
if (deepEquals(previousData['storage'], responseStorage)) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
previousData = { ...previousData, storage: responseStorage };
|
|
104
|
+
onData(previousData);
|
|
105
|
+
});
|
|
106
|
+
dispatch(
|
|
107
|
+
new CustomEvent(AmediaUserEvents.REQUEST_STORAGE_NAMESPACES, {
|
|
108
|
+
detail: this.namespaces,
|
|
109
|
+
})
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private processUserAttributesEvent(evt: CustomEvent<UserAttributes>) {
|
|
115
|
+
const userAttributes = evt.detail as UA;
|
|
116
|
+
|
|
117
|
+
// Reduce responseAttributes to match requested attributes
|
|
118
|
+
// @ts-ignore Cannot be undefined. Guarded above
|
|
119
|
+
const responseAttributes = this.attributes.reduce((map, attribute) => {
|
|
120
|
+
if (userAttributes[attribute] !== undefined) {
|
|
121
|
+
// @ts-ignore
|
|
122
|
+
// eslint-disable-next-line no-param-reassign
|
|
123
|
+
map[attribute] = userAttributes[attribute];
|
|
124
|
+
}
|
|
125
|
+
return map;
|
|
126
|
+
}, {} as UA);
|
|
127
|
+
return responseAttributes;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
private processStorageEvent(evt: CustomEvent<{ [index: string]: unknown }>) {
|
|
131
|
+
const storage = evt.detail as S;
|
|
132
|
+
|
|
133
|
+
// Reduce responseAttributes to match requested attributes
|
|
134
|
+
// @ts-ignore Cannot be undefined. Guarded above
|
|
135
|
+
return this.namespaces.reduce((map, namespace) => {
|
|
136
|
+
if (storage[namespace] !== undefined) {
|
|
137
|
+
// @ts-ignore
|
|
138
|
+
// eslint-disable-next-line no-param-reassign
|
|
139
|
+
map[namespace] = storage[namespace];
|
|
140
|
+
}
|
|
141
|
+
return map;
|
|
142
|
+
}, {} as S);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private setupLocalCache() {
|
|
146
|
+
// @ts-ignore
|
|
147
|
+
const previousData: SubscribeResponse = {
|
|
148
|
+
state: {},
|
|
149
|
+
};
|
|
150
|
+
if (this.attributes && this.attributes.length > 0) {
|
|
151
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
152
|
+
previousData['attributes'] = {};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (this.namespaces && this.namespaces.length > 0) {
|
|
156
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
157
|
+
previousData['storage'] = {};
|
|
158
|
+
}
|
|
159
|
+
return previousData;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export default User;
|
package/src/lib/cookie.ts
CHANGED
package/src/store/index.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { State, UserAttributes } from '../types';
|
|
|
3
3
|
type StoreDataMap = {
|
|
4
4
|
userAttributes: UserAttributes;
|
|
5
5
|
state: State;
|
|
6
|
+
storage: { [namespace: string]: { [index: string]: unknown } };
|
|
6
7
|
};
|
|
7
8
|
|
|
8
9
|
type Listener<T> = (data: T) => void;
|
|
@@ -43,6 +44,7 @@ const get = <K extends keyof StoreDataMap, T = StoreDataMap[K]>(
|
|
|
43
44
|
const clear = () => {
|
|
44
45
|
store.state = undefined;
|
|
45
46
|
store.userAttributes = undefined;
|
|
47
|
+
store.storage = undefined;
|
|
46
48
|
Object.keys(listeners).forEach((key) => {
|
|
47
49
|
listeners[key] = [];
|
|
48
50
|
});
|
package/src/types.ts
CHANGED
|
@@ -6,7 +6,6 @@ export type UserAttributes = {
|
|
|
6
6
|
extraData?: { [index: string]: unknown };
|
|
7
7
|
access?: string[];
|
|
8
8
|
privacyPreferences?: PrivacyPreferences;
|
|
9
|
-
storage?: { [index: string]: unknown };
|
|
10
9
|
};
|
|
11
10
|
|
|
12
11
|
export type State = {
|
|
@@ -14,13 +13,9 @@ export type State = {
|
|
|
14
13
|
emergencyMode?: string[];
|
|
15
14
|
};
|
|
16
15
|
|
|
17
|
-
export type AnonymousAttributes = {
|
|
18
|
-
age: number;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
16
|
export type PrivacyPreferences = {
|
|
22
|
-
allowResearchUsage:
|
|
23
|
-
connectWithSurveys:
|
|
24
|
-
personalizedAds:
|
|
25
|
-
personalizedContent:
|
|
17
|
+
allowResearchUsage: boolean;
|
|
18
|
+
connectWithSurveys: boolean;
|
|
19
|
+
personalizedAds: boolean;
|
|
20
|
+
personalizedContent: boolean;
|
|
26
21
|
};
|
|
@@ -28,7 +28,7 @@ export const listenWithExclusivity = <T extends keyof WindowEventMap>(
|
|
|
28
28
|
rootElement.addEventListener(type, (evt) => {
|
|
29
29
|
// Since this lib might me loaded multiple times, we need to prevent all other processing of this event.
|
|
30
30
|
evt.stopImmediatePropagation();
|
|
31
|
-
listener.apply(
|
|
31
|
+
listener.apply(rootElement, [evt]);
|
|
32
32
|
});
|
|
33
33
|
};
|
|
34
34
|
|
|
@@ -47,7 +47,7 @@ export const listenForAllEvents = <T extends keyof WindowEventMap>(
|
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
49
|
rootElement.addEventListener(type, (evt) => {
|
|
50
|
-
listener.apply(
|
|
50
|
+
listener.apply(rootElement, [evt]);
|
|
51
51
|
});
|
|
52
52
|
};
|
|
53
53
|
|