@amedia/user 0.14.58 → 0.14.60
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 +24 -0
- package/dist/personas.js +96 -0
- package/dist/user.d.ts +1 -1
- package/dist/user.js +138 -14
- package/package.json +4 -4
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.
|
package/dist/personas.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
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
|
+
birth_year: '1983',
|
|
32
|
+
birth_year_user: '1983',
|
|
33
|
+
customer_grouping_number: '0',
|
|
34
|
+
customer_grouping_number_user: '0',
|
|
35
|
+
customer_keys: 'TE-711e12f6168e1eb4ed8514a354b3b21f2af63f43',
|
|
36
|
+
customer_keys_user: 'TE-711e12f6168e1eb4ed8514a354b3b21f2af63f43',
|
|
37
|
+
gender: 'M',
|
|
38
|
+
gender_user: 'M',
|
|
39
|
+
geo_ids: '102675-1',
|
|
40
|
+
geo_ids_user: '102675-1',
|
|
41
|
+
grunnkrets: '38080602',
|
|
42
|
+
grunnkrets_user: '38080602',
|
|
43
|
+
last_pageview_daily: '2022-11-06T07:36:08+01:00',
|
|
44
|
+
last_pageview_daily_user: '2022-11-06T07:36:08+01:00',
|
|
45
|
+
plussalt_subscription: 'true',
|
|
46
|
+
plussalt_subscription_user: 'true',
|
|
47
|
+
plussalt_top_sites:
|
|
48
|
+
'telema,laagen,dramti,tonsbb,rjukan,avisao,rombla,bergen,ostpos,sanfjb',
|
|
49
|
+
plussalt_top_sites_user:
|
|
50
|
+
'telema,laagen,dramti,tonsbb,rjukan,avisao,rombla,bergen,ostpos,sanfjb',
|
|
51
|
+
postcode: '3674',
|
|
52
|
+
postcode_memento: '3684',
|
|
53
|
+
postcode_memento_user: '3684',
|
|
54
|
+
postcode_user: '3674',
|
|
55
|
+
shared_account: true,
|
|
56
|
+
shared_account_user: true,
|
|
57
|
+
sub_category: 'etablert kunde',
|
|
58
|
+
sub_category_user: 'etablert kunde',
|
|
59
|
+
user_grouping_number: '30',
|
|
60
|
+
user_grouping_number_user: '30',
|
|
61
|
+
},
|
|
62
|
+
access: ['newspaper'],
|
|
63
|
+
privacyPreferences: {
|
|
64
|
+
allowResearchUsage: true,
|
|
65
|
+
connectWithSurveys: true,
|
|
66
|
+
personalizedAds: true,
|
|
67
|
+
personalizedContent: true,
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
state: {
|
|
71
|
+
isLoggedIn: true,
|
|
72
|
+
},
|
|
73
|
+
storage: {
|
|
74
|
+
euucp: {
|
|
75
|
+
button_clicked: '1563131693949',
|
|
76
|
+
viewed: '1563131686005',
|
|
77
|
+
},
|
|
78
|
+
privacy_preferences: {
|
|
79
|
+
allow_research_usage: 'true',
|
|
80
|
+
allow_research_usage_seen: '2022-01-20T10:04:35.979+01:00',
|
|
81
|
+
allow_research_usage_set: '2020-07-06T12:24:53.050+02:00',
|
|
82
|
+
connect_with_surveys: 'false',
|
|
83
|
+
connect_with_surveys_seen: '2022-01-20T10:04:35.979+01:00',
|
|
84
|
+
connect_with_surveys_set: '2020-07-06T12:25:02.348+02:00',
|
|
85
|
+
info_popup_dismissed: '2020-05-19T14:59:29+02:00',
|
|
86
|
+
personalized_ads: 'true',
|
|
87
|
+
personalized_ads_seen: '2022-01-20T10:04:35.979+01:00',
|
|
88
|
+
personalized_ads_set: '2020-07-06T12:24:50.312+02:00',
|
|
89
|
+
personalized_content: 'true',
|
|
90
|
+
personalized_content_seen: '2022-10-10T17:16:32.631+02:00',
|
|
91
|
+
personalized_content_set: '2022-10-10T17:16:32.631+02:00',
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
};
|
package/dist/user.d.ts
CHANGED
|
@@ -97,7 +97,7 @@ declare class PaywallError extends Error {
|
|
|
97
97
|
constructor(message: string, code: string);
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
declare type Storage = Record<string, Record<string,
|
|
100
|
+
declare type Storage = Record<string, Record<string, string>>;
|
|
101
101
|
/**
|
|
102
102
|
* This will, if necessary, trigger a redirect to www.aid.no and return
|
|
103
103
|
* to the requestedUrl if supplied or the current url before redirecting away if not.
|
package/dist/user.js
CHANGED
|
@@ -1,30 +1,154 @@
|
|
|
1
|
-
|
|
2
|
-
throw new Error('You must use @amedia/user through Eik. Please read docs');
|
|
3
|
-
};
|
|
1
|
+
import personas from './personas.js';
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
function isRunningOnLocalhost() {
|
|
4
|
+
return (
|
|
5
|
+
location.host === 'localhost' || /localhost.api.no$/.test(location.host)
|
|
6
|
+
);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function notImplemented() {
|
|
10
|
+
throw new Error('Not implemented in @amedia/user mock mode');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
let currentPersona;
|
|
14
|
+
function getMockData() {
|
|
15
|
+
const availablePersonas = Object.keys(personas).reduce(
|
|
16
|
+
(p, c) => ({ ...p, [c]: personas[c].description }),
|
|
17
|
+
{}
|
|
18
|
+
);
|
|
19
|
+
let persona =
|
|
20
|
+
new URLSearchParams(location.search).get('aid_persona') || 'donald';
|
|
21
|
+
if (!personas[persona]) {
|
|
22
|
+
console.warn(
|
|
23
|
+
`[@amedia/user]: Unknown persona (${persona}) was selected. Falling back to 'donald'.`,
|
|
24
|
+
'\n\nAvailable personas\n',
|
|
25
|
+
availablePersonas
|
|
26
|
+
);
|
|
27
|
+
persona = 'donald';
|
|
28
|
+
}
|
|
29
|
+
const mockData = personas[persona].data;
|
|
30
|
+
if (!currentPersona || currentPersona !== persona) {
|
|
31
|
+
console.debug(
|
|
32
|
+
`[@amedia/user]: Running in mock-mode with as ${persona}.`,
|
|
33
|
+
mockData,
|
|
34
|
+
'\n\nOther personas available:\n',
|
|
35
|
+
Object.keys(availablePersonas)
|
|
36
|
+
.filter((p) => p !== persona)
|
|
37
|
+
.reduce((p, c) => (p[c] = availablePersonas[c]) && p, {})
|
|
38
|
+
);
|
|
9
39
|
}
|
|
40
|
+
currentPersona = persona;
|
|
41
|
+
return mockData;
|
|
10
42
|
}
|
|
11
43
|
|
|
12
44
|
export class UserDataRequest {
|
|
45
|
+
constructor(attributes, namespaces) {
|
|
46
|
+
this.attributes = attributes;
|
|
47
|
+
this.namespaces = namespaces;
|
|
48
|
+
this.setupMockData();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
setupMockData() {
|
|
52
|
+
this.mockData = getMockData();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
withAttributes(attributes) {
|
|
56
|
+
return new UserDataRequest(
|
|
57
|
+
[...(this.attributes || []), ...attributes],
|
|
58
|
+
this.namespaces
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
withStorage(namespaces) {
|
|
63
|
+
return new UserDataRequest(this.attributes, [
|
|
64
|
+
...(this.namespaces || []),
|
|
65
|
+
...namespaces,
|
|
66
|
+
]);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
subscribe(onData) {
|
|
70
|
+
onData({
|
|
71
|
+
...((this.attributes && {
|
|
72
|
+
attributes: this.attributes.reduce(
|
|
73
|
+
(attributes, attribute) => ({
|
|
74
|
+
...attributes,
|
|
75
|
+
[attribute]: this.mockData.attributes[attribute],
|
|
76
|
+
}),
|
|
77
|
+
{}
|
|
78
|
+
),
|
|
79
|
+
}) ||
|
|
80
|
+
{}),
|
|
81
|
+
...(this.namespaces && {
|
|
82
|
+
storage: this.namespaces.reduce(
|
|
83
|
+
(namespaces, namespace) => ({
|
|
84
|
+
...namespaces,
|
|
85
|
+
[namespace]: this.mockData.storage[namespace],
|
|
86
|
+
}),
|
|
87
|
+
{}
|
|
88
|
+
),
|
|
89
|
+
}),
|
|
90
|
+
state: this.mockData.state,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
fetch(options) {
|
|
95
|
+
return new Promise((resolve) => {
|
|
96
|
+
this.subscribe((data) => {
|
|
97
|
+
resolve(data);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export class FetchTimeoutError extends Error {
|
|
13
104
|
constructor() {
|
|
14
|
-
|
|
105
|
+
super();
|
|
106
|
+
notImplemented();
|
|
15
107
|
}
|
|
16
108
|
}
|
|
17
109
|
|
|
18
110
|
export class PaywallUnlockRequest {
|
|
111
|
+
requestedAccessFeatures = ['newspaper'];
|
|
112
|
+
|
|
19
113
|
constructor() {
|
|
20
|
-
|
|
114
|
+
this.persona = getMockData();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
withSiteDomain(domain) {
|
|
118
|
+
this.siteDomain = domain;
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
withAccessFeatures(accessFeatures) {
|
|
123
|
+
this.requestedAccessFeatures = accessFeatures;
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
redirectOnAccess(requestedUrl) {
|
|
128
|
+
this.requestedUrl = requestedUrl;
|
|
129
|
+
return this;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async tryUnlock() {
|
|
133
|
+
if (!this.persona.state.isLoggedIn) {
|
|
134
|
+
return { isLoggedIn: false, hasAccess: false, accessFeatures: [] };
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
isLoggedIn: true,
|
|
138
|
+
hasAccess: this.requestedAccessFeatures.every((raf) =>
|
|
139
|
+
this.persona.attributes.access.includes(raf)
|
|
140
|
+
),
|
|
141
|
+
accessFeatures: this.persona.attributes.access,
|
|
142
|
+
};
|
|
21
143
|
}
|
|
22
144
|
}
|
|
23
145
|
|
|
24
|
-
export const goToLoginPage =
|
|
25
|
-
export const logout =
|
|
146
|
+
export const goToLoginPage = notImplemented;
|
|
147
|
+
export const logout = notImplemented;
|
|
26
148
|
|
|
27
|
-
export const updateStorage =
|
|
28
|
-
export const saveStorage =
|
|
149
|
+
export const updateStorage = notImplemented;
|
|
150
|
+
export const saveStorage = notImplemented;
|
|
29
151
|
|
|
30
|
-
|
|
152
|
+
if (!isRunningOnLocalhost()) {
|
|
153
|
+
throw new Error('You must use @amedia/user through Eik. Please read docs');
|
|
154
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amedia/user",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.60",
|
|
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)",
|
|
@@ -49,17 +49,17 @@
|
|
|
49
49
|
"ts-deepmerge": "^4.0.0"
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
|
-
"@amedia/kragl": "^1.2.
|
|
52
|
+
"@amedia/kragl": "^1.2.3",
|
|
53
53
|
"@changesets/changelog-github": "^0.4.7",
|
|
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.
|
|
57
|
+
"@types/jest": "^29.2.2",
|
|
58
58
|
"concurrently": "^7.5.0",
|
|
59
59
|
"cors": "^2.8.5",
|
|
60
60
|
"esbuild": "0.15.13",
|
|
61
61
|
"express": "^4.18.2",
|
|
62
|
-
"jest": "^29.
|
|
62
|
+
"jest": "^29.3.1",
|
|
63
63
|
"jest-environment-jsdom": "^29.2.2",
|
|
64
64
|
"jest-extended": "^3.1.0",
|
|
65
65
|
"jest-fetch-mock": "^3.0.3",
|