@amedia/user 0.9.0 → 0.10.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.
Files changed (74) hide show
  1. package/.codeclimate.yml +1 -0
  2. package/dist/actions/authentication.d.ts +2 -1
  3. package/dist/actions/authentication.spec.d.ts +4 -0
  4. package/dist/actions/authorization.d.ts +5 -1
  5. package/dist/exceptions/PaywallError.d.ts +7 -0
  6. package/dist/index.d.ts +2 -0
  7. package/dist/index.js +1 -1
  8. package/dist/lib/PaywallUnlocker.d.ts +16 -0
  9. package/dist/lib/PaywallUnlocker.spec.d.ts +4 -0
  10. package/dist/lib/User.d.ts +1 -1
  11. package/dist/lib/cookie.d.ts +1 -1
  12. package/dist/utils/location.d.ts +3 -0
  13. package/esm/actions/authentication.d.ts +5 -0
  14. package/esm/actions/authorization.d.ts +7 -0
  15. package/esm/actions/preferences.d.ts +9 -0
  16. package/esm/actions/preferences.spec.d.ts +1 -0
  17. package/esm/actions/storage.d.ts +1 -0
  18. package/esm/actions/userData.d.ts +2 -0
  19. package/esm/cache/engines/global.d.ts +21 -0
  20. package/esm/cache/engines/global.spec.d.ts +1 -0
  21. package/esm/cache/engines/indexeddb.d.ts +7 -0
  22. package/esm/cache/index.d.ts +11 -0
  23. package/esm/cache/index.spec.d.ts +4 -0
  24. package/esm/cache/types.d.ts +11 -0
  25. package/esm/clients/europa.d.ts +28 -0
  26. package/esm/clients/europa.spec.d.ts +1 -0
  27. package/esm/clients/jupiter.d.ts +30 -0
  28. package/esm/clients/jupiter.spec.d.ts +1 -0
  29. package/esm/clients/mercury.d.ts +18 -0
  30. package/esm/clients/mercury.spec.d.ts +4 -0
  31. package/esm/clients/nebula.d.ts +2 -0
  32. package/esm/clients/nebula.spec.d.ts +1 -0
  33. package/esm/clients/pluto.d.ts +3 -0
  34. package/esm/clients/pluto.spec.d.ts +1 -0
  35. package/esm/eventHandlers/activation.d.ts +15 -0
  36. package/esm/eventHandlers/authentication.d.ts +17 -0
  37. package/esm/eventHandlers/userAttributes.d.ts +20 -0
  38. package/esm/exceptions/PaywallError.d.ts +7 -0
  39. package/esm/index-authentcation.spec.d.ts +4 -0
  40. package/esm/index-subscription.spec.d.ts +4 -0
  41. package/esm/index.d.ts +23 -0
  42. package/esm/lib/PaywallUnlocker.d.ts +16 -0
  43. package/esm/lib/PaywallUnlocker.spec.d.ts +4 -0
  44. package/esm/lib/User.d.ts +21 -0
  45. package/esm/lib/cookie.d.ts +5 -0
  46. package/esm/lib/rpc.d.ts +6 -0
  47. package/esm/store/index.d.ts +22 -0
  48. package/esm/store/index.spec.d.ts +1 -0
  49. package/esm/types.d.ts +21 -0
  50. package/esm/user.js +2 -0
  51. package/esm/user.js.map +1 -0
  52. package/esm/utils/customEventWrapper.d.ts +17 -0
  53. package/esm/utils/deepEquals.d.ts +4 -0
  54. package/esm/utils/getOrUpdate.d.ts +6 -0
  55. package/esm/utils/getOrUpdate.spec.d.ts +1 -0
  56. package/esm/utils/location.d.ts +2 -0
  57. package/esm/utils/log.d.ts +26 -0
  58. package/esm/utils/queue.d.ts +12 -0
  59. package/esm/utils/url.d.ts +3 -0
  60. package/esm/utils/url.spec.d.ts +1 -0
  61. package/package.json +1 -1
  62. package/src/actions/authentication.spec.ts +41 -0
  63. package/src/actions/authentication.ts +17 -5
  64. package/src/actions/authorization.ts +58 -3
  65. package/src/actions/preferences.spec.ts +2 -2
  66. package/src/clients/nebula.ts +1 -1
  67. package/src/exceptions/PaywallError.ts +13 -0
  68. package/src/index-subscription.spec.ts +10 -10
  69. package/src/index.ts +3 -0
  70. package/src/lib/PaywallUnlocker.spec.ts +196 -0
  71. package/src/lib/PaywallUnlocker.ts +113 -0
  72. package/src/lib/User.ts +2 -2
  73. package/src/lib/cookie.ts +1 -1
  74. package/src/utils/location.ts +8 -0
@@ -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 CHANGED
@@ -16,7 +16,7 @@ class User<
16
16
  ) {}
17
17
 
18
18
  withAttributes<NewAttributes extends Array<keyof UA>>(
19
- ...attribute: NewAttributes
19
+ attributes: NewAttributes
20
20
  ) {
21
21
  return new User<
22
22
  UA,
@@ -27,7 +27,7 @@ class User<
27
27
  attributes: Pick<UA, Attributes[number] & NewAttributes[number]>;
28
28
  }
29
29
  >(
30
- [...((this.attributes || ([] as unknown)) as Attributes), ...attribute],
30
+ [...((this.attributes || ([] as unknown)) as Attributes), ...attributes],
31
31
  this.namespaces
32
32
  );
33
33
  }
package/src/lib/cookie.ts CHANGED
@@ -104,6 +104,6 @@ export function setCookie(
104
104
  return cookiestring;
105
105
  }
106
106
 
107
- export function removeCookie(name): void {
107
+ export function removeCookie(name: string): void {
108
108
  setCookie(name);
109
109
  }
@@ -0,0 +1,8 @@
1
+ export const reload = () => window.location.reload();
2
+
3
+ export const replace = (requestedUrl: string) =>
4
+ window.location.replace(requestedUrl);
5
+
6
+ export const navigateTo = (url: string) => {
7
+ window.location.href = url;
8
+ };