Skip to content

Commit 309be61

Browse files
committed
[sc-9487] add setApiRequestInterceptor
1 parent 2338527 commit 309be61

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,17 @@ client.setThrottleTime(500);
413413
client.setApiHostname('api.addsearch.com');
414414
```
415415

416+
#### Set API request interceptor
417+
`configurationObject` contains 2 keys: <string>`url` and <object>`headers`. Modify the `configurationObject` before it is sent.
418+
```js
419+
function callback(configurationObject) {
420+
configurationObject.headers['X-Api-Key'] = 'YOUR API KEY';
421+
return configurationObject;
422+
}
423+
client.setApiRequestInterceptor(callback);
424+
```
425+
426+
416427
## Indexing API
417428
With the Indexing API, you can fetch, create, update, and delete single documents or
418429
batches of documents.

src/api.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
const axios = require('axios').default;
4+
const apiInstance = axios.create();
5+
6+
const setRequestInterceptor = (callback) => {
7+
apiInstance.interceptors.request.use( (config) => {
8+
const updatedConfig = callback({
9+
url: config.url,
10+
headers: config.headers
11+
});
12+
config = {...config, ...updatedConfig};
13+
return config;
14+
}, function (error) {
15+
return Promise.reject(error);
16+
});
17+
};
18+
19+
module.exports = {
20+
apiInstance,
21+
setRequestInterceptor
22+
};

src/apifetch.js

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

33
require('es6-promise').polyfill();
4-
const axios = require('axios').default;
4+
const apiInstance = require('./api').apiInstance;
55

66
/**
77
* Fetch search results of search suggestions from the Addsearch API
@@ -174,7 +174,7 @@ var executeApiFetch = function(apiHostname, sitekey, type, settings, cb, fuzzyRe
174174
'https://' + apiHostname + '/v1/' + apiPath + '/' + sitekey + '?configurationKey=' + recommendOptions.configurationKey + qs :
175175
'https://' + apiHostname + '/v1/' + apiPath + '/' + sitekey + '?term=' + kw + qs;
176176

177-
axios.get(api)
177+
apiInstance.get(api)
178178
.then(function(response) {
179179
var json = response.data;
180180
// Search again with fuzzy=true if no hits

src/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Settings = require('./settings');
77
var util = require('./util');
88
var throttle = require('./throttle');
99
var cookie = require('./cookie');
10+
var setRequestInterceptor = require('./api').setRequestInterceptor;
1011

1112
var API_HOSTNAME = 'api.addsearch.com';
1213
var USER_TOKEN_COOKIE_NAME = 'addsearchUserToken';
@@ -269,6 +270,17 @@ var client = function(sitekey, privatekey) {
269270
isAddSearchCookieConsented = !!isEnabled;
270271
};
271272

273+
/*
274+
* API interceptor
275+
*/
276+
this.setApiRequestInterceptor = function(callback) {
277+
if (typeof callback === 'function') {
278+
setRequestInterceptor(callback);
279+
} else {
280+
window.console.error('API interceptor must be a function');
281+
}
282+
};
283+
272284

273285
// Deprecated
274286
this.searchResultClicked = function(documentId, position) {

0 commit comments

Comments
 (0)