@faststore/api 1.12.46 → 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.
@@ -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>;
@@ -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.46",
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": "0a4c5ea1e23c7d867cdc3212369b95f5406f0425"
56
+ "gitHead": "fb17d8b4870de83e2d738c99e2f46ff51209407d"
57
57
  }
@@ -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 { ShippingDataBody } 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 }: Options,
34
47
  ctx: Context
@@ -108,7 +121,8 @@ export const VtexCommerce = (
108
121
  {
109
122
  ...BASE_INIT,
110
123
  body: JSON.stringify(body),
111
- }
124
+ },
125
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
112
126
  )
113
127
  },
114
128
  orderForm: ({
@@ -128,7 +142,8 @@ export const VtexCommerce = (
128
142
 
129
143
  return fetchAPI(
130
144
  `${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`,
131
- BASE_INIT
145
+ BASE_INIT,
146
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
132
147
  )
133
148
  },
134
149
  updateOrderFormItems: ({
@@ -158,7 +173,8 @@ export const VtexCommerce = (
158
173
  noSplitItem: !shouldSplitItem,
159
174
  }),
160
175
  method: 'PATCH',
161
- }
176
+ },
177
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
162
178
  )
163
179
  },
164
180
  setCustomData: ({
@@ -178,7 +194,8 @@ export const VtexCommerce = (
178
194
  ...BASE_INIT,
179
195
  body: JSON.stringify({ value }),
180
196
  method: 'PUT',
181
- }
197
+ },
198
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
182
199
  )
183
200
  },
184
201
  region: async ({
@@ -208,14 +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
- return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
212
- method: 'POST',
213
- headers: {
214
- 'content-type': 'application/json',
215
- cookie: ctx.headers.cookie,
228
+ return fetchAPI(
229
+ `${base}/api/sessions?${params.toString()}`,
230
+ {
231
+ method: 'POST',
232
+ headers: {
233
+ 'content-type': 'application/json',
234
+ cookie: ctx.headers.cookie,
235
+ },
236
+ body: '{}',
216
237
  },
217
- body: '{}',
218
- })
238
+ (headers) => setCheckoutOrderFormOwnershipCookie(headers, ctx)
239
+ )
219
240
  },
220
241
  subscribeToNewsletter: (data: {
221
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
  }
@@ -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
+ }