@faststore/api 1.12.50 → 1.12.51

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,5 @@
1
- export declare const fetchAPI: (info: RequestInfo, init?: RequestInit | undefined) => Promise<any>;
1
+ interface FetchAPIOptions {
2
+ storeCookies?: (headers: Headers) => void;
3
+ }
4
+ export declare const fetchAPI: (info: RequestInfo, init?: RequestInit | undefined, options?: FetchAPIOptions | undefined) => Promise<any>;
5
+ export {};
@@ -28,6 +28,11 @@ export interface Context {
28
28
  locale: string;
29
29
  flags: FeatureFlags;
30
30
  searchArgs?: Omit<SearchArgs, 'type'>;
31
+ /**
32
+ * Checkout cookies received while resolving this request, keyed by cookie
33
+ * name. The GraphQL handler forwards `setCookie` values to the browser.
34
+ */
35
+ cookies: Map<string, Record<string, string>>;
31
36
  };
32
37
  headers: Record<string, string>;
33
38
  }
@@ -0,0 +1,52 @@
1
+ import type { Context } from '../index';
2
+ export interface ContextForCookies {
3
+ headers: Context['headers'];
4
+ storage: Pick<Context['storage'], 'cookies'>;
5
+ }
6
+ /**
7
+ * Returns every `set-cookie` value of a fetch response.
8
+ *
9
+ * `Headers.getSetCookie()` only exists on Node >= 18.14 (undici). FastStore v1
10
+ * stores may still run on Node 16, where isomorphic-unfetch falls back to
11
+ * node-fetch v2, which exposes repeated headers through `raw()` instead.
12
+ */
13
+ export declare const getSetCookies: (headers: Headers) => string[];
14
+ /**
15
+ * This function updates the ctx.storage.cookies, that is used in each request.
16
+ *
17
+ * ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
18
+ * [1] cookie key
19
+ * [2] {
20
+ * value: cookie value,
21
+ * setCookie: setCookie used in browser response
22
+ * }
23
+ */
24
+ export declare const updatesContextStorageCookies: (ctx: Pick<Context, 'storage'>, setCookieValue: string) => void;
25
+ export declare const setCookie: (ctx: Pick<Context, 'storage'>, headers: Headers) => void;
26
+ export declare const getStoreCookie: (ctx: Pick<Context, 'storage'>) => (headers: Headers) => void;
27
+ /**
28
+ * This function returns a modified copy of the original cookie header (ctx.headers.cookie from the first request)
29
+ * with the cookie values that comes in each request (ctx.storage.cookies).
30
+ * If there is no cookies in storage, the ctx.headers?.cookie is used
31
+ *
32
+ * ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
33
+ * [1] cookie key
34
+ * [2] {
35
+ * value: cookie value,
36
+ * setCookie: setCookie used in browser response
37
+ * }
38
+ */
39
+ export declare const getUpdatedCookie: (ctx: ContextForCookies) => string | null;
40
+ export declare const getWithCookie: (ctx: ContextForCookies) => <T extends Record<string, any>>(headers: T) => T | (T & {
41
+ cookie: string;
42
+ });
43
+ /**
44
+ * This function updates the cookie value based on its key
45
+ *
46
+ * const existingCookies = 'key=value1; key2=; key3=value3';
47
+ * const storageCookieKey = 'key2';
48
+ * const storageCookieValue = 'value2'
49
+ *
50
+ * updatesCookieValueByKey(existingCookies, storageCookieKey, storageCookieValue) returns 'key=value1; key2=value2; key3=value3';
51
+ */
52
+ export declare const updatesCookieValueByKey: (existingCookies: string, storageCookieKey: string, storageCookieValue: string) => string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faststore/api",
3
- "version": "1.12.50",
3
+ "version": "1.12.51",
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": "6a034d875598025c79f2c6ff6b7801bf0c955aef"
56
+ "gitHead": "6f9ba2786d82b074125318339deac2ea456fb3d9"
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 { getStoreCookie, getWithCookie } from '../../utils/cookies'
22
23
 
