@faststore/api 3.41.8 → 3.44.0

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.
Files changed (57) hide show
  1. package/dist/cjs/package.json +1 -1
  2. package/dist/cjs/src/__generated__/schema.d.ts +14 -0
  3. package/dist/cjs/src/__generated__/schema.js.map +1 -1
  4. package/dist/cjs/src/index.d.ts +1 -0
  5. package/dist/cjs/src/platforms/vtex/clients/commerce/index.js +3 -2
  6. package/dist/cjs/src/platforms/vtex/clients/commerce/index.js.map +1 -1
  7. package/dist/cjs/src/platforms/vtex/clients/commerce/types/Address.d.ts +1 -1
  8. package/dist/cjs/src/platforms/vtex/clients/commerce/types/Session.d.ts +1 -0
  9. package/dist/cjs/src/platforms/vtex/clients/index.d.ts +1 -0
  10. package/dist/cjs/src/platforms/vtex/clients/search/index.d.ts +2 -0
  11. package/dist/cjs/src/platforms/vtex/clients/search/index.js +8 -0
  12. package/dist/cjs/src/platforms/vtex/clients/search/index.js.map +1 -1
  13. package/dist/cjs/src/platforms/vtex/clients/search/types/ProductCountResult.d.ts +6 -0
  14. package/dist/cjs/src/platforms/vtex/clients/search/types/ProductCountResult.js +3 -0
  15. package/dist/cjs/src/platforms/vtex/clients/search/types/ProductCountResult.js.map +1 -0
  16. package/dist/cjs/src/platforms/vtex/index.d.ts +1 -0
  17. package/dist/cjs/src/platforms/vtex/resolvers/query.d.ts +2 -1
  18. package/dist/cjs/src/platforms/vtex/resolvers/query.js +7 -0
  19. package/dist/cjs/src/platforms/vtex/resolvers/query.js.map +1 -1
  20. package/dist/cjs/src/platforms/vtex/resolvers/validateSession.js +54 -15
  21. package/dist/cjs/src/platforms/vtex/resolvers/validateSession.js.map +1 -1
  22. package/dist/cjs/src/typeDefs/query.graphql +18 -0
  23. package/dist/cjs/src/typeDefs/session.graphql +8 -0
  24. package/dist/esm/package.json +1 -1
  25. package/dist/esm/src/__generated__/schema.d.ts +14 -0
  26. package/dist/esm/src/__generated__/schema.js.map +1 -1
  27. package/dist/esm/src/index.d.ts +1 -0
  28. package/dist/esm/src/platforms/vtex/clients/commerce/index.js +3 -2
  29. package/dist/esm/src/platforms/vtex/clients/commerce/index.js.map +1 -1
  30. package/dist/esm/src/platforms/vtex/clients/commerce/types/Address.d.ts +1 -1
  31. package/dist/esm/src/platforms/vtex/clients/commerce/types/Session.d.ts +1 -0
  32. package/dist/esm/src/platforms/vtex/clients/index.d.ts +1 -0
  33. package/dist/esm/src/platforms/vtex/clients/search/index.d.ts +2 -0
  34. package/dist/esm/src/platforms/vtex/clients/search/index.js +8 -0
  35. package/dist/esm/src/platforms/vtex/clients/search/index.js.map +1 -1
  36. package/dist/esm/src/platforms/vtex/clients/search/types/ProductCountResult.d.ts +6 -0
  37. package/dist/esm/src/platforms/vtex/clients/search/types/ProductCountResult.js +2 -0
  38. package/dist/esm/src/platforms/vtex/clients/search/types/ProductCountResult.js.map +1 -0
  39. package/dist/esm/src/platforms/vtex/index.d.ts +1 -0
  40. package/dist/esm/src/platforms/vtex/resolvers/query.d.ts +2 -1
  41. package/dist/esm/src/platforms/vtex/resolvers/query.js +7 -0
  42. package/dist/esm/src/platforms/vtex/resolvers/query.js.map +1 -1
  43. package/dist/esm/src/platforms/vtex/resolvers/validateSession.js +54 -15
  44. package/dist/esm/src/platforms/vtex/resolvers/validateSession.js.map +1 -1
  45. package/dist/esm/src/typeDefs/query.graphql +18 -0
  46. package/dist/esm/src/typeDefs/session.graphql +8 -0
  47. package/package.json +2 -2
  48. package/src/__generated__/schema.ts +17 -0
  49. package/src/platforms/vtex/clients/commerce/index.ts +4 -2
  50. package/src/platforms/vtex/clients/commerce/types/Address.ts +1 -1
  51. package/src/platforms/vtex/clients/commerce/types/Session.ts +1 -0
  52. package/src/platforms/vtex/clients/search/index.ts +17 -0
  53. package/src/platforms/vtex/clients/search/types/ProductCountResult.ts +6 -0
  54. package/src/platforms/vtex/resolvers/query.ts +16 -0
  55. package/src/platforms/vtex/resolvers/validateSession.ts +75 -18
  56. package/src/typeDefs/query.graphql +18 -0
  57. package/src/typeDefs/session.graphql +8 -0
