createSearchFn
The createSearchFn
utility function streamlines the search implementation process by managing index fetching and search functionality in a single operation.
Type Definition
type SearchFn = (query: string) => Promise<SearchResult[] | StaticSeekError>;type SearchFnCallback = (isLoading: boolean) => void;
export function createSearchFn(url: string, callback: SearchFnCallback = () => {}): SearchFn;
Parameters
url
: The endpoint URL for fetching the search index. The endpoint must return a JSON-formatted index.callback
: An optional function that receives a boolean parameter indicating the index loading state. This callback is invoked withtrue
when index loading begins andfalse
when loading completes, enabling integration with loading indicators or other UI elements.
Return Value
Returns a SearchFn
function that is compatible with the standard search function, with one key difference: the returned function includes the StaticSeekIndex
in its closure. This eliminates the need to provide an index when invoking the SearchFn
.
The index loading process begins automatically upon the first invocation of the returned SearchFn
.
Usage
import { createSearchFn } from "staticseek";
const search_function = createSearchFn(index_url);const result = await search_function("search word");
Error Handling
While createSearchFn
itself doesn’t return StaticSeekError
, the returned SearchFn
may produce a StaticSeekError
when called. Therefore, implement error handling for the SearchFn
return value as you would for the standard search
function.