Skip to content

Commit d578636

Browse files
authored
Merge pull request #31 from AddSearch/replace-node-fetch-with-axios
replace node-fetch with axios
2 parents e331fc1 + a2db1f8 commit d578636

File tree

5 files changed

+54
-59
lines changed

5 files changed

+54
-59
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
],
3737
"license": "MIT",
3838
"dependencies": {
39+
"axios": "^0.27.2",
3940
"es6-promise": "^4.2.8",
40-
"isomorphic-fetch": "^3.0.0",
4141
"js-base64": "^3.6.0"
4242
},
4343
"devDependencies": {
@@ -49,7 +49,6 @@
4949
"esm": "^3.2.25",
5050
"fetch-mock": "^9.11.0",
5151
"mocha": "^8.2.1",
52-
"node-fetch": "^2.6.1",
5352
"uglify-js": "^3.12.0",
5453
"webpack": "^4.44.2",
5554
"webpack-cli": "^3.3.12"

src/apifetch.js

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
require('es6-promise').polyfill();
4-
require('isomorphic-fetch');
4+
const axios = require('axios').default;
55

66
/**
77
* Fetch search results of search suggestions from the Addsearch API
@@ -137,7 +137,7 @@ var executeApiFetch = function(apiHostname, sitekey, type, settings, cb, fuzzyRe
137137
// Suggest
138138
else if (type === 'suggest') {
139139
apiPath = type;
140-
qs = settingToQueryParam(settings.suggestionsSize, 'size') +
140+
qs = settingToQueryParam(settings.suggestionsSize, 'size') +
141141
settingToQueryParam(settings.lang, 'lang');
142142
kw = settings.suggestionsPrefix;
143143
}
@@ -152,34 +152,32 @@ var executeApiFetch = function(apiHostname, sitekey, type, settings, cb, fuzzyRe
152152

153153

154154
// Execute API call
155-
fetch('https://' + apiHostname + '/v1/' + apiPath + '/' + sitekey + '?term=' + kw + qs)
155+
axios.get('https://' + apiHostname + '/v1/' + apiPath + '/' + sitekey + '?term=' + kw + qs)
156156
.then(function(response) {
157-
return response.json();
158-
}).then(function(json) {
159-
160-
// Search again with fuzzy=true if no hits
161-
if (type === 'search' && settings.fuzzy === 'retry' && json.total_hits === 0 && fuzzyRetry !== true) {
162-
executeApiFetch(apiHostname, sitekey, type, settings, cb, true);
163-
}
157+
var json = response.data;
158+
// Search again with fuzzy=true if no hits
159+
if (type === 'search' && settings.fuzzy === 'retry' && json.total_hits === 0 && fuzzyRetry !== true) {
160+
executeApiFetch(apiHostname, sitekey, type, settings, cb, true);
161+
}
164162

165-
// Fuzzy not "retry" OR fuzzyRetry already returning
166-
else {
163+
// Fuzzy not "retry" OR fuzzyRetry already returning
164+
else {
167165

168-
// Cap fuzzy results to one page as quality decreases quickly
169-
if (fuzzyRetry === true) {
170-
var pageSize = settings.paging.pageSize;
171-
if (json.total_hits >= pageSize) {
172-
json.total_hits = pageSize;
166+
// Cap fuzzy results to one page as quality decreases quickly
167+
if (fuzzyRetry === true) {
168+
var pageSize = settings.paging.pageSize;
169+
if (json.total_hits >= pageSize) {
170+
json.total_hits = pageSize;
171+
}
173172
}
174-
}
175-
176-
// Callback
177-
cb(json);
178-
}
179173

180-
}).catch(function(ex) {
181-
console.log(ex);
182-
cb({error: {response: RESPONSE_SERVER_ERROR, message: 'invalid server response'}});
183-
});
174+
// Callback
175+
cb(json);
176+
}
177+
})
178+
.catch(function(ex) {
179+
console.log(ex);
180+
cb({error: {response: RESPONSE_SERVER_ERROR, message: 'invalid server response'}});
181+
});
184182
};
185183
module.exports = executeApiFetch;

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,28 +189,28 @@ var client = function(sitekey, privatekey) {
189189
this.enableLogicalOperators = function(enableLogicalOperators) { this.settings.enableLogicalOperators(enableLogicalOperators) }
190190
this.setSearchOperator = function(operator) { this.settings.setSearchOperator(operator) }
191191

192-
this.sendStatsEvent = function(type, keyword, data) {
192+
this.sendStatsEvent = function(type, keyword,data) {
193193
if (type === 'search') {
194-
var data = {
194+
var payload = {
195195
action: 'search',
196196
session: this.sessionId,
197197
keyword: keyword,
198198
numberOfResults: data.numberOfResults,
199199
analyticsTag: this.getSettings().analyticsTag
200200
};
201-
sendStats(this.apiHostname, this.sitekey, data);
201+
sendStats(this.apiHostname, this.sitekey, payload);
202202
}
203203

204204
else if (type === 'click') {
205-
var data = {
205+
var payload = {
206206
action: 'click',
207207
session: this.sessionId,
208208
keyword: keyword,
209209
docid: data.documentId,
210210
position: data.position,
211211
analyticsTag: this.getSettings().analyticsTag
212212
};
213-
sendStats(this.apiHostname, this.sitekey, data);
213+
sendStats(this.apiHostname, this.sitekey, payload);
214214
}
215215

216216
else {

src/indexingapi.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
require('isomorphic-fetch');
43
const util = require('./util');
54
const Promise = require('es6-promise').Promise;
5+
const axios = require('axios').default;
66

77
const getHeaders = function(sitekey, privatekey) {
88
return {
@@ -18,14 +18,13 @@ const getHeaders = function(sitekey, privatekey) {
1818
var getDocument = function(apiHostname, sitekey, privatekey, id) {
1919
const promise = new Promise((resolve, reject) => {
2020

21-
fetch('https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/' + id,
21+
axios.get('https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/' + id,
2222
{
23-
method: 'GET',
2423
headers: getHeaders(sitekey, privatekey)
2524
})
2625
.then((response) => {
2726
if (response.status == 200) {
28-
resolve(response.json());
27+
resolve(response.data);
2928
}
3029
else {
3130
reject({status: response.status, text: response.statusText});
@@ -44,14 +43,16 @@ var getDocument = function(apiHostname, sitekey, privatekey, id) {
4443
var saveDocument = function(apiHostname, sitekey, privatekey, document) {
4544

4645
// If the doc has id or url field, PUT instead of POST
46+
4747
const isPut = document.id || document.url;
4848

4949
const promise = new Promise((resolve, reject) => {
50-
fetch('https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/',
50+
axios(
5151
{
52-
method: isPut ? 'PUT' : 'POST',
52+
url: 'https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/',
53+
method: isPut ? 'put' : 'post',
5354
headers: getHeaders(sitekey, privatekey),
54-
body: JSON.stringify(document)
55+
data: document
5556
})
5657
.then((response) => {
5758
if (response.status == 202) {
@@ -61,8 +62,8 @@ var saveDocument = function(apiHostname, sitekey, privatekey, document) {
6162
reject({status: response.status, text: response.statusText});
6263
}
6364
}).catch((ex) => {
64-
reject({status: 400, text: ex});
65-
});
65+
reject({status: 400, text: ex});
66+
});
6667
});
6768

6869
return promise;
@@ -75,11 +76,12 @@ var saveDocument = function(apiHostname, sitekey, privatekey, document) {
7576
var saveDocumentsBatch = function(apiHostname, sitekey, privatekey, documents) {
7677

7778
const promise = new Promise((resolve, reject) => {
78-
fetch('https://' + apiHostname + '/v2/indices/' + sitekey + '/documents:batch',
79+
axios(
7980
{
80-
method: 'PUT',
81+
method: 'put',
82+
url: 'https://' + apiHostname + '/v2/indices/' + sitekey + '/documents:batch',
8183
headers: getHeaders(sitekey, privatekey),
82-
body: JSON.stringify(documents)
84+
data: documents
8385
})
8486
.then((response) => {
8587
if (response.status == 202) {
@@ -102,9 +104,8 @@ var saveDocumentsBatch = function(apiHostname, sitekey, privatekey, documents) {
102104
*/
103105
var deleteDocument = function(apiHostname, sitekey, privatekey, id) {
104106
const promise = new Promise((resolve, reject) => {
105-
fetch('https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/' + id,
107+
axios.delete('https://' + apiHostname + '/v2/indices/' + sitekey + '/documents/' + id,
106108
{
107-
method: 'DELETE',
108109
headers: getHeaders(sitekey, privatekey)
109110
})
110111
.then((response) => {
@@ -128,11 +129,10 @@ var deleteDocument = function(apiHostname, sitekey, privatekey, id) {
128129
*/
129130
var deleteDocumentsBatch = function(apiHostname, sitekey, privatekey, batch) {
130131
const promise = new Promise((resolve, reject) => {
131-
fetch('https://' + apiHostname + '/v2/indices/' + sitekey + '/documents:batch',
132+
axios.delete('https://' + apiHostname + '/v2/indices/' + sitekey + '/documents:batch',
132133
{
133-
method: 'DELETE',
134134
headers: getHeaders(sitekey, privatekey),
135-
body: JSON.stringify(batch)
135+
data: batch
136136
})
137137
.then((response) => {
138138
if (response.status == 202) {

src/stats.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
'use strict';
22

33
require('es6-promise').polyfill();
4-
require('isomorphic-fetch');
4+
const axios = require('axios').default;
55

6-
var sendStats = function(apiHostname, sitekey, data) {
6+
var sendStats = function(apiHostname, sitekey, payload) {
77

88
// Beacon in browsers
99
if (typeof window !== 'undefined' && window.navigator && window.navigator.sendBeacon) {
10-
navigator.sendBeacon('https://' + apiHostname + '/v1/stats/' + sitekey + '/', JSON.stringify(data));
10+
navigator.sendBeacon('https://' + apiHostname + '/v1/stats/' + sitekey + '/', JSON.stringify(payload));
1111
}
1212

1313
// POST in node
1414
else {
15-
fetch('https://' + apiHostname + '/v1/stats/' + sitekey + '/', {
16-
method: 'POST',
17-
headers: {
18-
'Content-Type': 'text/plain',
19-
},
20-
body: JSON.stringify(data)
15+
axios.post('https://' + apiHostname + '/v1/stats/' + sitekey + '/', payload, {
16+
headers: {
17+
'Content-Type': 'text/plain',
18+
}
2119
});
2220
}
2321
};

0 commit comments

Comments
 (0)