@faststore/api 1.12.45 → 1.12.47

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.
@@ -62,7 +62,6 @@ export declare const VtexCommerce: ({ account, environment }: Options, ctx: Cont
62
62
  address: ({ postalCode, country, }: AddressInput) => Promise<Address>;
63
63
  };
64
64
  session: (search: string) => Promise<Session>;
65
- getSessionOrder: () => Promise<Session>;
66
65
  subscribeToNewsletter: (data: {
67
66
  name: string;
68
67
  email: string;
@@ -1 +1 @@
1
- export declare const fetchAPI: (info: RequestInfo, init?: RequestInit | undefined) => Promise<any>;
1
+ export declare const fetchAPI: (info: RequestInfo, init?: RequestInit | undefined, getHeaders?: ((headers: Headers) => void) | undefined) => Promise<any>;
@@ -55,7 +55,6 @@ export declare const getClients: (options: Options, ctx: Context) => {
55
55
  address: ({ postalCode, country, }: import("./commerce/types/Address").AddressInput) => Promise<import("./commerce/types/Address").Address>;
56
56
  };
57
57
  session: (search: string) => Promise<import("./commerce/types/Session").Session>;
58
- getSessionOrder: () => Promise<import("./commerce/types/Session").Session>;
59
58
  subscribeToNewsletter: (data: {
60
59
  name: string;
61
60
  email: string;
@@ -28,6 +28,7 @@ export interface Context {
28
28
  locale: string;
29
29
  flags: FeatureFlags;
30
30
  searchArgs?: Omit<SearchArgs, 'type'>;
31
+ cookies?: string | null;
31
32
  };
32
33
  headers: Record<string, string>;
33
34
  }
@@ -0,0 +1 @@
1
+ export default function getCookieByName(cookiename: string, source: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faststore/api",
3
- "version": "1.12.45",
3
+ "version": "1.12.47",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -53,5 +53,5 @@
53
53
  "@envelop/core": "^1 || ^2",
54
54
  "graphql": "^15.6.0"
55
55
  },
56
- "gitHead": "121360b0bdbc8eac7c71d660e3e6b3604bda7703"
56
+ "gitHead": "fb17d8b4870de83e2d738c99e2f46ff51209407d"
57
57
  }
@@ -15,11 +15,11 @@ import type {
15
15
  } from './types/Simulation'
16
16
  import type { Session } from './types/Session'
17
17
  import type { Channel } from '../../utils/channel'
18
- import { getCookie } from '../../utils/getCookies'
19
18
  import type { SalesChannel } from './types/SalesChannel'
20
19
  import { MasterDataResponse } from './types/Newsletter'
21
20
  import type { Address, AddressInput } from './types/Address'
22
21
  import { ShippingDataBody } from './types/ShippingData'
22
+ import getCookieByName from '../../../../utils/get-cookie-by-name'
23
23
 
24
24
  type ValueOf<T> = T extends Record<string, infer K> ? K : never
25
25
 
@@ -30,6 +30,18 @@ const BASE_INIT = {
30
30
  },
31
31
  }
32
32
 
33
+ const setCheckoutOrderFormOwnershipCookie = (
34
+ headers: Headers,
35
+ ctx: Context
36
+ ) => {
37
+ if (headers) {
38
+ ctx.storage.cookies = `CheckoutOrderFormOwnership=${getCookieByName(
39
+ 'CheckoutOrderFormOwnership',
40
+ headers.get('set-cookie') ?? ''
41
+ )}`
42
+ }
43
+ }
44
+
33
45
  export const VtexCommerce = (
34
46
  { account, environment }: Options,
35
47
  ctx: Context
@@ -100,16 +112,17 @@ export const VtexCommerce = (
100
112
  if (body.selectedAddresses) {
101
113
  body.selectedAddresses.forEach((address) => {
102
114
  if (address.geoCoordinates === null) {
103
- address.geoCoordinates = [];
115
+ address.geoCoordinates = []
104
116
  }
105
- });
117
+ })
106
118
  }
107
119
  return fetchAPI(
108
120
  `${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`,
109
121
  {
110
122
  ...BASE_INIT,
111
123
  body: JSON.stringify(body),
112
- }
124
+ },
125
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
113
126
  )
114
127
  },
