search-client
Version:
Javascript library for executing searches in the Haive search-index via the SearchManager REST interface.
211 lines • 8.94 kB
JavaScript
import { __awaiter, __extends, __generator } from "tslib";
import { AuthToken } from '../Authentication';
import { BaseCall, Query, } from '../Common';
import { FindQueryConverter } from './FindQueryConverter';
import { FindSettings } from './FindSettings';
/**
* The Find service queries the search-engine for search-matches for the given query.
*
* It is normally used indirectly via the SearchClient class.
*/
var Find = /** @class */ (function (_super) {
__extends(Find, _super);
/**
* Creates a Find instance that handles fetching matches dependent on settings and query.
* @param settings - The settings that define how the Find instance is to operate.
* @param auth - An auth-object that handles the authentication.
*/
function Find(settings, auth, fetchMethod) {
var _this = _super.call(this) // dummy
|| this;
// prepare for super.init
settings = new FindSettings(settings);
auth = auth || new AuthToken();
_super.prototype.init.call(_this, settings, auth, fetchMethod);
// Set own this props
_this.queryConverter = new FindQueryConverter();
return _this;
// console.log(`search-client/Find.ctor: Created new Find instance`);
}
Find.prototype.clientIdChanged = function (oldValue, query) {
if (!this.shouldUpdate('clientId', query)) {
return;
}
if (this.settings.triggers.clientIdChanged) {
this.update(query);
}
};
Find.prototype.dateFromChanged = function (oldValue, query) {
if (!this.shouldUpdate('dateFrom', query)) {
return;
}
if (this.settings.triggers.dateFromChanged) {
this.update(query);
}
};
Find.prototype.dateToChanged = function (oldValue, query) {
if (!this.shouldUpdate('dateTo', query)) {
return;
}
if (this.settings.triggers.dateToChanged) {
this.update(query);
}
};
Find.prototype.filtersChanged = function (oldValue, query) {
if (!this.shouldUpdate('filters', query)) {
return;
}
if (this.settings.triggers.filtersChanged) {
this.update(query);
}
};
Find.prototype.matchGenerateContentChanged = function (oldValue, query) {
if (!this.shouldUpdate('matchGenerateContent', query)) {
return;
}
if (this.settings.triggers.matchGenerateContentChanged) {
this.update(query);
}
};
Find.prototype.matchGenerateContentHighlightsChanged = function (oldValue, query) {
if (!this.shouldUpdate('matchGenerateContentHighlights', query)) {
return;
}
if (this.settings.triggers.matchGenerateContentChanged &&
this.settings.triggers.matchGenerateContentHighlightsChanged) {
this.update(query);
}
};
Find.prototype.matchGroupingChanged = function (oldValue, query) {
if (!this.shouldUpdate('matchGrouping', query)) {
return;
}
if (this.settings.triggers.matchGroupingChanged) {
this.update(query);
}
};
Find.prototype.matchOrderByChanged = function (oldValue, query) {
if (!this.shouldUpdate('matchOrderBy', query)) {
return;
}
if (this.settings.triggers.matchOrderByChanged) {
this.update(query);
}
};
Find.prototype.matchPageChanged = function (oldValue, query) {
if (!this.shouldUpdate('matchPage', query)) {
return;
}
if (this.settings.triggers.matchPageChanged) {
this.update(query, null, true);
}
};
Find.prototype.matchPageSizeChanged = function (oldValue, query) {
if (!this.shouldUpdate('matchPageSize', query)) {
return;
}
if (this.settings.triggers.matchPageSizeChanged) {
this.update(query);
}
};
Find.prototype.queryTextChanged = function (oldValue, query) {
if (!this.shouldUpdate('queryText', query)) {
return;
}
if (this.settings.triggers.queryChange) {
if (query.queryText.trim().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);
};
Find.prototype.searchTypeChanged = function (oldValue, query) {
if (!this.shouldUpdate('searchType', query)) {
return;
}
if (this.settings.triggers.searchTypeChanged) {
this.update(query);
}
};
Find.prototype.uiLanguageCodeChanged = function (oldValue, query) {
if (!this.shouldUpdate('uiLanguageCode', query)) {
return;
}
if (this.settings.triggers.uiLanguageCodeChanged) {
this.update(query);
}
};
/**
* Fetches the search-result matches from the server.
* Note that if a request callback has been setup then if it returns false the request is skipped.
* @param query - The query-object that controls which results that are to be returned.
* @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).
*/
Find.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, matches, warning, 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()
// Handle situations where parsing was ok, but we have an error in the returned message from the server
];
case 3:
matches = _a.sent();
// Handle situations where parsing was ok, but we have an error in the returned message from the server
if (!matches || matches.errorMessage || matches.statusCode !== 0) {
warning = {
message: (matches === null || matches === void 0 ? void 0 : matches.errorMessage) || 'Unspecified issue',
statusCode: matches === null || matches === void 0 ? void 0 : matches.statusCode,
};
console.warn('search-client/Find.fetchInternal()> ', warning);
this.cbWarning(suppressCallbacks, warning, url, reqInit);
}
this.cbSuccess(suppressCallbacks, matches, url, reqInit);
return [2 /*return*/, matches];
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 Find;
}(BaseCall));
export { Find };
//# sourceMappingURL=Find.js.map