@faststore/api 1.12.29 → 1.12.35

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/dist/index.d.ts CHANGED
@@ -71,7 +71,10 @@ export declare const getResolvers: (options: Options) => {
71
71
  } & {
72
72
  attachmentsValues?: import("./platforms/vtex/clients/commerce/types/OrderForm").Attachment[] | undefined;
73
73
  }, unknown, any>>;
74
- StoreSearchResult: Record<string, import("./platforms/vtex").Resolver<Pick<import("./platforms/vtex/clients/search").SearchArgs, "sort" | "selectedFacets" | "hideUnavailableItems" | "query" | "page" | "count" | "fuzzy">, unknown, any>>;
74
+ StoreSearchResult: Record<string, import("./platforms/vtex").Resolver<{
75
+ searchArgs: Pick<import("./platforms/vtex/clients/search").SearchArgs, "sort" | "selectedFacets" | "hideUnavailableItems" | "query" | "page" | "count" | "fuzzy">;
76
+ productSearchPromise: Promise<import("./platforms/vtex/clients/search/types/ProductSearchResult").ProductSearchResult>;
77
+ }, unknown, any>>;
75
78
  StorePropertyValue: Record<string, import("./platforms/vtex").Resolver<import("./__generated__/schema").IStorePropertyValue, unknown, any>>;
