@faststore/api 1.8.33 → 1.8.36

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.
@@ -6,6 +6,7 @@ export interface Options {
6
6
  account: string;
7
7
  environment: 'vtexcommercestable' | 'vtexcommercebeta';
8
8
  channel: string;
9
+ locale: string;
9
10
  hideUnavailableItems: boolean;
10
11
  flags?: FeatureFlags;
11
12
  }
@@ -23,6 +24,7 @@ export interface Context {
23
24
  * */
24
25
  storage: {
25
26
  channel: Required<Channel>;
27
+ locale: string;
26
28
  flags: FeatureFlags;
27
29
  };
28
30
  headers: Record<string, string>;
@@ -93,6 +95,7 @@ export declare const getResolvers: (_: Options) => {
93
95
  attachmentsValues?: import("./clients/commerce/types/OrderForm").Attachment[] | undefined;
94
96
  }, unknown, any>>;
95
97
  StoreSearchResult: Record<string, Resolver<Pick<import("./clients/search").SearchArgs, "hideUnavailableItems" | "query" | "page" | "count" | "sort" | "selectedFacets" | "fuzzy">, unknown, any>>;
98
+ StorePropertyValue: Record<string, Resolver<import("../..").IStorePropertyValue, unknown, any>>;
96
99
  ObjectOrString: import("graphql").GraphQLScalarType;
97
100
  Query: {
98
101
  product: (_: unknown, { locator }: import("../..").QueryProductArgs, ctx: Context) => Promise<import("./utils/enhanceSku").EnhancedSku>;
@@ -0,0 +1,5 @@
1
+ import type { Resolver } from '..';
2
+ import type { IStorePropertyValue } from '../../../__generated__/schema';
3
+ declare type Root = IStorePropertyValue;
4
+ export declare const StorePropertyValue: Record<string, Resolver<Root>>;
5
+ export {};
@@ -1,4 +1,3 @@
1
- import type { Context } from '..';
2
1
  export interface Channel {
3
2
  regionId?: string;
4
3
  salesChannel?: string;
@@ -7,4 +6,3 @@ export default class ChannelMarshal {
7
6
  static parse(channelString: string): Required<Channel>;
8
7
  static stringify(channel: Channel): string;
9
8
  }
10
- export declare const mutateChannelContext: (ctx: Context, channelString: string) => void;
@@ -0,0 +1,3 @@
1
+ import type { Context } from '..';
2
+ export declare const mutateChannelContext: (ctx: Context, channelString: string) => void;
3
+ export declare const mutateLocaleContext: (ctx: Context, locale: string) => void;
@@ -13,3 +13,5 @@ export declare const transformSelectedFacet: ({ key, value }: SelectedFacet) =>
13
13
  key: string;
14
14
  value: string;
15
15
  };
16
+ export declare const findLocale: (facets?: SelectedFacet[] | null | undefined) => string | null;
17
+ export declare const findChannel: (facets?: SelectedFacet[] | null | undefined) => string | null;
@@ -1,5 +1,12 @@
1
+ import type { IStorePropertyValue } from '../../../__generated__/schema';
2
+ import type { Attachment } from '../clients/commerce/types/OrderForm';
1
3
  export declare const VALUE_REFERENCES: {
2
- readonly variation: "VARIATION";
3
4
  readonly attachment: "ATTACHMENT";
4
5
  readonly specification: "SPECIFICATION";
5
6
  };
7
+ export declare function attachmentToPropertyValue(attachment: Attachment): {
8
+ name: string;
9
+ value: Record<string, string>;
10
+ valueReference: "ATTACHMENT";
11
+ };
12
+ export declare function getPropertyId(item: IStorePropertyValue): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faststore/api",
3
- "version": "1.8.33",
3
+ "version": "1.8.36",
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": "bc168106d3da1b1a6ca2c0aefcb21f02c7b3df44"
48
+ "gitHead": "5d3f39d424dc7884807d0a7d724736f35ca34f61"
49
49
  }
@@ -472,6 +472,8 @@ export type StorePropertyValue = {
472
472
  __typename?: 'StorePropertyValue';
473
473
  /** Property name. */
474
474
  name: Scalars['String'];
475
+ /** Property id. This propert changes according to the content of the object. */
476
+ propertyID: Scalars['String'];
475
477
  /** Property value. May hold a string or the string representation of an object. */
476
478
  value: Scalars['ObjectOrString'];
477
479
  /** Specifies the nature of the value */
@@ -105,6 +105,7 @@ export const IntelligentSearch = (
105
105
  query,
106
106
  sort,
107
107
  fuzzy,
108
+ locale: ctx.storage.locale,
108
109
  })
109
110
 
110
111
  if (hideUnavailableItems !== undefined) {
@@ -125,20 +126,39 @@ export const IntelligentSearch = (
125
126
 
126
127
  const suggestedProducts = (
127
128
  args: Omit<SearchArgs, 'type'>
128
- ): Promise<ProductSearchResult> =>
129
- fetchAPI(
130
- `${base}/_v/api/intelligent-search/product_search?query=${args.query}`
129
+ ): Promise<ProductSearchResult> => {
130
+ const params = new URLSearchParams({
131
+ query: args.query?.toString() ?? '',
132
+ locale: ctx.storage.locale,
133
+ })
134
+
135
+ return fetchAPI(
136
+ `${base}/_v/api/intelligent-search/product_search?${params.toString()}`
131
137
  )
138
+ }
132
139
 
133
140
  const suggestedTerms = (
134
141
  args: Omit<SearchArgs, 'type'>
135
- ): Promise<Suggestion> =>
136
- fetchAPI(
137
- `${base}/_v/api/intelligent-search/search_suggestions?query=${args.query}`
142
+ ): Promise<Suggestion> => {
143
+ const params = new URLSearchParams({
144
+ query: args.query?.toString() ?? '',
145
+ locale: ctx.storage.locale,
146
+ })
147
+
148
+ return fetchAPI(
149
+ `${base}/_v/api/intelligent-search/search_suggestions?${params.toString()}`
138
150
  )
151
+ }
152
+
153
+ const topSearches = (): Promise<Suggestion> => {
154
+ const params = new URLSearchParams({
155
+ locale: ctx.storage.locale,
156
+ })
139
157
 
140
- const topSearches = (): Promise<Suggestion> =>
141
- fetchAPI(`${base}/_v/api/intelligent-search/top_searches`)
158
+ return fetchAPI(
159
+ `${base}/_v/api/intelligent-search/top_searches?${params.toString()}`
160
+ )
161
+ }
142
162
 
143
163
  const facets = (args: Omit<SearchArgs, 'type'>) =>
144
164
  search<FacetSearchResult>({ ...args, type: 'facets' })
@@ -14,6 +14,7 @@ import { StoreReview } from './resolvers/review'
14
14
  import { StoreSearchResult } from './resolvers/searchResult'
15
15
  import { StoreSeo } from './resolvers/seo'
16
16
  import { ObjectOrString } from './resolvers/objectOrString'
17
+ import { StorePropertyValue } from './resolvers/propertyValue'
17
18
  import type { Loaders } from './loaders'
18
19
  import type { Clients } from './clients'
19
20
  import type { Channel } from './utils/channel'
@@ -25,6 +26,7 @@ export interface Options {
25
26
  environment: 'vtexcommercestable' | 'vtexcommercebeta'
26
27
  // Default sales channel to use for fetching products
27
28
  channel: string
29
+ locale: string
28
30
  hideUnavailableItems: boolean
29
31
  flags?: FeatureFlags
30
32
  }
@@ -44,6 +46,7 @@ export interface Context {
44
46
  * */
45
47
  storage: {
46
48
  channel: Required<Channel>
49
+ locale: string
47
50
  flags: FeatureFlags
48
51
  }
49
52
  headers: Record<string, string>
@@ -68,6 +71,7 @@ const Resolvers = {
68
71
  StoreReview,
69
72
  StoreProductGroup,
70
73
  StoreSearchResult,
74
+ StorePropertyValue,
71
75
  ObjectOrString,
72
76
  Query,
73
77
  Mutation,
@@ -77,6 +81,7 @@ export const getContextFactory = (options: Options) => (ctx: any): Context => {
77
81
  ctx.storage = {
78
82
  channel: ChannelMarshal.parse(options.channel),
79
83
  flags: options.flags ?? {},
84
+ locale: options.locale,
80
85
  }
81
86
  ctx.clients = getClients(options, ctx)
82
87
  ctx.loaders = getLoaders(options, ctx)
@@ -5,7 +5,10 @@ import type { EnhancedCommercialOffer } from '../utils/enhanceCommercialOffer'
5
5
  import type { Resolver } from '..'
6
6
  import type { PromiseType } from '../../../typings'
7
7
  import type { Query } from './query'
8
- import { VALUE_REFERENCES } from '../utils/propertyValue'
8
+ import {
9
+ attachmentToPropertyValue,
10
+ VALUE_REFERENCES,
11
+ } from '../utils/propertyValue'
9
12
  import type { Attachment } from '../clients/commerce/types/OrderForm'
10
13
 
11
14
  type QueryProduct = PromiseType<ReturnType<typeof Query.product>>
@@ -91,23 +94,24 @@ export const StoreProduct: Record<string, Resolver<Root>> & {
91
94
  )
92
95
  .sort(bestOfferFirst),
93
96
  isVariantOf: (root) => root,
94
- additionalProperty: ({ variations = [], attachmentsValues }) => {
95
- const propertyValueVariations = variations.flatMap(({ name, values }) =>
96
- values.map((value) => ({
97
- name,
98
- value,
99
- valueReference: VALUE_REFERENCES.variation,
100
- }))
97
+ additionalProperty: ({
98
+ // Search uses the name variations for specifications
99
+ variations: specifications = [],
100
+ attachmentsValues = [],
101
+ }) => {
102
+ const propertyValueSpecifications = specifications.flatMap(
103
+ ({ name, values }) =>
104
+ values.map((value) => ({
105
+ name,
106
+ value,
107
+ valueReference: VALUE_REFERENCES.specification,
108
+ }))
101
109
  )
102
110
 
103
- const propertyValueAttachments = (attachmentsValues ?? []).map(
104
- (attachment) => ({
105
- name: attachment.name,
106
- value: attachment.content,
107
- valueReference: VALUE_REFERENCES.attachment,
108
- })
111
+ const propertyValueAttachments = attachmentsValues.map(
112
+ attachmentToPropertyValue
109
113
  )
110
114
 
111
- return [...propertyValueVariations, ...propertyValueAttachments]
115
+ return [...propertyValueSpecifications, ...propertyValueAttachments]
112
116
  },
113
117
  }
@@ -0,0 +1,12 @@
1
+ import type { Resolver } from '..'
2
+ import type { IStorePropertyValue } from '../../../__generated__/schema'
3
+ import { getPropertyId } from '../utils/propertyValue'
4
+
5
+ type Root = IStorePropertyValue
6
+
7
+ export const StorePropertyValue: Record<string, Resolver<Root>> = {
8
+ propertyID: (root) => getPropertyId(root),
9
+ name: ({ name }) => name,
10
+ value: ({ value }) => value,
11
+ valueReference: ({ valueReference }) => valueReference,
12
+ }
@@ -1,5 +1,10 @@
1
+ import { mutateChannelContext, mutateLocaleContext } from '../utils/contex'
1
2
  import { enhanceSku } from '../utils/enhanceSku'
2
- import { transformSelectedFacet } from '../utils/facets'
3
+ import {
4
+ findChannel,
5
+ findLocale,
6
+ transformSelectedFacet,
7
+ } from '../utils/facets'
3
8
  import { SORT_MAP } from '../utils/sort'
4
9
  import { StoreCollection } from './collection'
5
10
  import type {
@@ -11,16 +16,19 @@ import type {
11
16
  } from '../../../__generated__/schema'
12
17
  import type { CategoryTree } from '../clients/commerce/types/CategoryTree'
13
18
  import type { Context } from '../index'
14
- import { mutateChannelContext } from '../utils/channel'
15
19
 
16
20
  export const Query = {
17
21
  product: async (_: unknown, { locator }: QueryProductArgs, ctx: Context) => {
18
22
  // Insert channel in context for later usage
19
- const channelString = locator.find((facet) => facet.key === 'channel')
20
- ?.value
23
+ const channel = findChannel(locator)
24
+ const locale = findLocale(locator)
21
25
 
22
- if (channelString) {
23
- mutateChannelContext(ctx, channelString)
26
+ if (channel) {
27
+ mutateChannelContext(ctx, channel)
28
+ }
29
+
30
+ if (locale) {
31
+ mutateLocaleContext(ctx, locale)
24
32
  }
25
33
 
26
34
  const {
@@ -42,12 +50,15 @@ export const Query = {
42
50
  ctx: Context
43
51
  ) => {
44
52
  // Insert channel in context for later usage
45
- const channelString = selectedFacets?.find(
46
- (facet) => facet.key === 'channel'
47
- )?.value
53
+ const channel = findChannel(selectedFacets)
54
+ const locale = findLocale(selectedFacets)
55
+
56
+ if (channel) {
57
+ mutateChannelContext(ctx, channel)
58
+ }
48
59
 
49
- if (channelString) {
50
- mutateChannelContext(ctx, channelString)
60
+ if (locale) {
61
+ mutateLocaleContext(ctx, locale)
51
62
  }
52
63
 
53
64
  const after = maybeAfter ? Number(maybeAfter) : 0
@@ -12,7 +12,11 @@ import type {
12
12
  OrderFormItem,
13
13
  } from '../clients/commerce/types/OrderForm'
14
14
  import type { Context } from '..'
15
- import { VALUE_REFERENCES } from '../utils/propertyValue'
15
+ import {
16
+ attachmentToPropertyValue,
17
+ getPropertyId,
18
+ VALUE_REFERENCES,
19
+ } from '../utils/propertyValue'
16
20
 
17
21
  type Indexed<T> = T & { index?: number }
18
22
 
@@ -21,26 +25,12 @@ const getAttachments = (item: IStoreOffer) =>
21
25
  (i) => i.valueReference === VALUE_REFERENCES.attachment
22
26
  )
23
27
 
24
- const serializeAttachment = (item: IStoreOffer) => {
25
- const attachments = getAttachments(item)
26
-
27
- if (attachments?.length === 0) {
28
- return null
29
- }
30
-
31
- return attachments
32
- ?.map(
33
- (attachment) => `${attachment.name}:${JSON.stringify(attachment.value)}`
34
- )
35
- .join('-')
36
- }
37
-
38
28
  const getId = (item: IStoreOffer) =>
39
29
  [
40
30
  item.itemOffered.sku,
41
31
  item.seller.identifier,
42
32
  item.price,
43
- serializeAttachment(item),
33
+ item.itemOffered.additionalProperty?.map(getPropertyId).join('-'),
44
34
  ]
45
35
  .filter(Boolean)
46
36
  .join('::')
@@ -57,6 +47,7 @@ const orderFormItemToOffer = (
57
47
  sku: item.id,
58
48
  image: [],
59
49
  name: item.name,
50
+ additionalProperty: item.attachments.map(attachmentToPropertyValue),
60
51
  },
61
52
  index,
62
53
  })
@@ -68,7 +59,7 @@ const offerToOrderItemInput = (
68
59
  seller: offer.seller.identifier,
69
60
  id: offer.itemOffered.sku,
70
61
  index: offer.index,
71
- attachments: getAttachments(offer)?.map((attachment) => ({
62
+ attachments: (getAttachments(offer) ?? []).map((attachment) => ({
72
63
  name: attachment.name,
73
64
  content: attachment.value,
74
65
  })),
@@ -1,5 +1,3 @@
1
- import type { Context } from '..'
2
-
3
1
  export interface Channel {
4
2
  regionId?: string
5
3
  salesChannel?: string
@@ -25,7 +23,3 @@ export default class ChannelMarshal {
25
23
  return JSON.stringify(channel)
26
24
  }
27
25
  }
28
-
29
- export const mutateChannelContext = (ctx: Context, channelString: string) => {
30
- ctx.storage.channel = ChannelMarshal.parse(channelString)
31
- }
@@ -0,0 +1,10 @@
1
+ import ChannelMarshal from './channel'
2
+ import type { Context } from '..'
3
+
4
+ export const mutateChannelContext = (ctx: Context, channelString: string) => {
5
+ ctx.storage.channel = ChannelMarshal.parse(channelString)
6
+ }
7
+
8
+ export const mutateLocaleContext = (ctx: Context, locale: string) => {
9
+ ctx.storage.locale = locale
10
+ }
@@ -1,4 +1,5 @@
1
1
  import ChannelMarshal from './channel'
2
+ import type { Maybe } from '../../../__generated__/schema'
2
3
 
3
4
  export interface SelectedFacet {
4
5
  key: string
@@ -24,7 +25,17 @@ export const transformSelectedFacet = ({ key, value }: SelectedFacet) => {
24
25
  return channelFacets
25
26
  }
26
27
 
28
+ case 'locale': {
29
+ return [] // remove this facet from search
30
+ }
31
+
27
32
  default:
28
33
  return { key, value }
29
34
  }
30
35
  }
36
+
37
+ export const findLocale = (facets?: Maybe<SelectedFacet[]>) =>
38
+ facets?.find((x) => x.key === 'locale')?.value ?? null
39
+
40
+ export const findChannel = (facets?: Maybe<SelectedFacet[]>) =>
41
+ facets?.find((facet) => facet.key === 'channel')?.value ?? null
@@ -1,5 +1,22 @@
1
+ import type { IStorePropertyValue } from '../../../__generated__/schema'
2
+ import type { Attachment } from '../clients/commerce/types/OrderForm'
3
+ import { md5 } from './md5'
4
+
1
5
  export const VALUE_REFERENCES = {
2
- variation: 'VARIATION',
3
6
  attachment: 'ATTACHMENT',
4
7
  specification: 'SPECIFICATION',
5
8
  } as const
9
+
10
+ export function attachmentToPropertyValue(attachment: Attachment) {
11
+ return {
12
+ name: attachment.name,
13
+ value: attachment.content,
14
+ valueReference: VALUE_REFERENCES.attachment,
15
+ }
16
+ }
17
+
18
+ export function getPropertyId(item: IStorePropertyValue) {
19
+ return md5(
20
+ `${item.name}:${JSON.stringify(item.value)}:${item.valueReference}`
21
+ )
22
+ }
@@ -2,6 +2,10 @@
2
2
  Properties that can be associated with products and products groups.
3
3
  """
4
4
  type StorePropertyValue {
5
+ """
6
+ Property id. This propert changes according to the content of the object.
7
+ """
8
+ propertyID: String!
5
9
  """
6
10
  Property value. May hold a string or the string representation of an object.
7
11
  """