@algolia/client-personalization 5.0.0-alpha.10 → 5.0.0-alpha.101
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/dist/builds/browser.d.ts +5 -5
- package/dist/builds/browser.d.ts.map +1 -1
- package/dist/builds/node.d.ts +5 -5
- package/dist/builds/node.d.ts.map +1 -1
- package/dist/{client-personalization.cjs.js → client-personalization.cjs} +287 -285
- package/dist/client-personalization.esm.browser.js +499 -521
- package/dist/client-personalization.esm.node.js +287 -283
- package/dist/client-personalization.umd.js +2 -2
- package/dist/model/clientMethodProps.d.ts +78 -78
- package/dist/model/clientMethodProps.d.ts.map +1 -1
- package/dist/model/deleteUserProfileResponse.d.ts +10 -10
- package/dist/model/deleteUserProfileResponse.d.ts.map +1 -1
- package/dist/model/errorBase.d.ts +6 -6
- package/dist/model/errorBase.d.ts.map +1 -1
- package/dist/model/eventScoring.d.ts +14 -14
- package/dist/model/eventScoring.d.ts.map +1 -1
- package/dist/model/facetScoring.d.ts +10 -10
- package/dist/model/facetScoring.d.ts.map +1 -1
- package/dist/model/getUserTokenResponse.d.ts +14 -14
- package/dist/model/getUserTokenResponse.d.ts.map +1 -1
- package/dist/model/index.d.ts +8 -8
- package/dist/model/personalizationStrategyParams.d.ts +16 -16
- package/dist/model/personalizationStrategyParams.d.ts.map +1 -1
- package/dist/model/setPersonalizationStrategyResponse.d.ts +6 -6
- package/dist/model/setPersonalizationStrategyResponse.d.ts.map +1 -1
- package/dist/src/personalizationClient.d.ts +116 -112
- package/dist/src/personalizationClient.d.ts.map +1 -1
- package/index.js +1 -1
- package/model/clientMethodProps.ts +36 -36
- package/model/deleteUserProfileResponse.ts +1 -1
- package/model/errorBase.ts +1 -1
- package/model/eventScoring.ts +1 -1
- package/model/facetScoring.ts +1 -1
- package/model/getUserTokenResponse.ts +1 -1
- package/model/index.ts +1 -1
- package/model/personalizationStrategyParams.ts +1 -1
- package/model/setPersonalizationStrategyResponse.ts +1 -1
- package/package.json +30 -13
|
@@ -7,53 +7,69 @@ function createAuth(appId, apiKey, authMode = 'WithinHeaders') {
|
|
|
7
7
|
headers() {
|
|
8
8
|
return authMode === 'WithinHeaders' ? credentials : {};
|
|
9
9
|
},
|
|
10
|
-
|
|
11
10
|
queryParameters() {
|
|
12
11
|
return authMode === 'WithinQueryParameters' ? credentials : {};
|
|
13
12
|
}
|
|
14
|
-
|
|
15
13
|
};
|
|
16
14
|
}
|
|
17
15
|
|
|
18
16
|
function createBrowserLocalStorageCache(options) {
|
|
19
|
-
let storage;
|
|
20
|
-
|
|
17
|
+
let storage;
|
|
18
|
+
// We've changed the namespace to avoid conflicts with v4, as this version is a huge breaking change
|
|
21
19
|
const namespaceKey = `algolia-client-js-${options.key}`;
|
|
22
|
-
|
|
23
20
|
function getStorage() {
|
|
24
21
|
if (storage === undefined) {
|
|
25
22
|
storage = options.localStorage || window.localStorage;
|
|
26
23
|
}
|
|
27
|
-
|
|
28
24
|
return storage;
|
|
29
25
|
}
|
|
30
|
-
|
|
31
26
|
function getNamespace() {
|
|
32
27
|
return JSON.parse(getStorage().getItem(namespaceKey) || '{}');
|
|
33
28
|
}
|
|
34
|
-
|
|
29
|
+
function setNamespace(namespace) {
|
|
30
|
+
getStorage().setItem(namespaceKey, JSON.stringify(namespace));
|
|
31
|
+
}
|
|
32
|
+
function removeOutdatedCacheItems() {
|
|
33
|
+
const timeToLive = options.timeToLive ? options.timeToLive * 1000 : null;
|
|
34
|
+
const namespace = getNamespace();
|
|
35
|
+
const filteredNamespaceWithoutOldFormattedCacheItems = Object.fromEntries(Object.entries(namespace).filter(([, cacheItem]) => {
|
|
36
|
+
return cacheItem.timestamp !== undefined;
|
|
37
|
+
}));
|
|
38
|
+
setNamespace(filteredNamespaceWithoutOldFormattedCacheItems);
|
|
39
|
+
if (!timeToLive) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const filteredNamespaceWithoutExpiredItems = Object.fromEntries(Object.entries(filteredNamespaceWithoutOldFormattedCacheItems).filter(([, cacheItem]) => {
|
|
43
|
+
const currentTimestamp = new Date().getTime();
|
|
44
|
+
const isExpired = cacheItem.timestamp + timeToLive < currentTimestamp;
|
|
45
|
+
return !isExpired;
|
|
46
|
+
}));
|
|
47
|
+
setNamespace(filteredNamespaceWithoutExpiredItems);
|
|
48
|
+
}
|
|
35
49
|
return {
|
|
36
50
|
get(key, defaultValue, events = {
|
|
37
51
|
miss: () => Promise.resolve()
|
|
38
52
|
}) {
|
|
39
53
|
return Promise.resolve().then(() => {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
54
|
+
removeOutdatedCacheItems();
|
|
55
|
+
return getNamespace()[JSON.stringify(key)];
|
|
56
|
+
}).then(value => {
|
|
57
|
+
return Promise.all([value ? value.value : defaultValue(), value !== undefined]);
|
|
43
58
|
}).then(([value, exists]) => {
|
|
44
59
|
return Promise.all([value, exists || events.miss(value)]);
|
|
45
60
|
}).then(([value]) => value);
|
|
46
61
|
},
|
|
47
|
-
|
|
48
62
|
set(key, value) {
|
|
49
63
|
return Promise.resolve().then(() => {
|
|
50
64
|
const namespace = getNamespace();
|
|
51
|
-
namespace[JSON.stringify(key)] =
|
|
65
|
+
namespace[JSON.stringify(key)] = {
|
|
66
|
+
timestamp: new Date().getTime(),
|
|
67
|
+
value
|
|
68
|
+
};
|
|
52
69
|
getStorage().setItem(namespaceKey, JSON.stringify(namespace));
|
|
53
70
|
return value;
|
|
54
71
|
});
|
|
55
72
|
},
|
|
56
|
-
|
|
57
73
|
delete(key) {
|
|
58
74
|
return Promise.resolve().then(() => {
|
|
59
75
|
const namespace = getNamespace();
|
|
@@ -61,13 +77,11 @@ function createBrowserLocalStorageCache(options) {
|
|
|
61
77
|
getStorage().setItem(namespaceKey, JSON.stringify(namespace));
|
|
62
78
|
});
|
|
63
79
|
},
|
|
64
|
-
|
|
65
80
|
clear() {
|
|
66
81
|
return Promise.resolve().then(() => {
|
|
67
82
|
getStorage().removeItem(namespaceKey);
|
|
68
83
|
});
|
|
69
84
|
}
|
|
70
|
-
|
|
71
85
|
};
|
|
72
86
|
}
|
|
73
87
|
|
|
@@ -79,30 +93,24 @@ function createNullCache() {
|
|
|
79
93
|
const value = defaultValue();
|
|
80
94
|
return value.then(result => Promise.all([result, events.miss(result)])).then(([result]) => result);
|
|
81
95
|
},
|
|
82
|
-
|
|
83
96
|
set(_key, value) {
|
|
84
97
|
return Promise.resolve(value);
|
|
85
98
|
},
|
|
86
|
-
|
|
87
99
|
delete(_key) {
|
|
88
100
|
return Promise.resolve();
|
|
89
101
|
},
|
|
90
|
-
|
|
91
102
|
clear() {
|
|
92
103
|
return Promise.resolve();
|
|
93
104
|
}
|
|
94
|
-
|
|
95
105
|
};
|
|
96
106
|
}
|
|
97
107
|
|
|
98
108
|
function createFallbackableCache(options) {
|
|
99
109
|
const caches = [...options.caches];
|
|
100
110
|
const current = caches.shift();
|
|
101
|
-
|
|
102
111
|
if (current === undefined) {
|
|
103
112
|
return createNullCache();
|
|
104
113
|
}
|
|
105
|
-
|
|
106
114
|
return {
|
|
107
115
|
get(key, defaultValue, events = {
|
|
108
116
|
miss: () => Promise.resolve()
|
|
@@ -113,7 +121,6 @@ function createFallbackableCache(options) {
|
|
|
113
121
|
}).get(key, defaultValue, events);
|
|
114
122
|
});
|
|
115
123
|
},
|
|
116
|
-
|
|
117
124
|
set(key, value) {
|
|
118
125
|
return current.set(key, value).catch(() => {
|
|
119
126
|
return createFallbackableCache({
|
|
@@ -121,7 +128,6 @@ function createFallbackableCache(options) {
|
|
|
121
128
|
}).set(key, value);
|
|
122
129
|
});
|
|
123
130
|
},
|
|
124
|
-
|
|
125
131
|
delete(key) {
|
|
126
132
|
return current.delete(key).catch(() => {
|
|
127
133
|
return createFallbackableCache({
|
|
@@ -129,7 +135,6 @@ function createFallbackableCache(options) {
|
|
|
129
135
|
}).delete(key);
|
|
130
136
|
});
|
|
131
137
|
},
|
|
132
|
-
|
|
133
138
|
clear() {
|
|
134
139
|
return current.clear().catch(() => {
|
|
135
140
|
return createFallbackableCache({
|
|
@@ -137,7 +142,6 @@ function createFallbackableCache(options) {
|
|
|
137
142
|
}).clear();
|
|
138
143
|
});
|
|
139
144
|
}
|
|
140
|
-
|
|
141
145
|
};
|
|
142
146
|
}
|
|
143
147
|
|
|
@@ -150,30 +154,24 @@ function createMemoryCache(options = {
|
|
|
150
154
|
miss: () => Promise.resolve()
|
|
151
155
|
}) {
|
|
152
156
|
const keyAsString = JSON.stringify(key);
|
|
153
|
-
|
|
154
157
|
if (keyAsString in cache) {
|
|
155
158
|
return Promise.resolve(options.serializable ? JSON.parse(cache[keyAsString]) : cache[keyAsString]);
|
|
156
159
|
}
|
|
157
|
-
|
|
158
160
|
const promise = defaultValue();
|
|
159
161
|
return promise.then(value => events.miss(value)).then(() => promise);
|
|
160
162
|
},
|
|
161
|
-
|
|
162
163
|
set(key, value) {
|
|
163
164
|
cache[JSON.stringify(key)] = options.serializable ? JSON.stringify(value) : value;
|
|
164
165
|
return Promise.resolve(value);
|
|
165
166
|
},
|
|
166
|
-
|
|
167
167
|
delete(key) {
|
|
168
168
|
delete cache[JSON.stringify(key)];
|
|
169
169
|
return Promise.resolve();
|
|
170
170
|
},
|
|
171
|
-
|
|
172
171
|
clear() {
|
|
173
172
|
cache = {};
|
|
174
173
|
return Promise.resolve();
|
|
175
174
|
}
|
|
176
|
-
|
|
177
175
|
};
|
|
178
176
|
}
|
|
179
177
|
|
|
@@ -182,16 +180,14 @@ function createMemoryCache(options = {
|
|
|
182
180
|
const EXPIRATION_DELAY = 2 * 60 * 1000;
|
|
183
181
|
function createStatefulHost(host, status = 'up') {
|
|
184
182
|
const lastUpdate = Date.now();
|
|
185
|
-
|
|
186
183
|
function isUp() {
|
|
187
184
|
return status === 'up' || Date.now() - lastUpdate > EXPIRATION_DELAY;
|
|
188
185
|
}
|
|
189
|
-
|
|
190
186
|
function isTimedOut() {
|
|
191
187
|
return status === 'timed out' && Date.now() - lastUpdate <= EXPIRATION_DELAY;
|
|
192
188
|
}
|
|
193
|
-
|
|
194
|
-
|
|
189
|
+
return {
|
|
190
|
+
...host,
|
|
195
191
|
status,
|
|
196
192
|
lastUpdate,
|
|
197
193
|
isUp,
|
|
@@ -199,7 +195,22 @@ function createStatefulHost(host, status = 'up') {
|
|
|
199
195
|
};
|
|
200
196
|
}
|
|
201
197
|
|
|
198
|
+
function _toPrimitive(t, r) {
|
|
199
|
+
if ("object" != typeof t || !t) return t;
|
|
200
|
+
var e = t[Symbol.toPrimitive];
|
|
201
|
+
if (void 0 !== e) {
|
|
202
|
+
var i = e.call(t, r || "default");
|
|
203
|
+
if ("object" != typeof i) return i;
|
|
204
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
205
|
+
}
|
|
206
|
+
return ("string" === r ? String : Number)(t);
|
|
207
|
+
}
|
|
208
|
+
function _toPropertyKey(t) {
|
|
209
|
+
var i = _toPrimitive(t, "string");
|
|
210
|
+
return "symbol" == typeof i ? i : String(i);
|
|
211
|
+
}
|
|
202
212
|
function _defineProperty(obj, key, value) {
|
|
213
|
+
key = _toPropertyKey(key);
|
|
203
214
|
if (key in obj) {
|
|
204
215
|
Object.defineProperty(obj, key, {
|
|
205
216
|
value: value,
|
|
@@ -210,79 +221,71 @@ function _defineProperty(obj, key, value) {
|
|
|
210
221
|
} else {
|
|
211
222
|
obj[key] = value;
|
|
212
223
|
}
|
|
213
|
-
|
|
214
224
|
return obj;
|
|
215
225
|
}
|
|
216
226
|
|
|
217
227
|
class AlgoliaError extends Error {
|
|
218
228
|
constructor(message, name) {
|
|
219
229
|
super(message);
|
|
220
|
-
|
|
221
230
|
_defineProperty(this, "name", 'AlgoliaError');
|
|
222
|
-
|
|
223
231
|
if (name) {
|
|
224
232
|
this.name = name;
|
|
225
233
|
}
|
|
226
234
|
}
|
|
227
|
-
|
|
228
235
|
}
|
|
229
236
|
class ErrorWithStackTrace extends AlgoliaError {
|
|
230
237
|
constructor(message, stackTrace, name) {
|
|
231
|
-
super(message, name);
|
|
232
|
-
|
|
238
|
+
super(message, name);
|
|
239
|
+
// the array and object should be frozen to reflect the stackTrace at the time of the error
|
|
233
240
|
_defineProperty(this, "stackTrace", void 0);
|
|
234
|
-
|
|
235
241
|
this.stackTrace = stackTrace;
|
|
236
242
|
}
|
|
237
|
-
|
|
238
243
|
}
|
|
239
244
|
class RetryError extends ErrorWithStackTrace {
|
|
240
245
|
constructor(stackTrace) {
|
|
241
|
-
super('Unreachable hosts - your application id may be incorrect. If the error persists,
|
|
246
|
+
super('Unreachable hosts - your application id may be incorrect. If the error persists, please create a ticket at https://support.algolia.com/ sharing steps we can use to reproduce the issue.', stackTrace, 'RetryError');
|
|
242
247
|
}
|
|
243
|
-
|
|
244
248
|
}
|
|
245
249
|
class ApiError extends ErrorWithStackTrace {
|
|
246
|
-
constructor(message, status, stackTrace) {
|
|
247
|
-
super(message, stackTrace,
|
|
248
|
-
|
|
250
|
+
constructor(message, status, stackTrace, name = 'ApiError') {
|
|
251
|
+
super(message, stackTrace, name);
|
|
249
252
|
_defineProperty(this, "status", void 0);
|
|
250
|
-
|
|
251
253
|
this.status = status;
|
|
252
254
|
}
|
|
253
|
-
|
|
254
255
|
}
|
|
255
256
|
class DeserializationError extends AlgoliaError {
|
|
256
257
|
constructor(message, response) {
|
|
257
258
|
super(message, 'DeserializationError');
|
|
258
|
-
|
|
259
259
|
_defineProperty(this, "response", void 0);
|
|
260
|
-
|
|
261
260
|
this.response = response;
|
|
262
261
|
}
|
|
263
|
-
|
|
262
|
+
}
|
|
263
|
+
// DetailedApiError is only used by the ingestion client to return more informative error, other clients will use ApiClient.
|
|
264
|
+
class DetailedApiError extends ApiError {
|
|
265
|
+
constructor(message, status, error, stackTrace) {
|
|
266
|
+
super(message, status, stackTrace, 'DetailedApiError');
|
|
267
|
+
_defineProperty(this, "error", void 0);
|
|
268
|
+
this.error = error;
|
|
269
|
+
}
|
|
264
270
|
}
|
|
265
271
|
function serializeUrl(host, path, queryParameters) {
|
|
266
272
|
const queryParametersAsString = serializeQueryParameters(queryParameters);
|
|
267
|
-
let url = `${host.protocol}://${host.url}/${path.charAt(0) === '/' ? path.substr(1) : path}`;
|
|
268
|
-
|
|
273
|
+
let url = `${host.protocol}://${host.url}${host.port ? `:${host.port}` : ''}/${path.charAt(0) === '/' ? path.substr(1) : path}`;
|
|
269
274
|
if (queryParametersAsString.length) {
|
|
270
275
|
url += `?${queryParametersAsString}`;
|
|
271
276
|
}
|
|
272
|
-
|
|
273
277
|
return url;
|
|
274
278
|
}
|
|
275
279
|
function serializeQueryParameters(parameters) {
|
|
276
280
|
const isObjectOrArray = value => Object.prototype.toString.call(value) === '[object Object]' || Object.prototype.toString.call(value) === '[object Array]';
|
|
277
|
-
|
|
278
|
-
return Object.keys(parameters).map(key => `${key}=${isObjectOrArray(parameters[key]) ? JSON.stringify(parameters[key]) : parameters[key]}`).join('&');
|
|
281
|
+
return Object.keys(parameters).map(key => `${key}=${encodeURIComponent(isObjectOrArray(parameters[key]) ? JSON.stringify(parameters[key]) : parameters[key]).replaceAll('+', '%20')}`).join('&');
|
|
279
282
|
}
|
|
280
283
|
function serializeData(request, requestOptions) {
|
|
281
284
|
if (request.method === 'GET' || request.data === undefined && requestOptions.data === undefined) {
|
|
282
285
|
return undefined;
|
|
283
286
|
}
|
|
284
|
-
|
|
285
|
-
|
|
287
|
+
const data = Array.isArray(request.data) ? request.data : {
|
|
288
|
+
...request.data,
|
|
286
289
|
...requestOptions.data
|
|
287
290
|
};
|
|
288
291
|
return JSON.stringify(data);
|
|
@@ -312,14 +315,16 @@ function deserializeFailure({
|
|
|
312
315
|
content,
|
|
313
316
|
status
|
|
314
317
|
}, stackFrame) {
|
|
315
|
-
let message = content;
|
|
316
|
-
|
|
317
318
|
try {
|
|
318
|
-
|
|
319
|
-
|
|
319
|
+
const parsed = JSON.parse(content);
|
|
320
|
+
if ('error' in parsed) {
|
|
321
|
+
return new DetailedApiError(parsed.message, status, parsed.error, stackFrame);
|
|
322
|
+
}
|
|
323
|
+
return new ApiError(parsed.message, status, stackFrame);
|
|
324
|
+
} catch (e) {
|
|
325
|
+
// ..
|
|
320
326
|
}
|
|
321
|
-
|
|
322
|
-
return new ApiError(message, status, stackFrame);
|
|
327
|
+
return new ApiError(content, status, stackFrame);
|
|
323
328
|
}
|
|
324
329
|
|
|
325
330
|
function isNetworkError({
|
|
@@ -350,9 +355,12 @@ function stackFrameWithoutCredentials(stackFrame) {
|
|
|
350
355
|
const modifiedHeaders = stackFrame.request.headers['x-algolia-api-key'] ? {
|
|
351
356
|
'x-algolia-api-key': '*****'
|
|
352
357
|
} : {};
|
|
353
|
-
return {
|
|
354
|
-
|
|
355
|
-
|
|
358
|
+
return {
|
|
359
|
+
...stackFrame,
|
|
360
|
+
request: {
|
|
361
|
+
...stackFrame.request,
|
|
362
|
+
headers: {
|
|
363
|
+
...stackFrame.request.headers,
|
|
356
364
|
...modifiedHeaders
|
|
357
365
|
}
|
|
358
366
|
}
|
|
@@ -377,53 +385,49 @@ function createTransporter({
|
|
|
377
385
|
});
|
|
378
386
|
}));
|
|
379
387
|
const hostsUp = statefulHosts.filter(host => host.isUp());
|
|
380
|
-
const hostsTimedOut = statefulHosts.filter(host => host.isTimedOut());
|
|
381
|
-
|
|
388
|
+
const hostsTimedOut = statefulHosts.filter(host => host.isTimedOut());
|
|
389
|
+
// Note, we put the hosts that previously timed out on the end of the list.
|
|
382
390
|
const hostsAvailable = [...hostsUp, ...hostsTimedOut];
|
|
383
391
|
const compatibleHostsAvailable = hostsAvailable.length > 0 ? hostsAvailable : compatibleHosts;
|
|
384
392
|
return {
|
|
385
393
|
hosts: compatibleHostsAvailable,
|
|
386
|
-
|
|
387
394
|
getTimeout(timeoutsCount, baseTimeout) {
|
|
388
|
-
/**
|
|
389
|
-
* Imagine that you have 4 hosts, if timeouts will increase
|
|
390
|
-
* on the following way: 1 (timed out) > 4 (timed out) > 5 (200).
|
|
391
|
-
*
|
|
392
|
-
* Note that, the very next request, we start from the previous timeout.
|
|
393
|
-
*
|
|
394
|
-
* 5 (timed out) > 6 (timed out) > 7 ...
|
|
395
|
-
*
|
|
396
|
-
* This strategy may need to be reviewed, but is the strategy on the our
|
|
397
|
-
* current v3 version.
|
|
395
|
+
/**
|
|
396
|
+
* Imagine that you have 4 hosts, if timeouts will increase
|
|
397
|
+
* on the following way: 1 (timed out) > 4 (timed out) > 5 (200).
|
|
398
|
+
*
|
|
399
|
+
* Note that, the very next request, we start from the previous timeout.
|
|
400
|
+
*
|
|
401
|
+
* 5 (timed out) > 6 (timed out) > 7 ...
|
|
402
|
+
*
|
|
403
|
+
* This strategy may need to be reviewed, but is the strategy on the our
|
|
404
|
+
* current v3 version.
|
|
398
405
|
*/
|
|
399
406
|
const timeoutMultiplier = hostsTimedOut.length === 0 && timeoutsCount === 0 ? 1 : hostsTimedOut.length + 3 + timeoutsCount;
|
|
400
407
|
return timeoutMultiplier * baseTimeout;
|
|
401
408
|
}
|
|
402
|
-
|
|
403
409
|
};
|
|
404
410
|
}
|
|
405
|
-
|
|
406
411
|
async function retryableRequest(request, requestOptions, isRead = true) {
|
|
407
412
|
const stackTrace = [];
|
|
408
|
-
/**
|
|
409
|
-
* First we prepare the payload that do not depend from hosts.
|
|
413
|
+
/**
|
|
414
|
+
* First we prepare the payload that do not depend from hosts.
|
|
410
415
|
*/
|
|
411
|
-
|
|
412
416
|
const data = serializeData(request, requestOptions);
|
|
413
|
-
const headers = serializeHeaders(baseHeaders, request.headers, requestOptions.headers);
|
|
414
|
-
|
|
415
|
-
const dataQueryParameters = request.method === 'GET' ? {
|
|
417
|
+
const headers = serializeHeaders(baseHeaders, request.headers, requestOptions.headers);
|
|
418
|
+
// On `GET`, the data is proxied to query parameters.
|
|
419
|
+
const dataQueryParameters = request.method === 'GET' ? {
|
|
420
|
+
...request.data,
|
|
416
421
|
...requestOptions.data
|
|
417
422
|
} : {};
|
|
418
|
-
const queryParameters = {
|
|
423
|
+
const queryParameters = {
|
|
424
|
+
...baseQueryParameters,
|
|
419
425
|
...request.queryParameters,
|
|
420
426
|
...dataQueryParameters
|
|
421
427
|
};
|
|
422
|
-
|
|
423
428
|
if (algoliaAgent.value) {
|
|
424
429
|
queryParameters['x-algolia-agent'] = algoliaAgent.value;
|
|
425
430
|
}
|
|
426
|
-
|
|
427
431
|
if (requestOptions && requestOptions.queryParameters) {
|
|
428
432
|
for (const key of Object.keys(requestOptions.queryParameters)) {
|
|
429
433
|
// We want to keep `undefined` and `null` values,
|
|
@@ -436,25 +440,19 @@ function createTransporter({
|
|
|
436
440
|
}
|
|
437
441
|
}
|
|
438
442
|
}
|
|
439
|
-
|
|
440
443
|
let timeoutsCount = 0;
|
|
441
|
-
|
|
442
444
|
const retry = async (retryableHosts, getTimeout) => {
|
|
443
|
-
/**
|
|
444
|
-
* We iterate on each host, until there is no host left.
|
|
445
|
+
/**
|
|
446
|
+
* We iterate on each host, until there is no host left.
|
|
445
447
|
*/
|
|
446
448
|
const host = retryableHosts.pop();
|
|
447
|
-
|
|
448
449
|
if (host === undefined) {
|
|
449
450
|
throw new RetryError(stackTraceWithoutCredentials(stackTrace));
|
|
450
451
|
}
|
|
451
|
-
|
|
452
452
|
let responseTimeout = requestOptions.timeout;
|
|
453
|
-
|
|
454
453
|
if (responseTimeout === undefined) {
|
|
455
454
|
responseTimeout = isRead ? timeouts.read : timeouts.write;
|
|
456
455
|
}
|
|
457
|
-
|
|
458
456
|
const payload = {
|
|
459
457
|
data,
|
|
460
458
|
headers,
|
|
@@ -463,12 +461,11 @@ function createTransporter({
|
|
|
463
461
|
connectTimeout: getTimeout(timeoutsCount, timeouts.connect),
|
|
464
462
|
responseTimeout: getTimeout(timeoutsCount, responseTimeout)
|
|
465
463
|
};
|
|
466
|
-
/**
|
|
467
|
-
* The stackFrame is pushed to the stackTrace so we
|
|
468
|
-
* can have information about onRetry and onFailure
|
|
469
|
-
* decisions.
|
|
464
|
+
/**
|
|
465
|
+
* The stackFrame is pushed to the stackTrace so we
|
|
466
|
+
* can have information about onRetry and onFailure
|
|
467
|
+
* decisions.
|
|
470
468
|
*/
|
|
471
|
-
|
|
472
469
|
const pushToStackTrace = response => {
|
|
473
470
|
const stackFrame = {
|
|
474
471
|
request: payload,
|
|
@@ -479,102 +476,85 @@ function createTransporter({
|
|
|
479
476
|
stackTrace.push(stackFrame);
|
|
480
477
|
return stackFrame;
|
|
481
478
|
};
|
|
482
|
-
|
|
483
479
|
const response = await requester.send(payload);
|
|
484
|
-
|
|
485
480
|
if (isRetryable(response)) {
|
|
486
|
-
const stackFrame = pushToStackTrace(response);
|
|
487
|
-
|
|
481
|
+
const stackFrame = pushToStackTrace(response);
|
|
482
|
+
// If response is a timeout, we increase the number of timeouts so we can increase the timeout later.
|
|
488
483
|
if (response.isTimedOut) {
|
|
489
484
|
timeoutsCount++;
|
|
490
485
|
}
|
|
491
|
-
/**
|
|
492
|
-
* Failures are individually sent to the logger, allowing
|
|
493
|
-
* the end user to debug / store stack frames even
|
|
494
|
-
* when a retry error does not happen.
|
|
486
|
+
/**
|
|
487
|
+
* Failures are individually sent to the logger, allowing
|
|
488
|
+
* the end user to debug / store stack frames even
|
|
489
|
+
* when a retry error does not happen.
|
|
495
490
|
*/
|
|
496
491
|
// eslint-disable-next-line no-console -- this will be fixed by exposing a `logger` to the transporter
|
|
497
|
-
|
|
498
|
-
|
|
499
492
|
console.log('Retryable failure', stackFrameWithoutCredentials(stackFrame));
|
|
500
|
-
/**
|
|
501
|
-
* We also store the state of the host in failure cases. If the host, is
|
|
502
|
-
* down it will remain down for the next 2 minutes. In a timeout situation,
|
|
503
|
-
* this host will be added end of the list of hosts on the next request.
|
|
493
|
+
/**
|
|
494
|
+
* We also store the state of the host in failure cases. If the host, is
|
|
495
|
+
* down it will remain down for the next 2 minutes. In a timeout situation,
|
|
496
|
+
* this host will be added end of the list of hosts on the next request.
|
|
504
497
|
*/
|
|
505
|
-
|
|
506
498
|
await hostsCache.set(host, createStatefulHost(host, response.isTimedOut ? 'timed out' : 'down'));
|
|
507
499
|
return retry(retryableHosts, getTimeout);
|
|
508
500
|
}
|
|
509
|
-
|
|
510
501
|
if (isSuccess(response)) {
|
|
511
502
|
return deserializeSuccess(response);
|
|
512
503
|
}
|
|
513
|
-
|
|
514
504
|
pushToStackTrace(response);
|
|
515
505
|
throw deserializeFailure(response, stackTrace);
|
|
516
506
|
};
|
|
517
|
-
/**
|
|
518
|
-
* Finally, for each retryable host perform request until we got a non
|
|
519
|
-
* retryable response. Some notes here:
|
|
520
|
-
*
|
|
521
|
-
* 1. The reverse here is applied so we can apply a `pop` later on => more performant.
|
|
522
|
-
* 2. We also get from the retryable options a timeout multiplier that is tailored
|
|
523
|
-
* for the current context.
|
|
507
|
+
/**
|
|
508
|
+
* Finally, for each retryable host perform request until we got a non
|
|
509
|
+
* retryable response. Some notes here:
|
|
510
|
+
*
|
|
511
|
+
* 1. The reverse here is applied so we can apply a `pop` later on => more performant.
|
|
512
|
+
* 2. We also get from the retryable options a timeout multiplier that is tailored
|
|
513
|
+
* for the current context.
|
|
524
514
|
*/
|
|
525
|
-
|
|
526
|
-
|
|
527
515
|
const compatibleHosts = hosts.filter(host => host.accept === 'readWrite' || (isRead ? host.accept === 'read' : host.accept === 'write'));
|
|
528
516
|
const options = await createRetryableOptions(compatibleHosts);
|
|
529
517
|
return retry([...options.hosts].reverse(), options.getTimeout);
|
|
530
518
|
}
|
|
531
|
-
|
|
532
519
|
function createRequest(request, requestOptions = {}) {
|
|
533
|
-
/**
|
|
534
|
-
* A read request is either a `GET` request, or a request that we make
|
|
535
|
-
* via the `read` transporter (e.g. `search`).
|
|
520
|
+
/**
|
|
521
|
+
* A read request is either a `GET` request, or a request that we make
|
|
522
|
+
* via the `read` transporter (e.g. `search`).
|
|
536
523
|
*/
|
|
537
524
|
const isRead = request.useReadTransporter || request.method === 'GET';
|
|
538
|
-
|
|
539
525
|
if (!isRead) {
|
|
540
|
-
/**
|
|
541
|
-
* On write requests, no cache mechanisms are applied, and we
|
|
542
|
-
* proxy the request immediately to the requester.
|
|
526
|
+
/**
|
|
527
|
+
* On write requests, no cache mechanisms are applied, and we
|
|
528
|
+
* proxy the request immediately to the requester.
|
|
543
529
|
*/
|
|
544
530
|
return retryableRequest(request, requestOptions, isRead);
|
|
545
531
|
}
|
|
546
|
-
|
|
547
532
|
const createRetryableRequest = () => {
|
|
548
|
-
/**
|
|
549
|
-
* Then, we prepare a function factory that contains the construction of
|
|
550
|
-
* the retryable request. At this point, we may *not* perform the actual
|
|
551
|
-
* request. But we want to have the function factory ready.
|
|
533
|
+
/**
|
|
534
|
+
* Then, we prepare a function factory that contains the construction of
|
|
535
|
+
* the retryable request. At this point, we may *not* perform the actual
|
|
536
|
+
* request. But we want to have the function factory ready.
|
|
552
537
|
*/
|
|
553
538
|
return retryableRequest(request, requestOptions);
|
|
554
539
|
};
|
|
555
|
-
/**
|
|
556
|
-
* Once we have the function factory ready, we need to determine of the
|
|
557
|
-
* request is "cacheable" - should be cached. Note that, once again,
|
|
558
|
-
* the user can force this option.
|
|
540
|
+
/**
|
|
541
|
+
* Once we have the function factory ready, we need to determine of the
|
|
542
|
+
* request is "cacheable" - should be cached. Note that, once again,
|
|
543
|
+
* the user can force this option.
|
|
559
544
|
*/
|
|
560
|
-
|
|
561
|
-
|
|
562
545
|
const cacheable = requestOptions.cacheable || request.cacheable;
|
|
563
|
-
/**
|
|
564
|
-
* If is not "cacheable", we immediately trigger the retryable request, no
|
|
565
|
-
* need to check cache implementations.
|
|
546
|
+
/**
|
|
547
|
+
* If is not "cacheable", we immediately trigger the retryable request, no
|
|
548
|
+
* need to check cache implementations.
|
|
566
549
|
*/
|
|
567
|
-
|
|
568
550
|
if (cacheable !== true) {
|
|
569
551
|
return createRetryableRequest();
|
|
570
552
|
}
|
|
571
|
-
/**
|
|
572
|
-
* If the request is "cacheable", we need to first compute the key to ask
|
|
573
|
-
* the cache implementations if this request is on progress or if the
|
|
574
|
-
* response already exists on the cache.
|
|
553
|
+
/**
|
|
554
|
+
* If the request is "cacheable", we need to first compute the key to ask
|
|
555
|
+
* the cache implementations if this request is on progress or if the
|
|
556
|
+
* response already exists on the cache.
|
|
575
557
|
*/
|
|
576
|
-
|
|
577
|
-
|
|
578
558
|
const key = {
|
|
579
559
|
request,
|
|
580
560
|
requestOptions,
|
|
@@ -583,33 +563,31 @@ function createTransporter({
|
|
|
583
563
|
headers: baseHeaders
|
|
584
564
|
}
|
|
585
565
|
};
|
|
586
|
-
/**
|
|
587
|
-
* With the computed key, we first ask the responses cache
|
|
588
|
-
* implementation if this request was been resolved before.
|
|
566
|
+
/**
|
|
567
|
+
* With the computed key, we first ask the responses cache
|
|
568
|
+
* implementation if this request was been resolved before.
|
|
589
569
|
*/
|
|
590
|
-
|
|
591
570
|
return responsesCache.get(key, () => {
|
|
592
|
-
/**
|
|
593
|
-
* If the request has never resolved before, we actually ask if there
|
|
594
|
-
* is a current request with the same key on progress.
|
|
571
|
+
/**
|
|
572
|
+
* If the request has never resolved before, we actually ask if there
|
|
573
|
+
* is a current request with the same key on progress.
|
|
595
574
|
*/
|
|
596
575
|
return requestsCache.get(key, () =>
|
|
597
|
-
/**
|
|
598
|
-
* Finally, if there is no request in progress with the same key,
|
|
599
|
-
* this `createRetryableRequest()` will actually trigger the
|
|
600
|
-
* retryable request.
|
|
576
|
+
/**
|
|
577
|
+
* Finally, if there is no request in progress with the same key,
|
|
578
|
+
* this `createRetryableRequest()` will actually trigger the
|
|
579
|
+
* retryable request.
|
|
601
580
|
*/
|
|
602
581
|
requestsCache.set(key, createRetryableRequest()).then(response => Promise.all([requestsCache.delete(key), response]), err => Promise.all([requestsCache.delete(key), Promise.reject(err)])).then(([_, response]) => response));
|
|
603
582
|
}, {
|
|
604
|
-
/**
|
|
605
|
-
* Of course, once we get this response back from the server, we
|
|
606
|
-
* tell response cache to actually store the received response
|
|
607
|
-
* to be used later.
|
|
583
|
+
/**
|
|
584
|
+
* Of course, once we get this response back from the server, we
|
|
585
|
+
* tell response cache to actually store the received response
|
|
586
|
+
* to be used later.
|
|
608
587
|
*/
|
|
609
588
|
miss: response => responsesCache.set(key, response)
|
|
610
589
|
});
|
|
611
590
|
}
|
|
612
|
-
|
|
613
591
|
return {
|
|
614
592
|
hostsCache,
|
|
615
593
|
requester,
|
|
@@ -627,17 +605,13 @@ function createTransporter({
|
|
|
627
605
|
function createAlgoliaAgent(version) {
|
|
628
606
|
const algoliaAgent = {
|
|
629
607
|
value: `Algolia for JavaScript (${version})`,
|
|
630
|
-
|
|
631
608
|
add(options) {
|
|
632
609
|
const addedAlgoliaAgent = `; ${options.segment}${options.version !== undefined ? ` (${options.version})` : ''}`;
|
|
633
|
-
|
|
634
610
|
if (algoliaAgent.value.indexOf(addedAlgoliaAgent) === -1) {
|
|
635
611
|
algoliaAgent.value = `${algoliaAgent.value}${addedAlgoliaAgent}`;
|
|
636
612
|
}
|
|
637
|
-
|
|
638
613
|
return algoliaAgent;
|
|
639
614
|
}
|
|
640
|
-
|
|
641
615
|
};
|
|
642
616
|
return algoliaAgent;
|
|
643
617
|
}
|
|
@@ -659,349 +633,353 @@ const DEFAULT_CONNECT_TIMEOUT_BROWSER = 1000;
|
|
|
659
633
|
const DEFAULT_READ_TIMEOUT_BROWSER = 2000;
|
|
660
634
|
const DEFAULT_WRITE_TIMEOUT_BROWSER = 30000;
|
|
661
635
|
|
|
662
|
-
function createXhrRequester() {
|
|
663
|
-
function send(request) {
|
|
664
|
-
return new Promise((resolve) => {
|
|
665
|
-
const baseRequester = new XMLHttpRequest();
|
|
666
|
-
baseRequester.open(request.method, request.url, true);
|
|
667
|
-
Object.keys(request.headers).forEach((key) => baseRequester.setRequestHeader(key, request.headers[key]));
|
|
668
|
-
const createTimeout = (timeout, content) => {
|
|
669
|
-
return setTimeout(() => {
|
|
670
|
-
baseRequester.abort();
|
|
671
|
-
resolve({
|
|
672
|
-
status: 0,
|
|
673
|
-
content,
|
|
674
|
-
isTimedOut: true,
|
|
675
|
-
});
|
|
676
|
-
}, timeout);
|
|
677
|
-
};
|
|
678
|
-
const connectTimeout = createTimeout(request.connectTimeout, 'Connection timeout');
|
|
679
|
-
let responseTimeout;
|
|
680
|
-
baseRequester.onreadystatechange = () => {
|
|
681
|
-
if (baseRequester.readyState > baseRequester.OPENED &&
|
|
682
|
-
responseTimeout === undefined) {
|
|
683
|
-
clearTimeout(connectTimeout);
|
|
684
|
-
responseTimeout = createTimeout(request.responseTimeout, 'Socket timeout');
|
|
685
|
-
}
|
|
686
|
-
};
|
|
687
|
-
baseRequester.onerror = () => {
|
|
688
|
-
// istanbul ignore next
|
|
689
|
-
if (baseRequester.status === 0) {
|
|
690
|
-
clearTimeout(connectTimeout);
|
|
691
|
-
clearTimeout(responseTimeout);
|
|
692
|
-
resolve({
|
|
693
|
-
content: baseRequester.responseText || 'Network request failed',
|
|
694
|
-
status: baseRequester.status,
|
|
695
|
-
isTimedOut: false,
|
|
696
|
-
});
|
|
697
|
-
}
|
|
698
|
-
};
|
|
699
|
-
baseRequester.onload = () => {
|
|
700
|
-
clearTimeout(connectTimeout);
|
|
701
|
-
clearTimeout(responseTimeout);
|
|
702
|
-
resolve({
|
|
703
|
-
content: baseRequester.responseText,
|
|
704
|
-
status: baseRequester.status,
|
|
705
|
-
isTimedOut: false,
|
|
706
|
-
});
|
|
707
|
-
};
|
|
708
|
-
baseRequester.send(request.data);
|
|
709
|
-
});
|
|
710
|
-
}
|
|
711
|
-
return { send };
|
|
636
|
+
function createXhrRequester() {
|
|
637
|
+
function send(request) {
|
|
638
|
+
return new Promise((resolve) => {
|
|
639
|
+
const baseRequester = new XMLHttpRequest();
|
|
640
|
+
baseRequester.open(request.method, request.url, true);
|
|
641
|
+
Object.keys(request.headers).forEach((key) => baseRequester.setRequestHeader(key, request.headers[key]));
|
|
642
|
+
const createTimeout = (timeout, content) => {
|
|
643
|
+
return setTimeout(() => {
|
|
644
|
+
baseRequester.abort();
|
|
645
|
+
resolve({
|
|
646
|
+
status: 0,
|
|
647
|
+
content,
|
|
648
|
+
isTimedOut: true,
|
|
649
|
+
});
|
|
650
|
+
}, timeout);
|
|
651
|
+
};
|
|
652
|
+
const connectTimeout = createTimeout(request.connectTimeout, 'Connection timeout');
|
|
653
|
+
let responseTimeout;
|
|
654
|
+
baseRequester.onreadystatechange = () => {
|
|
655
|
+
if (baseRequester.readyState > baseRequester.OPENED &&
|
|
656
|
+
responseTimeout === undefined) {
|
|
657
|
+
clearTimeout(connectTimeout);
|
|
658
|
+
responseTimeout = createTimeout(request.responseTimeout, 'Socket timeout');
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
baseRequester.onerror = () => {
|
|
662
|
+
// istanbul ignore next
|
|
663
|
+
if (baseRequester.status === 0) {
|
|
664
|
+
clearTimeout(connectTimeout);
|
|
665
|
+
clearTimeout(responseTimeout);
|
|
666
|
+
resolve({
|
|
667
|
+
content: baseRequester.responseText || 'Network request failed',
|
|
668
|
+
status: baseRequester.status,
|
|
669
|
+
isTimedOut: false,
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
};
|
|
673
|
+
baseRequester.onload = () => {
|
|
674
|
+
clearTimeout(connectTimeout);
|
|
675
|
+
clearTimeout(responseTimeout);
|
|
676
|
+
resolve({
|
|
677
|
+
content: baseRequester.responseText,
|
|
678
|
+
status: baseRequester.status,
|
|
679
|
+
isTimedOut: false,
|
|
680
|
+
});
|
|
681
|
+
};
|
|
682
|
+
baseRequester.send(request.data);
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
return { send };
|
|
712
686
|
}
|
|
713
687
|
|
|
714
|
-
//
|
|
715
|
-
const apiClientVersion = '5.0.0-alpha.
|
|
716
|
-
const REGIONS = ['eu', 'us'];
|
|
717
|
-
function getDefaultHosts(region) {
|
|
718
|
-
const url = 'personalization.{region}.algolia.com'.replace('{region}', region);
|
|
719
|
-
return [{ url, accept: 'readWrite', protocol: 'https' }];
|
|
720
|
-
}
|
|
721
|
-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
722
|
-
function createPersonalizationClient({ appId: appIdOption, apiKey: apiKeyOption, authMode, algoliaAgents, region: regionOption, ...options }) {
|
|
723
|
-
const auth = createAuth(appIdOption, apiKeyOption, authMode);
|
|
724
|
-
const transporter = createTransporter({
|
|
725
|
-
hosts: getDefaultHosts(regionOption),
|
|
726
|
-
...options,
|
|
727
|
-
algoliaAgent: getAlgoliaAgent({
|
|
728
|
-
algoliaAgents,
|
|
729
|
-
client: 'Personalization',
|
|
730
|
-
version: apiClientVersion,
|
|
731
|
-
}),
|
|
732
|
-
baseHeaders: {
|
|
733
|
-
'content-type': 'text/plain',
|
|
734
|
-
...auth.headers(),
|
|
735
|
-
...options.baseHeaders,
|
|
736
|
-
},
|
|
737
|
-
baseQueryParameters: {
|
|
738
|
-
...auth.queryParameters(),
|
|
739
|
-
...options.baseQueryParameters,
|
|
740
|
-
},
|
|
741
|
-
});
|
|
742
|
-
return {
|
|
743
|
-
transporter,
|
|
744
|
-
/**
|
|
745
|
-
* The `appId` currently in use.
|
|
746
|
-
*/
|
|
747
|
-
appId: appIdOption,
|
|
748
|
-
/**
|
|
749
|
-
* Clears the cache of the transporter for the `requestsCache` and `responsesCache` properties.
|
|
750
|
-
*/
|
|
751
|
-
clearCache() {
|
|
752
|
-
return Promise.all([
|
|
753
|
-
transporter.requestsCache.clear(),
|
|
754
|
-
transporter.responsesCache.clear(),
|
|
755
|
-
]).then(() => undefined);
|
|
756
|
-
},
|
|
757
|
-
/**
|
|
758
|
-
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
|
|
759
|
-
*/
|
|
760
|
-
get _ua() {
|
|
761
|
-
return transporter.algoliaAgent.value;
|
|
762
|
-
},
|
|
763
|
-
/**
|
|
764
|
-
* Adds a `segment` to the `x-algolia-agent` sent with every requests.
|
|
765
|
-
*
|
|
766
|
-
* @param segment - The algolia agent (user-agent) segment to add.
|
|
767
|
-
* @param version - The version of the agent.
|
|
768
|
-
*/
|
|
769
|
-
addAlgoliaAgent(segment, version) {
|
|
770
|
-
transporter.algoliaAgent.add({ segment, version });
|
|
771
|
-
},
|
|
772
|
-
/**
|
|
773
|
-
* This method allow you to send requests to the Algolia REST API.
|
|
774
|
-
*
|
|
775
|
-
* @
|
|
776
|
-
* @param
|
|
777
|
-
* @param
|
|
778
|
-
* @param
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
}
|
|
785
|
-
const
|
|
786
|
-
const
|
|
787
|
-
const
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
*
|
|
798
|
-
*
|
|
799
|
-
* @
|
|
800
|
-
* @param
|
|
801
|
-
* @param
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
}
|
|
808
|
-
const
|
|
809
|
-
const
|
|
810
|
-
const
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
*
|
|
821
|
-
*
|
|
822
|
-
* @
|
|
823
|
-
* @param
|
|
824
|
-
* @param
|
|
825
|
-
* @param
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
}
|
|
832
|
-
const
|
|
833
|
-
const
|
|
834
|
-
const
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
};
|
|
841
|
-
return transporter.request(request, requestOptions);
|
|
842
|
-
},
|
|
843
|
-
/**
|
|
844
|
-
*
|
|
845
|
-
*
|
|
846
|
-
* @
|
|
847
|
-
* @param
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
};
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
}
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
*
|
|
915
|
-
*
|
|
916
|
-
*
|
|
917
|
-
*
|
|
918
|
-
*
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
const
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
*
|
|
940
|
-
*
|
|
941
|
-
*
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
688
|
+
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.
|
|
689
|
+
const apiClientVersion = '5.0.0-alpha.101';
|
|
690
|
+
const REGIONS = ['eu', 'us'];
|
|
691
|
+
function getDefaultHosts(region) {
|
|
692
|
+
const url = 'personalization.{region}.algolia.com'.replace('{region}', region);
|
|
693
|
+
return [{ url, accept: 'readWrite', protocol: 'https' }];
|
|
694
|
+
}
|
|
695
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
696
|
+
function createPersonalizationClient({ appId: appIdOption, apiKey: apiKeyOption, authMode, algoliaAgents, region: regionOption, ...options }) {
|
|
697
|
+
const auth = createAuth(appIdOption, apiKeyOption, authMode);
|
|
698
|
+
const transporter = createTransporter({
|
|
699
|
+
hosts: getDefaultHosts(regionOption),
|
|
700
|
+
...options,
|
|
701
|
+
algoliaAgent: getAlgoliaAgent({
|
|
702
|
+
algoliaAgents,
|
|
703
|
+
client: 'Personalization',
|
|
704
|
+
version: apiClientVersion,
|
|
705
|
+
}),
|
|
706
|
+
baseHeaders: {
|
|
707
|
+
'content-type': 'text/plain',
|
|
708
|
+
...auth.headers(),
|
|
709
|
+
...options.baseHeaders,
|
|
710
|
+
},
|
|
711
|
+
baseQueryParameters: {
|
|
712
|
+
...auth.queryParameters(),
|
|
713
|
+
...options.baseQueryParameters,
|
|
714
|
+
},
|
|
715
|
+
});
|
|
716
|
+
return {
|
|
717
|
+
transporter,
|
|
718
|
+
/**
|
|
719
|
+
* The `appId` currently in use.
|
|
720
|
+
*/
|
|
721
|
+
appId: appIdOption,
|
|
722
|
+
/**
|
|
723
|
+
* Clears the cache of the transporter for the `requestsCache` and `responsesCache` properties.
|
|
724
|
+
*/
|
|
725
|
+
clearCache() {
|
|
726
|
+
return Promise.all([
|
|
727
|
+
transporter.requestsCache.clear(),
|
|
728
|
+
transporter.responsesCache.clear(),
|
|
729
|
+
]).then(() => undefined);
|
|
730
|
+
},
|
|
731
|
+
/**
|
|
732
|
+
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
|
|
733
|
+
*/
|
|
734
|
+
get _ua() {
|
|
735
|
+
return transporter.algoliaAgent.value;
|
|
736
|
+
},
|
|
737
|
+
/**
|
|
738
|
+
* Adds a `segment` to the `x-algolia-agent` sent with every requests.
|
|
739
|
+
*
|
|
740
|
+
* @param segment - The algolia agent (user-agent) segment to add.
|
|
741
|
+
* @param version - The version of the agent.
|
|
742
|
+
*/
|
|
743
|
+
addAlgoliaAgent(segment, version) {
|
|
744
|
+
transporter.algoliaAgent.add({ segment, version });
|
|
745
|
+
},
|
|
746
|
+
/**
|
|
747
|
+
* This method allow you to send requests to the Algolia REST API.
|
|
748
|
+
*
|
|
749
|
+
* @param customDelete - The customDelete object.
|
|
750
|
+
* @param customDelete.path - Path of the endpoint, anything after \"/1\" must be specified.
|
|
751
|
+
* @param customDelete.parameters - Query parameters to apply to the current query.
|
|
752
|
+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
|
|
753
|
+
*/
|
|
754
|
+
customDelete({ path, parameters }, requestOptions) {
|
|
755
|
+
if (!path) {
|
|
756
|
+
throw new Error('Parameter `path` is required when calling `customDelete`.');
|
|
757
|
+
}
|
|
758
|
+
const requestPath = '/1{path}'.replace('{path}', path);
|
|
759
|
+
const headers = {};
|
|
760
|
+
const queryParameters = parameters ? parameters : {};
|
|
761
|
+
const request = {
|
|
762
|
+
method: 'DELETE',
|
|
763
|
+
path: requestPath,
|
|
764
|
+
queryParameters,
|
|
765
|
+
headers,
|
|
766
|
+
};
|
|
767
|
+
return transporter.request(request, requestOptions);
|
|
768
|
+
},
|
|
769
|
+
/**
|
|
770
|
+
* This method allow you to send requests to the Algolia REST API.
|
|
771
|
+
*
|
|
772
|
+
* @param customGet - The customGet object.
|
|
773
|
+
* @param customGet.path - Path of the endpoint, anything after \"/1\" must be specified.
|
|
774
|
+
* @param customGet.parameters - Query parameters to apply to the current query.
|
|
775
|
+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
|
|
776
|
+
*/
|
|
777
|
+
customGet({ path, parameters }, requestOptions) {
|
|
778
|
+
if (!path) {
|
|
779
|
+
throw new Error('Parameter `path` is required when calling `customGet`.');
|
|
780
|
+
}
|
|
781
|
+
const requestPath = '/1{path}'.replace('{path}', path);
|
|
782
|
+
const headers = {};
|
|
783
|
+
const queryParameters = parameters ? parameters : {};
|
|
784
|
+
const request = {
|
|
785
|
+
method: 'GET',
|
|
786
|
+
path: requestPath,
|
|
787
|
+
queryParameters,
|
|
788
|
+
headers,
|
|
789
|
+
};
|
|
790
|
+
return transporter.request(request, requestOptions);
|
|
791
|
+
},
|
|
792
|
+
/**
|
|
793
|
+
* This method allow you to send requests to the Algolia REST API.
|
|
794
|
+
*
|
|
795
|
+
* @param customPost - The customPost object.
|
|
796
|
+
* @param customPost.path - Path of the endpoint, anything after \"/1\" must be specified.
|
|
797
|
+
* @param customPost.parameters - Query parameters to apply to the current query.
|
|
798
|
+
* @param customPost.body - Parameters to send with the custom request.
|
|
799
|
+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
|
|
800
|
+
*/
|
|
801
|
+
customPost({ path, parameters, body }, requestOptions) {
|
|
802
|
+
if (!path) {
|
|
803
|
+
throw new Error('Parameter `path` is required when calling `customPost`.');
|
|
804
|
+
}
|
|
805
|
+
const requestPath = '/1{path}'.replace('{path}', path);
|
|
806
|
+
const headers = {};
|
|
807
|
+
const queryParameters = parameters ? parameters : {};
|
|
808
|
+
const request = {
|
|
809
|
+
method: 'POST',
|
|
810
|
+
path: requestPath,
|
|
811
|
+
queryParameters,
|
|
812
|
+
headers,
|
|
813
|
+
data: body ? body : {},
|
|
814
|
+
};
|
|
815
|
+
return transporter.request(request, requestOptions);
|
|
816
|
+
},
|
|
817
|
+
/**
|
|
818
|
+
* This method allow you to send requests to the Algolia REST API.
|
|
819
|
+
*
|
|
820
|
+
* @param customPut - The customPut object.
|
|
821
|
+
* @param customPut.path - Path of the endpoint, anything after \"/1\" must be specified.
|
|
822
|
+
* @param customPut.parameters - Query parameters to apply to the current query.
|
|
823
|
+
* @param customPut.body - Parameters to send with the custom request.
|
|
824
|
+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
|
|
825
|
+
*/
|
|
826
|
+
customPut({ path, parameters, body }, requestOptions) {
|
|
827
|
+
if (!path) {
|
|
828
|
+
throw new Error('Parameter `path` is required when calling `customPut`.');
|
|
829
|
+
}
|
|
830
|
+
const requestPath = '/1{path}'.replace('{path}', path);
|
|
831
|
+
const headers = {};
|
|
832
|
+
const queryParameters = parameters ? parameters : {};
|
|
833
|
+
const request = {
|
|
834
|
+
method: 'PUT',
|
|
835
|
+
path: requestPath,
|
|
836
|
+
queryParameters,
|
|
837
|
+
headers,
|
|
838
|
+
data: body ? body : {},
|
|
839
|
+
};
|
|
840
|
+
return transporter.request(request, requestOptions);
|
|
841
|
+
},
|
|
842
|
+
/**
|
|
843
|
+
* Delete the user profile and all its associated data. Returns, as part of the response, a date until which the data can safely be considered as deleted for the given user. This means if you send events for the given user before this date, they will be ignored. Any data received after the deletedUntil date will start building a new user profile. It might take a couple hours for the deletion request to be fully processed.
|
|
844
|
+
*
|
|
845
|
+
* Required API Key ACLs:
|
|
846
|
+
* - recommendation.
|
|
847
|
+
*
|
|
848
|
+
* @param deleteUserProfile - The deleteUserProfile object.
|
|
849
|
+
* @param deleteUserProfile.userToken - UserToken representing the user for which to fetch the Personalization profile.
|
|
850
|
+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
|
|
851
|
+
*/
|
|
852
|
+
deleteUserProfile({ userToken }, requestOptions) {
|
|
853
|
+
if (!userToken) {
|
|
854
|
+
throw new Error('Parameter `userToken` is required when calling `deleteUserProfile`.');
|
|
855
|
+
}
|
|
856
|
+
const requestPath = '/1/profiles/{userToken}'.replace('{userToken}', encodeURIComponent(userToken));
|
|
857
|
+
const headers = {};
|
|
858
|
+
const queryParameters = {};
|
|
859
|
+
const request = {
|
|
860
|
+
method: 'DELETE',
|
|
861
|
+
path: requestPath,
|
|
862
|
+
queryParameters,
|
|
863
|
+
headers,
|
|
864
|
+
};
|
|
865
|
+
return transporter.request(request, requestOptions);
|
|
866
|
+
},
|
|
867
|
+
/**
|
|
868
|
+
* The strategy contains information on the events and facets that impact user profiles and personalized search results.
|
|
869
|
+
*
|
|
870
|
+
* Required API Key ACLs:
|
|
871
|
+
* - recommendation.
|
|
872
|
+
*
|
|
873
|
+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
|
|
874
|
+
*/
|
|
875
|
+
getPersonalizationStrategy(requestOptions) {
|
|
876
|
+
const requestPath = '/1/strategies/personalization';
|
|
877
|
+
const headers = {};
|
|
878
|
+
const queryParameters = {};
|
|
879
|
+
const request = {
|
|
880
|
+
method: 'GET',
|
|
881
|
+
path: requestPath,
|
|
882
|
+
queryParameters,
|
|
883
|
+
headers,
|
|
884
|
+
};
|
|
885
|
+
return transporter.request(request, requestOptions);
|
|
886
|
+
},
|
|
887
|
+
/**
|
|
888
|
+
* Get the user profile built from Personalization strategy. The profile is structured by facet name used in the strategy. Each facet value is mapped to its score. Each score represents the user affinity for a specific facet value given the userToken past events and the Personalization strategy defined. Scores are bounded to 20. The last processed event timestamp is provided using the ISO 8601 format for debugging purposes.
|
|
889
|
+
*
|
|
890
|
+
* Required API Key ACLs:
|
|
891
|
+
* - recommendation.
|
|
892
|
+
*
|
|
893
|
+
* @param getUserTokenProfile - The getUserTokenProfile object.
|
|
894
|
+
* @param getUserTokenProfile.userToken - UserToken representing the user for which to fetch the Personalization profile.
|
|
895
|
+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
|
|
896
|
+
*/
|
|
897
|
+
getUserTokenProfile({ userToken }, requestOptions) {
|
|
898
|
+
if (!userToken) {
|
|
899
|
+
throw new Error('Parameter `userToken` is required when calling `getUserTokenProfile`.');
|
|
900
|
+
}
|
|
901
|
+
const requestPath = '/1/profiles/personalization/{userToken}'.replace('{userToken}', encodeURIComponent(userToken));
|
|
902
|
+
const headers = {};
|
|
903
|
+
const queryParameters = {};
|
|
904
|
+
const request = {
|
|
905
|
+
method: 'GET',
|
|
906
|
+
path: requestPath,
|
|
907
|
+
queryParameters,
|
|
908
|
+
headers,
|
|
909
|
+
};
|
|
910
|
+
return transporter.request(request, requestOptions);
|
|
911
|
+
},
|
|
912
|
+
/**
|
|
913
|
+
* A strategy defines the events and facets that impact user profiles and personalized search results.
|
|
914
|
+
*
|
|
915
|
+
* Required API Key ACLs:
|
|
916
|
+
* - recommendation.
|
|
917
|
+
*
|
|
918
|
+
* @param personalizationStrategyParams - The personalizationStrategyParams object.
|
|
919
|
+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
|
|
920
|
+
*/
|
|
921
|
+
setPersonalizationStrategy(personalizationStrategyParams, requestOptions) {
|
|
922
|
+
if (!personalizationStrategyParams) {
|
|
923
|
+
throw new Error('Parameter `personalizationStrategyParams` is required when calling `setPersonalizationStrategy`.');
|
|
924
|
+
}
|
|
925
|
+
if (!personalizationStrategyParams.eventScoring) {
|
|
926
|
+
throw new Error('Parameter `personalizationStrategyParams.eventScoring` is required when calling `setPersonalizationStrategy`.');
|
|
927
|
+
}
|
|
928
|
+
if (!personalizationStrategyParams.facetScoring) {
|
|
929
|
+
throw new Error('Parameter `personalizationStrategyParams.facetScoring` is required when calling `setPersonalizationStrategy`.');
|
|
930
|
+
}
|
|
931
|
+
if (!personalizationStrategyParams.personalizationImpact) {
|
|
932
|
+
throw new Error('Parameter `personalizationStrategyParams.personalizationImpact` is required when calling `setPersonalizationStrategy`.');
|
|
933
|
+
}
|
|
934
|
+
const requestPath = '/1/strategies/personalization';
|
|
935
|
+
const headers = {};
|
|
936
|
+
const queryParameters = {};
|
|
937
|
+
const request = {
|
|
938
|
+
method: 'POST',
|
|
939
|
+
path: requestPath,
|
|
940
|
+
queryParameters,
|
|
941
|
+
headers,
|
|
942
|
+
data: personalizationStrategyParams,
|
|
943
|
+
};
|
|
944
|
+
return transporter.request(request, requestOptions);
|
|
945
|
+
},
|
|
946
|
+
};
|
|
969
947
|
}
|
|
970
948
|
|
|
971
|
-
//
|
|
972
|
-
function personalizationClient(appId, apiKey, region, options) {
|
|
973
|
-
if (!appId || typeof appId !== 'string') {
|
|
974
|
-
throw new Error('`appId` is missing.');
|
|
975
|
-
}
|
|
976
|
-
if (!apiKey || typeof apiKey !== 'string') {
|
|
977
|
-
throw new Error('`apiKey` is missing.');
|
|
978
|
-
}
|
|
979
|
-
if (!region ||
|
|
980
|
-
(region && (typeof region !== 'string' || !REGIONS.includes(region)))) {
|
|
981
|
-
throw new Error(`\`region\` is required and must be one of the following: ${REGIONS.join(', ')}`);
|
|
982
|
-
}
|
|
983
|
-
return createPersonalizationClient({
|
|
984
|
-
appId,
|
|
985
|
-
apiKey,
|
|
986
|
-
region,
|
|
987
|
-
timeouts: {
|
|
988
|
-
connect: DEFAULT_CONNECT_TIMEOUT_BROWSER,
|
|
989
|
-
read: DEFAULT_READ_TIMEOUT_BROWSER,
|
|
990
|
-
write: DEFAULT_WRITE_TIMEOUT_BROWSER,
|
|
991
|
-
},
|
|
992
|
-
requester: createXhrRequester(),
|
|
993
|
-
algoliaAgents: [{ segment: 'Browser' }],
|
|
994
|
-
authMode: 'WithinQueryParameters',
|
|
995
|
-
responsesCache: createMemoryCache(),
|
|
996
|
-
requestsCache: createMemoryCache({ serializable: false }),
|
|
997
|
-
hostsCache: createFallbackableCache({
|
|
998
|
-
caches: [
|
|
999
|
-
createBrowserLocalStorageCache({ key: `${apiClientVersion}-${appId}` }),
|
|
1000
|
-
createMemoryCache(),
|
|
1001
|
-
],
|
|
1002
|
-
}),
|
|
1003
|
-
...options,
|
|
1004
|
-
});
|
|
949
|
+
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.
|
|
950
|
+
function personalizationClient(appId, apiKey, region, options) {
|
|
951
|
+
if (!appId || typeof appId !== 'string') {
|
|
952
|
+
throw new Error('`appId` is missing.');
|
|
953
|
+
}
|
|
954
|
+
if (!apiKey || typeof apiKey !== 'string') {
|
|
955
|
+
throw new Error('`apiKey` is missing.');
|
|
956
|
+
}
|
|
957
|
+
if (!region ||
|
|
958
|
+
(region && (typeof region !== 'string' || !REGIONS.includes(region)))) {
|
|
959
|
+
throw new Error(`\`region\` is required and must be one of the following: ${REGIONS.join(', ')}`);
|
|
960
|
+
}
|
|
961
|
+
return createPersonalizationClient({
|
|
962
|
+
appId,
|
|
963
|
+
apiKey,
|
|
964
|
+
region,
|
|
965
|
+
timeouts: {
|
|
966
|
+
connect: DEFAULT_CONNECT_TIMEOUT_BROWSER,
|
|
967
|
+
read: DEFAULT_READ_TIMEOUT_BROWSER,
|
|
968
|
+
write: DEFAULT_WRITE_TIMEOUT_BROWSER,
|
|
969
|
+
},
|
|
970
|
+
requester: createXhrRequester(),
|
|
971
|
+
algoliaAgents: [{ segment: 'Browser' }],
|
|
972
|
+
authMode: 'WithinQueryParameters',
|
|
973
|
+
responsesCache: createMemoryCache(),
|
|
974
|
+
requestsCache: createMemoryCache({ serializable: false }),
|
|
975
|
+
hostsCache: createFallbackableCache({
|
|
976
|
+
caches: [
|
|
977
|
+
createBrowserLocalStorageCache({ key: `${apiClientVersion}-${appId}` }),
|
|
978
|
+
createMemoryCache(),
|
|
979
|
+
],
|
|
980
|
+
}),
|
|
981
|
+
...options,
|
|
982
|
+
});
|
|
1005
983
|
}
|
|
1006
984
|
|
|
1007
985
|
export { apiClientVersion, personalizationClient };
|