@faststore/api 2.1.90 → 2.1.98

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.
@@ -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>;
@@ -29,6 +29,7 @@ export interface Context {
29
29
  locale: string;
30
30
  flags: FeatureFlags;
31
31
  searchArgs?: Omit<SearchArgs, 'type'>;
32
+ cookies?: string | null;
32
33
  };
33
34
  headers: Record<string, string>;
34
35
  }
@@ -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": "2.1.90",
3
+ "version": "2.1.98",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -56,5 +56,5 @@
56
56
  "@envelop/core": "^1 || ^2",
57
57
  "graphql": "^15.6.0"
58
58
  },
59
- "gitHead": "be6117a519217458b14ae3e2c5151a329e47e759"
59
+ "gitHead": "5b295774a676946af022c70fb0d04914d3bd3a44"
60
60
  }
@@ -19,6 +19,7 @@ import type { SalesChannel } from './types/SalesChannel'
19
19
  import { MasterDataResponse } from './types/Newsletter'
20
20
  import type { Address, AddressInput } from './types/Address'
21
21
  import { DeliveryMode, SelectedAddress } from './types/ShippingData'
22
+ import getCookieByName from '../../../../utils/get-cookie-by-name'
22
23
 
23
24
  type ValueOf<T> = T extends Record<string, infer K> ? K : never
24
25
 
@@ -29,6 +30,18 @@ const BASE_INIT = {
29
30
  },
30
31
  }
31
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
+
32
45
  export const VtexCommerce = (
33
46
  { account, environment, incrementAddress }: Options,
34
47
  ctx: Context
@@ -131,7 +144,8 @@ export const VtexCommerce = (
131
144
  'content-type': 'application/json',
132
145
  cookie: ctx.headers.cookie,
133
146
  },
134
- }
147
+ },
148
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
135
149
  )
136
150
  },
137
151
 
@@ -152,7 +166,8 @@ export const VtexCommerce = (
152
166
 
153
167
  return fetchAPI(
154
168
  `${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`,
155
- BASE_INIT
169
+ BASE_INIT,
170
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
156
171
  )
157
172
  },
158
173
  updateOrderFormItems: ({
@@ -182,7 +197,8 @@ export const VtexCommerce = (
182
197
  noSplitItem: !shouldSplitItem,
183
198
  }),
184
199
  method: 'PATCH',
185
- }
200
+ },
201
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
186
202
  )
187
203
  },
188
204
  setCustomData: ({
@@ -202,7 +218,8 @@ export const VtexCommerce = (
202
218
  ...BASE_INIT,
203
219
  body: JSON.stringify({ value }),
204
220
  method: 'PUT',
205
- }
221
+ },
222
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
206
223
  )
207
224
  },
208
225
  region: async ({
@@ -242,14 +259,18 @@ export const VtexCommerce = (
242
259
  'items',
243
260
  'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol'
244
261
  )
245
- return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
246
- method: 'POST',
247
- headers: {
248
- 'content-type': 'application/json',
249
- cookie: ctx.headers.cookie,
262
+ return fetchAPI(
263
+ `${base}/api/sessions?${params.toString()}`,
264
+ {
265
+ method: 'POST',
266
+ headers: {
267
+ 'content-type': 'application/json',
268
+ cookie: ctx.headers.cookie,
269
+ },
270
+ body: '{}',
250
271
  },
251
- body: '{}',
252
- })
272
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
273
+ )
253
274
  },
254
275
  subscribeToNewsletter: (data: {
255
276
  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
 
@@ -35,7 +35,7 @@ export interface Options {
35
35
  channel: string
36
36
  locale: string
37
37
  hideUnavailableItems: boolean
38
- incrementAddress: boolean,
38
+ incrementAddress: boolean
39
39
  flags?: FeatureFlags
40
40
  }
41
41
 
@@ -57,6 +57,7 @@ export interface Context {
57
57
  locale: string
58
58
  flags: FeatureFlags
59
59
  searchArgs?: Omit<SearchArgs, 'type'>
60
+ cookies?: string | null
60
61
  }
61
62
  headers: Record<string, string>
62
63
  }
@@ -92,16 +93,16 @@ const Resolvers = {
92
93
 
93
94
  export const getContextFactory =
94
95
  (options: Options) =>
95
- (ctx: any): Context => {
96
- ctx.storage = {
97
- channel: ChannelMarshal.parse(options.channel),
98
- flags: options.flags ?? {},
99
- locale: options.locale,
100
- }
101
- ctx.clients = getClients(options, ctx)
102
- ctx.loaders = getLoaders(options, ctx)
103
-
104
- return ctx
96
+ (ctx: any): Context => {
97
+ ctx.storage = {
98
+ channel: ChannelMarshal.parse(options.channel),
99
+ flags: options.flags ?? {},
100
+ locale: options.locale,
105
101
  }
102
+ ctx.clients = getClients(options, ctx)
103
+ ctx.loaders = getLoaders(options, ctx)
104
+
105
+ return ctx
106
+ }
106
107
 
107
108
  export const getResolvers = (_: Options) => Resolvers
@@ -391,6 +391,7 @@ export const validateCart = async (
391
391
  if (equals(order, updatedOrderForm)) {
392
392
  return null
393
393
  }
394
+
394
395
  // Step6: There were changes, convert orderForm to StoreCart
395
396
  return orderFormToCart(updatedOrderForm, skuLoader)
396
397
  }
@@ -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
+ }