76
79
  SkuVariants: Record<string, import("./platforms/vtex").Resolver<import("./platforms/vtex/clients/search/types/ProductSearchResult").Item & {
77
80
  isVariantOf: import("./platforms/vtex/clients/search/types/ProductSearchResult").Product;
@@ -89,14 +92,8 @@ export declare const getResolvers: (options: Options) => {
89
92
  product: (_: unknown, { locator }: import("./__generated__/schema").QueryProductArgs, ctx: import("./platforms/vtex").Context) => Promise<import("./platforms/vtex/utils/enhanceSku").EnhancedSku>;
90
93
  collection: (_: unknown, { slug }: import("./__generated__/schema").QueryCollectionArgs, ctx: import("./platforms/vtex").Context) => Promise<import("./platforms/vtex/clients/commerce/types/Portal").CollectionPageType>;
91
94
  search: (_: unknown, { first, after: maybeAfter, sort, term, selectedFacets }: import("./__generated__/schema").QuerySearchArgs, ctx: import("./platforms/vtex").Context) => Promise<{
92
- page: number;
93
- count: number;
94
- query: string | null | undefined;
95
- sort: import("./platforms/vtex/clients/search").Sort;
96
- selectedFacets: {
97
- key: string;
98
- value: string;
99
- }[];
95
+ searchArgs: Pick<import("./platforms/vtex/clients/search").SearchArgs, "sort" | "selectedFacets" | "hideUnavailableItems" | "query" | "page" | "count" | "fuzzy">;
96
+ productSearchPromise: Promise<import("./platforms/vtex/clients/search/types/ProductSearchResult").ProductSearchResult>;
100
97
  }>;
101
98
  allProducts: (_: unknown, { first, after: maybeAfter }: import("./__generated__/schema").QueryAllProductsArgs, ctx: import("./platforms/vtex").Context) => Promise<{
102
99
  pageInfo: {
@@ -99,7 +99,10 @@ export declare const getResolvers: (_: Options) => {
99
99
  } & {
100
100
  attachmentsValues?: import("./clients/commerce/types/OrderForm").Attachment[] | undefined;
101
101
  }, unknown, any>>;
102
- StoreSearchResult: Record<string, Resolver<Pick<SearchArgs, "hideUnavailableItems" | "query" | "page" | "count" | "sort" | "selectedFacets" | "fuzzy">, unknown, any>>;
102
+ StoreSearchResult: Record<string, Resolver<{
103
+ searchArgs: Pick<SearchArgs, "hideUnavailableItems" | "query" | "page" | "count" | "sort" | "selectedFacets" | "fuzzy">;
104
+ productSearchPromise: Promise<import("./clients/search/types/ProductSearchResult").ProductSearchResult>;
105
+ }, unknown, any>>;
103
106
  StorePropertyValue: Record<string, Resolver<import("../..").IStorePropertyValue, unknown, any>>;
104
107
  SkuVariants: Record<string, Resolver<import("./clients/search/types/ProductSearchResult").Item & {
105
108
  isVariantOf: import("./clients/search/types/ProductSearchResult").Product;
@@ -117,14 +120,8 @@ export declare const getResolvers: (_: Options) => {
117
120
  product: (_: unknown, { locator }: import("../..").QueryProductArgs, ctx: Context) => Promise<import("./utils/enhanceSku").EnhancedSku>;
118
121
  collection: (_: unknown, { slug }: import("../..").QueryCollectionArgs, ctx: Context) => Promise<import("./clients/commerce/types/Portal").CollectionPageType>;
119
122
  search: (_: unknown, { first, after: maybeAfter, sort, term, selectedFacets }: import("../..").QuerySearchArgs, ctx: Context) => Promise<{
120
- page: number;
121
- count: number;
122
- query: string | null | undefined;
123
- sort: import("./clients/search").Sort;
124
- selectedFacets: {
125
- key: string;
126
- value: string;
127
- }[];
123
+ searchArgs: Pick<SearchArgs, "hideUnavailableItems" | "query" | "page" | "count" | "sort" | "selectedFacets" | "fuzzy">;
124
+ productSearchPromise: Promise<import("./clients/search/types/ProductSearchResult").ProductSearchResult>;
128
125
  }>;
129
126
  allProducts: (_: unknown, { first, after: maybeAfter }: import("../..").QueryAllProductsArgs, ctx: Context) => Promise<{
130
127
  pageInfo: {
@@ -1,18 +1,13 @@
1
1
  import type { QueryAllCollectionsArgs, QueryAllProductsArgs, QueryCollectionArgs, QueryProductArgs, QuerySearchArgs, QueryShippingArgs } from "../../../__generated__/schema";
2
2
  import type { CategoryTree } from "../clients/commerce/types/CategoryTree";
3
3
  import type { Context } from "../index";
4
+ import { SearchArgs } from "../clients/search";
4
5
  export declare const Query: {
5
6
  product: (_: unknown, { locator }: QueryProductArgs, ctx: Context) => Promise<import("../utils/enhanceSku").EnhancedSku>;
6
7
  collection: (_: unknown, { slug }: QueryCollectionArgs, ctx: Context) => Promise<import("../clients/commerce/types/Portal").CollectionPageType>;
7
8
  search: (_: unknown, { first, after: maybeAfter, sort, term, selectedFacets }: QuerySearchArgs, ctx: Context) => Promise<{
8
- page: number;
9
- count: number;
10
- query: string | null | undefined;
11
- sort: import("../clients/search").Sort;
12
- selectedFacets: {
13
- key: string;
14
- value: string;
15
- }[];
9
+ searchArgs: Pick<SearchArgs, "sort" | "selectedFacets" | "hideUnavailableItems" | "query" | "page" | "count" | "fuzzy">;
10
+ productSearchPromise: Promise<import("../clients/search/types/ProductSearchResult").ProductSearchResult>;
16
11
  }>;
17
12
  allProducts: (_: unknown, { first, after: maybeAfter }: QueryAllProductsArgs, ctx: Context) => Promise<{
18
13
  pageInfo: {
@@ -1,5 +1,9 @@
1
1
  import type { Resolver } from '..';
2
2
  import type { SearchArgs } from '../clients/search';
3
- declare type Root = Omit<SearchArgs, 'type'>;
3
+ import { ProductSearchResult } from '../clients/search/types/ProductSearchResult';
4
+ declare type Root = {
5
+ searchArgs: Omit<SearchArgs, 'type'>;
6
+ productSearchPromise: Promise<ProductSearchResult>;
7
+ };
4
8
  export declare const StoreSearchResult: Record<string, Resolver<Root>>;
5
9
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faststore/api",
3
- "version": "1.12.29",
3
+ "version": "1.12.35",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -30,7 +30,7 @@
30
30
  "p-limit": "^3.1.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@faststore/shared": "^1.12.20",
33
+ "@faststore/shared": "^1.12.35",
34
34
  "@graphql-codegen/cli": "2.2.0",
35
35
  "@graphql-codegen/typescript": "2.2.2",
36
36
  "concurrently": "^6.2.1",
@@ -46,5 +46,5 @@
46
46
  "peerDependencies": {
47
47
  "graphql": "^15.6.0"
48
48
  },
49
- "gitHead": "6027c3d8b855417e543ed9f46a97a6659b75a6eb"
49
+ "gitHead": "5271bbd397d348bebbda2bd12006d9e75fc915cb"
50
50
  }
@@ -435,6 +435,15 @@ export type QueryShippingArgs = {
435
435
  postalCode: Scalars['String'];
436
436
  };
437
437
 
438
+ /** Search result. */
439
+ export type SearchMetadata = {
440
+ __typename?: 'SearchMetadata';
441
+ /** Indicates if the search term was misspelled. */
442
+ isTermMisspelled: Scalars['Boolean'];
443
+ /** Logical operator used to run the search. */
444
+ logicalOperator: Scalars['String'];
445
+ };
446
+
438
447
  /** Shipping Simulation information. */
439
448
  export type ShippingData = {
440
449
  __typename?: 'ShippingData';
@@ -908,6 +917,8 @@ export type StoreSearchResult = {
908
917
  __typename?: 'StoreSearchResult';
909
918
  /** Array of search result facets. */
910
919
  facets: Array<StoreFacet>;
920
+ /** Search result metadata. Additional data can be used to send analytics events. */
921
+ metadata?: Maybe<SearchMetadata>;
911
922
  /** Search result products. */
912
923
  products: StoreProductConnection;
913
924
  /** Search result suggestions. */
@@ -23,6 +23,7 @@ import type {
23
23
  import type { CategoryTree } from "../clients/commerce/types/CategoryTree"
24
24
  import type { Context } from "../index"
25
25
  import { isValidSkuId, pickBestSku } from "../utils/sku"
26
+ import { SearchArgs } from "../clients/search"
26
27
 
27
28
  export const Query = {
28
29
  product: async (_: unknown, { locator }: QueryProductArgs, ctx: Context) => {
@@ -147,15 +148,17 @@ export const Query = {
147
148
  }
148
149
 
149
150
  const after = maybeAfter ? Number(maybeAfter) : 0
150
- const searchArgs = {
151
+ const searchArgs: Omit<SearchArgs, 'type'> = {
151
152
  page: Math.ceil(after / first),
152
153
  count: first,
153
- query,
154
+ query: query ?? undefined,
154
155
  sort: SORT_MAP[sort ?? 'score_desc'],
155
156
  selectedFacets: selectedFacets?.flatMap(transformSelectedFacet) ?? [],
156
157
  }
157
158
 
158
- return searchArgs
159
+ const productSearchPromise = ctx.clients.search.products(searchArgs)
160
+
161
+ return { searchArgs, productSearchPromise }
159
162
  },
160
163
  allProducts: async (
161
164
  _: unknown,
@@ -2,17 +2,23 @@ import { enhanceSku } from '../utils/enhanceSku'
2
2
  import type { Resolver } from '..'
3
3
  import type { SearchArgs } from '../clients/search'
4
4
  import type { Facet } from '../clients/search/types/FacetSearchResult'
5
+ import { ProductSearchResult } from '../clients/search/types/ProductSearchResult'
5
6
 
6
- type Root = Omit<SearchArgs, 'type'>
7
+ type Root = {
8
+ searchArgs: Omit<SearchArgs, 'type'>
9
+ productSearchPromise: Promise<ProductSearchResult>
10
+ }
7
11
 
8
12
  const isRootFacet = (facet: Facet) => facet.key === 'category-1'
9
13
 
10
14
  export const StoreSearchResult: Record<string, Resolver<Root>> = {
11
- suggestions: async (searchArgs, _, ctx) => {
15
+ suggestions: async (root, _, ctx) => {
12
16
  const {
13
17
  clients: { search },
14
18
  } = ctx
15
19
 
20
+ const { searchArgs } = root
21
+
16
22
  // If there's no search query, suggest the most popular searches.
17
23
  if (!searchArgs.query) {
18
24
  const topSearches = await search.topSearches()
@@ -26,10 +32,13 @@ export const StoreSearchResult: Record<string, Resolver<Root>> = {
26
32
  }
27
33
  }
28
34
 
29
- const terms = await search.suggestedTerms(searchArgs)
30
- const products = await search.products(searchArgs)
35
+ const { productSearchPromise } = root
36
+ const [terms, productSearchResult] = await Promise.all([
37
+ search.suggestedTerms(searchArgs),
38
+ productSearchPromise,
39
+ ])
31
40
 
32
- const skus = products.products
41
+ const skus = productSearchResult.products
33
42
  .map((product) => {
34
43
  const [maybeSku] = product.items
35
44
 
@@ -44,27 +53,10 @@ export const StoreSearchResult: Record<string, Resolver<Root>> = {
44
53
  products: skus,
45
54
  }
46
55
  },
47
- products: async (searchArgs, _, ctx) => {
48
- const {
49
- clients: { search, sp },
50
- } = ctx
56
+ products: async ({ productSearchPromise }) => {
57
+ const productSearchResult = await productSearchPromise
51
58
 
52
- const products = await search.products(searchArgs)
53
-
54
- // Raise event on search's analytics API when performing
55
- // a full text search.
56
- if (searchArgs.query) {
57
- sp.sendEvent({
58
- type: 'search.query',
59
- text: searchArgs.query,
60
- misspelled: products.correction?.misspelled ?? false,
61
- match: products.recordsFiltered,
62
- operator: products.operator,
63
- locale: ctx.storage.locale,
64
- }).catch(console.error)
65
- }
66
-
67
- const skus = products.products
59
+ const skus = productSearchResult.products
68
60
  .map((product) => {
69
61
  const [maybeSku] = product.items
70
62
 
@@ -74,11 +66,11 @@ export const StoreSearchResult: Record<string, Resolver<Root>> = {
74
66
 
75
67
  return {
76
68
  pageInfo: {
77
- hasNextPage: products.pagination.after.length > 0,
78
- hasPreviousPage: products.pagination.before.length > 0,
69
+ hasNextPage: productSearchResult.pagination.after.length > 0,
70
+ hasPreviousPage: productSearchResult.pagination.before.length > 0,
79
71
  startCursor: '0',
80
- endCursor: products.recordsFiltered.toString(),
81
- totalCount: products.recordsFiltered,
72
+ endCursor: productSearchResult.recordsFiltered.toString(),
73
+ totalCount: productSearchResult.recordsFiltered,
82
74
  },
83
75
  edges: skus.map((sku, index) => ({
84
76
  node: sku,
@@ -86,7 +78,7 @@ export const StoreSearchResult: Record<string, Resolver<Root>> = {
86
78
  })),
87
79
  }
88
80
  },
89
- facets: async (searchArgs, _, ctx) => {
81
+ facets: async ({ searchArgs }, _, ctx) => {
90
82
  const {
91
83
  clients: { search: is },
92
84
  } = ctx
@@ -103,4 +95,16 @@ export const StoreSearchResult: Record<string, Resolver<Root>> = {
103
95
 
104
96
  return filteredFacets
105
97
  },
98
+ metadata: async ({ searchArgs, productSearchPromise }) => {
99
+ if (!searchArgs.query) {
100
+ return null
101
+ }
102
+
103
+ const productSearchResult = await productSearchPromise
104
+
105
+ return {
106
+ isTermMisspelled: productSearchResult.correction?.misspelled ?? false,
107
+ logicalOperator: productSearchResult.operator,
108
+ }
109
+ },
106
110
  }
@@ -148,6 +148,20 @@ type StoreSuggestions {
148
148
  products: [StoreProduct!]!
149
149
  }
150
150
 
151
+ """
152
+ Search result.
153
+ """
154
+ type SearchMetadata {
155
+ """
156
+ Indicates if the search term was misspelled.
157
+ """
158
+ isTermMisspelled: Boolean!
159
+ """
160
+ Logical operator used to run the search.
161
+ """
162
+ logicalOperator: String!
163
+ }
164
+
151
165
  """
152
166
  Search result.
153
167
  """
@@ -164,6 +178,10 @@ type StoreSearchResult {
164
178
  Search result suggestions.
165
179
  """
166
180
  suggestions: StoreSuggestions!
181
+ """
182
+ Search result metadata. Additional data can be used to send analytics events.
183
+ """
184
+ metadata: SearchMetadata
167
185
  }
168
186
 
169
187
  type Query {