addsearch-js-client
Advanced tools
Comparing version 0.8.9 to 0.8.10
{ | ||
"name": "addsearch-js-client", | ||
"version": "0.8.9", | ||
"version": "0.8.10", | ||
"description": "AddSearch API JavaScript client", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -411,2 +411,5 @@ # AddSearch Search API Client for JavaScript | ||
#### Set API hostname | ||
`option` is an object with the following properties, all of which are optional. If `option` is not defined, host name will be applied for all requests. | ||
- **searchApiRequestOnly**: If true, the new host name is only applied for searchApi requests (default: false) | ||
- **statsApiRequestOnly**: If true, the new host name is only applied for statsApi requests (default: false) | ||
```js | ||
@@ -417,2 +420,18 @@ // Set API hostname (e.g. for dedicated environments) | ||
#### Set API request interceptor | ||
`configurationObject` contains 2 keys: <string>`url` and <object>`headers`. Modify the `configurationObject` before it is sent. | ||
`option` is an object with the following properties, all of which are optional. If `option` is not defined, the interceptor will be used for all requests. | ||
- **searchApiRequestOnly**: If true, the interceptor is only used for searchApi requests (default: false) | ||
- **statsApiRequestOnly**: If true, the interceptor is only used for statsApi requests (default: false) | ||
```js | ||
function callback(configurationObject) { | ||
configurationObject.headers['X-Api-Key'] = 'YOUR API KEY'; | ||
return configurationObject; | ||
} | ||
client.setApiRequestInterceptor(callback, option); | ||
``` | ||
## Indexing API | ||
@@ -419,0 +438,0 @@ With the Indexing API, you can fetch, create, update, and delete single documents or |
'use strict'; | ||
require('es6-promise').polyfill(); | ||
const axios = require('axios').default; | ||
const apiInstance = require('./api').apiInstance; | ||
@@ -177,3 +177,3 @@ /** | ||
axios.get(api) | ||
apiInstance.get(api) | ||
.then(function(response) { | ||
@@ -180,0 +180,0 @@ var json = response.data; |
@@ -10,2 +10,3 @@ 'use strict'; | ||
var cookie = require('./cookie'); | ||
var setRequestInterceptor = require('./api').setRequestInterceptor; | ||
@@ -19,2 +20,3 @@ var API_HOSTNAME = 'api.addsearch.com'; | ||
this.apiHostname = API_HOSTNAME; | ||
this.statsApiHostname = API_HOSTNAME; | ||
this.settings = new Settings(); | ||
@@ -178,3 +180,10 @@ this.sessionId = ('a-' + (Math.random() * 100000000)).substring(0, 10); | ||
*/ | ||
this.setApiHostname = function(hostname) {this.apiHostname = hostname;} | ||
this.setApiHostname = function(hostname, conf) { | ||
if (!conf || !conf.statsApiRequestOnly) { | ||
this.apiHostname = hostname; | ||
} | ||
if (!conf || !conf.searchApiRequestOnly) { | ||
this.statsApiHostname = hostname; | ||
} | ||
} | ||
this.getSettings = function() {return this.settings.getSettings();} | ||
@@ -233,3 +242,3 @@ this.setLanguage = function(lang) {this.settings.setLanguage(lang);} | ||
}; | ||
sendStats(this.apiHostname, this.sitekey, payload); | ||
sendStats(this.statsApiHostname, this.sitekey, payload, this.settings.getSettings().statsRequestIntercepted); | ||
} | ||
@@ -246,3 +255,3 @@ | ||
}; | ||
sendStats(this.apiHostname, this.sitekey, payload); | ||
sendStats(this.statsApiHostname, this.sitekey, payload, this.settings.getSettings().statsRequestIntercepted); | ||
} | ||
@@ -277,3 +286,29 @@ | ||
/* | ||
* API interceptor | ||
*/ | ||
this.setApiRequestInterceptor = function(callback, option = {}) { | ||
if (typeof callback !== 'function') { | ||
window.console.error('API interceptor must be a function'); | ||
return; | ||
} | ||
const { searchApiRequestOnly = false, statsApiRequestOnly = false } = option; | ||
if (!searchApiRequestOnly && !statsApiRequestOnly) { | ||
setRequestInterceptor(callback, 'searchApi'); | ||
setRequestInterceptor(callback, 'statsApi'); | ||
this.settings.setStatsRequestIntercepted(true); | ||
} else { | ||
if (searchApiRequestOnly) { | ||
setRequestInterceptor(callback, 'searchApi'); | ||
} | ||
if (statsApiRequestOnly) { | ||
setRequestInterceptor(callback, 'statsApi'); | ||
this.settings.setStatsRequestIntercepted(true); | ||
} | ||
} | ||
}; | ||
// Deprecated | ||
@@ -280,0 +315,0 @@ this.searchResultClicked = function(documentId, position) { |
@@ -25,3 +25,4 @@ 'use strict'; | ||
enableLogicalOperators: false, | ||
cacheResponseTime: null | ||
cacheResponseTime: null, | ||
statsRequestIntercepted: false, | ||
}; | ||
@@ -236,4 +237,8 @@ | ||
} | ||
this.setStatsRequestIntercepted = function(isIntercepted) { | ||
this.settings.statsRequestIntercepted = isIntercepted; | ||
}; | ||
} | ||
module.exports = settings; |
'use strict'; | ||
require('es6-promise').polyfill(); | ||
const axios = require('axios').default; | ||
const statsInstance = require('./api').statsInstance; | ||
var sendStats = function(apiHostname, sitekey, payload) { | ||
var sendStats = function(apiHostname, sitekey, payload, statsRequestIntercepted) { | ||
// Beacon in browsers | ||
if (typeof window !== 'undefined' && window.navigator && window.navigator.sendBeacon) { | ||
if (typeof window !== 'undefined' && window.navigator && window.navigator.sendBeacon && !statsRequestIntercepted) { | ||
navigator.sendBeacon('https://' + apiHostname + '/v1/stats/' + sitekey + '/', JSON.stringify(payload)); | ||
@@ -15,3 +15,3 @@ } | ||
else { | ||
axios.post('https://' + apiHostname + '/v1/stats/' + sitekey + '/', payload, { | ||
statsInstance.post('https://' + apiHostname + '/v1/stats/' + sitekey + '/', payload, { | ||
headers: { | ||
@@ -24,2 +24,2 @@ 'Content-Type': 'text/plain', | ||
module.exports = sendStats; | ||
module.exports = sendStats; |
var assert = require('assert'); | ||
var apifetch = require('../src/apifetch'); | ||
var MockAdapter = require("axios-mock-adapter"); | ||
const axios = require('axios').default; | ||
var mock = new MockAdapter(axios); | ||
var apiInstance = require('../src/api').apiInstance; | ||
var mock = new MockAdapter(apiInstance); | ||
@@ -7,0 +7,0 @@ mock.onAny().reply(200, { response: 200 }); |
Sorry, the diff of this file is too big to display
155836
20
1123
649