23
24
  type ValueOf<T> = T extends Record<string, infer K> ? K : never
24
25
 
@@ -29,11 +30,43 @@ const BASE_INIT = {
29
30
  },
30
31
  }
31
32
 
33
+ const getHeader = (headers: Context['headers'] | undefined, name: string) => {
34
+ const value: string | string[] | undefined = (headers as any)?.[name]
35
+
36
+ return Array.isArray(value) ? value[0] : value
37
+ }
38
+
39
+ /**
40
+ * Host the Checkout API uses as the domain of the cookies it sets. Checkout
41
+ * only honors it for hosts registered in the account, so a forged value is
42
+ * ignored there.
43
+ */
44
+ const getForwardedHost = (headers: Context['headers'] | undefined) => {
45
+ // Proxies may append values: "client, proxy". Only the first one is the store host.
46
+ const host =
47
+ getHeader(headers, 'x-forwarded-host')?.split(',')[0].trim() ||
48
+ getHeader(headers, 'host') ||
49
+ ''
50
+
51
+ return host.trim().replace(/^www\./, '')
52
+ }
53
+
32
54
  export const VtexCommerce = (
33
55
  { account, environment }: Options,
34
56
  ctx: Context
35
57
  ) => {
36
58
  const base = `https://${account}.${environment}.com.br`
59
+ const storeCookies = getStoreCookie(ctx)
60
+ const withCookie = getWithCookie(ctx)
61
+ const forwardedHost = getForwardedHost(ctx.headers)
62
+
63
+ // Checkout calls forward the shopper cookies and keep the cookies Checkout
64
+ // sets (e.g. CheckoutOrderFormOwnership) so they can reach the browser.
65
+ const checkoutHeaders = () =>
66
+ withCookie({
67
+ 'content-type': 'application/json',
68
+ 'X-FORWARDED-HOST': forwardedHost,
69
+ })
37
70
 
38
71
  return {
39
72
  catalog: {
@@ -107,8 +140,10 @@ export const VtexCommerce = (
107
140
  `${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`,
108
141
  {
109
142
  ...BASE_INIT,
143
+ headers: checkoutHeaders(),
110
144
  body: JSON.stringify(body),
111
- }
145
+ },
146
+ { storeCookies }
112
147
  )
113
148
  },
114
149
  orderForm: ({
@@ -128,7 +163,11 @@ export const VtexCommerce = (
128
163
 
129
164
  return fetchAPI(
130
165
  `${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`,
131
- BASE_INIT
166
+ {
167
+ ...BASE_INIT,
168
+ headers: checkoutHeaders(),
169
+ },
170
+ { storeCookies }
132
171
  )
133
172
  },
134
173
  updateOrderFormItems: ({
@@ -153,12 +192,14 @@ export const VtexCommerce = (
153
192
  `${base}/api/checkout/pub/orderForm/${id}/items?${params}`,
154
193
  {
155
194
  ...BASE_INIT,
195
+ headers: checkoutHeaders(),
156
196
  body: JSON.stringify({
157
197
  orderItems,
158
198
  noSplitItem: !shouldSplitItem,
159
199
  }),
160
200
  method: 'PATCH',
161
- }
201
+ },
202
+ { storeCookies }
162
203
  )
163
204
  },
164
205
  setCustomData: ({
@@ -176,9 +217,11 @@ export const VtexCommerce = (
176
217
  `${base}/api/checkout/pub/orderForm/${id}/customData/${appId}/${key}`,
177
218
  {
178
219
  ...BASE_INIT,
220
+ headers: checkoutHeaders(),
179
221
  body: JSON.stringify({ value }),
180
222
  method: 'PUT',
181
- }
223
+ },
224
+ { storeCookies }
182
225
  )
183
226
  },
184
227
  region: async ({
@@ -3,7 +3,15 @@ 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
+ interface FetchAPIOptions {
7
+ storeCookies?: (headers: Headers) => void
8
+ }
9
+
10
+ export const fetchAPI = async (
11
+ info: RequestInfo,
12
+ init?: RequestInit,
13
+ options?: FetchAPIOptions
14
+ ) => {
7
15
  const response = await fetch(info, {
8
16
  ...init,
9
17
  headers: {
@@ -13,6 +21,10 @@ export const fetchAPI = async (info: RequestInfo, init?: RequestInit) => {
13
21
  })
14
22
 
15
23
  if (response.ok) {
24
+ if (options?.storeCookies) {
25
+ options.storeCookies(response.headers)
26
+ }
27
+
16
28
  return response.status !== 204 ? response.json() : undefined
17
29
  }
18
30
 
@@ -56,6 +56,11 @@ export interface Context {
56
56
  locale: string
57
57
  flags: FeatureFlags
58
58
  searchArgs?: Omit<SearchArgs, 'type'>
59
+ /**
60
+ * Checkout cookies received while resolving this request, keyed by cookie
61
+ * name. The GraphQL handler forwards `setCookie` values to the browser.
62
+ */
63
+ cookies: Map<string, Record<string, string>>
59
64
  }
60
65
  headers: Record<string, string>
61
66
  }
@@ -96,6 +101,7 @@ export const getContextFactory =
96
101
  channel: ChannelMarshal.parse(options.channel),
97
102
  flags: options.flags ?? {},
98
103
  locale: options.locale,
104
+ cookies: new Map<string, Record<string, string>>(),
99
105
  }
100
106
  ctx.clients = getClients(options, ctx)
101
107
  ctx.loaders = getLoaders(options, ctx)
@@ -0,0 +1,157 @@
1
+ import type { Context } from '../index'
2
+
3
+ export interface ContextForCookies {
4
+ headers: Context['headers']
5
+ storage: Pick<Context['storage'], 'cookies'>
6
+ }
7
+
8
+ const MATCH_FIRST_SET_COOKIE_KEY_VALUE = /^([^=]+)=([^;]*)/
9
+
10
+ /**
11
+ * Splits a combined `set-cookie` header value (cookies joined by `, `) into
12
+ * individual cookies. Commas inside `expires=...` dates are kept.
13
+ */
14
+ const splitSetCookieHeader = (value: string) =>
15
+ value.split(/,(?=\s*[^;,=\s]+=)/).map((setCookie) => setCookie.trim())
16
+
17
+ /**
18
+ * Returns every `set-cookie` value of a fetch response.
19
+ *
20
+ * `Headers.getSetCookie()` only exists on Node >= 18.14 (undici). FastStore v1
21
+ * stores may still run on Node 16, where isomorphic-unfetch falls back to
22
+ * node-fetch v2, which exposes repeated headers through `raw()` instead.
23
+ */
24
+ export const getSetCookies = (headers: Headers): string[] => {
25
+ const maybeHeaders = headers as Headers & {
26
+ getSetCookie?: () => string[]
27
+ raw?: () => Record<string, string[]>
28
+ }
29
+
30
+ if (typeof maybeHeaders.getSetCookie === 'function') {
31
+ return maybeHeaders.getSetCookie()
32
+ }
33
+
34
+ if (typeof maybeHeaders.raw === 'function') {
35
+ return maybeHeaders.raw()['set-cookie'] ?? []
36
+ }
37
+
38
+ const setCookie = headers.get('set-cookie')
39
+
40
+ return setCookie ? splitSetCookieHeader(setCookie) : []
41
+ }
42
+
43
+ /**
44
+ * This function updates the ctx.storage.cookies, that is used in each request.
45
+ *
46
+ * ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
47
+ * [1] cookie key
48
+ * [2] {
49
+ * value: cookie value,
50
+ * setCookie: setCookie used in browser response
51
+ * }
52
+ */
53
+ export const updatesContextStorageCookies = (
54
+ ctx: Pick<Context, 'storage'>,
55
+ setCookieValue: string
56
+ ) => {
57
+ const matchCookie = setCookieValue.match(MATCH_FIRST_SET_COOKIE_KEY_VALUE)
58
+
59
+ if (matchCookie) {
60
+ const cookieKey = matchCookie[1]
61
+ const cookieValue = matchCookie[2]
62
+
63
+ ctx.storage.cookies.set(cookieKey, {
64
+ value: cookieValue,
65
+ setCookie: setCookieValue,
66
+ })
67
+ }
68
+ }
69
+
70
+ export const setCookie = (ctx: Pick<Context, 'storage'>, headers: Headers) => {
71
+ getSetCookies(headers).forEach((setCookieValue) =>
72
+ updatesContextStorageCookies(ctx, setCookieValue)
73
+ )
74
+ }
75
+
76
+ export const getStoreCookie =
77
+ (ctx: Pick<Context, 'storage'>) => (headers: Headers) =>
78
+ setCookie(ctx, headers)
79
+
80
+ /**
81
+ * This function returns a modified copy of the original cookie header (ctx.headers.cookie from the first request)
82
+ * with the cookie values that comes in each request (ctx.storage.cookies).
83
+ * If there is no cookies in storage, the ctx.headers?.cookie is used
84
+ *
85
+ * ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
86
+ * [1] cookie key
87
+ * [2] {
88
+ * value: cookie value,
89
+ * setCookie: setCookie used in browser response
90
+ * }
91
+ */
92
+ export const getUpdatedCookie = (ctx: ContextForCookies) => {
93
+ if (!ctx.headers?.cookie) {
94
+ return null
95
+ }
96
+
97
+ const contextStorageCookies = Array.from(ctx.storage.cookies.entries())
98
+
99
+ if (contextStorageCookies.length === 0) {
100
+ return ctx.headers.cookie
101
+ }
102
+
103
+ return contextStorageCookies.reduce(
104
+ (existingCookies, [storageCookieKey, { value: storageCookieValue }]) =>
105
+ updatesCookieValueByKey(
106
+ existingCookies,
107
+ storageCookieKey,
108
+ storageCookieValue
109
+ ),
110
+ ctx.headers.cookie
111
+ )
112
+ }
113
+
114
+ export const getWithCookie = (ctx: ContextForCookies) =>
115
+ function withCookie<T extends Record<string, any>>(
116
+ headers: T
117
+ ): T | (T & { cookie: string }) {
118
+ const updatedCookie = getUpdatedCookie(ctx)
119
+
120
+ if (!updatedCookie) {
121
+ return headers
122
+ }
123
+
124
+ return {
125
+ ...headers,
126
+ cookie: updatedCookie,
127
+ }
128
+ }
129
+
130
+ /**
131
+ * This function updates the cookie value based on its key
132
+ *
133
+ * const existingCookies = 'key=value1; key2=; key3=value3';
134
+ * const storageCookieKey = 'key2';
135
+ * const storageCookieValue = 'value2'
136
+ *
137
+ * updatesCookieValueByKey(existingCookies, storageCookieKey, storageCookieValue) returns 'key=value1; key2=value2; key3=value3';
138
+ */
139
+ export const updatesCookieValueByKey = (
140
+ existingCookies: string,
141
+ storageCookieKey: string,
142
+ storageCookieValue: string
143
+ ) => {
144
+ const MATCH_COOKIE_KEY_VALUE = new RegExp(`(${storageCookieKey})=([^;]*)`)
145
+ const cookieParts = existingCookies.match(MATCH_COOKIE_KEY_VALUE)
146
+
147
+ // replaces original cookie with the one coming from storage
148
+ if (cookieParts) {
149
+ return existingCookies.replace(
150
+ cookieParts[0],
151
+ `${cookieParts[1]}=${storageCookieValue}`
152
+ )
153
+ }
154
+
155
+ // add new storage cookie to the original list of cookies
156
+ return `${existingCookies};${storageCookieKey}=${storageCookieValue}`
157
+ }