@amedia/user 0.14.152 → 1.0.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.
package/package.json CHANGED
@@ -1,81 +1,33 @@
1
1
  {
2
2
  "name": "@amedia/user",
3
- "version": "0.14.152",
4
3
  "description": "Client lib for working with aID user and associated data",
5
- "repository": {
6
- "type": "git",
7
- "url": "git+https://github.com/amedia/amedia-user-js.git"
8
- },
4
+ "repository": "https://github.com/amedia/amedia-user-js",
9
5
  "author": "Amedia Produkt og Teknologi AS (https://amedia.no)",
10
- "license": "MIT",
6
+ "version": "1.0.1",
11
7
  "type": "module",
12
- "module": "dist/user.js",
8
+ "types": "./index.d.ts",
13
9
  "exports": {
14
10
  ".": {
15
- "import": "./dist/user.js",
16
- "default": "./dist/user.js"
11
+ "types": "./index.d.ts",
12
+ "import": "./index.js",
13
+ "default": "./index.js"
17
14
  }
18
15
  },
19
16
  "files": [
20
- "dist/*"
17
+ "index.js",
18
+ "index.d.ts",
19
+ "README.md",
20
+ "CHANGELOG.md"
21
21
  ],
22
- "typings": "dist/user.d.ts",
23
22
  "eik": {
24
23
  "server": "https://assets.acdn.no",
25
24
  "files": {
26
- "user.js": "./dist/user.js",
27
- "user.js.map": "./dist/user.js.map",
28
- "user.d.ts": "./dist/user.d.ts"
25
+ "user.js": "../../dist/user.js",
26
+ "user.js.map": "../../dist/user.js.map",
27
+ "user.d.ts": "../../dist/user.d.ts"
29
28
  }
30
29
  },
31
30
  "scripts": {
32
- "changeset": "changeset",
33
- "release": "changeset publish",
34
- "test": "jest --coverage",
35
- "format": "kragl format",
36
- "lint": "kragl lint",
37
- "lint-fix": "kragl lint --fix",
38
- "clean-dist": "rm -rf ./dist",
39
- "build": "npm install && node --experimental-strip-types build.ts & npm run build:types",
40
- "build:types": "npx rollup -c ./types.js",
41
- "dev": "concurrently --kill-others \"node --experimental-strip-types --watch-path=./src build.ts\" \"node --experimental-strip-types server.ts \"",
42
- "typings": "tsc --noEmit --resolveJsonModule",
43
- "checkVersion": "node --experimental-json-modules checkVersion.js",
44
- "stub-npmjs-package": "find ./dist -type f ! -name '*.d.ts' -delete && cp -v stubs/* dist/.",
45
- "eik:package": "run-p typings lint && npm run test && npm run build && npx @eik/cli publish",
46
- "prepublishOnly": "run-p clean-dist typings lint && npm run test && npm run build && npm run stub-npmjs-package",
47
- "postpublish": "npm run build && npx @eik/cli publish"
48
- },
49
- "dependencies": {
50
- "@amedia/browserid": "1.1.9",
51
- "@amedia/frontend-rpc": "0.0.14",
52
- "ts-deepmerge": "7.0.3",
53
- "valibot": "1.1.0"
54
- },
55
- "devDependencies": {
56
- "@amedia/eslint-config": "4.0.7",
57
- "@amedia/kragl": "5.1.0",
58
- "@amedia/prettier-config": "2.1.1",
59
- "@changesets/changelog-github": "0.5.1",
60
- "@changesets/cli": "2.29.7",
61
- "@eik/esbuild-plugin": "2.0.5",
62
- "@fastify/cors": "^11.1.0",
63
- "@fastify/static": "^8.2.0",
64
- "@types/jest": "30.0.0",
65
- "@types/node": "24.9.1",
66
- "concurrently": "9.2.1",
67
- "esbuild": "0.25.11",
68
- "fastify": "^5.6.0",
69
- "jest": "30.2.0",
70
- "jest-environment-jsdom": "30.2.0",
71
- "jest-extended": "6.0.0",
72
- "jest-fetch-mock": "3.0.3",
73
- "mockdate": "3.0.5",
74
- "npm-run-all": "4.1.5",
75
- "rollup": "4.52.5",
76
- "rollup-plugin-dts": "6.2.3",
77
- "ts-jest": "29.4.5",
78
- "tslib": "2.8.1",
79
- "typescript": "5.9.3"
31
+ "postpublish": "(cd ../../ && npm run build) && npx @eik/cli publish"
80
32
  }
81
33
  }
