All files / src search.ts

100% Statements 11/11
89.47% Branches 34/38
100% Functions 1/1
100% Lines 11/11

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 1094x     4x                                                                                                                                                         4x       126x   126x 126x 126x 126x   126x 126x                           126x    
import { getNostoClient } from "./api/client"
import { InputSearchQueryWithFields, SearchOptions } from "./api/search"
 
const defaultProductFields = [
    "productId",
    "url",
    "name",
    "imageUrl",
    "thumbUrl",
    "description",
    "brand",
    "variantId",
    "availability",
    "price",
    "priceText",
    "categoryIds",
    "categories",
    "customFields.key",
    "customFields.value",
    "priceCurrencyCode",
    "datePublished",
    "listPrice",
    "unitPricingBaseMeasure",
    "unitPricingUnit",
    "unitPricingMeasure",
    "googleCategory",
    "gtin",
    "ageGroup",
    "gender",
    "condition",
    "alternateImageUrls",
    "ratingValue",
    "reviewCount",
    "inventoryLevel",
    "skus.id",
    "skus.name",
    "skus.price",
    "skus.listPrice",
    "skus.priceText",
    "skus.url",
    "skus.imageUrl",
    "skus.inventoryLevel",
    "skus.customFields.key",
    "skus.customFields.value",
    "skus.availability",
    "pid",
    "onDiscount",
    "extra.key",
    "extra.value",
    "saleable",
    "available",
    "tags1",
    "tags2",
    "tags3",
]
 
/**
 *
 * @param query Query object.
 * @param options Options object.
 * @returns Promise of search response.
 * @group Autocomplete
 * @category Core
 * @example
 * ```js
 * import { search } from "@nosto/autocomplete"
 *
 * search({
 *     query: "shoes",
 *     products: {
 *       fields: ["name", "price"],
 *       facets: ["brand", "category"],
 *       size: 10,
 *       from: 0,
 *     }
 * }).then((state) => {
 *    console.log(state.response)
 * })
 * ```
 */
export async function search(
    query: InputSearchQueryWithFields,
    options?: SearchOptions
) {
    const { redirect = false, track, isKeyword = false } = options ?? {}
 
    const fields = query.products?.fields ?? defaultProductFields
    const facets = query.products?.facets ?? ["*"]
    const size = query.products?.size ?? 20
    const from = query.products?.from ?? 0
 
    const api = await getNostoClient()
    const response = await api.search(
        {
            ...query,
            products: {
                ...query.products,
                fields,
                facets,
                size,
                from,
            },
        },
        { redirect, track, isKeyword }
    )
 
    return { query, response }
}