@faststore/api 1.7.41 → 1.7.44

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,12 @@
1
+ import type { IStoreSelectedFacet } from '../../../../__generated__/schema'
1
2
  import type { Context, Options } from '../../index'
2
- import { fetchAPI } from '../fetch'
3
3
  import type { SelectedFacet } from '../../utils/facets'
4
- import type { ProductSearchResult } from './types/ProductSearchResult'
4
+ import { fetchAPI } from '../fetch'
5
5
  import type { FacetSearchResult } from './types/FacetSearchResult'
6
- import type { IStoreSelectedFacet } from '../../../../__generated__/schema'
6
+ import type {
7
+ ProductSearchResult,
8
+ SuggestedTerms,
9
+ } from './types/ProductSearchResult'
7
10
 
8
11
  export type Sort =
9
12
  | 'price:desc'
@@ -84,11 +87,23 @@ export const IntelligentSearch = (
84
87
  const products = (args: Omit<SearchArgs, 'type'>) =>
85
88
  search<ProductSearchResult>({ ...args, type: 'product_search' })
86
89
 
90
+ const suggestedProducts = (
91
+ args: Omit<SearchArgs, 'type'>
92
+ ): Promise<ProductSearchResult> =>
93
+ fetchAPI(`${base}/api/suggestion_products/?term=${args.query}`)
94
+
95
+ const suggestedTerms = (
96
+ args: Omit<SearchArgs, 'type'>
97
+ ): Promise<SuggestedTerms> =>
98
+ fetchAPI(`${base}/api/split/suggestion_search/?q=${args.query}`)
99
+
87
100
  const facets = (args: Omit<SearchArgs, 'type'>) =>
88
101
  search<FacetSearchResult>({ ...args, type: 'facets' })
89
102
 
90
103
  return {
91
104
  facets,
92
105
  products,
106
+ suggestedTerms,
107
+ suggestedProducts,
93
108
  }
94
109
  }
@@ -53,6 +53,30 @@ interface Page {
53
53
  proxyURL: string
54
54
  }
55
55
 
56
+ export interface First {
57
+ index: number
58
+ }
59
+
60
+ export interface SuggestedTerms {
61
+ total: number
62
+ sampling: boolean
63
+ translated: boolean
64
+ locale: string
65
+ query: string
66
+ operator: string
67
+ suggestion: Suggestion
68
+ correction: Correction
69
+ }
70
+
71
+ export interface Suggestion {
72
+ searches: Search[]
73
+ }
74
+
75
+ export interface Search {
76
+ term: string
77
+ count: number
78
+ }
79
+
56
80
  export interface Product {
57
81
  productId: string
58
82
  productName: string
@@ -52,7 +52,7 @@ export const Query = {
52
52
 
53
53
  const after = maybeAfter ? Number(maybeAfter) : 0
54
54
  const searchArgs = {
55
- page: Math.ceil(after / first),
55
+ page: Math.ceil(after / first!),
56
56
  count: first,
57
57
  query: term,
58
58
  sort: SORT_MAP[sort ?? 'score_desc'],
@@ -1,13 +1,38 @@
1
- import { enhanceSku } from '../utils/enhanceSku'
2
1
  import type { Resolver } from '..'
3
2
  import type { SearchArgs } from '../clients/search'
4
3
  import type { Facet } from '../clients/search/types/FacetSearchResult'
4
+ import { enhanceSku } from '../utils/enhanceSku'
5
5
 
6
6
  type Root = Omit<SearchArgs, 'type'>
7
7
 
8
8
  const REMOVED_FACETS_FROM_COLLECTION_PAGE = ['departamento', 'Departamento']
9
9
 
10
10
  export const StoreSearchResult: Record<string, Resolver<Root>> = {
11
+ suggestions: async (searchArgs, _, ctx) => {
12
+ const {
13
+ clients: { search },
14
+ } = ctx
15
+
16
+ const terms = await search.suggestedTerms(searchArgs)
17
+ const products = await search.suggestedProducts(searchArgs)
18
+
19
+ const skus = products.products
20
+ .map((product) => {
21
+ const [maybeSku] = product.items
22
+
23
+ return maybeSku && enhanceSku(maybeSku, product)
24
+ })
25
+ .filter((sku) => !!sku)
26
+
27
+ const {
28
+ suggestion: { searches },
29
+ } = terms
30
+
31
+ return {
32
+ terms: searches.map((item) => item.term),
33
+ products: skus,
34
+ }
35
+ },
11
36
  products: async (searchArgs, _, ctx) => {
12
37
  const {
13
38
  clients: { search, sp },
@@ -39,9 +39,15 @@ enum StoreFacetType {
39
39
  RANGE
40
40
  }
41
41
 
42
+ type StoreSuggestions {
43
+ terms: [String!]
44
+ products: [StoreProduct!]
45
+ }
46
+
42
47
  type StoreSearchResult {
43
48
  products: StoreProductConnection!
44
49
  facets: [StoreFacet!]!
50
+ suggestions: StoreSuggestions!
45
51
  }
46
52
 
47
53
  type Query {