package/dist/personas.js DELETED
@@ -1,111 +0,0 @@
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
- storage: {},
12
- nonUserAccess: [
13
- {
14
- accessFeatures: ['newspaper'],
15
- customer: 'some_company',
16
- },
17
- ],
18
- },
19
- },
20
- donald: {
21
- description: 'Maxed out subscription',
22
- data: {
23
- attributes: {
24
- uuid: '1-13-37',
25
- name: 'Donald Duck',
26
- trackingKey: '1-13-38',
27
- avatar:
28
- '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',
29
- extraData: {
30
- age: 39,
31
- age_user: 39,
32
- agillic_person_id: 'ac2bcb3e-b73a-466a-99aa-0a75067c4c7c',
33
- agillic_person_id_user: 'ac2bcb3e-b73a-466a-99aa-0a75067c4c7c',
34
- aib_ad_segments:
35
- 'aib_ag_30,aib_co_38,aib_ge_01,aib_lp_08,aib_mu_3808',
36
- aib_ad_segments_user:
37
- 'aib_ag_30,aib_co_38,aib_ge_01,aib_lp_08,aib_mu_3808',
38
- altno_betatester: 'true',
39
- birth_year: '1983',
40
- birth_year_user: '1983',
41
- customer_grouping_number: '0',
42
- customer_grouping_number_user: '0',
43
- customer_keys: 'TE-711e12f6168e1eb4ed8514a354b3b21f2af63f43',
44
- customer_keys_user: 'TE-711e12f6168e1eb4ed8514a354b3b21f2af63f43',
45
- gender: 'M',
46
- gender_user: 'M',
47
- geo_ids: '102675-1',
48
- geo_ids_user: '102675-1',
49
- grunnkrets: '38080602',
50
- grunnkrets_user: '38080602',
51
- last_pageview_daily: '2022-11-06T07:36:08+01:00',
52
- last_pageview_daily_user: '2022-11-06T07:36:08+01:00',
53
- plussalt_subscription: 'true',
54
- plussalt_subscription_user: 'true',
55
- plussalt_top_sites:
56
- 'telema,laagen,dramti,tonsbb,rjukan,avisao,rombla,bergen,ostpos,sanfjb',
57
- plussalt_top_sites_user:
58
- 'telema,laagen,dramti,tonsbb,rjukan,avisao,rombla,bergen,ostpos,sanfjb',
59
- postcode: '3674',
60
- postcode_memento: '3684',
61
- postcode_memento_user: '3684',
62
- postcode_user: '3674',
63
- shared_account: true,
64
- shared_account_user: true,
65
- sub_category: 'etablert kunde',
66
- sub_category_user: 'etablert kunde',
67
- user_grouping_number: '30',
68
- user_grouping_number_user: '30',
69
- },
70
- access: ['newspaper', 'plussalt'],
71
- privacyPreferences: {
72
- allowResearchUsage: true,
73
- connectWithSurveys: true,
74
- personalizedAds: true,
75
- personalizedContent: true,
76
- },
77
- sessionTrackingKey: 'session-tracking-key',
78
- },
79
- state: {
80
- isLoggedIn: true,
81
- },
82
- storage: {
83
- euucp: {
84
- button_clicked: '1563131693949',
85
- viewed: '1563131686005',
86
- },
87
- privacy_preferences: {
88
- allow_research_usage: 'true',
89
- allow_research_usage_seen: '2022-01-20T10:04:35.979+01:00',
90
- allow_research_usage_set: '2020-07-06T12:24:53.050+02:00',
91
- connect_with_surveys: 'false',
92
- connect_with_surveys_seen: '2022-01-20T10:04:35.979+01:00',
93
- connect_with_surveys_set: '2020-07-06T12:25:02.348+02:00',
94
- info_popup_dismissed: '2020-05-19T14:59:29+02:00',
95
- personalized_ads: 'true',
96
- personalized_ads_seen: '2022-01-20T10:04:35.979+01:00',
97
- personalized_ads_set: '2020-07-06T12:24:50.312+02:00',
98
- personalized_content: 'true',
99
- personalized_content_seen: '2022-10-10T17:16:32.631+02:00',
100
- personalized_content_set: '2022-10-10T17:16:32.631+02:00',
101
- },
102
- },
103
- nonUserAccess: [
104
- {
105
- accessFeatures: ['newspaper'],
106
- customer: 'some_company',
107
- },
108
- ],
109
- },
110
- },
111
- };
package/dist/user.d.ts DELETED
@@ -1,189 +0,0 @@
1
- import * as v from 'valibot';
2
-
3
- type AccessFeatures = string[];
4
- type UserAttributes = {
5
- uuid: string | null;
6
- name: string | null;
7
- trackingKey: string | null;
8
- avatar: string | null;
9
- /** @deprecated use data from [@amedia/user-datapoints](https://github.com/amedia/amedia-user-datapoints) instead */
10
- extraData: {
11
- [index: string]: unknown;
12
- };
13
- access: AccessFeatures;
14
- /** @deprecated Returns false for all keys. CMP data is now used. */
15
- privacyPreferences: PrivacyPreferences | null;
16
- sessionTrackingKey: string | null;
17
- };
18
- type State = {
19
- isLoggedIn: boolean;
20
- isCircuitBreakerTripped?: boolean;
21
- emergencyMode?: string[];
22
- };
23
- /**
24
- * @deprecated This is set to false as default. CMP data is now used.
25
- */
26
- type PrivacyPreferences = {
27
- allowResearchUsage: boolean;
28
- personalizedContent: boolean;
29
- };
30
-
31
- type ClientId = string;
32
-
33
- type NamespaceList = Array<string>;
34
-
35
- type Millis = number;
36
- type UnsubscribeFn = () => void;
37
- declare class UserDataRequest<TUserAttributes extends UserAttributes, TStorage extends Record<ClientId, Record<string, string>>, TAttributes extends Array<keyof TUserAttributes>, TRequestedStorage extends Record<keyof TStorage, NamespaceList>, TSubscribeResponse extends Record<string, unknown> & {
38
- state: State;
39
- } = {
40
- state: State;
41
- }> {
42
- private requestedAttributes?;
43
- private requestedStorage?;
44
- private requestedNonUserAccess;
45
- private context;
46
- static FETCH_TIMEOUT: Millis;
47
- private static WITH_NON_USER_ACCESS;
48
- private fetchTimeoutId;
49
- private lastReceivedData?;
50
- private subscriptionEventListeners;
51
- constructor(requestedAttributes?: TAttributes, requestedStorage?: TRequestedStorage, requestedNonUserAccess?: boolean, context?: string);
52
- withContext(context: string): this;
53
- withAttributes<NewAttributes extends Array<keyof TUserAttributes>>(attributes: NewAttributes): UserDataRequest<TUserAttributes, TStorage, (TAttributes[number] & NewAttributes[number])[], TRequestedStorage, TSubscribeResponse & {
54
- attributes: Pick<TUserAttributes, TAttributes[number] & NewAttributes[number]>;
55
- }>;
56
- withStorage(namespaces: Array<string>): UserDataRequest<TUserAttributes, TStorage, TAttributes, TRequestedStorage, TSubscribeResponse & {
57
- storage: Record<string, Record<string, string>>;
58
- }>;
59
- withClientStorage(clientId: keyof TStorage, namespaces: Array<string>): UserDataRequest<TUserAttributes, TStorage, TAttributes, TRequestedStorage, TSubscribeResponse & {
60
- clientStorage: Record<ClientId, (typeof namespaces)[number]>;
61
- }>;
62
- withNonUserAccess(): UserDataRequest<TUserAttributes, TStorage, TAttributes, TRequestedStorage, TSubscribeResponse & {
63
- nonUserAccess: Array<Record<string, string>>;
64
- }>;
65
- subscribe(onData: (data: TSubscribeResponse) => void): UnsubscribeFn;
66
- fetch(options?: {
67
- timeout?: Millis;
68
- }): Promise<TSubscribeResponse>;
69
- private startFetchTimeout;
70
- private clearFetchTimeout;
71
- private restartFetchTimeout;
72
- private unsubscribe;
73
- private processUserAttributesEvent;
74
- private processStorageEventDetail;
75
- private setupLocalCache;
76
- private debugLogFetchCompleted;
77
- }
78
-
79
- declare enum AffiliationType {
80
- ACCESS = "access",
81
- FAMILY = "family",
82
- SUBSCRIPTION = "subscription"
83
- }
84
- declare const PrimarySiteSchema: v.ObjectSchema<{
85
- readonly domain: v.StringSchema<undefined>;
86
- readonly name: v.StringSchema<undefined>;
87
- readonly affiliation: v.LiteralSchema<AffiliationType.ACCESS, undefined>;
88
- readonly affiliations: v.ArraySchema<v.UnionSchema<[v.LiteralSchema<AffiliationType.ACCESS, undefined>, v.LiteralSchema<AffiliationType.FAMILY, undefined>, v.LiteralSchema<AffiliationType.SUBSCRIPTION, undefined>], undefined>, undefined>;
89
- }, undefined>;
90
- type PrimarySite = v.InferOutput<typeof PrimarySiteSchema>;
91
-
92
- type SiteAccessResponse = {
93
- isLoggedIn: boolean;
94
- hasAccess: boolean;
95
- primarySite?: null | PrimarySite;
96
- accessFeatures: string[];
97
- };
98
- declare class SiteAccessRequest {
99
- private requiredAccessFeatures;
100
- private siteDomain;
101
- constructor(requiredAccessFeatures?: Array<string>);
102
- withSiteDomain(siteDomain: string): this;
103
- /**
104
- * @deprecated Required now. Set in constructor
105
- */
106
- withAccessFeatures(accessFeatures: Array<string>): this;
107
- requestAccess(): Promise<SiteAccessResponse>;
108
- }
109
-
110
- type PaywallUnlockResponse = SiteAccessResponse;
111
- declare class PaywallUnlockRequest {
112
- private requestedAccessFeatures;
113
- private reload;
114
- private requestedUrl;
115
- constructor(requestedAccessFeatures?: string[]);
116
- /**
117
- * @deprecated Use SiteAccessRequest to check for access on another site.
118
- */
119
- withSiteDomain(siteDomain: string): this;
120
- /**
121
- * @deprecated Required now. Set in constructor
122
- */
123
- withAccessFeatures(accessFeatures: string[]): this;
124
- reloadOnAccess(): this;
125
- redirectOnAccess(requestedUrl: string): this;
126
- tryUnlock(): Promise<PaywallUnlockResponse>;
127
- }
128
-
129
- declare class FetchTimeoutError extends Error {
130
- partialData: Record<string, unknown>;
131
- constructor(message: string, partialData: Record<string, unknown>);
132
- }
133
-
134
- declare class PaywallError extends Error {
135
- static ERROR_RELOADED_NO_ACCESS: string;
136
- static ERROR_ENABLING_ACCESS_FAILED: string;
137
- static ERROR_NO_ACCESS_AFTER_SUCCESSFUL_ACTIVATION_WITH_REQUESTED_ACCESS_FEATURES: string;
138
- code: string;
139
- constructor(message: string, code: string);
140
- }
141
-
142
- declare class EmergencyModeError extends Error {
143
- activeEmergencyModes: string[];
144
- constructor(message?: string);
145
- }
146
-
147
- declare class ValidationError extends Error {
148
- readonly code: string;
149
- constructor(code: string, message: string);
150
- }
151
-
152
- declare class TimeoutError extends Error {
153
- }
154
- /**
155
- * Poll for access on current site.
156
- * Use this when you expect the reader to have gained access, but need to
157
- * be sure before telling the reader that everything's good.
158
- *
159
- * @throws EmergencyModeError if in emergency mode
160
- * @throws TimeoutError if access cannot be attained within given timeout
161
- * @throws Error if unknown error
162
- * @returns true
163
- */
164
- declare function pollForAccess(requiredAccessFeatures?: string[], timeout_millis?: number): Promise<true>;
165
-
166
- type LoginPageParams = {
167
- requestedUrl?: string;
168
- context?: string;
169
- };
170
- /**
171
- * This will, if necessary, trigger a redirect to www.aid.no and return
172
- * to the requestedUrl if supplied or the current url before redirecting away if not.
173
- * @param requestedUrl
174
- */
175
- declare const goToLoginPage: (requestedUrlOrParams?: string | LoginPageParams) => void;
176
- declare const getLoginUrl: (requestedUrlOrParams?: string | LoginPageParams) => URL;
177
- declare const logout: () => Promise<void>;
178
- declare const requestDataRefresh: () => void;
179
- declare const aidUrls: {
180
- mainPage: URL;
181
- privacyPolicy: URL;
182
- termsOfService: URL;
183
- family: URL;
184
- profile: URL;
185
- sites: URL;
186
- };
187
-
188
- export { EmergencyModeError, FetchTimeoutError, PaywallError, PaywallUnlockRequest, SiteAccessRequest, TimeoutError, UserDataRequest, ValidationError, aidUrls, getLoginUrl, goToLoginPage, logout, pollForAccess, requestDataRefresh };
189
- export type { PaywallUnlockResponse, PrivacyPreferences, SiteAccessResponse, State, UserAttributes };
package/dist/user.js DELETED
@@ -1,205 +0,0 @@
1
- import personas from './personas.js';
2
-
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
- );
42
- }
43
- currentPersona = persona;
44
- return mockData;
45
- }
46
-
47
- export class UserDataRequest {
48
- constructor(attributes, namespaces, nonUserAccess = false) {
49
- this.attributes = attributes;
50
- this.namespaces = namespaces;
51
- this.nonUserAccess = nonUserAccess;
52
- this.setupMockData();
53
- }
54
-
55
- setupMockData() {
56
- this.mockData = getMockData();
57
- }
58
-
59
- withAttributes(attributes) {
60
- return new UserDataRequest(
61
- [...(this.attributes || []), ...attributes],
62
- this.namespaces,
63
- this.nonUserAccess
64
- );
65
- }
66
-
67
- withStorage(namespaces) {
68
- return new UserDataRequest(
69
- this.attributes,
70
- [...(this.namespaces || []), ...namespaces],
71
- this.nonUserAccess
72
- );
73
- }
74
-
75
- withNonUserAccess() {
76
- return new UserDataRequest(this.attributes, this.namespaces, true);
77
- }
78
-
79
- subscribe(onData) {
80
- onData({
81
- ...((this.attributes && {
82
- attributes: this.attributes.reduce(
83
- (attributes, attribute) => ({
84
- ...attributes,
85
- [attribute]: this.mockData.attributes[attribute],
86
- }),
87
- {}
88
- ),
89
- }) ||
90
- {}),
91
- ...(this.namespaces && {
92
- storage: this.namespaces.reduce(
93
- (namespaces, namespace) => ({
94
- ...namespaces,
95
- [namespace]: this.mockData.storage[namespace],
96
- }),
97
- {}
98
- ),
99
- }),
100
- state: this.mockData.state,
101
- ...(this.nonUserAccess && { nonUserAccess: this.mockData.nonUserAccess }),
102
- });
103
- }
104
-
105
- fetch() {
106
- return new Promise((resolve) => {
107
- this.subscribe((data) => {
108
- resolve(data);
109
- });
110
- });
111
- }
112
- }
113
-
114
- export class FetchTimeoutError extends Error {
115
- constructor() {
116
- super();
117
- notImplemented();
118
- }
119
- }
120
-
121
- export class EmergencyModeError extends Error {
122
- constructor(props) {
123
- super(props);
124
- this.activeEmergencyModes = [];
125
- notImplemented();
126
- }
127
- }
128
-
129
- export class PaywallUnlockRequest {
130
- requestedAccessFeatures = ['newspaper'];
131
-
132
- constructor() {
133
- this.persona = getMockData();
134
- }
135
-
136
- withSiteDomain(domain) {
137
- this.siteDomain = domain;
138
- return this;
139
- }
140
-
141
- withAccessFeatures(accessFeatures) {
142
- this.requestedAccessFeatures = accessFeatures;
143
- return this;
144
- }
145
-
146
- redirectOnAccess(requestedUrl) {
147
- this.requestedUrl = requestedUrl;
148
- return this;
149
- }
150
-
151
- reloadOnAccess() {
152
- this.reload = true;
153
- return this;
154
- }
155
-
156
- async tryUnlock() {
157
- if (!this.persona.state.isLoggedIn) {
158
- return { isLoggedIn: false, hasAccess: false, accessFeatures: [] };
159
- }
160
-
161
- const hasAccess = this.requestedAccessFeatures.every((raf) =>
162
- this.persona.attributes.access.includes(raf)
163
- );
164
- if (hasAccess) {
165
- if (this.reload) {
166
- console.log(
167
- "[@amedia/user PaywallUnlockRequest]: Access granted. Would've performed reload at this time."
168
- );
169
- }
170
- if (this.requestedUrl) {
171
- console.log(
172
- `[@amedia/user PaywallUnlockRequest]: Access granted. Would've performed redirect (to '${this.requestedUrl}') at this time.`
173
- );
174
- }
175
- }
176
-
177
- return {
178
- isLoggedIn: true,
179
- hasAccess,
180
- accessFeatures: this.persona.attributes.access,
181
- };
182
- }
183
- }
184
-
185
- export const getLoginUrl = () => '/stubbed-login-url';
186
- export const goToLoginPage = notImplemented;
187
- export const logout = notImplemented;
188
-
189
- export const updateStorage = notImplemented;
190
- export const saveStorage = notImplemented;
191
-
192
- const baseUrl = `https://www.aid.no.localhost.api.no/aid`;
193
- export const aidUrls = {
194
- mainPage: new URL(baseUrl),
195
- privacyStatement: new URL(`${baseUrl}/personvern`),
196
- privacyPreferences: new URL(`${baseUrl}/personvern/oversikt`),
197
- termsOfService: new URL(`${baseUrl}/brukervilkar`),
198
- family: new URL(`${baseUrl}/familie`),
199
- profile: new URL(`${baseUrl}/profil`),
200
- sites: new URL(`${baseUrl}/nettsteder`),
201
- };
202
-
203
- if (!isRunningOnLocalhost()) {
204
- throw new Error('You must use @amedia/user through Eik. Please read docs');
205
- }