115
128
  orderForm: ({
@@ -129,7 +142,8 @@ export const VtexCommerce = (
129
142
 
130
143
  return fetchAPI(
131
144
  `${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`,
132
- BASE_INIT
145
+ BASE_INIT,
146
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
133
147
  )
134
148
  },
135
149
  updateOrderFormItems: ({
@@ -159,7 +173,8 @@ export const VtexCommerce = (
159
173
  noSplitItem: !shouldSplitItem,
160
174
  }),
161
175
  method: 'PATCH',
162
- }
176
+ },
177
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
163
178
  )
164
179
  },
165
180
  setCustomData: ({
@@ -179,7 +194,8 @@ export const VtexCommerce = (
179
194
  ...BASE_INIT,
180
195
  body: JSON.stringify({ value }),
181
196
  method: 'PUT',
182
- }
197
+ },
198
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
183
199
  )
184
200
  },
185
201
  region: async ({
@@ -188,7 +204,8 @@ export const VtexCommerce = (
188
204
  salesChannel,
189
205
  }: RegionInput): Promise<Region> => {
190
206
  return fetchAPI(
191
- `${base}/api/checkout/pub/regions/?postalCode=${postalCode}&country=${country}&sc=${salesChannel ?? ''
207
+ `${base}/api/checkout/pub/regions/?postalCode=${postalCode}&country=${country}&sc=${
208
+ salesChannel ?? ''
192
209
  }`
193
210
  )
194
211
  },
@@ -208,35 +225,18 @@ export const VtexCommerce = (
208
225
  'items',
209
226
  'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol'
210
227
  )
211
- if (getCookie('vtex_session', ctx.headers.cookie)) {
212
- // cookie set
213
- return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
214
- method: 'GET',
215
- headers: {
216
- 'content-type': 'application/json',
217
- cookie: ctx.headers.cookie,
218
- },
219
- })
220
- } else {
221
- // cookie unset -> create session
222
- return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
228
+ return fetchAPI(
229
+ `${base}/api/sessions?${params.toString()}`,
230
+ {
223
231
  method: 'POST',
224
232
  headers: {
225
233
  'content-type': 'application/json',
226
234
  cookie: ctx.headers.cookie,
227
235
  },
228
236
  body: '{}',
229
- })
230
- }
231
- },
232
- getSessionOrder: (): Promise<Session> => {
233
- return fetchAPI(`${base}/api/sessions?items=checkout.orderFormId`, {
234
- method: 'GET',
235
- headers: {
236
- 'content-type': 'application/json',
237
- cookie: ctx.headers.cookie,
238
237
  },
239
- })
238
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
239
+ )
240
240
  },
241
241
  subscribeToNewsletter: (data: {
242
242
  name: string
@@ -3,7 +3,11 @@ import packageJson from '../../../../package.json'
3
3
 
4
4
  const USER_AGENT = `${packageJson.name}@${packageJson.version}`
5
5
 
6
- export const fetchAPI = async (info: RequestInfo, init?: RequestInit) => {
6
+ export const fetchAPI = async (
7
+ info: RequestInfo,
8
+ init?: RequestInit,
9
+ getHeaders?: (headers: Headers) => void
10
+ ) => {
7
11
  const response = await fetch(info, {
8
12
  ...init,
9
13
  headers: {
@@ -13,6 +17,10 @@ export const fetchAPI = async (info: RequestInfo, init?: RequestInit) => {
13
17
  })
14
18
 
15
19
  if (response.ok) {
20
+ if (getHeaders) {
21
+ getHeaders(response.headers)
22
+ }
23
+
16
24
  return response.status !== 204 ? response.json() : undefined
17
25
  }
18
26
 
@@ -56,6 +56,7 @@ export interface Context {
56
56
  locale: string
57
57
  flags: FeatureFlags
58
58
  searchArgs?: Omit<SearchArgs, 'type'>
59
+ cookies?: string | null
59
60
  }
60
61
  headers: Record<string, string>
61
62
  }
@@ -1,25 +1,26 @@
1
1
  import deepEquals from 'fast-deep-equal'
2
2
 
3
3
  import { mutateChannelContext, mutateLocaleContext } from '../utils/contex'
4
- import { getCookie } from '../utils/getCookies'
5
4
  import { md5 } from '../utils/md5'
6
5
  import {
7
6
  attachmentToPropertyValue,
8
7
  getPropertyId,
9
- VALUE_REFERENCES
8
+ VALUE_REFERENCES,
10
9
  } from '../utils/propertyValue'
11
10
 
12
11
  import type { Context } from '..'
13
12
  import type {
14
13
  IStoreOffer,
15
14
  IStoreOrder,
16
- IStorePropertyValue, IStoreSession, Maybe,
17
- MutationValidateCartArgs
15
+ IStorePropertyValue,
16
+ IStoreSession,
17
+ Maybe,
18
+ MutationValidateCartArgs,
18
19
  } from '../../../__generated__/schema'
19
20
  import type {
20
21
  OrderForm,
21
22
  OrderFormInputItem,
22
- OrderFormItem
23
+ OrderFormItem,
23
24
  } from '../clients/commerce/types/OrderForm'
24
25
 
25
26
  type Indexed<T> = T & { index?: number }
@@ -199,20 +200,6 @@ const isOrderFormStale = (form: OrderForm) => {
199
200
  return newEtag !== oldEtag
200
201
  }
201
202
 
202
- async function getOrderNumberFromSession(
203
- headers: Record<string, string> = {},
204
- commerce: Context['clients']['commerce']
205
- ) {
206
-
207
- const cookieSession = getCookie('vtex_session', headers.cookie)
208
-
209
- if (cookieSession) {
210
- const { namespaces } = await commerce.getSessionOrder()
211
- return namespaces.checkout?.orderFormId?.value
212
- }
213
- return ;
214
- }
215
-
216
203
  // Returns the regionalized orderForm
217
204
  const getOrderForm = async (
218
205
  id: string,
@@ -266,11 +253,10 @@ export const validateCart = async (
266
253
  { cart: { order }, session }: MutationValidateCartArgs,
267
254
  ctx: Context
268
255
  ) => {
269
- const { orderNumber: orderNumberFromCart, acceptedOffer, shouldSplitItem } = order
256
+ const { orderNumber, acceptedOffer, shouldSplitItem } = order
270
257
  const {
271
258
  clients: { commerce },
272
259
  loaders: { skuLoader },
273
- headers,
274
260
  } = ctx
275
261
 
276
262
  const channel = session?.channel
@@ -284,13 +270,6 @@ export const validateCart = async (
284
270
  mutateLocaleContext(ctx, locale)
285
271
  }
286
272
 
287
- const orderNumberFromSession = await getOrderNumberFromSession(
288
- headers,
289
- commerce
290
- )
291
-
292
- const orderNumber = orderNumberFromSession ?? orderNumberFromCart ?? ''
293
-
294
273
  // Step1: Get OrderForm from VTEX Commerce
295
274
  const orderForm = await getOrderForm(orderNumber, session, ctx)
296
275
 
@@ -298,11 +277,11 @@ export const validateCart = async (
298
277
  // If so, this means the user interacted with this cart elsewhere and expects
299
278
  // to see this new cart state instead of what's stored on the user's browser.
300
279
  const isStale = isOrderFormStale(orderForm)
301
-
280
+
302
281
  if (isStale && orderNumber) {
303
282
  const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(
304
283
  joinItems
305
- )
284
+ )
306
285
  return orderFormToCart(newOrderForm, skuLoader)
307
286
  }
308
287
 
@@ -366,9 +345,7 @@ export const validateCart = async (
366
345
  shouldSplitItem,
367
346
  })
368
347
  // update orderForm etag so we know last time we touched this orderForm
369
- .then((form) =>
370
- setOrderFormEtag(form, commerce)
371
- )
348
+ .then((form) => setOrderFormEtag(form, commerce))
372
349
  .then(joinItems)
373
350
 
374
351
  // Step5: If no changes detected before/after updating orderForm, the order is validated
@@ -0,0 +1,6 @@
1
+ export default function getCookieByName(cookiename: string, source: string) {
2
+ var cookiestring = RegExp(cookiename + '=[^;]+').exec(source)
3
+ return decodeURIComponent(
4
+ !!cookiestring ? cookiestring.toString().replace(/^[^=]+./, '') : ''
5
+ )
6
+ }
@@ -1 +0,0 @@
1
- export declare const getCookie: (name: string, cookie: string) => string;
@@ -1,8 +0,0 @@
1
- export const getCookie = (name: string, cookie: string): string => {
2
- const value = `; ${cookie}`
3
- const parts = value.split(`; ${name}=`)
4
- if (parts.length === 2) {
5
- return parts?.pop()?.split(';').shift() ?? ''
6
- }
7
- return ''
8
- }