All files / src config.ts

100% Statements 6/6
71.42% Branches 5/7
100% Functions 2/2
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113    4x                                                                                                                                                         4x           4x 116x             90x         90x                            
import { InputSearchQueryWithFields } from "./api/search"
import { SearchAutocompleteOptions } from "./autocomplete"
import { search } from "./search"
 
/**
 * @group Autocomplete
 * @category Core
 */
export interface GoogleAnalyticsConfig {
    /**
     * Path of search page
     * @default "/search"
     */
    serpPath?: string
    /**
     * Search query url parameter name
     * @default "query"
     */
    queryParamName?: string
    /**
     * Enable Google Analytics
     * @default true
     */
    enabled?: boolean
}
 
type Selector = string | Element
 
/**
 * @group Autocomplete
 * @category Core
 */
export interface AutocompleteConfig<State> {
    /**
     * The input element to attach the autocomplete to
     */
    inputSelector: Selector
    /**
     * The dropdown element to attach the autocomplete to
     */
    dropdownSelector: Selector | ((input: HTMLInputElement) => Selector)
    /**
     * The function to use to render the dropdown
     */
    render: (container: HTMLElement, state: State) => void | PromiseLike<void>
    /**
     * Minimum length of the query before searching
     */
    minQueryLength?: number
    /**
     * The function to use to fetch the search state
     */
    fetch: InputSearchQueryWithFields | ((input: string) => PromiseLike<State>)
    /**
     * The function to use to submit the search
     */
    submit?: (
        query: string,
        config: AutocompleteConfig<State>,
        options?: SearchAutocompleteOptions
    ) => void
    /**
     * Enable history
     */
    historyEnabled?: boolean
    /**
     * Max number of history items to show
     */
    historySize?: number
    /**
     * Enable Nosto Analytics
     */
    nostoAnalytics?: boolean
    /**
     * Google Analytics configuration. Set to `false` to disable.
     */
    googleAnalytics?: GoogleAnalyticsConfig | boolean
}
 
export const defaultGaConfig = {
    serpPath: "/search",
    queryParamName: "query",
    enabled: true,
}
 
export function getDefaultConfig<State>() {
    return {
        minQueryLength: 2,
        historyEnabled: true,
        historySize: 5,
        nostoAnalytics: true,
        googleAnalytics: defaultGaConfig,
        submit: (query, config, options) => {
            if (
                query.length >=
                (config.minQueryLength ??
                    getDefaultConfig<State>().minQueryLength)
            ) {
                search(
                    {
                        query,
                    },
                    {
                        redirect: true,
                        track: config.nostoAnalytics ? "serp" : undefined,
                        ...options,
                    }
                )
            }
        },
    } satisfies Partial<AutocompleteConfig<State>>
}