@faststore/api 1.6.25 → 1.6.28

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.
@@ -15,6 +15,8 @@ import { StoreSearchResult } from './resolvers/searchResult'
15
15
  import { StoreSeo } from './resolvers/seo'
16
16
  import type { Loaders } from './loaders'
17
17
  import type { Clients } from './clients'
18
+ import type { Channel } from './utils/channel'
19
+ import ChannelMarshal from './utils/channel'
18
20
 
19
21
  export interface Options {
20
22
  platform: 'vtex'
@@ -35,7 +37,7 @@ export interface Context {
35
37
  * Use it with caution since dependecy injection leads to a more complex code
36
38
  * */
37
39
  storage: {
38
- channel: string
40
+ channel: Required<Channel>
39
41
  }
40
42
  headers: Record<string, string>
41
43
  }
@@ -63,9 +65,9 @@ const Resolvers = {
63
65
  Mutation,
64
66
  }
65
67
 
66
- export const getContextFactory = (options: Options) => (ctx: any) => {
68
+ export const getContextFactory = (options: Options) => (ctx: any): Context => {
67
69
  ctx.storage = {
68
- channel: options.channel,
70
+ channel: ChannelMarshal.parse(options.channel),
69
71
  }
70
72
  ctx.clients = getClients(options, ctx)
71
73
  ctx.loaders = getLoaders(options, ctx)
@@ -62,13 +62,16 @@ export const StoreProduct: Record<string, Resolver<Root>> = {
62
62
  } = ctx
63
63
 
64
64
  const { id, policies } = product
65
- const sellers = policies.find((policy) => policy.id === channel)?.sellers
66
65
 
67
- if (sellers == null) {
66
+ const sellers = policies.find(
67
+ (policy) => policy.id === channel.salesChannel
68
+ )?.sellers
69
+
70
+ if (sellers === null || sellers === undefined) {
68
71
  // This error will likely happen when you forget to forward the channel somewhere in your code.
69
72
  // Make sure all queries that lead to a product are forwarding the channel in context corectly
70
73
  throw new Error(
71
- `Product with id ${id} has no sellers for channel ${channel}.`
74
+ `Product with id ${id} has no sellers for sales channel ${channel.salesChannel}.`
72
75
  )
73
76
  }
74
77
 
@@ -11,22 +11,23 @@ import type {
11
11
  } from '../../../__generated__/schema'
12
12
  import type { CategoryTree } from '../clients/commerce/types/CategoryTree'
13
13
  import type { Context } from '../index'
14
+ import { mutateChannelContext } from '../utils/channel'
14
15
 
15
16
  export const Query = {
16
17
  product: async (_: unknown, { locator }: QueryProductArgs, ctx: Context) => {
17
18
  // Insert channel in context for later usage
18
- ctx.storage = {
19
- ...ctx.storage,
20
- channel:
21
- locator.find((facet) => facet.key === 'channel')?.value ??
22
- ctx.storage.channel,
19
+ const channelString = locator.find((facet) => facet.key === 'channel')
20
+ ?.value
21
+
22
+ if (channelString) {
23
+ mutateChannelContext(ctx, channelString)
23
24
  }
24
25
 
25
26
  const {
26
27
  loaders: { skuLoader },
27
28
  } = ctx
28
29
 
29
- return skuLoader.load(locator.map(transformSelectedFacet))
30
+ return skuLoader.load(locator.flatMap(transformSelectedFacet))
30
31
  },
31
32
  collection: (_: unknown, { slug }: QueryCollectionArgs, ctx: Context) => {
32
33
  const {
@@ -41,11 +42,12 @@ export const Query = {
41
42
  ctx: Context
42
43
  ) => {
43
44
  // Insert channel in context for later usage
44
- ctx.storage = {
45
- ...ctx.storage,
46
- channel:
47
- selectedFacets?.find((facet) => facet.key === 'channel')?.value ??
48
- ctx.storage.channel,
45
+ const channelString = selectedFacets?.find(
46
+ (facet) => facet.key === 'channel'
47
+ )?.value
48
+
49
+ if (channelString) {
50
+ mutateChannelContext(ctx, channelString)
49
51
  }
50
52
 
51
53
  const after = maybeAfter ? Number(maybeAfter) : 0
@@ -54,7 +56,7 @@ export const Query = {
54
56
  count: first,
55
57
  query: term,
56
58
  sort: SORT_MAP[sort ?? 'score_desc'],
57
- selectedFacets: selectedFacets?.map(transformSelectedFacet) ?? [],
59
+ selectedFacets: selectedFacets?.flatMap(transformSelectedFacet) ?? [],
58
60
  }
59
61
 
60
62
  return searchArgs
@@ -1,10 +1,12 @@
1
+ import type { Context } from '..'
2
+
1
3
  export interface Channel {
2
4
  regionId?: string
3
5
  salesChannel?: string
4
6
  }
5
7
 
6
8
  export default class ChannelMarshal {
7
- public static parse(channelString: string): Channel {
9
+ public static parse(channelString: string): Required<Channel> {
8
10
  try {
9
11
  const parsedChannel = JSON.parse(channelString) as Channel
10
12
 
@@ -23,3 +25,10 @@ export default class ChannelMarshal {
23
25
  return JSON.stringify(channel)
24
26
  }
25
27
  }
28
+
29
+ export const mutateChannelContext = (ctx: Context, channelString: string) => {
30
+ ctx.storage = {
31
+ ...ctx.storage,
32
+ channel: ChannelMarshal.parse(channelString),
33
+ }
34
+ }
@@ -1,3 +1,5 @@
1
+ import ChannelMarshal from './channel'
2
+
1
3
  export interface SelectedFacet {
2
4
  key: string
3
5
  value: string
@@ -5,12 +7,16 @@ export interface SelectedFacet {
5
7
 
6
8
  /**
7
9
  * Transform facets from the store to VTEX platform facets.
8
- * For instance, the channel in Store becomes trade-policy in VTEX's realm
10
+ * For instance, the channel in Store becomes trade-policy and regionId in VTEX's realm
9
11
  * */
10
12
  export const transformSelectedFacet = ({ key, value }: SelectedFacet) => {
11
13
  switch (key) {
12
- case 'channel':
13
- return { key: 'trade-policy', value }
14
+ case 'channel': {
15
+ const channel = ChannelMarshal.parse(value)
16
+
17
+ // This array should have all values from channel string
18
+ return [{ key: 'trade-policy', value: channel.salesChannel }]
19
+ }
14
20
 
15
21
  default:
16
22
  return { key, value }