@amedia/user 0.14.109 → 0.14.111

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 (3) hide show
  1. package/README.md +35 -6
  2. package/dist/user.d.ts +6 -1
  3. package/package.json +19 -19
package/README.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  The goal of this module is to simplify the usage of aID data and services in Amedia frontend code. The module is designed to be used several places on a webpage independently, and the module will ensure that requests from different parts of the webpage are handled in an efficient manner.
4
4
 
5
+ # TL;DR (details in the linked chapters)
6
+
7
+ - [UserDataRequest](#getting-user-data)<br/>
8
+ All current user data (including login state and access). Call it as much as you like, but only ask for what you actually need.
9
+ - [PaywallUnlockRequest](#unlocking-the-paywall)<br/>
10
+ When you need to actively verify login state and access. May perform redirects and expensive calls for subscription activation, and thus limited in how much it accepts to be called.
11
+ - [SiteAccessRequest](#activating-subscriptions--finding-upgrade-path)<br/>
12
+ When you need to actively verify access. Specify which access feature(s) you need. May attempt subscription activation.
13
+ - [User actions](#actions)<br/>
14
+ Login, logout, that sort of thing.
15
+
5
16
  # Usage
6
17
 
7
18
  Please be advised that you are required to use ESM in order to use this module.
@@ -65,7 +76,7 @@ NOTE2: If you need to know if the current user is an employee, you are encourage
65
76
 
66
77
  #### Storage
67
78
 
68
- Storage are namespaces stored in Europa. A namespace is a named set of key value pairs stored for a specific user. An example could be favourite_teams, which could contain team_id as key and timestamp it was stored as favourite as value.
79
+ Storage contains namespaces stored in Europa. A namespace is a named set of key value pairs stored for a specific user. An example could be favourite_teams, which could contain team_id as key and timestamp it was stored as favourite as value.
69
80
 
70
81
  Storage is requested by calling `.withStorage(namespaces: Array<string>)` on a `UserDataRequest`. If a namespace does not exist it will be "created" in that you'll get an empty object back. Saving data to it will perform the actual creation.
71
82
 
@@ -96,7 +107,7 @@ You access this as any old object, but it provides a save()-method for when to w
96
107
  import { UserDataRequest } from '@amedia/user';
97
108
 
98
109
  const { storage } = await new UserDataRequest()
99
- .withStorage('my_namespace')
110
+ .withStorage(['my_namespace'])
100
111
  .fetch();
101
112
  storage.my_namespace.my_key = 'my data';
102
113
  storage.my_namespace.save();
@@ -109,7 +120,7 @@ In this example, `my_key` in `my_namespace` would be updated to `my data` (and t
109
120
 
110
121
  #### State
111
122
 
112
- State is meant to describe the current context the user is in right now. It's not directly requested, but will always be a part of the response.
123
+ State is ment to describe the current context the user is in right now. It's not directly requested, but will always be a part of the response.
113
124
 
114
125
  Current attributes in `state`:
115
126
 
@@ -161,11 +172,13 @@ The callback you supplied to subscribe is called whenever there are any changes
161
172
 
162
173
  Also, your requested properties might not arrive all at once. This callback will be called whenever we get data in from the various services.
163
174
 
175
+ You should check the state object for potential emergency modes and act accordingly.
176
+
164
177
  Should you wish to end your subscription, just call the returned function from subscribe.
165
178
 
166
179
  #### Fetching all the data once (no re-renders)
167
180
 
168
- In many use cases it's more convenient to just wait until everything is ready, so you don't have to handle multiple callbacks. In these cases it's easier to call `.fetch()`. This method will return a `Promise` that resolves the same object you would get in the `subscribe` callback.
181
+ In many use-cases, it's more convenient to just wait until everything is ready, so you don't have to handle multiple callbacks. In these cases, it's easier to call `.fetch()`. This method will return a `Promise` that resolves the same object you would get in the `subscribe` callback.
169
182
 
170
183
  Full usage example:
171
184
 
@@ -217,16 +230,32 @@ new UserDataRequest()
217
230
 
218
231
  This may also be lowered below the default, but network request do take some time, even on fast networks, so don't shoot your app in the foot by lowering it too much.
219
232
 
220
- ##### Emergency mode
233
+ ##### Emergency mode when using fetch
221
234
 
222
235
  To avoid hanging on timeout, fetch rejects with `EmergencyModeError`, when a relevant emergency mode is active.
223
236
  If you receive this error, there will be no user data available.
224
237
  Your app should act accordingly, eg:
225
238
 
226
- - Render non personalized
239
+ - Render non-personalized
227
240
  - Show placeholder with an error message
228
241
  - Not rendering at all
229
242
 
243
+ ```ts
244
+ new UserDataRequest()
245
+ .withAttributes(['name'])
246
+ .withStorage(['my_namespace'])
247
+ .fetch()
248
+ .catch((e) => {
249
+ if (e instanceof FetchTimeoutError) {
250
+ console.log(e.partialData);
251
+ // Maybe you're able to render your app in a reduced state (using partialData).
252
+ } else if (e instanceOf EmergencyModeError) {
253
+ console.log(e.activeEmergencyModes);
254
+ // Maybe you're able to render your app in a non-personalized way?
255
+ }
256
+ });
257
+ ```
258
+
230
259
  ## Actions
231
260
 
232
261
  ### `getLoginUrl({requestedUrl: string context: string}): string`
package/dist/user.d.ts CHANGED
@@ -127,6 +127,11 @@ declare class EmergencyModeError extends Error {
127
127
  constructor(message?: string);
128
128
  }
129
129
 
130
+ declare class ValidationError extends Error {
131
+ readonly code: string;
132
+ constructor(code: string, message: string);
133
+ }
134
+
130
135
  /** @deprecated Should be deleted when removing saveStorage and updateStorage */
131
136
  type Storage = Record<string, Record<string, string>>;
132
137
  type LoginPageParams = {
@@ -160,4 +165,4 @@ declare const aidUrls: {
160
165
  sites: URL;
161
166
  };
162
167
 
163
- export { EmergencyModeError, FetchTimeoutError, PaywallError, PaywallUnlockRequest, PaywallUnlockResponse, PrivacyPreferences, SiteAccessRequest, SiteAccessResponse, State, UserAttributes, UserDataRequest, aidUrls, getLoginUrl, goToLoginPage, logout, requestDataRefresh, saveStorage, updateStorage };
168
+ export { EmergencyModeError, FetchTimeoutError, PaywallError, PaywallUnlockRequest, type PaywallUnlockResponse, type PrivacyPreferences, SiteAccessRequest, type SiteAccessResponse, type State, type UserAttributes, UserDataRequest, ValidationError, aidUrls, getLoginUrl, goToLoginPage, logout, requestDataRefresh, saveStorage, updateStorage };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amedia/user",
3
- "version": "0.14.109",
3
+ "version": "0.14.111",
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)",
@@ -46,30 +46,30 @@
46
46
  "@amedia/browserid": "^1.0.17",
47
47
  "@amedia/frontend-rpc": "^0.0.12",
48
48
  "runtypes": "^6.7.0",
49
- "ts-deepmerge": "^6.0.3"
49
+ "ts-deepmerge": "^6.2.0"
50
50
  },
51
51
  "devDependencies": {
52
- "@amedia/kragl": "^2.0.0",
53
- "@changesets/changelog-github": "^0.4.8",
54
- "@changesets/cli": "^2.26.1",
55
- "@eik/esbuild-plugin": "^1.1.34",
56
- "@types/jest": "^29.5.2",
57
- "@types/node": "^20.3.1",
58
- "concurrently": "^8.2.0",
52
+ "@amedia/kragl": "^2.0.4",
53
+ "@amedia/prettier-config": "^1.0.5",
54
+ "@changesets/changelog-github": "^0.5.0",
55
+ "@changesets/cli": "^2.27.1",
56
+ "@eik/esbuild-plugin": "^1.1.45",
57
+ "@types/jest": "^29.5.11",
58
+ "@types/node": "^20.10.6",
59
+ "concurrently": "^8.2.2",
59
60
  "cors": "^2.8.5",
60
- "esbuild": "0.18.2",
61
+ "esbuild": "0.19.11",
61
62
  "express": "^4.18.2",
62
- "jest": "^29.5.0",
63
- "jest-environment-jsdom": "^29.5.0",
64
- "jest-extended": "^4.0.0",
63
+ "jest": "^29.7.0",
64
+ "jest-environment-jsdom": "^29.7.0",
65
+ "jest-extended": "^4.0.2",
65
66
  "jest-fetch-mock": "^3.0.3",
66
67
  "mockdate": "^3.0.5",
67
68
  "npm-run-all": "^4.1.5",
68
- "prettier": "^2.8.8",
69
- "rollup": "3.25.1",
70
- "rollup-plugin-dts": "5.3.0",
71
- "ts-jest": "^29.1.0",
72
- "tslib": "^2.5.3",
73
- "typescript": "^5.1.3"
69
+ "rollup": "4.9.2",
70
+ "rollup-plugin-dts": "6.1.0",
71
+ "ts-jest": "^29.1.1",
72
+ "tslib": "^2.6.2",
73
+ "typescript": "^5.3.3"
74
74
  }
75
75
  }