Skip to content

Commit e072805

Browse files
committed
Pass sitekey and settings as a parameter
1 parent f63f9f1 commit e072805

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/apifetch.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require('isomorphic-fetch');
66
/**
77
* Fetch search results of search suggestions from the Addsearch API
88
*/
9-
var executeApiFetch = function(type, keyword, cb) {
9+
var executeApiFetch = function(sitekey, type, keyword, settings, cb) {
1010

1111
const RESPONSE_BAD_REQUEST = 400;
1212
const RESPONSE_SERVER_ERROR = 500;
@@ -26,8 +26,17 @@ var executeApiFetch = function(type, keyword, cb) {
2626
// Escape
2727
kw = encodeURIComponent(kw);
2828

29+
// Construct query string from settings
30+
var qs = '';
31+
if (type === 'search') {
32+
if (settings.lang) {
33+
qs = qs + '&lang=' + settings.lang;
34+
}
35+
}
36+
37+
2938
// Execute API call
30-
fetch('https://api.addsearch.com/v1/' + type + '/' + (this ? this.sitekey : null) + '?term=' + kw)
39+
fetch('https://api.addsearch.com/v1/' + type + '/' + sitekey + '?term=' + kw + qs)
3140
.then(function(response) {
3241
return response.json();
3342
}).then(function(json) {

test/apifetch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('apifetch', function() {
1010
fetchMock.get('*', { response: 200 });
1111
const expect = {response: 200};
1212

13-
apifetch('search', 'keyword', (res) => {
13+
apifetch('sitekey', 'search', 'keyword', {}, (res) => {
1414
assert.deepEqual(res, expect);
1515
});
1616
});
@@ -20,7 +20,7 @@ describe('apifetch', function() {
2020
fetchMock.get('*', 'foo');
2121
const expect = 400;
2222

23-
apifetch('ping', 'keyword', (res) => {
23+
apifetch('sitekey', 'ping', 'keyword', {}, (res) => {
2424
assert.equal(res.error.response, expect);
2525
});
2626
});

0 commit comments

Comments
 (0)