Skip to content

Commit faaa2e2

Browse files
committed
Add function to fetch custom fields autocomplete results
1 parent 1483340 commit faaa2e2

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/apifetch.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var executeApiFetch = function(sitekey, type, settings, cb) {
2020

2121

2222
// Validate query type
23-
if (type !== 'search' && type !== 'suggest') {
23+
if (type !== 'search' && type !== 'suggest' && type !== 'autocomplete') {
2424
cb({error: {response: RESPONSE_BAD_REQUEST, message: 'invalid query type'}});
2525
return;
2626
}
@@ -29,8 +29,14 @@ var executeApiFetch = function(sitekey, type, settings, cb) {
2929
var kw = '';
3030
var qs = '';
3131

32+
// API Path (eq. /search, /suggest, /autocomplete/document-field)
33+
var apiPath = null;
34+
3235
// Search
3336
if (type === 'search') {
37+
// Path
38+
apiPath = type;
39+
3440
// Keyword
3541
kw = settings.keyword;
3642

@@ -93,13 +99,21 @@ var executeApiFetch = function(sitekey, type, settings, cb) {
9399

94100
// Suggest
95101
else if (type === 'suggest') {
102+
apiPath = type;
96103
qs = settingToQueryParam(settings.suggestionsSize, 'size');
97104
kw = settings.suggestionsPrefix;
98105
}
99106

107+
// Autocomplete
108+
else if (type === 'autocomplete') {
109+
apiPath = 'autocomplete/document-field';
110+
qs = settingToQueryParam(settings.autocomplete.field, 'source');
111+
kw = settings.autocomplete.prefix;
112+
}
113+
100114

101115
// Execute API call
102-
fetch('https://api.addsearch.com/v1/' + type + '/' + sitekey + '?term=' + kw + qs)
116+
fetch('https://api.addsearch.com/v1/' + apiPath + '/' + sitekey + '?term=' + kw + qs)
103117
.then(function(response) {
104118
return response.json();
105119
}).then(function(json) {

src/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ var client = function(sitekey) {
6161
}
6262

6363

64+
/**
65+
* Fetch field autocompletes
66+
*
67+
* @param keyword
68+
*/
69+
this.autocomplete = function(field, prefix, callback) {
70+
if (!field || !prefix || !callback || !util.isFunction(callback)) {
71+
throw "Illegal autocomplete parameters. Should be (field, prefix, callbackFunction)";
72+
}
73+
this.settings.setAutocompleteParams(field, prefix);
74+
console.log('autocomplete..');
75+
executeApiFetch(this.sitekey, 'autocomplete', this.settings.getSettings(), callback);
76+
}
77+
78+
6479
/**
6580
* Public functions
6681
*/

src/settings.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ var settings = function() {
3737
this.settings.suggestionsSize = size;
3838
}
3939

40+
this.setAutocompleteParams = function(field, prefix) {
41+
this.settings.autocomplete = {
42+
field: field,
43+
prefix: prefix
44+
};
45+
}
46+
4047
this.setLanguage = function(language) {
4148
if (language && language.length !== 2) {
4249
throw "use 2-char language code (e.g. \"en\")";

0 commit comments

Comments
 (0)