Skip to content

Commit 39051cf

Browse files
committed
Add paging and store keyword in settings
1 parent ef0c193 commit 39051cf

File tree

6 files changed

+112
-31
lines changed

6 files changed

+112
-31
lines changed

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,35 @@ client.search('keyword', cb);
2222
client.suggest('api', cb);
2323
```
2424

25-
## Settings
25+
## Publicly accessible functions
2626

27-
The following settings are supported by the client
27+
The client provides the following functions.
28+
29+
#### Fetch search results
30+
```js
31+
// Search with a specific keyword
32+
client.search('keyword', callback);
33+
34+
// Search with the previously used keyword or execute a "match all" query
35+
client.search(callback);
36+
```
37+
38+
#### Define language filter (e.g. "en" or "de")
2839
```js
29-
// Get search results only in a specified language (e.g. "en" or "de")
3040
client.setLanguage('en');
3141
```
3242

43+
#### Manage paging
44+
```js
45+
// Defaults: page "1", pageSize "10", sortBy "relevance", sortOrder "desc"
46+
client.setPaging(page, pageSize, sortBy, sortOrder);
47+
48+
// Next {pageSize} results
49+
client.nextPage();
50+
51+
// Previous {pageSize} results
52+
client.previousPage();
53+
```
3354

3455
## Browser support
3556
The client is tested on following browsers

src/apifetch.js

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

1111
const RESPONSE_BAD_REQUEST = 400;
1212
const RESPONSE_SERVER_ERROR = 500;
1313

14+
var settingToQueryParam = function(setting, key) {
15+
if (setting) {
16+
return '&' + key + '=' + setting;
17+
}
18+
return '';
19+
}
20+
21+
1422
// Validate query type
1523
if (type !== 'search' && type !== 'suggest') {
1624
cb({error: {response: RESPONSE_BAD_REQUEST, message: 'invalid query type'}});
1725
return;
1826
}
1927

20-
// If no keyword, fetch all results
21-
let kw = keyword || '*';
28+
// Keyword
29+
let kw = settings.keyword;
2230

2331
// Boolean operators (AND, OR, NOT) uppercase
2432
kw = kw.replace(/ and /g, ' AND ').replace(/ or /g, ' OR ').replace(/ not /g, ' NOT ');
@@ -29,9 +37,11 @@ var executeApiFetch = function(sitekey, type, keyword, settings, cb) {
2937
// Construct query string from settings
3038
var qs = '';
3139
if (type === 'search') {
32-
if (settings.lang) {
33-
qs = qs + '&lang=' + settings.lang;
34-
}
40+
qs = settingToQueryParam(settings.lang, 'lang') +
41+
settingToQueryParam(settings.paging.page, 'page') +
42+
settingToQueryParam(settings.paging.pageSize, 'limit') +
43+
settingToQueryParam(settings.paging.sortBy, 'sort') +
44+
settingToQueryParam(settings.paging.order, 'order');
3545
}
3646

3747

@@ -42,6 +52,7 @@ var executeApiFetch = function(sitekey, type, keyword, settings, cb) {
4252
}).then(function(json) {
4353
cb(json);
4454
}).catch(function(ex) {
55+
console.log(ex);
4556
cb({error: {response: RESPONSE_SERVER_ERROR, message: 'invalid server response'}});
4657
});
4758
};

src/index.js

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

33
var executeApiFetch = require('./apifetch');
4-
var settings = require('./settings');
4+
var Settings = require('./settings');
5+
var util = require('./util');
56

67
var client = function(sitekey) {
78
this.sitekey = sitekey;
9+
this.settings = new Settings();
810

911
/**
1012
* Fetch search results
1113
*
12-
* @param keyword
14+
* @param a1 Argument 1: Keyword. If no Argument 2, then this is the callback function and search is executed with
15+
* the previous keyword. If there is no Argument 2 and no previous keywords, then search is executed
16+
* without keyword (i.e. match all query)
17+
* @param a2 Callback function to call with search results
1318
*/
14-
this.search = function(keyword, cb) {
15-
executeApiFetch(this.sitekey, 'search', keyword, settings.getSettings(), cb);
19+
this.search = function(a1, a2) {
20+
21+
var keyword = a1;
22+
var callback = a2;
23+
24+
// If function is called with callback only, use previous keyword from settings object
25+
if (!a2 && util.isFunction(a1)) {
26+
keyword = this.settings.getSettings().keyword;
27+
callback = a1;
28+
}
29+
30+
this.settings.setKeyword(keyword);
31+
executeApiFetch(this.sitekey, 'search', this.settings.getSettings(), callback);
1632
};
1733

1834

@@ -21,15 +37,20 @@ var client = function(sitekey) {
2137
*
2238
* @param keyword
2339
*/
24-
this.suggest = function(keyword, cb) {
25-
executeApiFetch(this.sitekey, 'suggest', keyword, settings.getSettings(), cb);
40+
this.suggest = function(keyword, callback) {
41+
executeApiFetch(this.sitekey, 'suggest', this.settings.getSettings(), callback);
2642
};
2743

2844

2945
/**
3046
* Public functions
3147
*/
32-
this.setLanguage = settings.setLanguage;
48+
this.getSettings = function() { return this.settings.getSettings(); }
49+
this.setLanguage = function(lang) { this.settings.setLanguage(lang); };
50+
this.setPaging = function(page, pageSize, sortBy, sortOder) { this.settings.setPaging(page, pageSize, sortBy, sortOder); };
51+
this.nextPage = function() { this.settings.nextPage(); };
52+
this.previousPage = function() { this.settings.previousPage(); };
53+
3354
}
3455

3556
module.exports = client;

src/settings.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
11
'use strict';
22

3-
var settings = {};
3+
var settings = function() {
4+
this.settings = {
5+
keyword: '*',
6+
paging: {
7+
page: 1,
8+
pageSize: 10,
9+
sortBy: 'relevance',
10+
sortOrder: 'desc'
11+
}
12+
};
413

5-
var getSettings = function() {
6-
return settings;
7-
}
14+
this.getSettings = function () {
15+
return this.settings;
16+
}
17+
18+
this.setKeyword = function (keyword) {
19+
this.settings.keyword = keyword || '*';
20+
}
21+
22+
this.setLanguage = function (language) {
23+
console.log('LANG SET TO ' + language);
24+
this.settings.lang = language;
25+
}
826

9-
var setLanguage = function(language) {
10-
settings.lang = language;
27+
this.setPaging = function (page, pageSize, sortBy, sortOder) {
28+
this.settings.paging.page = page;
29+
this.settings.paging.pageSize = pageSize;
30+
this.settings.paging.sortBy = sortBy;
31+
this.settings.paging.sortOder = sortOder;
32+
}
33+
this.nextPage = function () {
34+
this.settings.paging.page = this.settings.paging.page + 1;
35+
}
36+
this.previousPage = function () {
37+
if (this.settings.paging.page > 0) {
38+
this.settings.paging.page = this.settings.paging.page - 1;
39+
}
40+
}
1141
}
1242

13-
module.exports = {
14-
getSettings,
15-
setLanguage
16-
};
43+
module.exports = settings;

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('sitekey', 'search', 'keyword', {}, (res) => {
13+
apifetch('sitekey', 'search', {paging:{}, keyword: 'foo'}, (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('sitekey', 'ping', 'keyword', {}, (res) => {
23+
apifetch('sitekey', 'ping', {paging:{}, keyword: 'foo'}, (res) => {
2424
assert.equal(res.error.response, expect);
2525
});
2626
});

test/settings.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
var assert = require('assert');
2-
var settings = require('../src/settings');
2+
var Settings = require('../src/settings');
33

44

55
describe('settings', function() {
66
describe('setLanguage', function() {
77

88
it('language setting should be set', function() {
99
const expect = {lang: 'en'};
10-
settings.setLanguage('en');
11-
assert.deepEqual(settings.getSettings(), expect);
10+
var s = new Settings();
11+
s.setLanguage('en');
12+
assert.equal(s.getSettings().lang, expect.lang);
1213
});
1314

1415
});

0 commit comments

Comments
 (0)