@@ -1,12 +1,34 @@
1
1
  import deepEquals from 'fast-deep-equal'
2
2
 
3
- import ChannelMarshal from '../utils/channel'
4
3
  import type { Context } from '..'
5
4
  import type {
6
5
  MutationValidateSessionArgs,
7
6
  StoreMarketingData,
8
7
  StoreSession,
9
8
  } from '../../../__generated__/schema'
9
+ import ChannelMarshal from '../utils/channel'
10
+
11
+ async function getPreciseLocationData(
12
+ clients: Context['clients'],
13
+ country: string,
14
+ postalCode: string
15
+ ) {
16
+ try {
17
+ const address = await clients.commerce.checkout.address({
18
+ postalCode,
19
+ country,
20
+ })
21
+
22
+ const [longitude, latitude] = address.geoCoordinates
23
+ return { city: address.city, geoCoordinates: { latitude, longitude } }
24
+ } catch (err) {
25
+ console.error(
26
+ `Error while getting geo coordinates for the current postal code (${postalCode}) and country (${country}).\n`
27
+ )
28
+
29
+ throw err
30
+ }
31
+ }
10
32
 
11
33
  export const validateSession = async (
12
34
  _: any,
@@ -15,15 +37,45 @@ export const validateSession = async (
15
37
  ): Promise<StoreSession | null> => {
16
38
  const channel = ChannelMarshal.parse(oldSession.channel ?? '')
17
39
  const postalCode = String(oldSession.postalCode ?? '')
18
- const geoCoordinates = oldSession.geoCoordinates ?? null
19
-
20
40
  const country = oldSession.country ?? ''
41
+ let city = oldSession.city ?? null
42
+ let geoCoordinates = oldSession.geoCoordinates ?? null
43
+
44
+ // Update location data if postal code and country are provided
45
+ const shouldGetPreciseLocation = !city || !geoCoordinates
46
+ if (shouldGetPreciseLocation && postalCode !== '' && country !== '') {
47
+ const preciseLocation = await getPreciseLocationData(
48
+ clients,
49
+ country,
50
+ postalCode
51
+ )
52
+ city = preciseLocation.city
53
+ geoCoordinates = preciseLocation.geoCoordinates
54
+ }
21
55
 
56
+ /**
57
+ * The Session Manager API (https://developers.vtex.com/docs/api-reference/session-manager-api#patch-/api/sessions) adds the query params to the session public namespace.
58
+ * This is used by Checkout (checkout-session) and Intelligent Search (search-session)
59
+ */
22
60
  const params = new URLSearchParams(search)
23
61
  const salesChannel = params.get('sc') ?? channel.salesChannel
24
-
25
62
  params.set('sc', salesChannel)
26
63
 
64
+ if (!!postalCode) {
65
+ params.set('postalCode', postalCode)
66
+ }
67
+
68
+ if (!!country) {
69
+ params.set('country', country)
70
+ }
71
+
72
+ if (!!geoCoordinates) {
73
+ params.set(
74
+ 'geoCoordinates',
75
+ `${geoCoordinates.latitude},${geoCoordinates.longitude}`
76
+ )
77
+ }
78
+
27
79
  const { marketingData: oldMarketingData } = oldSession
28
80
 
29
81
  const marketingData: StoreMarketingData = {
@@ -36,24 +88,27 @@ export const validateSession = async (
36
88
  utmiPart: params.get('utmi_pc') ?? oldMarketingData?.utmiPart ?? '',
37
89
  }
38
90
 
39
- const [regionData, sessionData] = await Promise.all([
40
- postalCode || geoCoordinates
41
- ? clients.commerce.checkout.region({
42
- postalCode,
43
- geoCoordinates,
44
- country,
45
- salesChannel,
46
- })
47
- : Promise.resolve(null),
48
- clients.commerce.session(params.toString()).catch(() => null),
49
- ])
91
+ const sessionData = await clients.commerce
92
+ .session(params.toString())
93
+ .catch(() => null)
50
94
 
51
95
  const profile = sessionData?.namespaces.profile ?? null
52
96
  const store = sessionData?.namespaces.store ?? null
53
97
  const authentication = sessionData?.namespaces.authentication ?? null
54
- const region = regionData?.[0]
98
+ const checkout = sessionData?.namespaces.checkout ?? null
99
+
55
100
  // Set seller only if it's inside a region
56
- const seller = region?.sellers.find((seller) => channel.seller === seller.id)
101
+ let seller
102
+ if (!!channel.seller && (postalCode || geoCoordinates)) {
103
+ const regionData = await clients.commerce.checkout.region({
104
+ postalCode,
105
+ geoCoordinates,
106
+ country,
107
+ salesChannel,
108
+ })
109
+ const region = regionData?.[0]
110
+ seller = region?.sellers.find((seller) => channel.seller === seller.id)
111
+ }
57
112
 
58
113
  const newSession = {
59
114
  ...oldSession,
@@ -64,7 +119,7 @@ export const validateSession = async (
64
119
  country: store?.countryCode?.value ?? oldSession.country,
65
120
  channel: ChannelMarshal.stringify({
66
121
  salesChannel: store?.channel?.value ?? channel.salesChannel,
67
- regionId: region?.id ?? channel.regionId,
122
+ regionId: checkout?.regionId?.value ?? channel.regionId,
68
123
  seller: seller?.id,
69
124
  hasOnlyDefaultSalesChannel: !store?.channel?.value,
70
125
  }),
@@ -80,6 +135,8 @@ export const validateSession = async (
80
135
  familyName: profile.lastName?.value ?? '',
81
136
  }
82
137
  : null,
138
+ geoCoordinates,
139
+ city,
83
140
  }
84
141
 
85
142
  if (deepEquals(oldSession, newSession)) {
@@ -199,6 +199,13 @@ input IGeoCoordinates {
199
199
  longitude: Float!
200
200
  }
201
201
 
202
+ type ProductCountResult {
203
+ """
204
+ Total product count.
205
+ """
206
+ total: Int!
207
+ }
208
+
202
209
  type Query {
203
210
  """
204
211
  Returns the details of a product based on the specified locator.
@@ -349,6 +356,17 @@ type Query {
349
356
  id: String!
350
357
  ): Profile
351
358
  @cacheControl(scope: "public", sMaxAge: 120, staleWhileRevalidate: 3600)
359
+
360
+ """
361
+ Returns the total product count information based on a specific location accessible through the VTEX segment cookie.
362
+ """
363
+ productCount(
364
+ """
365
+ Search term.
366
+ """
367
+ term: String
368
+ ): ProductCountResult
369
+ @cacheControl(scope: "public", sMaxAge: 120, staleWhileRevalidate: 3600)
352
370
  }
353
371
 
354
372
  """
@@ -159,6 +159,10 @@ type StoreSession {
159
159
  """
160
160
  addressType: String
161
161
  """
162
+ Session city.
163
+ """
164
+ city: String
165
+ """
162
166
  Session postal code.
163
167
  """
164
168
  postalCode: String
@@ -217,6 +221,10 @@ input IStoreSession {
217
221
  """
218
222
  addressType: String
219
223
  """
224
+ Session input city.
225
+ """
226
+ city: String
227
+ """
220
228
  Session input postal code.
221
229
  """
222
230
  postalCode: String