createSearchFn
The createSearchFn utility function streamlines the search implementation process by managing index fetching and search functionality in a single operation.
Type Definition
Section titled “Type Definition”type SearchFn = (query: string) => Promise<SearchResult[] | StaticSeekError>;type SearchFnCallback = (isLoading: boolean) => void;
export function createSearchFn(url: string, callback: SearchFnCallback = () => {}): SearchFn;Parameters
Section titled “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 withtruewhen index loading begins andfalsewhen loading completes, enabling integration with loading indicators or other UI elements.
Return Value
Section titled “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.
import { createSearchFn } from "staticseek";
const search_function = createSearchFn(index_url);const result = await search_function("search word");Error Handling
Section titled “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.