@faststore/api 1.8.17 → 1.8.20

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,7 +1,7 @@
1
+ import { fetchAPI } from '../fetch'
1
2
  import type { IStoreSelectedFacet } from '../../../../__generated__/schema'
2
3
  import type { Context, Options } from '../../index'
3
4
  import type { SelectedFacet } from '../../utils/facets'
4
- import { fetchAPI } from '../fetch'
5
5
  import type { FacetSearchResult } from './types/FacetSearchResult'
6
6
  import type {
7
7
  ProductSearchResult,
@@ -34,24 +34,60 @@ export interface ProductLocator {
34
34
  value: string
35
35
  }
36
36
 
37
+ const POLICY_KEY = 'trade-policy'
38
+ const REGION_KEY = 'region-id'
39
+ const CHANNEL_KEYS = new Set([POLICY_KEY, REGION_KEY])
40
+
37
41
  export const IntelligentSearch = (
38
42
  { account, environment, hideUnavailableItems }: Options,
39
43
  ctx: Context
40
44
  ) => {
41
45
  const base = `https://${account}.${environment}.com.br/api/io`
42
- const policyFacet: IStoreSelectedFacet = {
43
- key: 'trade-policy',
44
- value: ctx.storage.channel.salesChannel,
46
+
47
+ const getPolicyFacet = (): IStoreSelectedFacet | null => {
48
+ const { salesChannel } = ctx.storage.channel
49
+
50
+ if (!salesChannel) {
51
+ return null
52
+ }
53
+
54
+ return {
55
+ key: POLICY_KEY,
56
+ value: salesChannel,
57
+ }
58
+ }
59
+
60
+ const getRegionFacet = (): IStoreSelectedFacet | null => {
61
+ const { regionId } = ctx.storage.channel
62
+
63
+ if (!regionId) {
64
+ return null
65
+ }
66
+
67
+ return {
68
+ key: REGION_KEY,
69
+ value: regionId,
70
+ }
45
71
  }
46
72
 
47
73
  const addDefaultFacets = (facets: SelectedFacet[]) => {
48
- const facet = facets.find(({ key }) => key === policyFacet.key)
74
+ const withDefaltFacets = facets.filter(({ key }) => !CHANNEL_KEYS.has(key))
75
+
76
+ const policyFacet =
77
+ facets.find(({ key }) => key === POLICY_KEY) ?? getPolicyFacet()
49
78
 
50
- if (facet === undefined) {
51
- return [...facets, policyFacet]
79
+ const regionFacet =
80
+ facets.find(({ key }) => key === REGION_KEY) ?? getRegionFacet()
81
+
82
+ if (policyFacet !== null) {
83
+ withDefaltFacets.push(policyFacet)
84
+ }
85
+
86
+ if (regionFacet !== null) {
87
+ withDefaltFacets.push(regionFacet)
52
88
  }
53
89
 
54
- return facets
90
+ return withDefaltFacets
55
91
  }
56
92
 
57
93
  const search = <T>({
@@ -101,6 +137,9 @@ export const IntelligentSearch = (
101
137
  `${base}/_v/api/intelligent-search/search_suggestions?query=${args.query}`
102
138
  )
103
139
 
140
+ const topSearches = (): Promise<Suggestion> =>
141
+ fetchAPI(`${base}/_v/api/intelligent-search/top_searches`)
142
+
104
143
  const facets = (args: Omit<SearchArgs, 'type'>) =>
105
144
  search<FacetSearchResult>({ ...args, type: 'facets' })
106
145
 
@@ -109,5 +148,6 @@ export const IntelligentSearch = (
109
148
  products,
110
149
  suggestedTerms,
111
150
  suggestedProducts,
151
+ topSearches,
112
152
  }
113
153
  }
@@ -27,7 +27,7 @@ export const Query = {
27
27
  loaders: { skuLoader },
28
28
  } = ctx
29
29
 
30
- return skuLoader.load(locator.flatMap(transformSelectedFacet))
30
+ return skuLoader.load(locator)
31
31
  },
32
32
  collection: (_: unknown, { slug }: QueryCollectionArgs, ctx: Context) => {
33
33
  const {
@@ -13,6 +13,16 @@ export const StoreSearchResult: Record<string, Resolver<Root>> = {
13
13
  clients: { search },
14
14
  } = ctx
15
15
 
16
+ // If there's no search query, suggest the most popular searches.
17
+ if (!searchArgs.query) {
18
+ const topSearches = await search.topSearches()
19
+
20
+ return {
21
+ terms: topSearches.searches.map((item) => item.term),
22
+ products: [],
23
+ }
24
+ }
25
+
16
26
  const terms = await search.suggestedTerms(searchArgs)
17
27
  const products = await search.suggestedProducts(searchArgs)
18
28
 
@@ -27,8 +27,5 @@ export default class ChannelMarshal {
27
27
  }
28
28
 
29
29
  export const mutateChannelContext = (ctx: Context, channelString: string) => {
30
- ctx.storage = {
31
- ...ctx.storage,
32
- channel: ChannelMarshal.parse(channelString),
33
- }
30
+ ctx.storage.channel = ChannelMarshal.parse(channelString)
34
31
  }