@algolia/recommend 4.14.2 → 5.0.0-alpha.3
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 +6 -0
- package/dist/builds/browser.d.ts.map +1 -0
- package/dist/builds/node.d.ts +6 -0
- package/dist/builds/node.d.ts.map +1 -0
- package/dist/model/advancedSyntaxFeatures.d.ts +2 -0
- package/dist/model/advancedSyntaxFeatures.d.ts.map +1 -0
- package/dist/model/alternativesAsExact.d.ts +2 -0
- package/dist/model/alternativesAsExact.d.ts.map +1 -0
- package/dist/model/aroundRadius.d.ts +6 -0
- package/dist/model/aroundRadius.d.ts.map +1 -0
- package/dist/model/aroundRadiusAll.d.ts +2 -0
- package/dist/model/aroundRadiusAll.d.ts.map +1 -0
- package/dist/model/baseRecommendRequest.d.ts +18 -0
- package/dist/model/baseRecommendRequest.d.ts.map +1 -0
- package/dist/model/baseRecommendationRequest.d.ts +9 -0
- package/dist/model/baseRecommendationRequest.d.ts.map +1 -0
- package/dist/model/baseSearchParams.d.ts +4 -0
- package/dist/model/baseSearchParams.d.ts.map +1 -0
- package/dist/model/baseSearchParamsWithoutQuery.d.ts +123 -0
- package/dist/model/baseSearchParamsWithoutQuery.d.ts.map +1 -0
- package/dist/model/baseSearchResponse.d.ts +102 -0
- package/dist/model/baseSearchResponse.d.ts.map +1 -0
- package/dist/model/baseTrendingRequest.d.ts +13 -0
- package/dist/model/baseTrendingRequest.d.ts.map +1 -0
- package/dist/model/clientMethodProps.d.ts +61 -0
- package/dist/model/clientMethodProps.d.ts.map +1 -0
- package/dist/model/distinct.d.ts +5 -0
- package/dist/model/distinct.d.ts.map +1 -0
- package/dist/model/errorBase.d.ts +7 -0
- package/dist/model/errorBase.d.ts.map +1 -0
- package/dist/model/exactOnSingleWordQuery.d.ts +5 -0
- package/dist/model/exactOnSingleWordQuery.d.ts.map +1 -0
- package/dist/model/facetFilters.d.ts +6 -0
- package/dist/model/facetFilters.d.ts.map +1 -0
- package/dist/model/facetOrdering.d.ts +13 -0
- package/dist/model/facetOrdering.d.ts.map +1 -0
- package/dist/model/facets.d.ts +10 -0
- package/dist/model/facets.d.ts.map +1 -0
- package/dist/model/facetsStats.d.ts +19 -0
- package/dist/model/facetsStats.d.ts.map +1 -0
- package/dist/model/getRecommendationsParams.d.ts +11 -0
- package/dist/model/getRecommendationsParams.d.ts.map +1 -0
- package/dist/model/getRecommendationsResponse.d.ts +5 -0
- package/dist/model/getRecommendationsResponse.d.ts.map +1 -0
- package/dist/model/highlightResult.d.ts +3 -0
- package/dist/model/highlightResult.d.ts.map +1 -0
- package/dist/model/highlightResultOption.d.ts +20 -0
- package/dist/model/highlightResultOption.d.ts.map +1 -0
- package/dist/model/ignorePlurals.d.ts +5 -0
- package/dist/model/ignorePlurals.d.ts.map +1 -0
- package/dist/model/index.d.ts +54 -0
- package/dist/model/index.d.ts.map +1 -0
- package/dist/model/indexSettingsAsSearchParams.d.ts +157 -0
- package/dist/model/indexSettingsAsSearchParams.d.ts.map +1 -0
- package/dist/model/matchLevel.d.ts +5 -0
- package/dist/model/matchLevel.d.ts.map +1 -0
- package/dist/model/matchedGeoLocation.d.ts +15 -0
- package/dist/model/matchedGeoLocation.d.ts.map +1 -0
- package/dist/model/mixedSearchFilters.d.ts +2 -0
- package/dist/model/mixedSearchFilters.d.ts.map +1 -0
- package/dist/model/numericFilters.d.ts +6 -0
- package/dist/model/numericFilters.d.ts.map +1 -0
- package/dist/model/optionalFilters.d.ts +6 -0
- package/dist/model/optionalFilters.d.ts.map +1 -0
- package/dist/model/personalization.d.ts +15 -0
- package/dist/model/personalization.d.ts.map +1 -0
- package/dist/model/queryType.d.ts +5 -0
- package/dist/model/queryType.d.ts.map +1 -0
- package/dist/model/rankingInfo.d.ts +51 -0
- package/dist/model/rankingInfo.d.ts.map +1 -0
- package/dist/model/reRankingApplyFilter.d.ts +6 -0
- package/dist/model/reRankingApplyFilter.d.ts.map +1 -0
- package/dist/model/recommendHit.d.ts +27 -0
- package/dist/model/recommendHit.d.ts.map +1 -0
- package/dist/model/recommendHits.d.ts +5 -0
- package/dist/model/recommendHits.d.ts.map +1 -0
- package/dist/model/recommendationModels.d.ts +5 -0
- package/dist/model/recommendationModels.d.ts.map +1 -0
- package/dist/model/recommendationRequest.d.ts +4 -0
- package/dist/model/recommendationRequest.d.ts.map +1 -0
- package/dist/model/recommendationsRequest.d.ts +4 -0
- package/dist/model/recommendationsRequest.d.ts.map +1 -0
- package/dist/model/recommendationsResponse.d.ts +4 -0
- package/dist/model/recommendationsResponse.d.ts.map +1 -0
- package/dist/model/removeStopWords.d.ts +5 -0
- package/dist/model/removeStopWords.d.ts.map +1 -0
- package/dist/model/removeWordsIfNoResults.d.ts +5 -0
- package/dist/model/removeWordsIfNoResults.d.ts.map +1 -0
- package/dist/model/renderingContent.d.ts +8 -0
- package/dist/model/renderingContent.d.ts.map +1 -0
- package/dist/model/searchParamsObject.d.ts +4 -0
- package/dist/model/searchParamsObject.d.ts.map +1 -0
- package/dist/model/searchParamsQuery.d.ts +7 -0
- package/dist/model/searchParamsQuery.d.ts.map +1 -0
- package/dist/model/snippetResult.d.ts +3 -0
- package/dist/model/snippetResult.d.ts.map +1 -0
- package/dist/model/snippetResultOption.d.ts +12 -0
- package/dist/model/snippetResultOption.d.ts.map +1 -0
- package/dist/model/sortRemainingBy.d.ts +5 -0
- package/dist/model/sortRemainingBy.d.ts.map +1 -0
- package/dist/model/tagFilters.d.ts +6 -0
- package/dist/model/tagFilters.d.ts.map +1 -0
- package/dist/model/trendingModels.d.ts +5 -0
- package/dist/model/trendingModels.d.ts.map +1 -0
- package/dist/model/trendingRequest.d.ts +4 -0
- package/dist/model/trendingRequest.d.ts.map +1 -0
- package/dist/model/typoTolerance.d.ts +6 -0
- package/dist/model/typoTolerance.d.ts.map +1 -0
- package/dist/model/typoToleranceEnum.d.ts +2 -0
- package/dist/model/typoToleranceEnum.d.ts.map +1 -0
- package/dist/model/value.d.ts +9 -0
- package/dist/model/value.d.ts.map +1 -0
- package/dist/recommend.cjs.js +212 -142
- package/dist/recommend.esm.browser.js +884 -824
- package/dist/recommend.esm.node.js +230 -0
- package/dist/recommend.umd.js +2 -2
- package/dist/src/recommendClient.d.ts +74 -0
- package/dist/src/recommendClient.d.ts.map +1 -0
- package/index.d.ts +2 -3
- package/index.js +2 -15
- package/model/advancedSyntaxFeatures.ts +3 -0
- package/model/alternativesAsExact.ts +6 -0
- package/model/aroundRadius.ts +8 -0
- package/model/aroundRadiusAll.ts +3 -0
- package/model/baseRecommendRequest.ts +24 -0
- package/model/baseRecommendationRequest.ts +12 -0
- package/model/baseSearchParams.ts +6 -0
- package/model/baseSearchParamsWithoutQuery.ts +157 -0
- package/model/baseSearchResponse.ts +128 -0
- package/model/baseTrendingRequest.ts +17 -0
- package/model/clientMethodProps.ts +65 -0
- package/model/distinct.ts +6 -0
- package/model/errorBase.ts +8 -0
- package/model/exactOnSingleWordQuery.ts +6 -0
- package/model/facetFilters.ts +8 -0
- package/model/facetOrdering.ts +16 -0
- package/model/facets.ts +11 -0
- package/model/facetsStats.ts +23 -0
- package/model/getRecommendationsParams.ts +13 -0
- package/model/getRecommendationsResponse.ts +7 -0
- package/model/highlightResult.ts +5 -0
- package/model/highlightResultOption.ts +25 -0
- package/model/ignorePlurals.ts +6 -0
- package/model/index.ts +55 -0
- package/model/indexSettingsAsSearchParams.ts +200 -0
- package/model/matchLevel.ts +6 -0
- package/model/matchedGeoLocation.ts +18 -0
- package/model/mixedSearchFilters.ts +3 -0
- package/model/numericFilters.ts +8 -0
- package/model/optionalFilters.ts +8 -0
- package/model/personalization.ts +18 -0
- package/model/queryType.ts +6 -0
- package/model/rankingInfo.ts +65 -0
- package/model/reRankingApplyFilter.ts +8 -0
- package/model/recommendHit.ts +34 -0
- package/model/recommendHits.ts +7 -0
- package/model/recommendationModels.ts +6 -0
- package/model/recommendationRequest.ts +7 -0
- package/model/recommendationsRequest.ts +6 -0
- package/model/recommendationsResponse.ts +6 -0
- package/model/removeStopWords.ts +6 -0
- package/model/removeWordsIfNoResults.ts +10 -0
- package/model/renderingContent.ts +10 -0
- package/model/searchParamsObject.ts +6 -0
- package/model/searchParamsQuery.ts +8 -0
- package/model/snippetResult.ts +5 -0
- package/model/snippetResultOption.ts +15 -0
- package/model/sortRemainingBy.ts +6 -0
- package/model/tagFilters.ts +8 -0
- package/model/trendingModels.ts +6 -0
- package/model/trendingRequest.ts +6 -0
- package/model/typoTolerance.ts +8 -0
- package/model/typoToleranceEnum.ts +3 -0
- package/model/value.ts +12 -0
- package/package.json +22 -24
- package/README.md +0 -76
- package/dist/recommend.d.ts +0 -162
|
@@ -1,509 +1,530 @@
|
|
|
1
|
-
function
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
miss: () => Promise.resolve(),
|
|
17
|
-
}) {
|
|
18
|
-
return Promise.resolve()
|
|
19
|
-
.then(() => {
|
|
20
|
-
const keyAsString = JSON.stringify(key);
|
|
21
|
-
const value = getNamespace()[keyAsString];
|
|
22
|
-
return Promise.all([value || defaultValue(), value !== undefined]);
|
|
23
|
-
})
|
|
24
|
-
.then(([value, exists]) => {
|
|
25
|
-
return Promise.all([value, exists || events.miss(value)]);
|
|
26
|
-
})
|
|
27
|
-
.then(([value]) => value);
|
|
28
|
-
},
|
|
29
|
-
set(key, value) {
|
|
30
|
-
return Promise.resolve().then(() => {
|
|
31
|
-
const namespace = getNamespace();
|
|
32
|
-
// eslint-disable-next-line functional/immutable-data
|
|
33
|
-
namespace[JSON.stringify(key)] = value;
|
|
34
|
-
getStorage().setItem(namespaceKey, JSON.stringify(namespace));
|
|
35
|
-
return value;
|
|
36
|
-
});
|
|
37
|
-
},
|
|
38
|
-
delete(key) {
|
|
39
|
-
return Promise.resolve().then(() => {
|
|
40
|
-
const namespace = getNamespace();
|
|
41
|
-
// eslint-disable-next-line functional/immutable-data
|
|
42
|
-
delete namespace[JSON.stringify(key)];
|
|
43
|
-
getStorage().setItem(namespaceKey, JSON.stringify(namespace));
|
|
44
|
-
});
|
|
45
|
-
},
|
|
46
|
-
clear() {
|
|
47
|
-
return Promise.resolve().then(() => {
|
|
48
|
-
getStorage().removeItem(namespaceKey);
|
|
49
|
-
});
|
|
50
|
-
},
|
|
51
|
-
};
|
|
1
|
+
function createAuth(appId, apiKey, authMode = 'WithinHeaders') {
|
|
2
|
+
const credentials = {
|
|
3
|
+
'x-algolia-api-key': apiKey,
|
|
4
|
+
'x-algolia-application-id': appId
|
|
5
|
+
};
|
|
6
|
+
return {
|
|
7
|
+
headers() {
|
|
8
|
+
return authMode === 'WithinHeaders' ? credentials : {};
|
|
9
|
+
},
|
|
10
|
+
|
|
11
|
+
queryParameters() {
|
|
12
|
+
return authMode === 'WithinQueryParameters' ? credentials : {};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
};
|
|
52
16
|
}
|
|
53
17
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
18
|
+
function createBrowserLocalStorageCache(options) {
|
|
19
|
+
let storage; // We've changed the namespace to avoid conflicts with v4, as this version is a huge breaking change
|
|
20
|
+
|
|
21
|
+
const namespaceKey = `algolia-client-js-${options.key}`;
|
|
22
|
+
|
|
23
|
+
function getStorage() {
|
|
24
|
+
if (storage === undefined) {
|
|
25
|
+
storage = options.localStorage || window.localStorage;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return storage;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function getNamespace() {
|
|
32
|
+
return JSON.parse(getStorage().getItem(namespaceKey) || '{}');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
get(key, defaultValue, events = {
|
|
37
|
+
miss: () => Promise.resolve()
|
|
38
|
+
}) {
|
|
39
|
+
return Promise.resolve().then(() => {
|
|
40
|
+
const keyAsString = JSON.stringify(key);
|
|
41
|
+
const value = getNamespace()[keyAsString];
|
|
42
|
+
return Promise.all([value || defaultValue(), value !== undefined]);
|
|
43
|
+
}).then(([value, exists]) => {
|
|
44
|
+
return Promise.all([value, exists || events.miss(value)]);
|
|
45
|
+
}).then(([value]) => value);
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
set(key, value) {
|
|
49
|
+
return Promise.resolve().then(() => {
|
|
50
|
+
const namespace = getNamespace();
|
|
51
|
+
namespace[JSON.stringify(key)] = value;
|
|
52
|
+
getStorage().setItem(namespaceKey, JSON.stringify(namespace));
|
|
53
|
+
return value;
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
delete(key) {
|
|
58
|
+
return Promise.resolve().then(() => {
|
|
59
|
+
const namespace = getNamespace();
|
|
60
|
+
delete namespace[JSON.stringify(key)];
|
|
61
|
+
getStorage().setItem(namespaceKey, JSON.stringify(namespace));
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
clear() {
|
|
66
|
+
return Promise.resolve().then(() => {
|
|
67
|
+
getStorage().removeItem(namespaceKey);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
};
|
|
85
72
|
}
|
|
86
73
|
|
|
87
|
-
function createNullCache() {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
74
|
+
function createNullCache() {
|
|
75
|
+
return {
|
|
76
|
+
get(_key, defaultValue, events = {
|
|
77
|
+
miss: () => Promise.resolve()
|
|
78
|
+
}) {
|
|
79
|
+
const value = defaultValue();
|
|
80
|
+
return value.then(result => Promise.all([result, events.miss(result)])).then(([result]) => result);
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
set(_key, value) {
|
|
84
|
+
return Promise.resolve(value);
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
delete(_key) {
|
|
88
|
+
return Promise.resolve();
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
clear() {
|
|
92
|
+
return Promise.resolve();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
};
|
|
107
96
|
}
|
|
108
97
|
|
|
109
|
-
function
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
98
|
+
function createFallbackableCache(options) {
|
|
99
|
+
const caches = [...options.caches];
|
|
100
|
+
const current = caches.shift();
|
|
101
|
+
|
|
102
|
+
if (current === undefined) {
|
|
103
|
+
return createNullCache();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
get(key, defaultValue, events = {
|
|
108
|
+
miss: () => Promise.resolve()
|
|
109
|
+
}) {
|
|
110
|
+
return current.get(key, defaultValue, events).catch(() => {
|
|
111
|
+
return createFallbackableCache({
|
|
112
|
+
caches
|
|
113
|
+
}).get(key, defaultValue, events);
|
|
114
|
+
});
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
set(key, value) {
|
|
118
|
+
return current.set(key, value).catch(() => {
|
|
119
|
+
return createFallbackableCache({
|
|
120
|
+
caches
|
|
121
|
+
}).set(key, value);
|
|
122
|
+
});
|
|
123
|
+
},
|
|
124
|
+
|
|
125
|
+
delete(key) {
|
|
126
|
+
return current.delete(key).catch(() => {
|
|
127
|
+
return createFallbackableCache({
|
|
128
|
+
caches
|
|
129
|
+
}).delete(key);
|
|
130
|
+
});
|
|
131
|
+
},
|
|
132
|
+
|
|
133
|
+
clear() {
|
|
134
|
+
return current.clear().catch(() => {
|
|
135
|
+
return createFallbackableCache({
|
|
136
|
+
caches
|
|
137
|
+
}).clear();
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
};
|
|
139
142
|
}
|
|
140
143
|
|
|
141
|
-
function
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
144
|
+
function createMemoryCache(options = {
|
|
145
|
+
serializable: true
|
|
146
|
+
}) {
|
|
147
|
+
let cache = {};
|
|
148
|
+
return {
|
|
149
|
+
get(key, defaultValue, events = {
|
|
150
|
+
miss: () => Promise.resolve()
|
|
151
|
+
}) {
|
|
152
|
+
const keyAsString = JSON.stringify(key);
|
|
153
|
+
|
|
154
|
+
if (keyAsString in cache) {
|
|
155
|
+
return Promise.resolve(options.serializable ? JSON.parse(cache[keyAsString]) : cache[keyAsString]);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const promise = defaultValue();
|
|
159
|
+
return promise.then(value => events.miss(value)).then(() => promise);
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
set(key, value) {
|
|
163
|
+
cache[JSON.stringify(key)] = options.serializable ? JSON.stringify(value) : value;
|
|
164
|
+
return Promise.resolve(value);
|
|
165
|
+
},
|
|
166
|
+
|
|
167
|
+
delete(key) {
|
|
168
|
+
delete cache[JSON.stringify(key)];
|
|
169
|
+
return Promise.resolve();
|
|
170
|
+
},
|
|
171
|
+
|
|
172
|
+
clear() {
|
|
173
|
+
cache = {};
|
|
174
|
+
return Promise.resolve();
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
};
|
|
154
178
|
}
|
|
155
179
|
|
|
156
|
-
//
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
return base;
|
|
177
|
-
}
|
|
178
|
-
function encode(format, ...args) {
|
|
179
|
-
// eslint-disable-next-line functional/no-let
|
|
180
|
-
let i = 0;
|
|
181
|
-
return format.replace(/%s/g, () => encodeURIComponent(args[i++]));
|
|
180
|
+
// By default, API Clients at Algolia have expiration delay of 5 mins.
|
|
181
|
+
// In the JavaScript client, we have 2 mins.
|
|
182
|
+
const EXPIRATION_DELAY = 2 * 60 * 1000;
|
|
183
|
+
function createStatefulHost(host, status = 'up') {
|
|
184
|
+
const lastUpdate = Date.now();
|
|
185
|
+
|
|
186
|
+
function isUp() {
|
|
187
|
+
return status === 'up' || Date.now() - lastUpdate > EXPIRATION_DELAY;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function isTimedOut() {
|
|
191
|
+
return status === 'timed out' && Date.now() - lastUpdate <= EXPIRATION_DELAY;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return { ...host,
|
|
195
|
+
status,
|
|
196
|
+
lastUpdate,
|
|
197
|
+
isUp,
|
|
198
|
+
isTimedOut
|
|
199
|
+
};
|
|
182
200
|
}
|
|
183
201
|
|
|
184
|
-
|
|
202
|
+
function _defineProperty(obj, key, value) {
|
|
203
|
+
if (key in obj) {
|
|
204
|
+
Object.defineProperty(obj, key, {
|
|
205
|
+
value: value,
|
|
206
|
+
enumerable: true,
|
|
207
|
+
configurable: true,
|
|
208
|
+
writable: true
|
|
209
|
+
});
|
|
210
|
+
} else {
|
|
211
|
+
obj[key] = value;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return obj;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
class AlgoliaError extends Error {
|
|
218
|
+
constructor(message, name) {
|
|
219
|
+
super(message);
|
|
220
|
+
|
|
221
|
+
_defineProperty(this, "name", 'AlgoliaError');
|
|
222
|
+
|
|
223
|
+
if (name) {
|
|
224
|
+
this.name = name;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
185
227
|
|
|
186
|
-
const AuthMode = {
|
|
187
|
-
/**
|
|
188
|
-
* If auth credentials should be in query parameters.
|
|
189
|
-
*/
|
|
190
|
-
WithinQueryParameters: 0,
|
|
191
|
-
/**
|
|
192
|
-
* If auth credentials should be in headers.
|
|
193
|
-
*/
|
|
194
|
-
WithinHeaders: 1,
|
|
195
|
-
};
|
|
196
|
-
|
|
197
|
-
const LogLevelEnum = {
|
|
198
|
-
Debug: 1,
|
|
199
|
-
Info: 2,
|
|
200
|
-
Error: 3,
|
|
201
|
-
};
|
|
202
|
-
|
|
203
|
-
/* eslint no-console: 0 */
|
|
204
|
-
function createConsoleLogger(logLevel) {
|
|
205
|
-
return {
|
|
206
|
-
debug(message, args) {
|
|
207
|
-
if (LogLevelEnum.Debug >= logLevel) {
|
|
208
|
-
console.debug(message, args);
|
|
209
|
-
}
|
|
210
|
-
return Promise.resolve();
|
|
211
|
-
},
|
|
212
|
-
info(message, args) {
|
|
213
|
-
if (LogLevelEnum.Info >= logLevel) {
|
|
214
|
-
console.info(message, args);
|
|
215
|
-
}
|
|
216
|
-
return Promise.resolve();
|
|
217
|
-
},
|
|
218
|
-
error(message, args) {
|
|
219
|
-
console.error(message, args);
|
|
220
|
-
return Promise.resolve();
|
|
221
|
-
},
|
|
222
|
-
};
|
|
223
228
|
}
|
|
229
|
+
class ErrorWithStackTrace extends AlgoliaError {
|
|
230
|
+
constructor(message, stackTrace, name) {
|
|
231
|
+
super(message, name); // the array and object should be frozen to reflect the stackTrace at the time of the error
|
|
232
|
+
|
|
233
|
+
_defineProperty(this, "stackTrace", void 0);
|
|
234
|
+
|
|
235
|
+
this.stackTrace = stackTrace;
|
|
236
|
+
}
|
|
224
237
|
|
|
225
|
-
function createBrowserXhrRequester() {
|
|
226
|
-
return {
|
|
227
|
-
send(request) {
|
|
228
|
-
return new Promise((resolve) => {
|
|
229
|
-
const baseRequester = new XMLHttpRequest();
|
|
230
|
-
baseRequester.open(request.method, request.url, true);
|
|
231
|
-
Object.keys(request.headers).forEach(key => baseRequester.setRequestHeader(key, request.headers[key]));
|
|
232
|
-
const createTimeout = (timeout, content) => {
|
|
233
|
-
return setTimeout(() => {
|
|
234
|
-
baseRequester.abort();
|
|
235
|
-
resolve({
|
|
236
|
-
status: 0,
|
|
237
|
-
content,
|
|
238
|
-
isTimedOut: true,
|
|
239
|
-
});
|
|
240
|
-
}, timeout * 1000);
|
|
241
|
-
};
|
|
242
|
-
const connectTimeout = createTimeout(request.connectTimeout, 'Connection timeout');
|
|
243
|
-
// eslint-disable-next-line functional/no-let
|
|
244
|
-
let responseTimeout;
|
|
245
|
-
// eslint-disable-next-line functional/immutable-data
|
|
246
|
-
baseRequester.onreadystatechange = () => {
|
|
247
|
-
if (baseRequester.readyState > baseRequester.OPENED && responseTimeout === undefined) {
|
|
248
|
-
clearTimeout(connectTimeout);
|
|
249
|
-
responseTimeout = createTimeout(request.responseTimeout, 'Socket timeout');
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
// eslint-disable-next-line functional/immutable-data
|
|
253
|
-
baseRequester.onerror = () => {
|
|
254
|
-
// istanbul ignore next
|
|
255
|
-
if (baseRequester.status === 0) {
|
|
256
|
-
clearTimeout(connectTimeout);
|
|
257
|
-
clearTimeout(responseTimeout);
|
|
258
|
-
resolve({
|
|
259
|
-
content: baseRequester.responseText || 'Network request failed',
|
|
260
|
-
status: baseRequester.status,
|
|
261
|
-
isTimedOut: false,
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
};
|
|
265
|
-
// eslint-disable-next-line functional/immutable-data
|
|
266
|
-
baseRequester.onload = () => {
|
|
267
|
-
clearTimeout(connectTimeout);
|
|
268
|
-
clearTimeout(responseTimeout);
|
|
269
|
-
resolve({
|
|
270
|
-
content: baseRequester.responseText,
|
|
271
|
-
status: baseRequester.status,
|
|
272
|
-
isTimedOut: false,
|
|
273
|
-
});
|
|
274
|
-
};
|
|
275
|
-
baseRequester.send(request.data);
|
|
276
|
-
});
|
|
277
|
-
},
|
|
278
|
-
};
|
|
279
238
|
}
|
|
239
|
+
class RetryError extends ErrorWithStackTrace {
|
|
240
|
+
constructor(stackTrace) {
|
|
241
|
+
super('Unreachable hosts - your application id may be incorrect. If the error persists, contact support@algolia.com.', stackTrace, 'RetryError');
|
|
242
|
+
}
|
|
280
243
|
|
|
281
|
-
function createMappedRequestOptions(requestOptions, timeout) {
|
|
282
|
-
const options = requestOptions || {};
|
|
283
|
-
const data = options.data || {};
|
|
284
|
-
Object.keys(options).forEach(key => {
|
|
285
|
-
if (['timeout', 'headers', 'queryParameters', 'data', 'cacheable'].indexOf(key) === -1) {
|
|
286
|
-
data[key] = options[key]; // eslint-disable-line functional/immutable-data
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
return {
|
|
290
|
-
data: Object.entries(data).length > 0 ? data : undefined,
|
|
291
|
-
timeout: options.timeout || timeout,
|
|
292
|
-
headers: options.headers || {},
|
|
293
|
-
queryParameters: options.queryParameters || {},
|
|
294
|
-
cacheable: options.cacheable,
|
|
295
|
-
};
|
|
296
244
|
}
|
|
245
|
+
class ApiError extends ErrorWithStackTrace {
|
|
246
|
+
constructor(message, status, stackTrace) {
|
|
247
|
+
super(message, stackTrace, 'ApiError');
|
|
248
|
+
|
|
249
|
+
_defineProperty(this, "status", void 0);
|
|
250
|
+
|
|
251
|
+
this.status = status;
|
|
252
|
+
}
|
|
297
253
|
|
|
298
|
-
const CallEnum = {
|
|
299
|
-
/**
|
|
300
|
-
* If the host is read only.
|
|
301
|
-
*/
|
|
302
|
-
Read: 1,
|
|
303
|
-
/**
|
|
304
|
-
* If the host is write only.
|
|
305
|
-
*/
|
|
306
|
-
Write: 2,
|
|
307
|
-
/**
|
|
308
|
-
* If the host is both read and write.
|
|
309
|
-
*/
|
|
310
|
-
Any: 3,
|
|
311
|
-
};
|
|
312
|
-
|
|
313
|
-
const HostStatusEnum = {
|
|
314
|
-
Up: 1,
|
|
315
|
-
Down: 2,
|
|
316
|
-
Timeouted: 3,
|
|
317
|
-
};
|
|
318
|
-
|
|
319
|
-
// By default, API Clients at Algolia have expiration delay
|
|
320
|
-
// of 5 mins. In the JavaScript client, we have 2 mins.
|
|
321
|
-
const EXPIRATION_DELAY = 2 * 60 * 1000;
|
|
322
|
-
function createStatefulHost(host, status = HostStatusEnum.Up) {
|
|
323
|
-
return {
|
|
324
|
-
...host,
|
|
325
|
-
status,
|
|
326
|
-
lastUpdate: Date.now(),
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
function isStatefulHostUp(host) {
|
|
330
|
-
return host.status === HostStatusEnum.Up || Date.now() - host.lastUpdate > EXPIRATION_DELAY;
|
|
331
|
-
}
|
|
332
|
-
function isStatefulHostTimeouted(host) {
|
|
333
|
-
return (host.status === HostStatusEnum.Timeouted && Date.now() - host.lastUpdate <= EXPIRATION_DELAY);
|
|
334
254
|
}
|
|
255
|
+
class DeserializationError extends AlgoliaError {
|
|
256
|
+
constructor(message, response) {
|
|
257
|
+
super(message, 'DeserializationError');
|
|
258
|
+
|
|
259
|
+
_defineProperty(this, "response", void 0);
|
|
260
|
+
|
|
261
|
+
this.response = response;
|
|
262
|
+
}
|
|
335
263
|
|
|
336
|
-
function createStatelessHost(options) {
|
|
337
|
-
if (typeof options === 'string') {
|
|
338
|
-
return {
|
|
339
|
-
protocol: 'https',
|
|
340
|
-
url: options,
|
|
341
|
-
accept: CallEnum.Any,
|
|
342
|
-
};
|
|
343
|
-
}
|
|
344
|
-
return {
|
|
345
|
-
protocol: options.protocol || 'https',
|
|
346
|
-
url: options.url,
|
|
347
|
-
accept: options.accept || CallEnum.Any,
|
|
348
|
-
};
|
|
349
264
|
}
|
|
350
265
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
});
|
|
363
|
-
})).then(statefulHosts => {
|
|
364
|
-
const hostsUp = statefulHosts.filter(host => isStatefulHostUp(host));
|
|
365
|
-
const hostsTimeouted = statefulHosts.filter(host => isStatefulHostTimeouted(host));
|
|
366
|
-
/**
|
|
367
|
-
* Note, we put the hosts that previously timeouted on the end of the list.
|
|
368
|
-
*/
|
|
369
|
-
const hostsAvailable = [...hostsUp, ...hostsTimeouted];
|
|
370
|
-
const statelessHostsAvailable = hostsAvailable.length > 0
|
|
371
|
-
? hostsAvailable.map(host => createStatelessHost(host))
|
|
372
|
-
: statelessHosts;
|
|
373
|
-
return {
|
|
374
|
-
getTimeout(timeoutsCount, baseTimeout) {
|
|
375
|
-
/**
|
|
376
|
-
* Imagine that you have 4 hosts, if timeouts will increase
|
|
377
|
-
* on the following way: 1 (timeouted) > 4 (timeouted) > 5 (200)
|
|
378
|
-
*
|
|
379
|
-
* Note that, the very next request, we start from the previous timeout
|
|
380
|
-
*
|
|
381
|
-
* 5 (timeouted) > 6 (timeouted) > 7 ...
|
|
382
|
-
*
|
|
383
|
-
* This strategy may need to be reviewed, but is the strategy on the our
|
|
384
|
-
* current v3 version.
|
|
385
|
-
*/
|
|
386
|
-
const timeoutMultiplier = hostsTimeouted.length === 0 && timeoutsCount === 0
|
|
387
|
-
? 1
|
|
388
|
-
: hostsTimeouted.length + 3 + timeoutsCount;
|
|
389
|
-
return timeoutMultiplier * baseTimeout;
|
|
390
|
-
},
|
|
391
|
-
statelessHosts: statelessHostsAvailable,
|
|
392
|
-
};
|
|
393
|
-
});
|
|
266
|
+
function shuffle(array) {
|
|
267
|
+
const shuffledArray = array;
|
|
268
|
+
|
|
269
|
+
for (let c = array.length - 1; c > 0; c--) {
|
|
270
|
+
const b = Math.floor(Math.random() * (c + 1));
|
|
271
|
+
const a = array[c];
|
|
272
|
+
shuffledArray[c] = array[b];
|
|
273
|
+
shuffledArray[b] = a;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
return shuffledArray;
|
|
394
277
|
}
|
|
278
|
+
function serializeUrl(host, path, queryParameters) {
|
|
279
|
+
const queryParametersAsString = serializeQueryParameters(queryParameters);
|
|
280
|
+
let url = `${host.protocol}://${host.url}/${path.charAt(0) === '/' ? path.substr(1) : path}`;
|
|
395
281
|
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
282
|
+
if (queryParametersAsString.length) {
|
|
283
|
+
url += `?${queryParametersAsString}`;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
return url;
|
|
287
|
+
}
|
|
288
|
+
function serializeQueryParameters(parameters) {
|
|
289
|
+
const isObjectOrArray = value => Object.prototype.toString.call(value) === '[object Object]' || Object.prototype.toString.call(value) === '[object Array]';
|
|
290
|
+
|
|
291
|
+
return Object.keys(parameters).map(key => `${key}=${isObjectOrArray(parameters[key]) ? JSON.stringify(parameters[key]) : parameters[key]}`).join('&');
|
|
292
|
+
}
|
|
293
|
+
function serializeData(request, requestOptions) {
|
|
294
|
+
if (request.method === 'GET' || request.data === undefined && requestOptions.data === undefined) {
|
|
295
|
+
return undefined;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const data = Array.isArray(request.data) ? request.data : { ...request.data,
|
|
299
|
+
...requestOptions.data
|
|
300
|
+
};
|
|
301
|
+
return JSON.stringify(data);
|
|
302
|
+
}
|
|
303
|
+
function serializeHeaders(baseHeaders, requestHeaders, requestOptionsHeaders) {
|
|
304
|
+
const headers = {
|
|
305
|
+
Accept: 'application/json',
|
|
306
|
+
...baseHeaders,
|
|
307
|
+
...requestHeaders,
|
|
308
|
+
...requestOptionsHeaders
|
|
309
|
+
};
|
|
310
|
+
const serializedHeaders = {};
|
|
311
|
+
Object.keys(headers).forEach(header => {
|
|
312
|
+
const value = headers[header];
|
|
313
|
+
serializedHeaders[header.toLowerCase()] = value;
|
|
314
|
+
});
|
|
315
|
+
return serializedHeaders;
|
|
316
|
+
}
|
|
317
|
+
function deserializeSuccess(response) {
|
|
318
|
+
try {
|
|
319
|
+
return JSON.parse(response.content);
|
|
320
|
+
} catch (e) {
|
|
321
|
+
throw new DeserializationError(e.message, response);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
function deserializeFailure({
|
|
325
|
+
content,
|
|
326
|
+
status
|
|
327
|
+
}, stackFrame) {
|
|
328
|
+
let message = content;
|
|
329
|
+
|
|
330
|
+
try {
|
|
331
|
+
message = JSON.parse(content).message;
|
|
332
|
+
} catch (e) {// ..
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return new ApiError(message, status, stackFrame);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function isNetworkError({
|
|
339
|
+
isTimedOut,
|
|
340
|
+
status
|
|
341
|
+
}) {
|
|
342
|
+
return !isTimedOut && ~~status === 0;
|
|
343
|
+
}
|
|
344
|
+
function isRetryable({
|
|
345
|
+
isTimedOut,
|
|
346
|
+
status
|
|
347
|
+
}) {
|
|
348
|
+
return isTimedOut || isNetworkError({
|
|
349
|
+
isTimedOut,
|
|
350
|
+
status
|
|
351
|
+
}) || ~~(status / 100) !== 2 && ~~(status / 100) !== 4;
|
|
352
|
+
}
|
|
353
|
+
function isSuccess({
|
|
354
|
+
status
|
|
355
|
+
}) {
|
|
356
|
+
return ~~(status / 100) === 2;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function stackTraceWithoutCredentials(stackTrace) {
|
|
360
|
+
return stackTrace.map(stackFrame => stackFrameWithoutCredentials(stackFrame));
|
|
361
|
+
}
|
|
362
|
+
function stackFrameWithoutCredentials(stackFrame) {
|
|
363
|
+
const modifiedHeaders = stackFrame.request.headers['x-algolia-api-key'] ? {
|
|
364
|
+
'x-algolia-api-key': '*****'
|
|
365
|
+
} : {};
|
|
366
|
+
return { ...stackFrame,
|
|
367
|
+
request: { ...stackFrame.request,
|
|
368
|
+
headers: { ...stackFrame.request.headers,
|
|
369
|
+
...modifiedHeaders
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
}
|
|
416
374
|
|
|
417
|
-
function
|
|
418
|
-
|
|
375
|
+
function createTransporter({
|
|
376
|
+
hosts,
|
|
377
|
+
hostsCache,
|
|
378
|
+
baseHeaders,
|
|
379
|
+
baseQueryParameters,
|
|
380
|
+
algoliaAgent,
|
|
381
|
+
timeouts,
|
|
382
|
+
requester,
|
|
383
|
+
requestsCache,
|
|
384
|
+
responsesCache
|
|
385
|
+
}) {
|
|
386
|
+
async function createRetryableOptions(compatibleHosts) {
|
|
387
|
+
const statefulHosts = await Promise.all(compatibleHosts.map(compatibleHost => {
|
|
388
|
+
return hostsCache.get(compatibleHost, () => {
|
|
389
|
+
return Promise.resolve(createStatefulHost(compatibleHost));
|
|
390
|
+
});
|
|
391
|
+
}));
|
|
392
|
+
const hostsUp = statefulHosts.filter(host => host.isUp());
|
|
393
|
+
const hostsTimedOut = statefulHosts.filter(host => host.isTimedOut()); // Note, we put the hosts that previously timed out on the end of the list.
|
|
394
|
+
|
|
395
|
+
const hostsAvailable = [...hostsUp, ...hostsTimedOut];
|
|
396
|
+
const compatibleHostsAvailable = hostsAvailable.length > 0 ? hostsAvailable : compatibleHosts;
|
|
397
|
+
return {
|
|
398
|
+
hosts: compatibleHostsAvailable,
|
|
399
|
+
|
|
400
|
+
getTimeout(timeoutsCount, baseTimeout) {
|
|
401
|
+
/**
|
|
402
|
+
* Imagine that you have 4 hosts, if timeouts will increase
|
|
403
|
+
* on the following way: 1 (timed out) > 4 (timed out) > 5 (200).
|
|
404
|
+
*
|
|
405
|
+
* Note that, the very next request, we start from the previous timeout.
|
|
406
|
+
*
|
|
407
|
+
* 5 (timed out) > 6 (timed out) > 7 ...
|
|
408
|
+
*
|
|
409
|
+
* This strategy may need to be reviewed, but is the strategy on the our
|
|
410
|
+
* current v3 version.
|
|
411
|
+
*/
|
|
412
|
+
const timeoutMultiplier = hostsTimedOut.length === 0 && timeoutsCount === 0 ? 1 : hostsTimedOut.length + 3 + timeoutsCount;
|
|
413
|
+
return timeoutMultiplier * baseTimeout;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
async function retryableRequest(request, requestOptions, isRead = true) {
|
|
420
|
+
const stackTrace = [];
|
|
419
421
|
/**
|
|
420
422
|
* First we prepare the payload that do not depend from hosts.
|
|
421
|
-
*/
|
|
422
|
-
|
|
423
|
-
const
|
|
424
|
-
const
|
|
425
|
-
|
|
426
|
-
const dataQueryParameters = request.method
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
423
|
+
*/
|
|
424
|
+
|
|
425
|
+
const data = serializeData(request, requestOptions);
|
|
426
|
+
const headers = serializeHeaders(baseHeaders, request.headers, requestOptions.headers); // On `GET`, the data is proxied to query parameters.
|
|
427
|
+
|
|
428
|
+
const dataQueryParameters = request.method === 'GET' ? { ...request.data,
|
|
429
|
+
...requestOptions.data
|
|
430
|
+
} : {};
|
|
431
|
+
const queryParameters = {
|
|
432
|
+
'x-algolia-agent': algoliaAgent.value,
|
|
433
|
+
...baseQueryParameters,
|
|
434
|
+
...request.queryParameters,
|
|
435
|
+
...dataQueryParameters
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
if (requestOptions && requestOptions.queryParameters) {
|
|
439
|
+
for (const key of Object.keys(requestOptions.queryParameters)) {
|
|
440
|
+
// We want to keep `undefined` and `null` values,
|
|
441
|
+
// but also avoid stringifying `object`s, as they are
|
|
442
|
+
// handled in the `serializeUrl` step right after.
|
|
443
|
+
if (!requestOptions.queryParameters[key] || Object.prototype.toString.call(requestOptions.queryParameters[key]) === '[object Object]') {
|
|
444
|
+
queryParameters[key] = requestOptions.queryParameters[key];
|
|
445
|
+
} else {
|
|
446
|
+
queryParameters[key] = requestOptions.queryParameters[key].toString();
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
let timeoutsCount = 0;
|
|
452
|
+
|
|
453
|
+
const retry = async (retryableHosts, getTimeout) => {
|
|
454
|
+
/**
|
|
455
|
+
* We iterate on each host, until there is no host left.
|
|
456
|
+
*/
|
|
457
|
+
const host = retryableHosts.pop();
|
|
458
|
+
|
|
459
|
+
if (host === undefined) {
|
|
460
|
+
throw new RetryError(stackTraceWithoutCredentials(stackTrace));
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
let responseTimeout = requestOptions.timeout;
|
|
464
|
+
|
|
465
|
+
if (responseTimeout === undefined) {
|
|
466
|
+
responseTimeout = isRead ? timeouts.read : timeouts.write;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const payload = {
|
|
470
|
+
data,
|
|
471
|
+
headers,
|
|
472
|
+
method: request.method,
|
|
473
|
+
url: serializeUrl(host, request.path, queryParameters),
|
|
474
|
+
connectTimeout: getTimeout(timeoutsCount, timeouts.connect),
|
|
475
|
+
responseTimeout: getTimeout(timeoutsCount, responseTimeout)
|
|
476
|
+
};
|
|
477
|
+
/**
|
|
478
|
+
* The stackFrame is pushed to the stackTrace so we
|
|
479
|
+
* can have information about onRetry and onFailure
|
|
480
|
+
* decisions.
|
|
481
|
+
*/
|
|
482
|
+
|
|
483
|
+
const pushToStackTrace = response => {
|
|
484
|
+
const stackFrame = {
|
|
485
|
+
request: payload,
|
|
486
|
+
response,
|
|
487
|
+
host,
|
|
488
|
+
triesLeft: retryableHosts.length
|
|
489
|
+
};
|
|
490
|
+
stackTrace.push(stackFrame);
|
|
491
|
+
return stackFrame;
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
const response = await requester.send(payload);
|
|
495
|
+
|
|
496
|
+
if (isRetryable(response)) {
|
|
497
|
+
const stackFrame = pushToStackTrace(response); // If response is a timeout, we increase the number of timeouts so we can increase the timeout later.
|
|
498
|
+
|
|
499
|
+
if (response.isTimedOut) {
|
|
500
|
+
timeoutsCount++;
|
|
501
|
+
}
|
|
441
502
|
/**
|
|
442
|
-
*
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
headers,
|
|
451
|
-
method,
|
|
452
|
-
url: serializeUrl(host, request.path, queryParameters),
|
|
453
|
-
connectTimeout: getTimeout(timeoutsCount, transporter.timeouts.connect),
|
|
454
|
-
responseTimeout: getTimeout(timeoutsCount, requestOptions.timeout),
|
|
455
|
-
};
|
|
503
|
+
* Failures are individually sent to the logger, allowing
|
|
504
|
+
* the end user to debug / store stack frames even
|
|
505
|
+
* when a retry error does not happen.
|
|
506
|
+
*/
|
|
507
|
+
// eslint-disable-next-line no-console -- this will be fixed by exposing a `logger` to the transporter
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
console.log('Retryable failure', stackFrameWithoutCredentials(stackFrame));
|
|
456
511
|
/**
|
|
457
|
-
*
|
|
458
|
-
*
|
|
459
|
-
*
|
|
460
|
-
*/
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
onSuccess: response => deserializeSuccess(response),
|
|
474
|
-
onRetry(response) {
|
|
475
|
-
const stackFrame = pushToStackTrace(response);
|
|
476
|
-
/**
|
|
477
|
-
* If response is a timeout, we increaset the number of
|
|
478
|
-
* timeouts so we can increase the timeout later.
|
|
479
|
-
*/
|
|
480
|
-
if (response.isTimedOut) {
|
|
481
|
-
timeoutsCount++;
|
|
482
|
-
}
|
|
483
|
-
return Promise.all([
|
|
484
|
-
/**
|
|
485
|
-
* Failures are individually send the logger, allowing
|
|
486
|
-
* the end user to debug / store stack frames even
|
|
487
|
-
* when a retry error does not happen.
|
|
488
|
-
*/
|
|
489
|
-
transporter.logger.info('Retryable failure', stackFrameWithoutCredentials(stackFrame)),
|
|
490
|
-
/**
|
|
491
|
-
* We also store the state of the host in failure cases. If the host, is
|
|
492
|
-
* down it will remain down for the next 2 minutes. In a timeout situation,
|
|
493
|
-
* this host will be added end of the list of hosts on the next request.
|
|
494
|
-
*/
|
|
495
|
-
transporter.hostsCache.set(host, createStatefulHost(host, response.isTimedOut ? HostStatusEnum.Timeouted : HostStatusEnum.Down)),
|
|
496
|
-
]).then(() => retry(hosts, getTimeout));
|
|
497
|
-
},
|
|
498
|
-
onFail(response) {
|
|
499
|
-
pushToStackTrace(response);
|
|
500
|
-
throw deserializeFailure(response, stackTraceWithoutCredentials(stackTrace));
|
|
501
|
-
},
|
|
502
|
-
};
|
|
503
|
-
return transporter.requester.send(payload).then(response => {
|
|
504
|
-
return retryDecision(response, decisions);
|
|
505
|
-
});
|
|
506
|
-
};
|
|
512
|
+
* We also store the state of the host in failure cases. If the host, is
|
|
513
|
+
* down it will remain down for the next 2 minutes. In a timeout situation,
|
|
514
|
+
* this host will be added end of the list of hosts on the next request.
|
|
515
|
+
*/
|
|
516
|
+
|
|
517
|
+
await hostsCache.set(host, createStatefulHost(host, response.isTimedOut ? 'timed out' : 'down'));
|
|
518
|
+
return retry(retryableHosts, getTimeout);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
if (isSuccess(response)) {
|
|
522
|
+
return deserializeSuccess(response);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
pushToStackTrace(response);
|
|
526
|
+
throw deserializeFailure(response, stackTrace);
|
|
527
|
+
};
|
|
507
528
|
/**
|
|
508
529
|
* Finally, for each retryable host perform request until we got a non
|
|
509
530
|
* retryable response. Some notes here:
|
|
@@ -511,387 +532,426 @@ function retryableRequest(transporter, statelessHosts, request, requestOptions)
|
|
|
511
532
|
* 1. The reverse here is applied so we can apply a `pop` later on => more performant.
|
|
512
533
|
* 2. We also get from the retryable options a timeout multiplier that is tailored
|
|
513
534
|
* for the current context.
|
|
514
|
-
*/
|
|
515
|
-
return createRetryableOptions(transporter.hostsCache, statelessHosts).then(options => {
|
|
516
|
-
return retry([...options.statelessHosts].reverse(), options.getTimeout);
|
|
517
|
-
});
|
|
518
|
-
}
|
|
535
|
+
*/
|
|
519
536
|
|
|
520
|
-
function createTransporter(options) {
|
|
521
|
-
const { hostsCache, logger, requester, requestsCache, responsesCache, timeouts, userAgent, hosts, queryParameters, headers, } = options;
|
|
522
|
-
const transporter = {
|
|
523
|
-
hostsCache,
|
|
524
|
-
logger,
|
|
525
|
-
requester,
|
|
526
|
-
requestsCache,
|
|
527
|
-
responsesCache,
|
|
528
|
-
timeouts,
|
|
529
|
-
userAgent,
|
|
530
|
-
headers,
|
|
531
|
-
queryParameters,
|
|
532
|
-
hosts: hosts.map(host => createStatelessHost(host)),
|
|
533
|
-
read(request, requestOptions) {
|
|
534
|
-
/**
|
|
535
|
-
* First, we compute the user request options. Now, keep in mind,
|
|
536
|
-
* that using request options the user is able to modified the intire
|
|
537
|
-
* payload of the request. Such as headers, query parameters, and others.
|
|
538
|
-
*/
|
|
539
|
-
const mappedRequestOptions = createMappedRequestOptions(requestOptions, transporter.timeouts.read);
|
|
540
|
-
const createRetryableRequest = () => {
|
|
541
|
-
/**
|
|
542
|
-
* Then, we prepare a function factory that contains the construction of
|
|
543
|
-
* the retryable request. At this point, we may *not* perform the actual
|
|
544
|
-
* request. But we want to have the function factory ready.
|
|
545
|
-
*/
|
|
546
|
-
return retryableRequest(transporter, transporter.hosts.filter(host => (host.accept & CallEnum.Read) !== 0), request, mappedRequestOptions);
|
|
547
|
-
};
|
|
548
|
-
/**
|
|
549
|
-
* Once we have the function factory ready, we need to determine of the
|
|
550
|
-
* request is "cacheable" - should be cached. Note that, once again,
|
|
551
|
-
* the user can force this option.
|
|
552
|
-
*/
|
|
553
|
-
const cacheable = mappedRequestOptions.cacheable !== undefined
|
|
554
|
-
? mappedRequestOptions.cacheable
|
|
555
|
-
: request.cacheable;
|
|
556
|
-
/**
|
|
557
|
-
* If is not "cacheable", we immediatly trigger the retryable request, no
|
|
558
|
-
* need to check cache implementations.
|
|
559
|
-
*/
|
|
560
|
-
if (cacheable !== true) {
|
|
561
|
-
return createRetryableRequest();
|
|
562
|
-
}
|
|
563
|
-
/**
|
|
564
|
-
* If the request is "cacheable", we need to first compute the key to ask
|
|
565
|
-
* the cache implementations if this request is on progress or if the
|
|
566
|
-
* response already exists on the cache.
|
|
567
|
-
*/
|
|
568
|
-
const key = {
|
|
569
|
-
request,
|
|
570
|
-
mappedRequestOptions,
|
|
571
|
-
transporter: {
|
|
572
|
-
queryParameters: transporter.queryParameters,
|
|
573
|
-
headers: transporter.headers,
|
|
574
|
-
},
|
|
575
|
-
};
|
|
576
|
-
/**
|
|
577
|
-
* With the computed key, we first ask the responses cache
|
|
578
|
-
* implemention if this request was been resolved before.
|
|
579
|
-
*/
|
|
580
|
-
return transporter.responsesCache.get(key, () => {
|
|
581
|
-
/**
|
|
582
|
-
* If the request has never resolved before, we actually ask if there
|
|
583
|
-
* is a current request with the same key on progress.
|
|
584
|
-
*/
|
|
585
|
-
return transporter.requestsCache.get(key, () => {
|
|
586
|
-
return (transporter.requestsCache
|
|
587
|
-
/**
|
|
588
|
-
* Finally, if there is no request in progress with the same key,
|
|
589
|
-
* this `createRetryableRequest()` will actually trigger the
|
|
590
|
-
* retryable request.
|
|
591
|
-
*/
|
|
592
|
-
.set(key, createRetryableRequest())
|
|
593
|
-
.then(response => Promise.all([transporter.requestsCache.delete(key), response]), err => Promise.all([transporter.requestsCache.delete(key), Promise.reject(err)]))
|
|
594
|
-
.then(([_, response]) => response));
|
|
595
|
-
});
|
|
596
|
-
}, {
|
|
597
|
-
/**
|
|
598
|
-
* Of course, once we get this response back from the server, we
|
|
599
|
-
* tell response cache to actually store the received response
|
|
600
|
-
* to be used later.
|
|
601
|
-
*/
|
|
602
|
-
miss: response => transporter.responsesCache.set(key, response),
|
|
603
|
-
});
|
|
604
|
-
},
|
|
605
|
-
write(request, requestOptions) {
|
|
606
|
-
/**
|
|
607
|
-
* On write requests, no cache mechanisms are applied, and we
|
|
608
|
-
* proxy the request immediately to the requester.
|
|
609
|
-
*/
|
|
610
|
-
return retryableRequest(transporter, transporter.hosts.filter(host => (host.accept & CallEnum.Write) !== 0), request, createMappedRequestOptions(requestOptions, transporter.timeouts.write));
|
|
611
|
-
},
|
|
612
|
-
};
|
|
613
|
-
return transporter;
|
|
614
|
-
}
|
|
615
537
|
|
|
616
|
-
|
|
617
|
-
const
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
const addedUserAgent = `; ${options.segment}${options.version !== undefined ? ` (${options.version})` : ''}`;
|
|
621
|
-
if (userAgent.value.indexOf(addedUserAgent) === -1) {
|
|
622
|
-
// eslint-disable-next-line functional/immutable-data
|
|
623
|
-
userAgent.value = `${userAgent.value}${addedUserAgent}`;
|
|
624
|
-
}
|
|
625
|
-
return userAgent;
|
|
626
|
-
},
|
|
627
|
-
};
|
|
628
|
-
return userAgent;
|
|
629
|
-
}
|
|
538
|
+
const compatibleHosts = hosts.filter(host => host.accept === 'readWrite' || (isRead ? host.accept === 'read' : host.accept === 'write'));
|
|
539
|
+
const options = await createRetryableOptions(compatibleHosts);
|
|
540
|
+
return retry([...options.hosts].reverse(), options.getTimeout);
|
|
541
|
+
}
|
|
630
542
|
|
|
631
|
-
function
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
543
|
+
function createRequest(request, requestOptions = {}) {
|
|
544
|
+
/**
|
|
545
|
+
* A read request is either a `GET` request, or a request that we make
|
|
546
|
+
* via the `read` transporter (e.g. `search`).
|
|
547
|
+
*/
|
|
548
|
+
const isRead = request.useReadTransporter || request.method === 'GET';
|
|
549
|
+
|
|
550
|
+
if (!isRead) {
|
|
551
|
+
/**
|
|
552
|
+
* On write requests, no cache mechanisms are applied, and we
|
|
553
|
+
* proxy the request immediately to the requester.
|
|
554
|
+
*/
|
|
555
|
+
return retryableRequest(request, requestOptions, isRead);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
const createRetryableRequest = () => {
|
|
559
|
+
/**
|
|
560
|
+
* Then, we prepare a function factory that contains the construction of
|
|
561
|
+
* the retryable request. At this point, we may *not* perform the actual
|
|
562
|
+
* request. But we want to have the function factory ready.
|
|
563
|
+
*/
|
|
564
|
+
return retryableRequest(request, requestOptions);
|
|
565
|
+
};
|
|
566
|
+
/**
|
|
567
|
+
* Once we have the function factory ready, we need to determine of the
|
|
568
|
+
* request is "cacheable" - should be cached. Note that, once again,
|
|
569
|
+
* the user can force this option.
|
|
570
|
+
*/
|
|
652
571
|
|
|
653
|
-
function serializeUrl(host, path, queryParameters) {
|
|
654
|
-
const queryParametersAsString = serializeQueryParameters(queryParameters);
|
|
655
|
-
// eslint-disable-next-line functional/no-let
|
|
656
|
-
let url = `${host.protocol}://${host.url}/${path.charAt(0) === '/' ? path.substr(1) : path}`;
|
|
657
|
-
if (queryParametersAsString.length) {
|
|
658
|
-
url += `?${queryParametersAsString}`;
|
|
659
|
-
}
|
|
660
|
-
return url;
|
|
661
|
-
}
|
|
662
|
-
function serializeQueryParameters(parameters) {
|
|
663
|
-
const isObjectOrArray = (value) => Object.prototype.toString.call(value) === '[object Object]' ||
|
|
664
|
-
Object.prototype.toString.call(value) === '[object Array]';
|
|
665
|
-
return Object.keys(parameters)
|
|
666
|
-
.map(key => encode('%s=%s', key, isObjectOrArray(parameters[key]) ? JSON.stringify(parameters[key]) : parameters[key]))
|
|
667
|
-
.join('&');
|
|
668
|
-
}
|
|
669
|
-
function serializeData(request, requestOptions) {
|
|
670
|
-
if (request.method === MethodEnum.Get ||
|
|
671
|
-
(request.data === undefined && requestOptions.data === undefined)) {
|
|
672
|
-
return undefined;
|
|
673
|
-
}
|
|
674
|
-
const data = Array.isArray(request.data)
|
|
675
|
-
? request.data
|
|
676
|
-
: { ...request.data, ...requestOptions.data };
|
|
677
|
-
return JSON.stringify(data);
|
|
678
|
-
}
|
|
679
|
-
function serializeHeaders(transporter, requestOptions) {
|
|
680
|
-
const headers = {
|
|
681
|
-
...transporter.headers,
|
|
682
|
-
...requestOptions.headers,
|
|
683
|
-
};
|
|
684
|
-
const serializedHeaders = {};
|
|
685
|
-
Object.keys(headers).forEach(header => {
|
|
686
|
-
const value = headers[header];
|
|
687
|
-
// @ts-ignore
|
|
688
|
-
// eslint-disable-next-line functional/immutable-data
|
|
689
|
-
serializedHeaders[header.toLowerCase()] = value;
|
|
690
|
-
});
|
|
691
|
-
return serializedHeaders;
|
|
692
|
-
}
|
|
693
572
|
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
573
|
+
const cacheable = requestOptions.cacheable || request.cacheable;
|
|
574
|
+
/**
|
|
575
|
+
* If is not "cacheable", we immediately trigger the retryable request, no
|
|
576
|
+
* need to check cache implementations.
|
|
577
|
+
*/
|
|
578
|
+
|
|
579
|
+
if (cacheable !== true) {
|
|
580
|
+
return createRetryableRequest();
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* If the request is "cacheable", we need to first compute the key to ask
|
|
584
|
+
* the cache implementations if this request is on progress or if the
|
|
585
|
+
* response already exists on the cache.
|
|
586
|
+
*/
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
const key = {
|
|
590
|
+
request,
|
|
591
|
+
requestOptions,
|
|
592
|
+
transporter: {
|
|
593
|
+
queryParameters: baseQueryParameters,
|
|
594
|
+
headers: baseHeaders
|
|
595
|
+
}
|
|
596
|
+
};
|
|
597
|
+
/**
|
|
598
|
+
* With the computed key, we first ask the responses cache
|
|
599
|
+
* implementation if this request was been resolved before.
|
|
600
|
+
*/
|
|
601
|
+
|
|
602
|
+
return responsesCache.get(key, () => {
|
|
603
|
+
/**
|
|
604
|
+
* If the request has never resolved before, we actually ask if there
|
|
605
|
+
* is a current request with the same key on progress.
|
|
606
|
+
*/
|
|
607
|
+
return requestsCache.get(key, () =>
|
|
608
|
+
/**
|
|
609
|
+
* Finally, if there is no request in progress with the same key,
|
|
610
|
+
* this `createRetryableRequest()` will actually trigger the
|
|
611
|
+
* retryable request.
|
|
612
|
+
*/
|
|
613
|
+
requestsCache.set(key, createRetryableRequest()).then(response => Promise.all([requestsCache.delete(key), response]), err => Promise.all([requestsCache.delete(key), Promise.reject(err)])).then(([_, response]) => response));
|
|
614
|
+
}, {
|
|
615
|
+
/**
|
|
616
|
+
* Of course, once we get this response back from the server, we
|
|
617
|
+
* tell response cache to actually store the received response
|
|
618
|
+
* to be used later.
|
|
619
|
+
*/
|
|
620
|
+
miss: response => responsesCache.set(key, response)
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
return {
|
|
625
|
+
hostsCache,
|
|
626
|
+
requester,
|
|
627
|
+
timeouts,
|
|
628
|
+
algoliaAgent,
|
|
629
|
+
baseHeaders,
|
|
630
|
+
baseQueryParameters,
|
|
631
|
+
hosts,
|
|
632
|
+
request: createRequest,
|
|
633
|
+
requestsCache,
|
|
634
|
+
responsesCache
|
|
635
|
+
};
|
|
711
636
|
}
|
|
712
637
|
|
|
713
|
-
function
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
638
|
+
function createAlgoliaAgent(version) {
|
|
639
|
+
const algoliaAgent = {
|
|
640
|
+
value: `Algolia for JavaScript (${version})`,
|
|
641
|
+
|
|
642
|
+
add(options) {
|
|
643
|
+
const addedAlgoliaAgent = `; ${options.segment}${options.version !== undefined ? ` (${options.version})` : ''}`;
|
|
644
|
+
|
|
645
|
+
if (algoliaAgent.value.indexOf(addedAlgoliaAgent) === -1) {
|
|
646
|
+
algoliaAgent.value = `${algoliaAgent.value}${addedAlgoliaAgent}`;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
return algoliaAgent;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
};
|
|
653
|
+
return algoliaAgent;
|
|
720
654
|
}
|
|
721
655
|
|
|
722
|
-
function
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
656
|
+
function getAlgoliaAgent({
|
|
657
|
+
algoliaAgents,
|
|
658
|
+
client,
|
|
659
|
+
version
|
|
660
|
+
}) {
|
|
661
|
+
const defaultAlgoliaAgent = createAlgoliaAgent(version).add({
|
|
662
|
+
segment: client,
|
|
663
|
+
version
|
|
664
|
+
});
|
|
665
|
+
algoliaAgents.forEach(algoliaAgent => defaultAlgoliaAgent.add(algoliaAgent));
|
|
666
|
+
return defaultAlgoliaAgent;
|
|
728
667
|
}
|
|
729
668
|
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
669
|
+
const DEFAULT_CONNECT_TIMEOUT_BROWSER = 1000;
|
|
670
|
+
const DEFAULT_READ_TIMEOUT_BROWSER = 2000;
|
|
671
|
+
const DEFAULT_WRITE_TIMEOUT_BROWSER = 30000;
|
|
672
|
+
|
|
673
|
+
function createXhrRequester() {
|
|
674
|
+
function send(request) {
|
|
675
|
+
return new Promise((resolve) => {
|
|
676
|
+
const baseRequester = new XMLHttpRequest();
|
|
677
|
+
baseRequester.open(request.method, request.url, true);
|
|
678
|
+
Object.keys(request.headers).forEach((key) => baseRequester.setRequestHeader(key, request.headers[key]));
|
|
679
|
+
const createTimeout = (timeout, content) => {
|
|
680
|
+
return setTimeout(() => {
|
|
681
|
+
baseRequester.abort();
|
|
682
|
+
resolve({
|
|
683
|
+
status: 0,
|
|
684
|
+
content,
|
|
685
|
+
isTimedOut: true,
|
|
686
|
+
});
|
|
687
|
+
}, timeout);
|
|
688
|
+
};
|
|
689
|
+
const connectTimeout = createTimeout(request.connectTimeout, 'Connection timeout');
|
|
690
|
+
let responseTimeout;
|
|
691
|
+
baseRequester.onreadystatechange = () => {
|
|
692
|
+
if (baseRequester.readyState > baseRequester.OPENED &&
|
|
693
|
+
responseTimeout === undefined) {
|
|
694
|
+
clearTimeout(connectTimeout);
|
|
695
|
+
responseTimeout = createTimeout(request.responseTimeout, 'Socket timeout');
|
|
696
|
+
}
|
|
697
|
+
};
|
|
698
|
+
baseRequester.onerror = () => {
|
|
699
|
+
// istanbul ignore next
|
|
700
|
+
if (baseRequester.status === 0) {
|
|
701
|
+
clearTimeout(connectTimeout);
|
|
702
|
+
clearTimeout(responseTimeout);
|
|
703
|
+
resolve({
|
|
704
|
+
content: baseRequester.responseText || 'Network request failed',
|
|
705
|
+
status: baseRequester.status,
|
|
706
|
+
isTimedOut: false,
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
};
|
|
710
|
+
baseRequester.onload = () => {
|
|
711
|
+
clearTimeout(connectTimeout);
|
|
712
|
+
clearTimeout(responseTimeout);
|
|
713
|
+
resolve({
|
|
714
|
+
content: baseRequester.responseText,
|
|
715
|
+
status: baseRequester.status,
|
|
716
|
+
isTimedOut: false,
|
|
717
|
+
});
|
|
718
|
+
};
|
|
719
|
+
baseRequester.send(request.data);
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
return { send };
|
|
736
723
|
}
|
|
737
724
|
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
725
|
+
// This file is generated, manual changes will be lost - read more on https://github.com/algolia/api-clients-automation.
|
|
726
|
+
const apiClientVersion = '5.0.0-alpha.3';
|
|
727
|
+
function getDefaultHosts(appId) {
|
|
728
|
+
return [
|
|
729
|
+
{
|
|
730
|
+
url: `${appId}-dsn.algolia.net`,
|
|
731
|
+
accept: 'read',
|
|
732
|
+
protocol: 'https',
|
|
733
|
+
},
|
|
734
|
+
{
|
|
735
|
+
url: `${appId}.algolia.net`,
|
|
736
|
+
accept: 'write',
|
|
737
|
+
protocol: 'https',
|
|
738
|
+
},
|
|
739
|
+
].concat(shuffle([
|
|
740
|
+
{
|
|
741
|
+
url: `${appId}-1.algolianet.com`,
|
|
742
|
+
accept: 'readWrite',
|
|
743
|
+
protocol: 'https',
|
|
744
|
+
},
|
|
745
|
+
{
|
|
746
|
+
url: `${appId}-2.algolianet.com`,
|
|
747
|
+
accept: 'readWrite',
|
|
748
|
+
protocol: 'https',
|
|
749
|
+
},
|
|
750
|
+
{
|
|
751
|
+
url: `${appId}-3.algolianet.com`,
|
|
752
|
+
accept: 'readWrite',
|
|
753
|
+
protocol: 'https',
|
|
754
|
+
},
|
|
755
|
+
]));
|
|
756
|
+
}
|
|
757
|
+
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
758
|
+
function createRecommendClient({ appId: appIdOption, apiKey: apiKeyOption, authMode, algoliaAgents, ...options }) {
|
|
759
|
+
const auth = createAuth(appIdOption, apiKeyOption, authMode);
|
|
741
760
|
const transporter = createTransporter({
|
|
742
|
-
hosts:
|
|
743
|
-
{ url: `${appId}-dsn.algolia.net`, accept: CallEnum.Read },
|
|
744
|
-
{ url: `${appId}.algolia.net`, accept: CallEnum.Write },
|
|
745
|
-
].concat(shuffle([
|
|
746
|
-
{ url: `${appId}-1.algolianet.com` },
|
|
747
|
-
{ url: `${appId}-2.algolianet.com` },
|
|
748
|
-
{ url: `${appId}-3.algolianet.com` },
|
|
749
|
-
])),
|
|
761
|
+
hosts: getDefaultHosts(appIdOption),
|
|
750
762
|
...options,
|
|
751
|
-
|
|
763
|
+
algoliaAgent: getAlgoliaAgent({
|
|
764
|
+
algoliaAgents,
|
|
765
|
+
client: 'Recommend',
|
|
766
|
+
version: apiClientVersion,
|
|
767
|
+
}),
|
|
768
|
+
baseHeaders: {
|
|
769
|
+
'content-type': 'text/plain',
|
|
752
770
|
...auth.headers(),
|
|
753
|
-
...
|
|
754
|
-
...options.headers,
|
|
771
|
+
...options.baseHeaders,
|
|
755
772
|
},
|
|
756
|
-
|
|
773
|
+
baseQueryParameters: {
|
|
757
774
|
...auth.queryParameters(),
|
|
758
|
-
...options.
|
|
775
|
+
...options.baseQueryParameters,
|
|
759
776
|
},
|
|
760
777
|
});
|
|
761
|
-
|
|
778
|
+
return {
|
|
762
779
|
transporter,
|
|
763
|
-
|
|
780
|
+
/**
|
|
781
|
+
* Get the value of the `algoliaAgent`, used by our libraries internally and telemetry system.
|
|
782
|
+
*/
|
|
783
|
+
get _ua() {
|
|
784
|
+
return transporter.algoliaAgent.value;
|
|
785
|
+
},
|
|
786
|
+
/**
|
|
787
|
+
* Adds a `segment` to the `x-algolia-agent` sent with every requests.
|
|
788
|
+
*
|
|
789
|
+
* @param segment - The algolia agent (user-agent) segment to add.
|
|
790
|
+
* @param version - The version of the agent.
|
|
791
|
+
*/
|
|
764
792
|
addAlgoliaAgent(segment, version) {
|
|
765
|
-
transporter.
|
|
793
|
+
transporter.algoliaAgent.add({ segment, version });
|
|
766
794
|
},
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
795
|
+
/**
|
|
796
|
+
* This method allow you to send requests to the Algolia REST API.
|
|
797
|
+
*
|
|
798
|
+
* @summary Send requests to the Algolia REST API.
|
|
799
|
+
* @param del - The del object.
|
|
800
|
+
* @param del.path - The path of the API endpoint to target, anything after the /1 needs to be specified.
|
|
801
|
+
* @param del.parameters - Query parameters to be applied to the current query.
|
|
802
|
+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
|
|
803
|
+
*/
|
|
804
|
+
del({ path, parameters }, requestOptions) {
|
|
805
|
+
if (!path) {
|
|
806
|
+
throw new Error('Parameter `path` is required when calling `del`.');
|
|
807
|
+
}
|
|
808
|
+
const requestPath = '/1{path}'.replace('{path}', path);
|
|
809
|
+
const headers = {};
|
|
810
|
+
const queryParameters = parameters ? parameters : {};
|
|
811
|
+
const request = {
|
|
812
|
+
method: 'DELETE',
|
|
813
|
+
path: requestPath,
|
|
814
|
+
queryParameters,
|
|
815
|
+
headers,
|
|
816
|
+
};
|
|
817
|
+
return transporter.request(request, requestOptions);
|
|
818
|
+
},
|
|
819
|
+
/**
|
|
820
|
+
* This method allow you to send requests to the Algolia REST API.
|
|
821
|
+
*
|
|
822
|
+
* @summary Send requests to the Algolia REST API.
|
|
823
|
+
* @param get - The get object.
|
|
824
|
+
* @param get.path - The path of the API endpoint to target, anything after the /1 needs to be specified.
|
|
825
|
+
* @param get.parameters - Query parameters to be applied to the current query.
|
|
826
|
+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
|
|
827
|
+
*/
|
|
828
|
+
get({ path, parameters }, requestOptions) {
|
|
829
|
+
if (!path) {
|
|
830
|
+
throw new Error('Parameter `path` is required when calling `get`.');
|
|
831
|
+
}
|
|
832
|
+
const requestPath = '/1{path}'.replace('{path}', path);
|
|
833
|
+
const headers = {};
|
|
834
|
+
const queryParameters = parameters ? parameters : {};
|
|
835
|
+
const request = {
|
|
836
|
+
method: 'GET',
|
|
837
|
+
path: requestPath,
|
|
838
|
+
queryParameters,
|
|
839
|
+
headers,
|
|
840
|
+
};
|
|
841
|
+
return transporter.request(request, requestOptions);
|
|
842
|
+
},
|
|
843
|
+
/**
|
|
844
|
+
* Returns recommendations or trending results, for a specific model and `objectID`.
|
|
845
|
+
*
|
|
846
|
+
* @summary Get results.
|
|
847
|
+
* @param getRecommendationsParams - The getRecommendationsParams object.
|
|
848
|
+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
|
|
849
|
+
*/
|
|
850
|
+
getRecommendations(getRecommendationsParams, requestOptions) {
|
|
851
|
+
if (!getRecommendationsParams) {
|
|
852
|
+
throw new Error('Parameter `getRecommendationsParams` is required when calling `getRecommendations`.');
|
|
853
|
+
}
|
|
854
|
+
if (!getRecommendationsParams.requests) {
|
|
855
|
+
throw new Error('Parameter `getRecommendationsParams.requests` is required when calling `getRecommendations`.');
|
|
856
|
+
}
|
|
857
|
+
const requestPath = '/1/indexes/*/recommendations';
|
|
858
|
+
const headers = {};
|
|
859
|
+
const queryParameters = {};
|
|
860
|
+
const request = {
|
|
861
|
+
method: 'POST',
|
|
862
|
+
path: requestPath,
|
|
863
|
+
queryParameters,
|
|
864
|
+
headers,
|
|
865
|
+
data: getRecommendationsParams,
|
|
866
|
+
useReadTransporter: true,
|
|
867
|
+
cacheable: true,
|
|
868
|
+
};
|
|
869
|
+
return transporter.request(request, requestOptions);
|
|
870
|
+
},
|
|
871
|
+
/**
|
|
872
|
+
* This method allow you to send requests to the Algolia REST API.
|
|
873
|
+
*
|
|
874
|
+
* @summary Send requests to the Algolia REST API.
|
|
875
|
+
* @param post - The post object.
|
|
876
|
+
* @param post.path - The path of the API endpoint to target, anything after the /1 needs to be specified.
|
|
877
|
+
* @param post.parameters - Query parameters to be applied to the current query.
|
|
878
|
+
* @param post.body - The parameters to send with the custom request.
|
|
879
|
+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
|
|
880
|
+
*/
|
|
881
|
+
post({ path, parameters, body }, requestOptions) {
|
|
882
|
+
if (!path) {
|
|
883
|
+
throw new Error('Parameter `path` is required when calling `post`.');
|
|
884
|
+
}
|
|
885
|
+
const requestPath = '/1{path}'.replace('{path}', path);
|
|
886
|
+
const headers = {};
|
|
887
|
+
const queryParameters = parameters ? parameters : {};
|
|
888
|
+
const request = {
|
|
889
|
+
method: 'POST',
|
|
890
|
+
path: requestPath,
|
|
891
|
+
queryParameters,
|
|
892
|
+
headers,
|
|
893
|
+
data: body ? body : {},
|
|
894
|
+
};
|
|
895
|
+
return transporter.request(request, requestOptions);
|
|
896
|
+
},
|
|
897
|
+
/**
|
|
898
|
+
* This method allow you to send requests to the Algolia REST API.
|
|
899
|
+
*
|
|
900
|
+
* @summary Send requests to the Algolia REST API.
|
|
901
|
+
* @param put - The put object.
|
|
902
|
+
* @param put.path - The path of the API endpoint to target, anything after the /1 needs to be specified.
|
|
903
|
+
* @param put.parameters - Query parameters to be applied to the current query.
|
|
904
|
+
* @param put.body - The parameters to send with the custom request.
|
|
905
|
+
* @param requestOptions - The requestOptions to send along with the query, they will be merged with the transporter requestOptions.
|
|
906
|
+
*/
|
|
907
|
+
put({ path, parameters, body }, requestOptions) {
|
|
908
|
+
if (!path) {
|
|
909
|
+
throw new Error('Parameter `path` is required when calling `put`.');
|
|
910
|
+
}
|
|
911
|
+
const requestPath = '/1{path}'.replace('{path}', path);
|
|
912
|
+
const headers = {};
|
|
913
|
+
const queryParameters = parameters ? parameters : {};
|
|
914
|
+
const request = {
|
|
915
|
+
method: 'PUT',
|
|
916
|
+
path: requestPath,
|
|
917
|
+
queryParameters,
|
|
918
|
+
headers,
|
|
919
|
+
data: body ? body : {},
|
|
920
|
+
};
|
|
921
|
+
return transporter.request(request, requestOptions);
|
|
772
922
|
},
|
|
773
923
|
};
|
|
774
|
-
|
|
775
|
-
};
|
|
776
|
-
|
|
777
|
-
const getRecommendations = base => {
|
|
778
|
-
return (queries, requestOptions) => {
|
|
779
|
-
const requests = queries.map(query => ({
|
|
780
|
-
...query,
|
|
781
|
-
// The `threshold` param is required by the endpoint to make it easier
|
|
782
|
-
// to provide a default value later, so we default it in the client
|
|
783
|
-
// so that users don't have to provide a value.
|
|
784
|
-
threshold: query.threshold || 0,
|
|
785
|
-
}));
|
|
786
|
-
return base.transporter.read({
|
|
787
|
-
method: MethodEnum.Post,
|
|
788
|
-
path: '1/indexes/*/recommendations',
|
|
789
|
-
data: {
|
|
790
|
-
requests,
|
|
791
|
-
},
|
|
792
|
-
cacheable: true,
|
|
793
|
-
}, requestOptions);
|
|
794
|
-
};
|
|
795
|
-
};
|
|
796
|
-
|
|
797
|
-
const getFrequentlyBoughtTogether = base => {
|
|
798
|
-
return (queries, requestOptions) => {
|
|
799
|
-
return getRecommendations(base)(queries.map(query => ({
|
|
800
|
-
...query,
|
|
801
|
-
fallbackParameters: {},
|
|
802
|
-
model: 'bought-together',
|
|
803
|
-
})), requestOptions);
|
|
804
|
-
};
|
|
805
|
-
};
|
|
806
|
-
|
|
807
|
-
const getRelatedProducts = base => {
|
|
808
|
-
return (queries, requestOptions) => {
|
|
809
|
-
return getRecommendations(base)(queries.map(query => ({
|
|
810
|
-
...query,
|
|
811
|
-
model: 'related-products',
|
|
812
|
-
})), requestOptions);
|
|
813
|
-
};
|
|
814
|
-
};
|
|
815
|
-
|
|
816
|
-
const getTrendingFacets = base => {
|
|
817
|
-
return (queries, requestOptions) => {
|
|
818
|
-
const requests = queries.map(query => ({
|
|
819
|
-
...query,
|
|
820
|
-
model: 'trending-facets',
|
|
821
|
-
// The `threshold` param is required by the endpoint to make it easier
|
|
822
|
-
// to provide a default value later, so we default it in the client
|
|
823
|
-
// so that users don't have to provide a value.
|
|
824
|
-
threshold: query.threshold || 0,
|
|
825
|
-
}));
|
|
826
|
-
return base.transporter.read({
|
|
827
|
-
method: MethodEnum.Post,
|
|
828
|
-
path: '1/indexes/*/recommendations',
|
|
829
|
-
data: {
|
|
830
|
-
requests,
|
|
831
|
-
},
|
|
832
|
-
cacheable: true,
|
|
833
|
-
}, requestOptions);
|
|
834
|
-
};
|
|
835
|
-
};
|
|
836
|
-
|
|
837
|
-
const getTrendingItems = base => {
|
|
838
|
-
return (queries, requestOptions) => {
|
|
839
|
-
const requests = queries.map(query => ({
|
|
840
|
-
...query,
|
|
841
|
-
model: 'trending-items',
|
|
842
|
-
// The `threshold` param is required by the endpoint to make it easier
|
|
843
|
-
// to provide a default value later, so we default it in the client
|
|
844
|
-
// so that users don't have to provide a value.
|
|
845
|
-
threshold: query.threshold || 0,
|
|
846
|
-
}));
|
|
847
|
-
return base.transporter.read({
|
|
848
|
-
method: MethodEnum.Post,
|
|
849
|
-
path: '1/indexes/*/recommendations',
|
|
850
|
-
data: {
|
|
851
|
-
requests,
|
|
852
|
-
},
|
|
853
|
-
cacheable: true,
|
|
854
|
-
}, requestOptions);
|
|
855
|
-
};
|
|
856
|
-
};
|
|
924
|
+
}
|
|
857
925
|
|
|
858
|
-
|
|
859
|
-
|
|
926
|
+
// This file is generated, manual changes will be lost - read more on https://github.com/algolia/api-clients-automation.
|
|
927
|
+
function recommendClient(appId, apiKey, options) {
|
|
928
|
+
if (!appId || typeof appId !== 'string') {
|
|
929
|
+
throw new Error('`appId` is missing.');
|
|
930
|
+
}
|
|
931
|
+
if (!apiKey || typeof apiKey !== 'string') {
|
|
932
|
+
throw new Error('`apiKey` is missing.');
|
|
933
|
+
}
|
|
934
|
+
return createRecommendClient({
|
|
860
935
|
appId,
|
|
861
936
|
apiKey,
|
|
862
937
|
timeouts: {
|
|
863
|
-
connect:
|
|
864
|
-
read:
|
|
865
|
-
write:
|
|
866
|
-
},
|
|
867
|
-
requester:
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
938
|
+
connect: DEFAULT_CONNECT_TIMEOUT_BROWSER,
|
|
939
|
+
read: DEFAULT_READ_TIMEOUT_BROWSER,
|
|
940
|
+
write: DEFAULT_WRITE_TIMEOUT_BROWSER,
|
|
941
|
+
},
|
|
942
|
+
requester: createXhrRequester(),
|
|
943
|
+
algoliaAgents: [{ segment: 'Browser' }],
|
|
944
|
+
authMode: 'WithinQueryParameters',
|
|
945
|
+
responsesCache: createMemoryCache(),
|
|
946
|
+
requestsCache: createMemoryCache({ serializable: false }),
|
|
871
947
|
hostsCache: createFallbackableCache({
|
|
872
948
|
caches: [
|
|
873
|
-
createBrowserLocalStorageCache({ key: `${
|
|
874
|
-
|
|
949
|
+
createBrowserLocalStorageCache({ key: `${apiClientVersion}-${appId}` }),
|
|
950
|
+
createMemoryCache(),
|
|
875
951
|
],
|
|
876
952
|
}),
|
|
877
|
-
userAgent: createUserAgent(version)
|
|
878
|
-
.add({ segment: 'Recommend', version })
|
|
879
|
-
.add({ segment: 'Browser' }),
|
|
880
|
-
authMode: AuthMode.WithinQueryParameters,
|
|
881
|
-
};
|
|
882
|
-
return createRecommendClient({
|
|
883
|
-
...commonOptions,
|
|
884
953
|
...options,
|
|
885
|
-
methods: {
|
|
886
|
-
getFrequentlyBoughtTogether,
|
|
887
|
-
getRecommendations,
|
|
888
|
-
getRelatedProducts,
|
|
889
|
-
getTrendingFacets,
|
|
890
|
-
getTrendingItems,
|
|
891
|
-
},
|
|
892
954
|
});
|
|
893
|
-
}
|
|
894
|
-
// eslint-disable-next-line functional/immutable-data
|
|
895
|
-
recommend.version = version;
|
|
955
|
+
}
|
|
896
956
|
|
|
897
|
-
export
|
|
957
|
+
export { apiClientVersion, recommendClient };
|