@amedia/user 0.14.59 → 0.14.61

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 CHANGED
@@ -35,6 +35,30 @@ When your source code is bundled together for production, it's important that th
35
35
 
36
36
  You can read more about Eik on [Slite](https://amedia.slite.com/app/channels/hOlqx3Miez/notes/iexhL4MZn3).
37
37
 
38
+ ## Mocking
39
+
40
+ When working exclusively on a frontend app, it might be cumbersome to boot the entire aid rig locally for development. To make this easier, the npm package provides a mock for you to use.
41
+
42
+ **To enable mocked data** you just comment out the eik import. This will cause your bundler to include the mock in the bundle.
43
+
44
+ Then mocked, you'll be logged in as a user with full access. Check the browser console for information about this persona, other personas available, and how to choose between them.
45
+
46
+ ### Caveats of the mock
47
+
48
+ This is not intended to be a complete mock as it would've soon required us to reimplement aID in the mock :)
49
+
50
+ Supported actions:
51
+
52
+ - **UserDataRequest** will return data based on current persona
53
+ - **PaywallUnlockRequest** will only return state based on current persona (e.g. no reload/redirect)
54
+
55
+ Unsupported (will throw Error):
56
+
57
+ - goToLoginPage
58
+ - logout
59
+ - updateStorage
60
+ - saveStorage
61
+
38
62
  ## Getting user data
39
63
 
40
64
  The most common use case for aID on newspapers is to get some kind of data about a user, this could be the user's name, some stored preference, access or data that has been derived about the user.
@@ -0,0 +1,97 @@
1
+ export default {
2
+ logged_out: {
3
+ description: 'Not logged in',
4
+ data: {
5
+ attributes: {
6
+ uuid: null,
7
+ },
8
+ state: {
9
+ isLoggedIn: false,
10
+ },
11
+ },
12
+ },
13
+ donald: {
14
+ description: 'Maxed out subscription',
15
+ data: {
16
+ attributes: {
17
+ uuid: '1-13-37',
18
+ name: 'Donald Duck',
19
+ trackingKey: '1-13-38',
20
+ avatar:
21
+ 'https://g.api.no/obscura/API/dynamic/r1/aidp/ts_1x1_c_m-tr_320_320_l_f/1/static/unknown-gray.png?chk=399E5A',
22
+ extraData: {
23
+ age: 39,
24
+ age_user: 39,
25
+ agillic_person_id: 'ac2bcb3e-b73a-466a-99aa-0a75067c4c7c',
26
+ agillic_person_id_user: 'ac2bcb3e-b73a-466a-99aa-0a75067c4c7c',
27
+ aib_ad_segments:
28
+ 'aib_ag_30,aib_co_38,aib_ge_01,aib_lp_08,aib_mu_3808',
29
+ aib_ad_segments_user:
30
+ 'aib_ag_30,aib_co_38,aib_ge_01,aib_lp_08,aib_mu_3808',
31
+ altno_betatester: 'true',
32
+ birth_year: '1983',
33
+ birth_year_user: '1983',
34
+ customer_grouping_number: '0',
35
+ customer_grouping_number_user: '0',
36
+ customer_keys: 'TE-711e12f6168e1eb4ed8514a354b3b21f2af63f43',
37
+ customer_keys_user: 'TE-711e12f6168e1eb4ed8514a354b3b21f2af63f43',
38
+ gender: 'M',
39
+ gender_user: 'M',
40
+ geo_ids: '102675-1',
41
+ geo_ids_user: '102675-1',
42
+ grunnkrets: '38080602',
43
+ grunnkrets_user: '38080602',
44
+ last_pageview_daily: '2022-11-06T07:36:08+01:00',
45
+ last_pageview_daily_user: '2022-11-06T07:36:08+01:00',
46
+ plussalt_subscription: 'true',
47
+ plussalt_subscription_user: 'true',
48
+ plussalt_top_sites:
49
+ 'telema,laagen,dramti,tonsbb,rjukan,avisao,rombla,bergen,ostpos,sanfjb',
50
+ plussalt_top_sites_user:
51
+ 'telema,laagen,dramti,tonsbb,rjukan,avisao,rombla,bergen,ostpos,sanfjb',
52
+ postcode: '3674',
53
+ postcode_memento: '3684',
54
+ postcode_memento_user: '3684',
55
+ postcode_user: '3674',
56
+ shared_account: true,
57
+ shared_account_user: true,
58
+ sub_category: 'etablert kunde',
59
+ sub_category_user: 'etablert kunde',
60
+ user_grouping_number: '30',
61
+ user_grouping_number_user: '30',
62
+ },
63
+ access: ['newspaper'],
64
+ privacyPreferences: {
65
+ allowResearchUsage: true,
66
+ connectWithSurveys: true,
67
+ personalizedAds: true,
68
+ personalizedContent: true,
69
+ },
70
+ },
71
+ state: {
72
+ isLoggedIn: true,
73
+ },
74
+ storage: {
75
+ euucp: {
76
+ button_clicked: '1563131693949',
77
+ viewed: '1563131686005',
78
+ },
79
+ privacy_preferences: {
80
+ allow_research_usage: 'true',
81
+ allow_research_usage_seen: '2022-01-20T10:04:35.979+01:00',
82
+ allow_research_usage_set: '2020-07-06T12:24:53.050+02:00',
83
+ connect_with_surveys: 'false',
84
+ connect_with_surveys_seen: '2022-01-20T10:04:35.979+01:00',
85
+ connect_with_surveys_set: '2020-07-06T12:25:02.348+02:00',
86
+ info_popup_dismissed: '2020-05-19T14:59:29+02:00',
87
+ personalized_ads: 'true',
88
+ personalized_ads_seen: '2022-01-20T10:04:35.979+01:00',
89
+ personalized_ads_set: '2020-07-06T12:24:50.312+02:00',
90
+ personalized_content: 'true',
91
+ personalized_content_seen: '2022-10-10T17:16:32.631+02:00',
92
+ personalized_content_set: '2022-10-10T17:16:32.631+02:00',
93
+ },
94
+ },
95
+ },
96
+ },
97
+ };
package/dist/user.js CHANGED
@@ -1,30 +1,158 @@
1
- const stub = () => {
2
- throw new Error('You must use @amedia/user through Eik. Please read docs');
3
- };
1
+ import personas from './personas.js';
4
2
 
5
- export class FetchTimeoutError extends Error {
6
- constructor() {
7
- super();
8
- stub();
3
+ function isRunningOnLocalhost() {
4
+ return (
5
+ location.hostname === 'localhost' ||
6
+ /localhost.api.no$/.test(location.hostname)
7
+ );
8
+ }
9
+
10
+ function notImplemented() {
11
+ throw new Error('Not implemented in @amedia/user mock mode');
12
+ }
13
+
14
+ let currentPersona;
15
+ function getMockData() {
16
+ const availablePersonas = Object.keys(personas).reduce(
17
+ (p, c) => ({ ...p, [c]: personas[c].description }),
18
+ {}
19
+ );
20
+ let persona =
21
+ new URLSearchParams(location.search).get('aid_persona') || 'donald';
22
+ if (!personas[persona]) {
23
+ console.warn(
24
+ `[@amedia/user]: Unknown persona (${persona}) was selected. Falling back to 'donald'.`,
25
+ '\n\nAvailable personas\n',
26
+ availablePersonas,
27
+ 'Control active persona by setting aid_persona=persona search param.'
28
+ );
29
+ persona = 'donald';
30
+ }
31
+ const mockData = personas[persona].data;
32
+ if (!currentPersona || currentPersona !== persona) {
33
+ console.debug(
34
+ `[@amedia/user]: Running in mock-mode with as ${persona}.`,
35
+ mockData,
36
+ '\n\nOther personas available:\n',
37
+ Object.keys(availablePersonas)
38
+ .filter((p) => p !== persona)
39
+ .reduce((p, c) => (p[c] = availablePersonas[c]) && p, {}),
40
+ 'Control active persona by setting aid_persona=persona search param.'
41
+ );
9
42
  }
43
+ currentPersona = persona;
44
+ return mockData;
10
45
  }
11
46
 
12
47
  export class UserDataRequest {
48
+ constructor(attributes, namespaces) {
49
+ this.attributes = attributes;
50
+ this.namespaces = namespaces;
51
+ this.setupMockData();
52
+ }
53
+
54
+ setupMockData() {
55
+ this.mockData = getMockData();
56
+ }
57
+
58
+ withAttributes(attributes) {
59
+ return new UserDataRequest(
60
+ [...(this.attributes || []), ...attributes],
61
+ this.namespaces
62
+ );
63
+ }
64
+
65
+ withStorage(namespaces) {
66
+ return new UserDataRequest(this.attributes, [
67
+ ...(this.namespaces || []),
68
+ ...namespaces,
69
+ ]);
70
+ }
71
+
72
+ subscribe(onData) {
73
+ onData({
74
+ ...((this.attributes && {
75
+ attributes: this.attributes.reduce(
76
+ (attributes, attribute) => ({
77
+ ...attributes,
78
+ [attribute]: this.mockData.attributes[attribute],
79
+ }),
80
+ {}
81
+ ),
82
+ }) ||
83
+ {}),
84
+ ...(this.namespaces && {
85
+ storage: this.namespaces.reduce(
86
+ (namespaces, namespace) => ({
87
+ ...namespaces,
88
+ [namespace]: this.mockData.storage[namespace],
89
+ }),
90
+ {}
91
+ ),
92
+ }),
93
+ state: this.mockData.state,
94
+ });
95
+ }
96
+
97
+ fetch(options) {
98
+ return new Promise((resolve) => {
99
+ this.subscribe((data) => {
100
+ resolve(data);
101
+ });
102
+ });
103
+ }
104
+ }
105
+
106
+ export class FetchTimeoutError extends Error {
13
107
  constructor() {
14
- stub();
108
+ super();
109
+ notImplemented();
15
110
  }
16
111
  }
17
112
 
18
113
  export class PaywallUnlockRequest {
114
+ requestedAccessFeatures = ['newspaper'];
115
+
19
116
  constructor() {
20
- stub();
117
+ this.persona = getMockData();
118
+ }
119
+
120
+ withSiteDomain(domain) {
121
+ this.siteDomain = domain;
122
+ return this;
123
+ }
124
+
125
+ withAccessFeatures(accessFeatures) {
126
+ this.requestedAccessFeatures = accessFeatures;
127
+ return this;
128
+ }
129
+
130
+ redirectOnAccess(requestedUrl) {
131
+ this.requestedUrl = requestedUrl;
132
+ return this;
133
+ }
134
+
135
+ async tryUnlock() {
136
+ if (!this.persona.state.isLoggedIn) {
137
+ return { isLoggedIn: false, hasAccess: false, accessFeatures: [] };
138
+ }
139
+ return {
140
+ isLoggedIn: true,
141
+ hasAccess: this.requestedAccessFeatures.every((raf) =>
142
+ this.persona.attributes.access.includes(raf)
143
+ ),
144
+ accessFeatures: this.persona.attributes.access,
145
+ };
21
146
  }
22
147
  }
23
148
 
24
- export const goToLoginPage = stub;
25
- export const logout = stub;
149
+ export const getLoginUrl = () => '/stubbed-login-url';
150
+ export const goToLoginPage = notImplemented;
151
+ export const logout = notImplemented;
26
152
 
27
- export const updateStorage = stub;
28
- export const saveStorage = stub;
153
+ export const updateStorage = notImplemented;
154
+ export const saveStorage = notImplemented;
29
155
 
30
- stub();
156
+ if (!isRunningOnLocalhost()) {
157
+ throw new Error('You must use @amedia/user through Eik. Please read docs');
158
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amedia/user",
3
- "version": "0.14.59",
3
+ "version": "0.14.61",
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)",
@@ -54,19 +54,19 @@
54
54
  "@changesets/cli": "^2.25.2",
55
55
  "@eik/cli": "^2.0.22",
56
56
  "@eik/esbuild-plugin": "^1.1.23",
57
- "@types/jest": "^29.2.2",
57
+ "@types/jest": "^29.2.3",
58
58
  "concurrently": "^7.5.0",
59
59
  "cors": "^2.8.5",
60
- "esbuild": "0.15.13",
60
+ "esbuild": "0.15.14",
61
61
  "express": "^4.18.2",
62
62
  "jest": "^29.3.1",
63
- "jest-environment-jsdom": "^29.2.2",
63
+ "jest-environment-jsdom": "^29.3.1",
64
64
  "jest-extended": "^3.1.0",
65
65
  "jest-fetch-mock": "^3.0.3",
66
66
  "mockdate": "^3.0.5",
67
67
  "npm-run-all": "^4.1.5",
68
68
  "prettier": "^2.7.1",
69
- "rollup": "3.2.5",
69
+ "rollup": "3.3.0",
70
70
  "rollup-plugin-dts": "5.0.0",
71
71
  "ts-jest": "^29.0.3",
72
72
  "tslib": "^2.4.1",