UNPKG

search-client

Version:

Javascript library for executing searches in the Haive search-index via the SearchManager REST interface.

129 lines (128 loc) 6.57 kB
import fetch from 'cross-fetch'; import { AuthToken } from '../Authentication/AuthToken'; import { IBaseSettings } from './BaseSettings'; import { OrderBy } from './OrderBy'; import { DateSpecification, IQuery } from './Query'; import { SearchType } from './SearchType'; import { CategorizationType } from './CategorizationType'; import { Filter } from './Filter'; export declare type Fetch = typeof fetch; export interface IWarning { message: string; statusCode?: number; } /** * A common service base-class for the descending Autocomplete, Categorize and Find classes. * * @param TDataType Defines the data-type that the descendant service-class needs to return on lookups. */ export declare abstract class BaseCall<TDataType> { protected fetchMethod: Fetch; protected settings?: IBaseSettings<TDataType>; protected auth?: AuthToken; protected deferUpdate: boolean; protected deferredQuery: IQuery | null; protected deferredUseMatchPage: boolean | null; protected delay: number; /** * The query used for the last fetch operation. * When the actual query is different from this then the UI should reflect that it is * not representative for the current query. It should then call a callback to notify * on this state. * The UI can then decide what to do with the no-longer representative results, for instance: * - Remove the results * - "Disable" the results * - "Ghost" the results (but allow operating on them) */ protected fetchQuery: IQuery; protected requested: number; protected aborted: number; protected cancelled: number; protected failed: number; protected completed: number; private abortController; /** * The number of times this service-instance has started a request. */ get numRequested(): number; /** * The number of times this service-instance has aborted a request (typically as a result to starting a new request while the previous is still running and needs to be aborted). */ get numAborted(): number; /** * The number of times this service-instance has cancelled a request (as directed from a user cbRequest rejection). */ get numCancelled(): number; /** * The number of times this service-instance has failed during a request (not counting aborted or cancelled requests). */ get numFailed(): number; /** * The number of times this service-instance has completed a request (not counting aborted or cancelled requests). */ get numCompleted(): number; /** * Decides whether an update should be executed or not. Typically used to temporarily turn off update-execution. * When turned back on the second param can be used to indicate whether pending updates should be executed or not. * @param state Turns on or off deferring of updates. * @param skipPending Used to indicate if a pending update is to be executed or skipped when deferring is turned off. The param is ignored for state=true. */ deferUpdates(state: boolean, skipPending?: boolean): void; /** * Can be used to check the state of deferUpdates. */ get deferUpdateState(): boolean; /** * Fetches the results from the server. * @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 the data. */ fetch(query?: IQuery, suppressCallbacks?: boolean): Promise<TDataType>; /** * Sets up the Request that is to be executed, with headers and auth as needed. * * @param includeAuthorizationHeader Set to false to not include the auth jwt token in the request headers. Default=true */ requestObject(includeAuthorizationHeader?: boolean): RequestInit; /** * Call the service, but take into account deferredUpdates. * * @param query The query object to create the fetch for. * @param delay A delay for when to execute the update, in milliseconds. Defaults to undefined. * @param useQueryMatchPage If true then the query match-page number will not be reset to 1. Otherwise it is by default always 1. */ update(query: IQuery, delay?: number, useQueryMatchPage?: boolean): void; shouldUpdate(fieldName?: string, query?: IQuery): boolean; clientIdChanged(oldValue: string, query: IQuery): void; categorizationTypeChanged(oldValue: CategorizationType, query: IQuery): void; dateFromChanged(oldValue: DateSpecification, query: IQuery): void; dateToChanged(oldValue: DateSpecification, query: IQuery): void; filtersChanged(oldValue: Filter[], query: IQuery): void; matchGenerateContentChanged(oldValue: boolean, query: IQuery): void; matchGenerateContentHighlightsChanged(oldValue: boolean, query: IQuery): void; matchGroupingChanged(oldValue: boolean, query: IQuery): void; matchOrderByChanged(oldValue: OrderBy, query: IQuery): void; matchPageChanged(oldValue: number, query: IQuery): void; matchPageSizeChanged(oldValue: number, query: IQuery): void; maxSuggestionsChanged(oldValue: number, query: IQuery): void; queryTextChanged(oldValue: string, query: IQuery): void; searchTypeChanged(oldValue: SearchType, query: IQuery): void; uiLanguageCodeChanged(oldValue: string, query: IQuery): void; /** * Sets up a the common base handling for services, such as checking that the url is valid and handling the authentication. * * @param settings - The base url for the service to be setup. * @param auth - The auth-object that controls authentication for the service. */ protected init(settings: IBaseSettings<TDataType>, auth?: AuthToken, fetchMethod?: Fetch): void; protected abstract fetchInternal(query?: IQuery, suppressCallbacks?: boolean): Promise<TDataType>; protected cbRequest(suppressCallbacks: boolean, url: string, reqInit: RequestInit): boolean; protected cbError(suppressCallbacks: boolean, error: any, url: string, reqInit: RequestInit): void; protected cbWarning(suppressCallbacks: boolean, warning: IWarning, url: string, reqInit: RequestInit): void; protected cbSuccess(suppressCallbacks: boolean, data: TDataType, url: string, reqInit: RequestInit): void; /** * Checks whether or not to notify that the results are invalidated (no longer representative for the query). */ private outdatedWarning; }