search-client
Version:
Javascript library for executing searches in the Haive search-index via the SearchManager REST interface.
109 lines • 5.21 kB
JavaScript
import { __awaiter, __extends, __generator } from "tslib";
import { AuthToken } from '../Authentication';
import { BaseCall, Query } from '../Common';
import { AutocompleteQueryConverter } from './AutocompleteQueryConverter';
import { AutocompleteSettings, } from './AutocompleteSettings';
/**
* This class allows you to create a service that executes autocomplete lookups for the Haive SearchManager service.
*
* Note: Typically you will not instantiate this class. Instead you will use it indirectly via the SearchClient class.
*/
var Autocomplete = /** @class */ (function (_super) {
__extends(Autocomplete, _super);
/**
* Creates an Autocomplete instance that knows how to get query-suggestions.
* @param settings - The settings for how the Autocomplete is to operate.
* @param auth - The object that handles authentication.
*/
function Autocomplete(settings, auth, fetchMethod) {
var _this = _super.call(this) // dummy
|| this;
// prepare for super.init
settings = new AutocompleteSettings(settings);
auth = auth || new AuthToken();
_super.prototype.init.call(_this, settings, auth, fetchMethod);
_this.queryConverter = new AutocompleteQueryConverter();
return _this;
}
Autocomplete.prototype.maxSuggestionsChanged = function (oldValue, query) {
if (!this.shouldUpdate('maxSuggestions', query)) {
return;
}
if (this.settings.triggers.maxSuggestionsChanged) {
this.update(query);
}
};
Autocomplete.prototype.queryTextChanged = function (oldValue, query) {
if (!this.shouldUpdate('queryText', query)) {
return;
}
if (this.settings.triggers.queryChange) {
if (query.queryText.length >
this.settings.triggers.queryChangeMinLength) {
if (this.settings.triggers.queryChangeInstantRegex &&
this.settings.triggers.queryChangeInstantRegex.test(query.queryText)) {
this.update(query);
return;
}
else {
if (this.settings.triggers.queryChangeDelay > -1) {
this.update(query, this.settings.triggers.queryChangeDelay);
return;
}
}
}
}
clearTimeout(this.delay);
};
/**
* When called it will execute a rest-call to the base-url and fetch autocomplete suggestions based on the query passed.
* Note that if a request callback has been setup then if it returns false the request is skipped.
* @param query - Is used to find out which autocomplete suggestions and from what sources they should be retrieved.
* @param suppressCallbacks - Set to true if you have defined callbacks, but somehow don't want them to be called.
* @returns a Promise that when resolved returns a string array of suggestions (or undefined if a callback stops the request).
*/
Autocomplete.prototype.fetchInternal = function (query, suppressCallbacks) {
if (query === void 0) { query = new Query(); }
if (suppressCallbacks === void 0) { suppressCallbacks = false; }
return __awaiter(this, void 0, void 0, function () {
var reqInit, url, err, response, suggestions, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
reqInit = this.requestObject();
this.fetchQuery = new Query(query);
url = this.queryConverter.getUrl(this.settings.url, this.fetchQuery);
_a.label = 1;
case 1:
_a.trys.push([1, 4, , 5]);
if (!this.cbRequest(suppressCallbacks, url, reqInit)) {
err = new Error();
err.name = 'cbRequestCancelled';
throw err;
}
return [4 /*yield*/, this.fetchMethod(url, reqInit)];
case 2:
response = _a.sent();
if (!response.ok) {
throw Error(response.status + " " + response.statusText + " for request url '" + url + "'");
}
return [4 /*yield*/, response.json()];
case 3:
suggestions = _a.sent();
this.cbSuccess(suppressCallbacks, suggestions, url, reqInit);
return [2 /*return*/, suggestions];
case 4:
error_1 = _a.sent();
if (error_1.name !== 'AbortError') {
this.cbError(suppressCallbacks, error_1, url, reqInit);
}
throw error_1;
case 5: return [2 /*return*/];
}
});
});
};
return Autocomplete;
}(BaseCall));
export { Autocomplete };
//# sourceMappingURL=Autocomplete.js.map