@faststore/api 1.7.41 → 1.7.42

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faststore/api",
3
- "version": "1.7.41",
3
+ "version": "1.7.42",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -45,5 +45,5 @@
45
45
  "peerDependencies": {
46
46
  "graphql": "^15.6.0"
47
47
  },
48
- "gitHead": "ccf230bffafab4663461e8afba9f1056b599d284"
48
+ "gitHead": "b2b8d64ca2b10d14ad3f92523ef9a38bbaab95ae"
49
49
  }
@@ -105,7 +105,7 @@ export type QueryProductArgs = {
105
105
 
106
106
  export type QuerySearchArgs = {
107
107
  after?: Maybe<Scalars['String']>;
108
- first: Scalars['Int'];
108
+ first?: Maybe<Scalars['Int']>;
109
109
  selectedFacets?: Maybe<Array<IStoreSelectedFacet>>;
110
110
  sort?: Maybe<StoreSort>;
111
111
  term?: Maybe<Scalars['String']>;
@@ -329,8 +329,9 @@ export type StoreReviewRating = {
329
329
 
330
330
  export type StoreSearchResult = {
331
331
  __typename?: 'StoreSearchResult';
332
- facets: Array<StoreFacet>;
333
- products: StoreProductConnection;
332
+ facets?: Maybe<Array<StoreFacet>>;
333
+ products?: Maybe<StoreProductConnection>;
334
+ suggestions?: Maybe<StoreSuggestions>;
334
335
  };
335
336
 
336
337
  export type StoreSeo = {
@@ -364,3 +365,9 @@ export const enum StoreStatus {
364
365
  Info = 'INFO',
365
366
  Warning = 'WARNING'
366
367
  };
368
+
369
+ export type StoreSuggestions = {
370
+ __typename?: 'StoreSuggestions';
371
+ products?: Maybe<Array<StoreProduct>>;
372
+ terms?: Maybe<Array<Scalars['String']>>;
373
+ };
@@ -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
- products: StoreProductConnection!
44
- facets: [StoreFacet!]!
48
+ products: StoreProductConnection
49
+ facets: [StoreFacet!]
50
+ suggestions: StoreSuggestions
45
51
  }
46
52
 
47
53
  type Query {
@@ -50,7 +56,7 @@ type Query {
50
56
  collection(slug: String!): StoreCollection!
51
57
 
52
58
  search(
53
- first: Int!
59
+ first: Int
54
60
  after: String
55
61
  sort: StoreSort = score_desc
56
62
  term: String = ""