@amedia/user 0.13.22 → 0.13.24

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@amedia/user",
3
- "version": "0.13.22",
3
+ "version": "0.13.24",
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)",
@@ -98,16 +98,115 @@ describe('Europa', () => {
98
98
  });
99
99
 
100
100
  describe('getNamespaces', () => {
101
- it('should return namespace', () => {
102
- const data = { euucp: { x: 'x' } };
103
- fetch.mockResponse(JSON.stringify({ namespaces: data }));
104
- expect(getNamespaces(['euucp'])).resolves.toEqual(data);
101
+ let response;
102
+ describe('with cold cache', () => {
103
+ describe('with one namespace', () => {
104
+ const data = { euucp: { x: 'x' } };
105
+ beforeEach(async () => {
106
+ fetch.mockResponse(JSON.stringify({ namespaces: data }));
107
+ response = await getNamespaces(['euucp']);
108
+ });
109
+ it('should return namespace', () => {
110
+ expect(response).toEqual(data);
111
+ });
112
+ });
113
+
114
+ describe('with multiple namespaces', () => {
115
+ const data = { euucp: { x: 'x' }, user_info: { a: 'a' } };
116
+ beforeEach(async () => {
117
+ fetch.mockResponse(JSON.stringify({ namespaces: data }));
118
+ response = await getNamespaces(['euucp', 'user_info']);
119
+ });
120
+ it('should return multiple namespaces', () => {
121
+ expect(response).toEqual(data);
122
+ });
123
+ });
124
+
125
+ describe('when doing sequence of calls for different namespaces', () => {
126
+ const data = { euucp: { x: 'x' } };
127
+ const data2 = { user_info: { a: 'a' } };
128
+ let response2;
129
+ beforeEach(async () => {
130
+ fetch.mockResponseOnce(JSON.stringify({ namespaces: data }));
131
+ fetch.mockResponseOnce(JSON.stringify({ namespaces: data2 }));
132
+ response = await getNamespaces(['euucp']);
133
+ response2 = await getNamespaces(['user_info']);
134
+ });
135
+ it('should return multiple namespaces', () => {
136
+ expect(response).toEqual(data);
137
+ expect(response2).toEqual(data2);
138
+ });
139
+ });
140
+
141
+ describe('when doing sequence of calls for different namespaces before first response returns', () => {
142
+ const data = { euucp: { x: 'x' } };
143
+ const data2 = { user_info: { a: 'a' } };
144
+ let response2;
145
+ beforeEach(async () => {
146
+ fetch.mockResponseOnce(JSON.stringify({ namespaces: data }));
147
+ fetch.mockResponseOnce(JSON.stringify({ namespaces: data2 }));
148
+ const futureEuucp = getNamespaces(['euucp']);
149
+ response2 = await getNamespaces(['user_info']);
150
+ response = await futureEuucp;
151
+ });
152
+ it('should return euucp in first response', () => {
153
+ expect(response).toEqual(data);
154
+ });
155
+ it('should return user_info in second response', () => {
156
+ expect(response2).toEqual(data2);
157
+ });
158
+ });
105
159
  });
106
160
 
107
- it('should return multiple namespaces', () => {
108
- const data = { euucp: { x: 'x' }, user_info: { a: 'a' } };
109
- fetch.mockResponse(JSON.stringify({ namespaces: data }));
110
- expect(getNamespaces(['euucp', 'user_info'])).resolves.toEqual(data);
161
+ describe('with one namespace in cache', () => {
162
+ const data = { euucp: { x: 'x' } };
163
+ beforeEach(() => {
164
+ cache.set(
165
+ 'europa',
166
+ {
167
+ namespaces: data,
168
+ },
169
+ 100000
170
+ );
171
+ });
172
+
173
+ describe('when requesting cached namespace', () => {
174
+ beforeEach(async () => {
175
+ response = await getNamespaces(['euucp']);
176
+ });
177
+ it('should return namespace', () => {
178
+ expect(response).toEqual(data);
179
+ });
180
+ });
181
+
182
+ describe('when requesting namespace not found in cache', () => {
183
+ const expectedData = {
184
+ privacy_preferences: { allow_research_usage: true },
185
+ };
186
+
187
+ beforeEach(async () => {
188
+ fetch.mockResponse(JSON.stringify({ namespaces: expectedData }));
189
+ response = await getNamespaces(['privacy_preferences']);
190
+ });
191
+ it('should return namespace from backend, not from cache', () => {
192
+ expect(response).toEqual(expectedData);
193
+ });
194
+ });
195
+
196
+ describe('when requesting multiple namespaces and only one found in cache', () => {
197
+ const expectedData = {
198
+ privacy_preferences: { allow_research_usage: true },
199
+ euucp: { x: 'x' },
200
+ };
201
+
202
+ beforeEach(async () => {
203
+ fetch.mockResponse(JSON.stringify({ namespaces: expectedData }));
204
+ response = await getNamespaces(['privacy_preferences', 'euucp']);
205
+ });
206
+ it('should return namespace from backend, not from cache', () => {
207
+ expect(response).toEqual(expectedData);
208
+ });
209
+ });
111
210
  });
112
211
 
113
212
  it('should require at least one namespace', () => {
@@ -20,15 +20,23 @@ const doFetch = (url: string, method: string = 'GET', body?: Object) =>
20
20
  },
21
21
  });
22
22
 
23
+ function mergeCache(cachedData, newData) {
24
+ return {
25
+ namespaces: Object.entries(newData.namespaces).reduce(
26
+ (mergedNamespaces, [namespace, data]) => ({
27
+ ...mergedNamespaces,
28
+ [namespace]: data,
29
+ }),
30
+ cachedData?.namespaces || {}
31
+ ),
32
+ };
33
+ }
34
+
23
35
  const updateNamespaceInCache = async (namespace: string, data: Object) => {
24
36
  const cachedData = await cache.get('europa', EuropaSchema);
25
- const updatedData = cachedData
26
- ? {
27
- ...cachedData,
28
- namespaces: { ...cachedData.namespaces, [namespace]: data },
29
- }
30
- : { namespaces: { [namespace]: data } };
31
-
37
+ const updatedData = mergeCache(cachedData, {
38
+ namespaces: { [namespace]: data },
39
+ });
32
40
  return cache.set('europa', updatedData, Date.now() + CACHE_DURATION);
33
41
  };
34
42
 
@@ -55,32 +63,58 @@ export const getNamespaces = async <
55
63
  throw new Error('namespaces array cannot be of length 0');
56
64
  }
57
65
 
58
- return getOrUpdate(
59
- 'europa',
60
- EuropaSchema,
61
- () =>
62
- doFetch(`/api/europa/v1/me/${namespaces.join(',')}`)
63
- .then(async (response) => {
64
- const body = await response.text();
65
- if (!response.ok) {
66
- // TODO we should possibly extend Europa to say what param we're missing so we can ignore missing aid.session cookie
67
- if (response.status !== 400) {
68
- log.error(
69
- `Could not get namespaces for: ${namespaces}, status: ${response.status}`,
70
- { response, body }
71
- );
72
- }
73
- return { namespaces: {} };
74
- }
75
- return JSON.parse(body);
76
- })
77
- .then((data) => ({ ttl: CACHE_DURATION, data })),
78
- (cacheData) =>
79
- !options.ignoreCache ||
80
- Object.keys(cacheData.namespaces).filter(
81
- (namespace) => !Object.keys(namespaces).includes(namespace)
82
- ).length === 0
83
- ).then((d) => d.namespaces as Pick<S, Namespaces[number]>);
66
+ let missingNamespaces = namespaces as string[];
67
+ const cachedData = await cache.get('europa', EuropaSchema);
68
+ if (!options.ignoreCache) {
69
+ missingNamespaces = (
70
+ cachedData
71
+ ? namespaces.filter(
72
+ (namespace) =>
73
+ !Object.keys(cachedData.namespaces).includes(namespace as string)
74
+ )
75
+ : namespaces
76
+ ) as string[];
77
+ if (cachedData && missingNamespaces.length === 0) {
78
+ // Yay! Everything we need is in the cache!
79
+ return Promise.resolve(
80
+ cachedData.namespaces as Pick<S, Namespaces[number]>
81
+ );
82
+ }
83
+ }
84
+
85
+ const fetchedNamespaces = await doFetch(
86
+ `/api/europa/v1/me/${missingNamespaces.join(',')}`
87
+ ).then(async (response) => {
88
+ const body = await response.text();
89
+ if (!response.ok) {
90
+ // TODO we should possibly extend Europa to say what param we're missing so we can ignore missing aid.session cookie
91
+ if (response.status !== 400) {
92
+ log.error(
93
+ `Could not get namespaces for: ${namespaces}, status: ${response.status}`,
94
+ { response, body }
95
+ );
96
+ }
97
+ return { namespaces: {} };
98
+ }
99
+ return JSON.parse(body);
100
+ });
101
+
102
+ Object.entries(fetchedNamespaces.namespaces).forEach(
103
+ ([namespace, fetchedNamespace]) =>
104
+ updateNamespaceInCache(namespace, fetchedNamespace as Object)
105
+ );
106
+
107
+ return Promise.resolve(
108
+ Object.entries(mergeCache(cachedData, fetchedNamespaces).namespaces)
109
+ .filter(([namespace]) => namespaces.includes(namespace))
110
+ .reduce(
111
+ (returnedEntries, [namespace, data]) => ({
112
+ ...returnedEntries,
113
+ [namespace]: data,
114
+ }),
115
+ {} as Pick<S, Namespaces[number]>
116
+ )
117
+ );
84
118
  };
85
119
 
86
120
  export const saveNamespace = (namespace: string, data: Object) =>
@@ -6,6 +6,7 @@ export class UserDeactivatedError extends Error {}
6
6
  const EXPECTED_ERRORS = [
7
7
  'no_session_found', // Not logged in
8
8
  'subscription_not_found', // No active subscription
9
+ 'publication_not_subscribable', // Activation is not possible on current publication
9
10
  ];
10
11
 
11
12
  export const activateSubscription = async (