@amedia/user 0.13.17 → 0.13.19
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/amedia-user-js.iml +9 -0
- package/dist/cache/engines/indexeddb.d.ts +7 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +3 -3
- package/dist/index.js +1 -1
- package/dist/index.js.LICENSE.txt +14 -0
- package/dist/index.js.map +3 -3
- package/dist/janitor/index.spec.d.ts +4 -0
- package/dist/janitor/jobs/daxsubCookie.d.ts +2 -0
- package/dist/janitor/jobs/daxsubCookie.spec.d.ts +4 -0
- 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/utils/logValidationError.d.ts +1 -0
- package/esm/actions/authentication.d.ts +5 -0
- package/esm/actions/authentication.spec.d.ts +4 -0
- package/esm/actions/authorization.d.ts +9 -0
- package/esm/actions/preferences.d.ts +9 -0
- package/esm/actions/preferences.spec.d.ts +1 -0
- package/esm/actions/storage.d.ts +4 -0
- package/esm/actions/userData.d.ts +2 -0
- package/esm/autologin/index.d.ts +1 -0
- package/esm/autologin/thirdPartyCookie.d.ts +3 -0
- package/esm/autologin/thirdPartyCookie.spec.d.ts +1 -0
- package/esm/cache/engines/global.d.ts +21 -0
- package/esm/cache/engines/global.spec.d.ts +1 -0
- package/esm/cache/engines/indexeddb.d.ts +7 -0
- package/esm/cache/index.d.ts +11 -0
- package/esm/cache/index.spec.d.ts +4 -0
- package/esm/cache/types.d.ts +11 -0
- package/esm/clients/europa.d.ts +28 -0
- package/esm/clients/europa.spec.d.ts +1 -0
- package/esm/clients/jupiter.d.ts +30 -0
- package/esm/clients/jupiter.spec.d.ts +1 -0
- package/esm/clients/mercury.d.ts +18 -0
- package/esm/clients/mercury.spec.d.ts +4 -0
- package/esm/clients/nebula.d.ts +2 -0
- package/esm/clients/nebula.spec.d.ts +1 -0
- package/esm/clients/pluto.d.ts +3 -0
- package/esm/clients/pluto.spec.d.ts +1 -0
- package/esm/eventHandlers/activation.d.ts +15 -0
- package/esm/eventHandlers/authentication.d.ts +17 -0
- package/esm/eventHandlers/userAttributes.d.ts +24 -0
- package/esm/exceptions/PaywallError.d.ts +7 -0
- package/esm/index-authentcation.spec.d.ts +4 -0
- package/esm/index-storage.spec.d.ts +4 -0
- package/esm/index-subscription.spec.d.ts +4 -0
- package/esm/index.d.ts +26 -0
- package/esm/janitor/index.d.ts +10 -0
- package/esm/janitor/jobs/accessCookieRefresher.d.ts +1 -0
- package/esm/janitor/jobs/accessCookieRefresher.spec.d.ts +4 -0
- package/esm/lib/PaywallUnlocker.d.ts +16 -0
- package/esm/lib/PaywallUnlocker.spec.d.ts +4 -0
- package/esm/lib/User.d.ts +21 -0
- package/esm/lib/cookie.d.ts +5 -0
- package/esm/lib/rpc.d.ts +6 -0
- package/esm/store/index.d.ts +22 -0
- package/esm/store/index.spec.d.ts +1 -0
- package/esm/types.d.ts +22 -0
- package/esm/user.js +2 -0
- package/esm/user.js.map +1 -0
- package/esm/utils/customEventWrapper.d.ts +17 -0
- package/esm/utils/deepEquals.d.ts +4 -0
- package/esm/utils/getOrUpdate.d.ts +6 -0
- package/esm/utils/getOrUpdate.spec.d.ts +1 -0
- package/esm/utils/location.d.ts +3 -0
- package/esm/utils/log.d.ts +26 -0
- package/esm/utils/queue.d.ts +12 -0
- package/esm/utils/url.d.ts +6 -0
- package/esm/utils/url.spec.d.ts +1 -0
- package/package.json +1 -1
- package/src/actions/authentication.ts +1 -0
- package/src/janitor/index.ts +7 -1
- package/src/janitor/jobs/daxsubCookie.spec.ts +117 -0
- package/src/janitor/jobs/daxsubCookie.ts +37 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare const setRootElement: (element: any) => void;
|
|
2
|
+
/**
|
|
3
|
+
* This listener MUST be used by all listeners in ../eventHandlers
|
|
4
|
+
* We want to ensure that only _one_ instance of this package actually
|
|
5
|
+
* orchestrates data and performs requests against backends.
|
|
6
|
+
* @param type
|
|
7
|
+
* @param listener
|
|
8
|
+
*/
|
|
9
|
+
export declare const listenWithExclusivity: <T extends keyof WindowEventMap>(type: T, listener: (this: Window, ev: WindowEventMap[T]) => void) => void;
|
|
10
|
+
/**
|
|
11
|
+
* Use this for response listeners. (../index.ts) where we want
|
|
12
|
+
* all updates with data - no matter who sent the actual request
|
|
13
|
+
* @param type
|
|
14
|
+
* @param listener
|
|
15
|
+
*/
|
|
16
|
+
export declare const listenForAllEvents: <T extends keyof WindowEventMap>(type: T, listener: (this: Window, ev: WindowEventMap[T]) => void) => void;
|
|
17
|
+
export declare const dispatch: <T extends Event>(event: T) => void;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Runtype, Static } from 'runtypes';
|
|
2
|
+
export declare type DataFetcherWithCacheTTL<T extends Runtype> = Promise<{
|
|
3
|
+
ttl: number;
|
|
4
|
+
data: Static<T>;
|
|
5
|
+
}>;
|
|
6
|
+
export default function getOrUpdate<T extends Runtype>(key: string, schema: T, fetch: () => DataFetcherWithCacheTTL<T>, cacheQualifier?: (cacheData: Static<T>) => boolean): Promise<Static<T>>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare type Logger = {
|
|
2
|
+
debug: (message: string | any, ...extra: any[]) => void;
|
|
3
|
+
info: (message: string | any, ...extra: any[]) => void;
|
|
4
|
+
warn: (message: string | any, ...extra: any[]) => void;
|
|
5
|
+
error: (message: string | any, ...extra: any[]) => void;
|
|
6
|
+
};
|
|
7
|
+
export declare const setLogger: (engine: Logger) => void;
|
|
8
|
+
declare const _default: {
|
|
9
|
+
debug: {
|
|
10
|
+
(...data: any[]): void;
|
|
11
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
12
|
+
};
|
|
13
|
+
info: {
|
|
14
|
+
(...data: any[]): void;
|
|
15
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
16
|
+
};
|
|
17
|
+
warn: {
|
|
18
|
+
(...data: any[]): void;
|
|
19
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
20
|
+
};
|
|
21
|
+
error: {
|
|
22
|
+
(...data: any[]): void;
|
|
23
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export default _default;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
add: (key: string, promiseCallbacks: {
|
|
3
|
+
resolve: (PromiseLike: any) => void;
|
|
4
|
+
reject: (reason?: any) => void;
|
|
5
|
+
}) => void;
|
|
6
|
+
getAll: (key: string) => {
|
|
7
|
+
resolve: (PromiseLike: any) => void;
|
|
8
|
+
reject: (reason?: any) => void;
|
|
9
|
+
}[];
|
|
10
|
+
peekQueueSize: (key: string) => number;
|
|
11
|
+
};
|
|
12
|
+
export default _default;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const setCurrentHostname: (hostname: string) => void;
|
|
2
|
+
export declare const getCurrentHostname: () => string;
|
|
3
|
+
export declare const getCurrentUrl: () => string;
|
|
4
|
+
export declare const getCurrentEnvironment: () => string | null;
|
|
5
|
+
export declare const getCurrentPublicationDomain: () => string;
|
|
6
|
+
export declare const getAidDomain: () => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
package/src/janitor/index.ts
CHANGED
|
@@ -2,8 +2,14 @@ import accessCookieRefresher from './jobs/accessCookieRefresher';
|
|
|
2
2
|
import { removeUserStorage } from './jobs/removeUserStorage';
|
|
3
3
|
import { dispatch, listenWithExclusivity } from '../utils/customEventWrapper';
|
|
4
4
|
import { attemptAutoLoginJob } from '../autologin';
|
|
5
|
+
import daxSubCookieHandler from './jobs/daxsubCookie';
|
|
5
6
|
|
|
6
|
-
const jobs = [
|
|
7
|
+
const jobs = [
|
|
8
|
+
attemptAutoLoginJob,
|
|
9
|
+
accessCookieRefresher,
|
|
10
|
+
daxSubCookieHandler,
|
|
11
|
+
removeUserStorage,
|
|
12
|
+
];
|
|
7
13
|
|
|
8
14
|
async function performJobs() {
|
|
9
15
|
// eslint-disable-next-line no-restricted-syntax
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import fetch from 'jest-fetch-mock';
|
|
6
|
+
import daxSubCookieHandler from './daxsubCookie';
|
|
7
|
+
import * as cookie from '../../lib/cookie';
|
|
8
|
+
import store from '../../store';
|
|
9
|
+
import { setRootElement } from '../../utils/customEventWrapper';
|
|
10
|
+
import { listenForStateRequests } from '../../eventHandlers/state';
|
|
11
|
+
import { listenForUserAttributeRequests } from '../../eventHandlers/userAttributes';
|
|
12
|
+
import UserDataRequest from '../../lib/UserDataRequest';
|
|
13
|
+
import log from '../../utils/log';
|
|
14
|
+
|
|
15
|
+
describe('daxSubCookieHandler', () => {
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
store.clear();
|
|
18
|
+
jest.restoreAllMocks();
|
|
19
|
+
spyOn(cookie, 'setCookie');
|
|
20
|
+
spyOn(cookie, 'removeCookie');
|
|
21
|
+
fetch.resetMocks();
|
|
22
|
+
|
|
23
|
+
// We cannot use window as our event root during testing
|
|
24
|
+
// since tests are executed in parallel.
|
|
25
|
+
setRootElement(new Document());
|
|
26
|
+
|
|
27
|
+
// Also, since we keep creating new root elements, we also
|
|
28
|
+
// need to attach to these new roots.
|
|
29
|
+
listenForStateRequests();
|
|
30
|
+
listenForUserAttributeRequests();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe('when fetching user data failes', () => {
|
|
34
|
+
beforeEach(async () => {
|
|
35
|
+
spyOn(UserDataRequest.prototype, 'fetch').and.returnValue(
|
|
36
|
+
Promise.reject(new Error('Could not fetch'))
|
|
37
|
+
);
|
|
38
|
+
spyOn(log, 'error');
|
|
39
|
+
await daxSubCookieHandler();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should log error when fetch fails', () => {
|
|
43
|
+
expect(log.error).toHaveBeenCalledWith(
|
|
44
|
+
`Setting daxsub cookie failed. Error: Could not fetch`,
|
|
45
|
+
new Error('Could not fetch')
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('when user is not logged in', () => {
|
|
51
|
+
beforeEach(async () => {
|
|
52
|
+
store.set('state', { isLoggedIn: false });
|
|
53
|
+
store.set('userAttributes', { extraData: {} });
|
|
54
|
+
await daxSubCookieHandler();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('Should call removeCookie', () => {
|
|
58
|
+
expect(cookie.removeCookie).toHaveBeenCalled();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should not call setCookie', () => {
|
|
62
|
+
expect(cookie.setCookie).not.toHaveBeenCalled();
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('when user is logged in', () => {
|
|
67
|
+
beforeEach(async () => {
|
|
68
|
+
store.set('state', { isLoggedIn: true });
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
describe('when has subscription', () => {
|
|
72
|
+
beforeEach(async () => {
|
|
73
|
+
store.set('userAttributes', {
|
|
74
|
+
extraData: {
|
|
75
|
+
customer_keys: 'someString',
|
|
76
|
+
},
|
|
77
|
+
trackingKey: '148d24b2-f28e-4b1c-9958-8abd5066fc59',
|
|
78
|
+
});
|
|
79
|
+
await daxSubCookieHandler();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('Should not call removeCookie', () => {
|
|
83
|
+
expect(cookie.removeCookie).not.toHaveBeenCalled();
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('Should set cookie with a_sub_status active', () => {
|
|
87
|
+
expect(cookie.setCookie).toHaveBeenCalledWith(
|
|
88
|
+
'daxsub',
|
|
89
|
+
'a_sub_status%3Dactive%26a_user_key%3D148d24b2-f28e-4b1c-9958-8abd5066fc59',
|
|
90
|
+
1
|
|
91
|
+
);
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('when has no subscription', () => {
|
|
96
|
+
beforeEach(async () => {
|
|
97
|
+
store.set('userAttributes', {
|
|
98
|
+
extraData: {},
|
|
99
|
+
trackingKey: '148d24b2-f28e-4b1c-9958-8abd5066fc59',
|
|
100
|
+
});
|
|
101
|
+
await daxSubCookieHandler();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('Should not call removeCookie', () => {
|
|
105
|
+
expect(cookie.removeCookie).not.toHaveBeenCalled();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('Should set cookie with a_sub_status null', () => {
|
|
109
|
+
expect(cookie.setCookie).toHaveBeenCalledWith(
|
|
110
|
+
'daxsub',
|
|
111
|
+
'a_sub_status%3Dnull%26a_user_key%3D148d24b2-f28e-4b1c-9958-8abd5066fc59',
|
|
112
|
+
1
|
|
113
|
+
);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { setCookie, removeCookie } from '../../lib/cookie';
|
|
2
|
+
import UserDataRequest from '../../lib/UserDataRequest';
|
|
3
|
+
import log from '../../utils/log';
|
|
4
|
+
|
|
5
|
+
export function getCookieValue(attributes) {
|
|
6
|
+
// If user has the customer_keys field set, assume its a paying subscriber and set subscriptionStatus = active.
|
|
7
|
+
const subscriptionStatus = attributes.extraData.customer_keys
|
|
8
|
+
? 'active'
|
|
9
|
+
: 'null';
|
|
10
|
+
|
|
11
|
+
const cookieValue = `a_sub_status=${subscriptionStatus}&a_user_key=${attributes.trackingKey}`;
|
|
12
|
+
return cookieValue;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function setDaxsubCookie(attributes) {
|
|
16
|
+
const name = 'daxsub';
|
|
17
|
+
const cookieValue = getCookieValue(attributes);
|
|
18
|
+
const cookieValueEncoded = encodeURIComponent(cookieValue);
|
|
19
|
+
const validTime = 1; // 1 day (24 hours);
|
|
20
|
+
setCookie(name, cookieValueEncoded, validTime);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default function daxSubCookieHandler() {
|
|
24
|
+
return new UserDataRequest()
|
|
25
|
+
.withAttributes(['trackingKey', 'extraData'])
|
|
26
|
+
.fetch()
|
|
27
|
+
.then(({ attributes, state }) => {
|
|
28
|
+
if (!state.isLoggedIn) {
|
|
29
|
+
removeCookie('daxsub');
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
setDaxsubCookie(attributes);
|
|
33
|
+
})
|
|
34
|
+
.catch((e) => {
|
|
35
|
+
log.error(`Setting daxsub cookie failed. Error: ${e.message}`, e);
|
|
36
|
+
});
|
|
37
|
+
}
|