@faststore/api 2.2.46 → 2.2.49
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.
- package/dist/cjs/package.json +1 -1
- package/dist/cjs/src/platforms/vtex/clients/commerce/index.js +85 -27
- package/dist/cjs/src/platforms/vtex/clients/commerce/index.js.map +1 -1
- package/dist/cjs/src/platforms/vtex/index.d.ts +1 -1
- package/dist/cjs/src/platforms/vtex/index.js +1 -1
- package/dist/cjs/src/platforms/vtex/index.js.map +1 -1
- package/dist/cjs/src/platforms/vtex/utils/cookies.d.ts +23 -0
- package/dist/cjs/src/platforms/vtex/utils/cookies.js +67 -6
- package/dist/cjs/src/platforms/vtex/utils/cookies.js.map +1 -1
- package/dist/esm/package.json +1 -1
- package/dist/esm/src/platforms/vtex/clients/commerce/index.js +86 -28
- package/dist/esm/src/platforms/vtex/clients/commerce/index.js.map +1 -1
- package/dist/esm/src/platforms/vtex/index.d.ts +1 -1
- package/dist/esm/src/platforms/vtex/index.js +1 -1
- package/dist/esm/src/platforms/vtex/index.js.map +1 -1
- package/dist/esm/src/platforms/vtex/utils/cookies.d.ts +23 -0
- package/dist/esm/src/platforms/vtex/utils/cookies.js +64 -5
- package/dist/esm/src/platforms/vtex/utils/cookies.js.map +1 -1
- package/package.json +2 -2
- package/src/platforms/vtex/clients/commerce/index.ts +99 -34
- package/src/platforms/vtex/index.ts +2 -2
- package/src/platforms/vtex/utils/cookies.ts +89 -8
package/dist/cjs/package.json
CHANGED
|
@@ -12,6 +12,8 @@ const BASE_INIT = {
|
|
|
12
12
|
const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
13
13
|
const base = `https://${account}.${environment}.com.br`;
|
|
14
14
|
const storeCookies = (0, cookies_1.getStoreCookie)(ctx);
|
|
15
|
+
// replacing www. only for testing while www.vtexfaststore.com is configured with www
|
|
16
|
+
const forwardedHost = (new Headers(ctx.headers).get('x-forwarded-host') ?? ctx.headers?.host ?? '').replace('www.', '');
|
|
15
17
|
return {
|
|
16
18
|
catalog: {
|
|
17
19
|
salesChannel: (sc) => (0, fetch_1.fetchAPI)(`${base}/api/catalog_system/pub/saleschannel/${sc}`, undefined, { storeCookies }),
|
|
@@ -41,8 +43,16 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
41
43
|
const params = new URLSearchParams({
|
|
42
44
|
sc: salesChannel,
|
|
43
45
|
});
|
|
46
|
+
const headers = {
|
|
47
|
+
'content-type': 'application/json',
|
|
48
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
49
|
+
};
|
|
50
|
+
const cookie = (0, cookies_1.getUpdatedCookie)(ctx);
|
|
51
|
+
if (cookie)
|
|
52
|
+
headers.cookie = cookie;
|
|
44
53
|
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForms/simulation?${params.toString()}`, {
|
|
45
54
|
...BASE_INIT,
|
|
55
|
+
headers,
|
|
46
56
|
body: JSON.stringify(args),
|
|
47
57
|
}, { storeCookies });
|
|
48
58
|
},
|
|
@@ -63,13 +73,17 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
63
73
|
selectedAddresses: selectedAddresses,
|
|
64
74
|
clearAddressIfPostalCodeNotFound: incrementAddress,
|
|
65
75
|
};
|
|
76
|
+
const headers = {
|
|
77
|
+
'content-type': 'application/json',
|
|
78
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
79
|
+
};
|
|
80
|
+
const cookie = (0, cookies_1.getUpdatedCookie)(ctx);
|
|
81
|
+
if (cookie)
|
|
82
|
+
headers.cookie = cookie;
|
|
66
83
|
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`, {
|
|
67
84
|
...BASE_INIT,
|
|
85
|
+
headers,
|
|
68
86
|
body: JSON.stringify(mappedBody),
|
|
69
|
-
headers: {
|
|
70
|
-
'content-type': 'application/json',
|
|
71
|
-
cookie: ctx.headers.cookie,
|
|
72
|
-
},
|
|
73
87
|
}, { storeCookies });
|
|
74
88
|
},
|
|
75
89
|
orderForm: ({ id, refreshOutdatedData = true, channel = ctx.storage.channel, }) => {
|
|
@@ -78,33 +92,46 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
78
92
|
refreshOutdatedData: refreshOutdatedData.toString(),
|
|
79
93
|
sc: salesChannel,
|
|
80
94
|
});
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
95
|
+
const headers = {
|
|
96
|
+
'content-type': 'application/json',
|
|
97
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
98
|
+
};
|
|
99
|
+
const cookie = (0, cookies_1.getUpdatedCookie)(ctx);
|
|
100
|
+
if (cookie)
|
|
101
|
+
headers.cookie = cookie;
|
|
102
|
+
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, {
|
|
103
|
+
...BASE_INIT,
|
|
104
|
+
headers,
|
|
105
|
+
}, { storeCookies });
|
|
91
106
|
},
|
|
92
107
|
clearOrderFormMessages: ({ id }) => {
|
|
108
|
+
const headers = {
|
|
109
|
+
'content-type': 'application/json',
|
|
110
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
111
|
+
};
|
|
112
|
+
const cookie = (0, cookies_1.getUpdatedCookie)(ctx);
|
|
113
|
+
if (cookie)
|
|
114
|
+
headers.cookie = cookie;
|
|
93
115
|
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}/messages/clear`, {
|
|
94
116
|
...BASE_INIT,
|
|
117
|
+
headers,
|
|
95
118
|
body: '{}',
|
|
96
|
-
}
|
|
119
|
+
});
|
|
97
120
|
},
|
|
98
121
|
updateOrderFormItems: ({ id, orderItems, allowOutdatedData = 'paymentData', salesChannel = ctx.storage.channel.salesChannel, shouldSplitItem = true, }) => {
|
|
99
122
|
const params = new URLSearchParams({
|
|
100
123
|
allowOutdatedData,
|
|
101
124
|
sc: salesChannel,
|
|
102
125
|
});
|
|
126
|
+
const headers = {
|
|
127
|
+
'content-type': 'application/json',
|
|
128
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
129
|
+
};
|
|
130
|
+
const cookie = (0, cookies_1.getUpdatedCookie)(ctx);
|
|
131
|
+
if (cookie)
|
|
132
|
+
headers.cookie = cookie;
|
|
103
133
|
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}/items?${params}`, {
|
|
104
|
-
headers
|
|
105
|
-
'content-type': 'application/json',
|
|
106
|
-
cookie: ctx.headers?.cookie,
|
|
107
|
-
},
|
|
134
|
+
headers,
|
|
108
135
|
body: JSON.stringify({
|
|
109
136
|
orderItems,
|
|
110
137
|
noSplitItem: !shouldSplitItem,
|
|
@@ -113,11 +140,18 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
113
140
|
}, { storeCookies });
|
|
114
141
|
},
|
|
115
142
|
setCustomData: ({ id, appId, key, value, }) => {
|
|
143
|
+
const headers = {
|
|
144
|
+
'content-type': 'application/json',
|
|
145
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
146
|
+
};
|
|
147
|
+
const cookie = (0, cookies_1.getUpdatedCookie)(ctx);
|
|
148
|
+
if (cookie)
|
|
149
|
+
headers.cookie = cookie;
|
|
116
150
|
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}/customData/${appId}/${key}`, {
|
|
117
|
-
|
|
151
|
+
headers,
|
|
118
152
|
body: JSON.stringify({ value }),
|
|
119
153
|
method: 'PUT',
|
|
120
|
-
}
|
|
154
|
+
});
|
|
121
155
|
},
|
|
122
156
|
region: async ({ postalCode, geoCoordinates, country, salesChannel, }) => {
|
|
123
157
|
const params = new URLSearchParams({
|
|
@@ -128,21 +162,45 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
128
162
|
? params.append('postalCode', postalCode)
|
|
129
163
|
: params.append('geoCoordinates', `${geoCoordinates?.longitude};${geoCoordinates?.latitude}`);
|
|
130
164
|
const url = `${base}/api/checkout/pub/regions/?${params.toString()}`;
|
|
131
|
-
|
|
165
|
+
const headers = {
|
|
166
|
+
'content-type': 'application/json',
|
|
167
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
168
|
+
};
|
|
169
|
+
const cookie = (0, cookies_1.getUpdatedCookie)(ctx);
|
|
170
|
+
if (cookie)
|
|
171
|
+
headers.cookie = cookie;
|
|
172
|
+
return (0, fetch_1.fetchAPI)(url, {
|
|
173
|
+
headers,
|
|
174
|
+
}, { storeCookies });
|
|
132
175
|
},
|
|
133
176
|
address: async ({ postalCode, country, }) => {
|
|
134
|
-
|
|
177
|
+
const headers = {
|
|
178
|
+
'content-type': 'application/json',
|
|
179
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
180
|
+
};
|
|
181
|
+
const cookie = (0, cookies_1.getUpdatedCookie)(ctx);
|
|
182
|
+
if (cookie)
|
|
183
|
+
headers.cookie = cookie;
|
|
184
|
+
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/postal-code/${country}/${postalCode}`, {
|
|
185
|
+
headers,
|
|
186
|
+
}, { storeCookies });
|
|
135
187
|
},
|
|
136
188
|
},
|
|
137
189
|
session: (search) => {
|
|
138
190
|
const params = new URLSearchParams(search);
|
|
139
191
|
params.set('items', 'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol');
|
|
192
|
+
const cookie = (0, cookies_1.getUpdatedCookie)(ctx);
|
|
193
|
+
const headers = cookie
|
|
194
|
+
? {
|
|
195
|
+
'content-type': 'application/json',
|
|
196
|
+
cookie,
|
|
197
|
+
}
|
|
198
|
+
: {
|
|
199
|
+
'content-type': 'application/json',
|
|
200
|
+
};
|
|
140
201
|
return (0, fetch_1.fetchAPI)(`${base}/api/sessions?${params.toString()}`, {
|
|
141
202
|
method: 'POST',
|
|
142
|
-
headers
|
|
143
|
-
'content-type': 'application/json',
|
|
144
|
-
cookie: ctx.headers.cookie,
|
|
145
|
-
},
|
|
203
|
+
headers,
|
|
146
204
|
body: '{}',
|
|
147
205
|
}, { storeCookies });
|
|
148
206
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../src/platforms/vtex/clients/commerce/index.ts"],"names":[],"mappings":";;;AACA,oCAAmC;AAoBnC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../src/platforms/vtex/clients/commerce/index.ts"],"names":[],"mappings":";;;AACA,oCAAmC;AAoBnC,iDAAsE;AAItE,MAAM,SAAS,GAAG;IAChB,MAAM,EAAE,MAAM;IACd,OAAO,EAAE;QACP,cAAc,EAAE,kBAAkB;KACnC;CACF,CAAA;AAEM,MAAM,YAAY,GAAG,CAC1B,EAAE,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAW,EACnD,GAAY,EACZ,EAAE;IACF,MAAM,IAAI,GAAG,WAAW,OAAO,IAAI,WAAW,SAAS,CAAA;IACvD,MAAM,YAAY,GAAG,IAAA,wBAAc,EAAC,GAAG,CAAC,CAAA;IACxC,qFAAqF;IACrF,MAAM,aAAa,GAAG,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,kBAAkB,CAAC,IAAI,GAAG,CAAC,OAAO,EAAE,IAAI,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;IAEvH,OAAO;QACL,OAAO,EAAE;YACP,YAAY,EAAE,CAAC,EAAU,EAAyB,EAAE,CAClD,IAAA,gBAAQ,EACN,GAAG,IAAI,wCAAwC,EAAE,EAAE,EACnD,SAAS,EACT,EAAE,YAAY,EAAE,CACjB;YACH,KAAK,EAAE;gBACL,IAAI,EAAE,GAAqB,EAAE,CAC3B,IAAA,gBAAQ,EAA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|
|
49
49
|
flags: options.flags ?? {},
|
|
50
50
|
locale: options.locale,
|
|
51
|
-
cookies: new
|
|
51
|
+
cookies: new Map(),
|
|
52
52
|
};
|
|
53
53
|
ctx.clients = (0, clients_1.getClients)(options, ctx);
|
|
54
54
|
ctx.loaders = (0, loaders_1.getLoaders)(options, ctx);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/platforms/vtex/index.ts"],"names":[],"mappings":";;;;AAAA,uCAAsC;AACtC,uCAAsC;AACtC,+DAAgE;AAChE,iEAAkE;AAClE,uDAAwD;AACxD,6CAI0B;AAC1B,qDAA8D;AAC9D,mDAA+C;AAC/C,+DAA2D;AAC3D,6CAA8C;AAC9C,iDAAkD;AAClD,2DAA4D;AAC5D,6DAA8D;AAC9D,6CAAyC;AACzC,+CAAgD;AAChD,2DAA4D;AAC5D,yCAA0C;AAC1C,yDAAqD;AACrD,6DAAuD;AACvD,sEAA4C;AAgD5C,MAAM,SAAS,GAAG;IAChB,eAAe,EAAf,4BAAe;IACf,mBAAmB,EAAnB,oCAAmB;IACnB,YAAY,EAAZ,sBAAY;IACZ,QAAQ,EAAR,cAAQ;IACR,UAAU,EAAV,kBAAU;IACV,iBAAiB,EAAjB,yBAAiB;IACjB,eAAe,EAAf,uBAAe;IACf,sBAAsB,EAAtB,kCAAsB;IACtB,UAAU,EAAV,kBAAU;IACV,oBAAoB,EAApB,sCAAoB;IACpB,WAAW,EAAX,oBAAW;IACX,iBAAiB,EAAjB,gCAAiB;IACjB,iBAAiB,EAAjB,gCAAiB;IACjB,kBAAkB,EAAlB,kCAAkB;IAClB,WAAW,EAAX,2BAAW;IACX,WAAW,EAAX,yBAAW;IACX,cAAc,EAAd,+BAAc;IACd,KAAK,EAAL,aAAK;IACL,QAAQ,EAAR,mBAAQ;CACT,CAAA;AAEM,MAAM,iBAAiB,GAC5B,CAAC,OAAgB,EAAE,EAAE,CACrB,CAAC,GAAQ,EAAW,EA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ACpB,GAAG,CAAC,OAAO,GAAG;QACZ,OAAO,EAAE,iBAAc,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;QAC9C,KAAK,EAAE,OAAO,CAAC,KAAK,IAAI,EAAE;QAC1B,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,OAAO,EAAE,IAAI,GAAG,EAAkC;KACnD,CAAA;IACD,GAAG,CAAC,OAAO,GAAG,IAAA,oBAAU,EAAC,OAAO,EAAE,GAAG,CAAC,CAAA;IACtC,GAAG,CAAC,OAAO,GAAG,IAAA,oBAAU,EAAC,OAAO,EAAE,GAAG,CAAC,CAAA;IAEtC,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AAbU,QAAA,iBAAiB,qBAa3B;AAEI,MAAM,YAAY,GAAG,CAAC,CAAU,EAAE,EAAE,CAAC,SAAS,CAAA;AAAxC,QAAA,YAAY,gBAA4B"}
|
|
@@ -1,3 +1,26 @@
|
|
|
1
1
|
import type { Context } from '../index';
|
|
2
2
|
export declare const setCookie: (headers: Headers, ctx: Context) => void;
|
|
3
3
|
export declare const getStoreCookie: (ctx: Context) => (headers: Headers) => void;
|
|
4
|
+
/**
|
|
5
|
+
* This function returns a modified copy of the original cookie header (ctx.headers.cookie from the first request)
|
|
6
|
+
* with the cookie values that comes in each request (ctx.storage.cookies).
|
|
7
|
+
* If there is no cookies in storage, the ctx.headers?.cookie is used
|
|
8
|
+
*
|
|
9
|
+
* ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
|
|
10
|
+
* [1] cookie key
|
|
11
|
+
* [2] {
|
|
12
|
+
* value: cookie value,
|
|
13
|
+
* setCookie: setCookie used in browser response
|
|
14
|
+
* }
|
|
15
|
+
*/
|
|
16
|
+
export declare const getUpdatedCookie: (ctx: Context) => string | null;
|
|
17
|
+
/**
|
|
18
|
+
* This function updates the cookie value based on its key
|
|
19
|
+
*
|
|
20
|
+
* const existingCookies = 'key=value1; key2=; key3=value3';
|
|
21
|
+
* const storageCookieKey = 'key2';
|
|
22
|
+
* const storageCookieValue = 'value2'
|
|
23
|
+
*
|
|
24
|
+
* updatesCookieValueByKey(existingCookies, storageCookieKey, storageCookieValue) returns 'key=value1; key2=value2; key3=value3';
|
|
25
|
+
*/
|
|
26
|
+
export declare const updatesCookieValueByKey: (existingCookies: string, storageCookieKey: string, storageCookieValue: string) => string;
|
|
@@ -1,16 +1,77 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getStoreCookie = exports.setCookie = void 0;
|
|
4
|
-
const
|
|
3
|
+
exports.updatesCookieValueByKey = exports.getUpdatedCookie = exports.getStoreCookie = exports.setCookie = void 0;
|
|
4
|
+
const MATCH_FIRST_SET_COOKIE_KEY_VALUE = /^([^=]+)=([^;]*)/;
|
|
5
|
+
/**
|
|
6
|
+
* This function updates the ctx.storage.cookies, that is used in each request.
|
|
7
|
+
*
|
|
8
|
+
* ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
|
|
9
|
+
* [1] cookie key
|
|
10
|
+
* [2] {
|
|
11
|
+
* value: cookie value,
|
|
12
|
+
* setCookie: setCookie used in browser response
|
|
13
|
+
* }
|
|
14
|
+
*/
|
|
15
|
+
const updatesContextStorageCookies = (setCookieValue, ctx) => {
|
|
16
|
+
const matchCookie = setCookieValue.match(MATCH_FIRST_SET_COOKIE_KEY_VALUE);
|
|
17
|
+
if (matchCookie) {
|
|
18
|
+
const cookieKey = matchCookie[1];
|
|
19
|
+
const cookieValue = matchCookie[2];
|
|
20
|
+
ctx.storage.cookies.set(cookieKey, {
|
|
21
|
+
value: cookieValue,
|
|
22
|
+
setCookie: setCookieValue,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
};
|
|
5
26
|
const setCookie = (headers, ctx) => {
|
|
6
|
-
const faststoreAPIHostname = new URL(`https://${ctx.headers.host}`).hostname;
|
|
7
27
|
headers
|
|
8
28
|
.getSetCookie()
|
|
9
|
-
.forEach((
|
|
10
|
-
// Replaces original cookie domain for FastStore API's domain hostname
|
|
11
|
-
cookie.replace(MATCH_DOMAIN_REGEXP, `; domain=${faststoreAPIHostname}`)));
|
|
29
|
+
.forEach((setCookieValue) => updatesContextStorageCookies(setCookieValue, ctx));
|
|
12
30
|
};
|
|
13
31
|
exports.setCookie = setCookie;
|
|
14
32
|
const getStoreCookie = (ctx) => (headers) => (0, exports.setCookie)(headers, ctx);
|
|
15
33
|
exports.getStoreCookie = getStoreCookie;
|
|
34
|
+
/**
|
|
35
|
+
* This function returns a modified copy of the original cookie header (ctx.headers.cookie from the first request)
|
|
36
|
+
* with the cookie values that comes in each request (ctx.storage.cookies).
|
|
37
|
+
* If there is no cookies in storage, the ctx.headers?.cookie is used
|
|
38
|
+
*
|
|
39
|
+
* ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
|
|
40
|
+
* [1] cookie key
|
|
41
|
+
* [2] {
|
|
42
|
+
* value: cookie value,
|
|
43
|
+
* setCookie: setCookie used in browser response
|
|
44
|
+
* }
|
|
45
|
+
*/
|
|
46
|
+
const getUpdatedCookie = (ctx) => {
|
|
47
|
+
if (!ctx.headers?.cookie) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
const contextStorageCookies = Array.from(ctx.storage.cookies.entries());
|
|
51
|
+
if (contextStorageCookies.length === 0) {
|
|
52
|
+
return ctx.headers.cookie;
|
|
53
|
+
}
|
|
54
|
+
return contextStorageCookies.reduce((existingCookies, [storageCookieKey, { value: storageCookieValue }]) => (0, exports.updatesCookieValueByKey)(existingCookies, storageCookieKey, storageCookieValue), ctx.headers.cookie);
|
|
55
|
+
};
|
|
56
|
+
exports.getUpdatedCookie = getUpdatedCookie;
|
|
57
|
+
/**
|
|
58
|
+
* This function updates the cookie value based on its key
|
|
59
|
+
*
|
|
60
|
+
* const existingCookies = 'key=value1; key2=; key3=value3';
|
|
61
|
+
* const storageCookieKey = 'key2';
|
|
62
|
+
* const storageCookieValue = 'value2'
|
|
63
|
+
*
|
|
64
|
+
* updatesCookieValueByKey(existingCookies, storageCookieKey, storageCookieValue) returns 'key=value1; key2=value2; key3=value3';
|
|
65
|
+
*/
|
|
66
|
+
const updatesCookieValueByKey = (existingCookies, storageCookieKey, storageCookieValue) => {
|
|
67
|
+
const MATCH_COOKIE_KEY_VALUE = new RegExp(`(${storageCookieKey})=([^;]*)`);
|
|
68
|
+
const cookieParts = existingCookies.match(MATCH_COOKIE_KEY_VALUE);
|
|
69
|
+
// replaces original cookie with the one coming from storage
|
|
70
|
+
if (cookieParts) {
|
|
71
|
+
return existingCookies.replace(MATCH_FIRST_SET_COOKIE_KEY_VALUE, `${cookieParts[1]}=${storageCookieValue}`);
|
|
72
|
+
}
|
|
73
|
+
// add new storage cookie to the original list of cookies
|
|
74
|
+
return `${existingCookies};${storageCookieKey}=${storageCookieValue}`;
|
|
75
|
+
};
|
|
76
|
+
exports.updatesCookieValueByKey = updatesCookieValueByKey;
|
|
16
77
|
//# sourceMappingURL=cookies.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookies.js","sourceRoot":"","sources":["../../../../../../src/platforms/vtex/utils/cookies.ts"],"names":[],"mappings":";;;AAEA,MAAM,
|
|
1
|
+
{"version":3,"file":"cookies.js","sourceRoot":"","sources":["../../../../../../src/platforms/vtex/utils/cookies.ts"],"names":[],"mappings":";;;AAEA,MAAM,gCAAgC,GAAG,kBAAkB,CAAA;AAE3D;;;;;;;;;GASG;AACH,MAAM,4BAA4B,GAAG,CAAC,cAAsB,EAAE,GAAY,EAAE,EAAE;IAC5E,MAAM,WAAW,GAAG,cAAc,CAAC,KAAK,CAAC,gCAAgC,CAAC,CAAA;IAC1E,IAAI,WAAW,EAAE,CAAC;QAChB,MAAM,SAAS,GAAG,WAAW,CAAC,CAAC,CAAC,CAAA;QAChC,MAAM,WAAW,GAAG,WAAW,CAAC,CAAC,CAAC,CAAA;QAElC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,GAA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GAAG,CACrC,eAAuB,EACvB,gBAAwB,EACxB,kBAA0B,EAC1B,EAAE;IACF,MAAM,sBAAsB,GAAG,IAAI,MAAM,CAAC,IAAI,gBAAgB,WAAW,CAAC,CAAA;IAC1E,MAAM,WAAW,GAAG,eAAe,CAAC,KAAK,CAAC,sBAAsB,CAAC,CAAA;IAEjE,4DAA4D;IAC5D,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO,eAAe,CAAC,OAAO,CAC5B,gCAAgC,EAChC,GAAG,WAAW,CAAC,CAAC,CAAC,IAAI,kBAAkB,EAAE,CAC1C,CAAA;IACH,CAAC;IAED,yDAAyD;IACzD,OAAO,GAAG,eAAe,IAAI,gBAAgB,IAAI,kBAAkB,EAAE,CAAA;AACvE,CAAC,CAAA;AAlBY,QAAA,uBAAuB,2BAkBnC"}
|
package/dist/esm/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { fetchAPI } from '../fetch';
|
|
2
|
-
import { getStoreCookie } from '../../utils/cookies';
|
|
2
|
+
import { getUpdatedCookie, getStoreCookie } from '../../utils/cookies';
|
|
3
3
|
const BASE_INIT = {
|
|
4
4
|
method: 'POST',
|
|
5
5
|
headers: {
|
|
@@ -9,6 +9,8 @@ const BASE_INIT = {
|
|
|
9
9
|
export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
10
10
|
const base = `https://${account}.${environment}.com.br`;
|
|
11
11
|
const storeCookies = getStoreCookie(ctx);
|
|
12
|
+
// replacing www. only for testing while www.vtexfaststore.com is configured with www
|
|
13
|
+
const forwardedHost = (new Headers(ctx.headers).get('x-forwarded-host') ?? ctx.headers?.host ?? '').replace('www.', '');
|
|
12
14
|
return {
|
|
13
15
|
catalog: {
|
|
14
16
|
salesChannel: (sc) => fetchAPI(`${base}/api/catalog_system/pub/saleschannel/${sc}`, undefined, { storeCookies }),
|
|
@@ -38,8 +40,16 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
38
40
|
const params = new URLSearchParams({
|
|
39
41
|
sc: salesChannel,
|
|
40
42
|
});
|
|
43
|
+
const headers = {
|
|
44
|
+
'content-type': 'application/json',
|
|
45
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
46
|
+
};
|
|
47
|
+
const cookie = getUpdatedCookie(ctx);
|
|
48
|
+
if (cookie)
|
|
49
|
+
headers.cookie = cookie;
|
|
41
50
|
return fetchAPI(`${base}/api/checkout/pub/orderForms/simulation?${params.toString()}`, {
|
|
42
51
|
...BASE_INIT,
|
|
52
|
+
headers,
|
|
43
53
|
body: JSON.stringify(args),
|
|
44
54
|
}, { storeCookies });
|
|
45
55
|
},
|
|
@@ -60,13 +70,17 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
60
70
|
selectedAddresses: selectedAddresses,
|
|
61
71
|
clearAddressIfPostalCodeNotFound: incrementAddress,
|
|
62
72
|
};
|
|
73
|
+
const headers = {
|
|
74
|
+
'content-type': 'application/json',
|
|
75
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
76
|
+
};
|
|
77
|
+
const cookie = getUpdatedCookie(ctx);
|
|
78
|
+
if (cookie)
|
|
79
|
+
headers.cookie = cookie;
|
|
63
80
|
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`, {
|
|
64
81
|
...BASE_INIT,
|
|
82
|
+
headers,
|
|
65
83
|
body: JSON.stringify(mappedBody),
|
|
66
|
-
headers: {
|
|
67
|
-
'content-type': 'application/json',
|
|
68
|
-
cookie: ctx.headers.cookie,
|
|
69
|
-
},
|
|
70
84
|
}, { storeCookies });
|
|
71
85
|
},
|
|
72
86
|
orderForm: ({ id, refreshOutdatedData = true, channel = ctx.storage.channel, }) => {
|
|
@@ -75,33 +89,46 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
75
89
|
refreshOutdatedData: refreshOutdatedData.toString(),
|
|
76
90
|
sc: salesChannel,
|
|
77
91
|
});
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
92
|
+
const headers = {
|
|
93
|
+
'content-type': 'application/json',
|
|
94
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
95
|
+
};
|
|
96
|
+
const cookie = getUpdatedCookie(ctx);
|
|
97
|
+
if (cookie)
|
|
98
|
+
headers.cookie = cookie;
|
|
99
|
+
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, {
|
|
100
|
+
...BASE_INIT,
|
|
101
|
+
headers,
|
|
102
|
+
}, { storeCookies });
|
|
88
103
|
},
|
|
89
104
|
clearOrderFormMessages: ({ id }) => {
|
|
105
|
+
const headers = {
|
|
106
|
+
'content-type': 'application/json',
|
|
107
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
108
|
+
};
|
|
109
|
+
const cookie = getUpdatedCookie(ctx);
|
|
110
|
+
if (cookie)
|
|
111
|
+
headers.cookie = cookie;
|
|
90
112
|
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/messages/clear`, {
|
|
91
113
|
...BASE_INIT,
|
|
114
|
+
headers,
|
|
92
115
|
body: '{}',
|
|
93
|
-
}
|
|
116
|
+
});
|
|
94
117
|
},
|
|
95
118
|
updateOrderFormItems: ({ id, orderItems, allowOutdatedData = 'paymentData', salesChannel = ctx.storage.channel.salesChannel, shouldSplitItem = true, }) => {
|
|
96
119
|
const params = new URLSearchParams({
|
|
97
120
|
allowOutdatedData,
|
|
98
121
|
sc: salesChannel,
|
|
99
122
|
});
|
|
123
|
+
const headers = {
|
|
124
|
+
'content-type': 'application/json',
|
|
125
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
126
|
+
};
|
|
127
|
+
const cookie = getUpdatedCookie(ctx);
|
|
128
|
+
if (cookie)
|
|
129
|
+
headers.cookie = cookie;
|
|
100
130
|
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/items?${params}`, {
|
|
101
|
-
headers
|
|
102
|
-
'content-type': 'application/json',
|
|
103
|
-
cookie: ctx.headers?.cookie,
|
|
104
|
-
},
|
|
131
|
+
headers,
|
|
105
132
|
body: JSON.stringify({
|
|
106
133
|
orderItems,
|
|
107
134
|
noSplitItem: !shouldSplitItem,
|
|
@@ -110,11 +137,18 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
110
137
|
}, { storeCookies });
|
|
111
138
|
},
|
|
112
139
|
setCustomData: ({ id, appId, key, value, }) => {
|
|
140
|
+
const headers = {
|
|
141
|
+
'content-type': 'application/json',
|
|
142
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
143
|
+
};
|
|
144
|
+
const cookie = getUpdatedCookie(ctx);
|
|
145
|
+
if (cookie)
|
|
146
|
+
headers.cookie = cookie;
|
|
113
147
|
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/customData/${appId}/${key}`, {
|
|
114
|
-
|
|
148
|
+
headers,
|
|
115
149
|
body: JSON.stringify({ value }),
|
|
116
150
|
method: 'PUT',
|
|
117
|
-
}
|
|
151
|
+
});
|
|
118
152
|
},
|
|
119
153
|
region: async ({ postalCode, geoCoordinates, country, salesChannel, }) => {
|
|
120
154
|
const params = new URLSearchParams({
|
|
@@ -125,21 +159,45 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
125
159
|
? params.append('postalCode', postalCode)
|
|
126
160
|
: params.append('geoCoordinates', `${geoCoordinates?.longitude};${geoCoordinates?.latitude}`);
|
|
127
161
|
const url = `${base}/api/checkout/pub/regions/?${params.toString()}`;
|
|
128
|
-
|
|
162
|
+
const headers = {
|
|
163
|
+
'content-type': 'application/json',
|
|
164
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
165
|
+
};
|
|
166
|
+
const cookie = getUpdatedCookie(ctx);
|
|
167
|
+
if (cookie)
|
|
168
|
+
headers.cookie = cookie;
|
|
169
|
+
return fetchAPI(url, {
|
|
170
|
+
headers,
|
|
171
|
+
}, { storeCookies });
|
|
129
172
|
},
|
|
130
173
|
address: async ({ postalCode, country, }) => {
|
|
131
|
-
|
|
174
|
+
const headers = {
|
|
175
|
+
'content-type': 'application/json',
|
|
176
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
177
|
+
};
|
|
178
|
+
const cookie = getUpdatedCookie(ctx);
|
|
179
|
+
if (cookie)
|
|
180
|
+
headers.cookie = cookie;
|
|
181
|
+
return fetchAPI(`${base}/api/checkout/pub/postal-code/${country}/${postalCode}`, {
|
|
182
|
+
headers,
|
|
183
|
+
}, { storeCookies });
|
|
132
184
|
},
|
|
133
185
|
},
|
|
134
186
|
session: (search) => {
|
|
135
187
|
const params = new URLSearchParams(search);
|
|
136
188
|
params.set('items', 'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol');
|
|
189
|
+
const cookie = getUpdatedCookie(ctx);
|
|
190
|
+
const headers = cookie
|
|
191
|
+
? {
|
|
192
|
+
'content-type': 'application/json',
|
|
193
|
+
cookie,
|
|
194
|
+
}
|
|
195
|
+
: {
|
|
196
|
+
'content-type': 'application/json',
|
|
197
|
+
};
|
|
137
198
|
return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
|
|
138
199
|
method: 'POST',
|
|
139
|
-
headers
|
|
140
|
-
'content-type': 'application/json',
|
|
141
|
-
cookie: ctx.headers.cookie,
|
|
142
|
-
},
|
|
200
|
+
headers,
|
|
143
201
|
body: '{}',
|
|
144
202
|
}, { storeCookies });
|
|
145
203
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../src/platforms/vtex/clients/commerce/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAA;AAoBnC,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sour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AgB,CAAC,GAAG,CAAC,CAAA;YACpC,MAAM,OAAO,GAAgB,MAAM;gBACjC,CAAC,CAAC;oBACE,cAAc,EAAE,kBAAkB;oBAClC,MAAM;iBACP;gBACH,CAAC,CAAC;oBACE,cAAc,EAAE,kBAAkB;iBACnC,CAAA;YACL,OAAO,QAAQ,CACb,GAAG,IAAI,iBAAiB,MAAM,CAAC,QAAQ,EAAE,EAAE,EAC3C;gBACE,MAAM,EAAE,MAAM;gBACd,OAAO;gBACP,IAAI,EAAE,IAAI;aACX,EACD,EAAE,YAAY,EAAE,CACjB,CAAA;QACH,CAAC;QACD,qBAAqB,EAAE,CAAC,IAGvB,EAA+B,EAAE;YAChC,OAAO,QAAQ,CACb,GAAG,IAAI,iCAAiC,EACxC;gBACE,GAAG,SAAS;gBACZ,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,GAAG,IAAI,EAAE,iBAAiB,EAAE,IAAI,EAAE,CAAC;gBAC1D,MAAM,EAAE,OAAO;aAChB,EACD,EAAE,YAAY,EAAE,CACjB,CAAA;QACH,CAAC;KACF,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -44,7 +44,7 @@ export const getContextFactory = (options) => (ctx) => {
|
|
|
44
44
|
channel: ChannelMarshal.parse(options.channel),
|
|
45
45
|
flags: options.flags ?? {},
|
|
46
46
|
locale: options.locale,
|
|
47
|
-
cookies: new
|
|
47
|
+
cookies: new Map(),
|
|
48
48
|
};
|
|
49
49
|
ctx.clients = getClients(options, ctx);
|
|
50
50
|
ctx.loaders = getLoaders(options, ctx);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/platforms/vtex/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AACtC,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAA;AACtC,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAA;AAChE,OAAO,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AO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAA;AACvD,OAAO,cAAc,MAAM,iBAAiB,CAAA;AAgD5C,MAAM,SAAS,GAAG;IAChB,eAAe;IACf,mBAAmB;IACnB,YAAY;IACZ,QAAQ;IACR,UAAU;IACV,iBAAiB;IACjB,eAAe;IACf,sBAAsB;IACtB,UAAU;IACV,oBAAoB;IACpB,WAAW;IACX,iBAAiB;IACjB,iBAAiB;IACjB,kBAAkB;IAClB,WAAW;IACX,WAAW;IACX,cAAc;IACd,KAAK;IACL,QAAQ;CACT,CAAA;AAED,MAAM,CAAC,MAAM,iBAAiB,GAC5B,CAAC,OAAgB,EAAE,EAAE,CACrB,CAAC,GAAQ,EAAW,EAAE;IACpB,GAAG,CAAC,OAAO,GAAG;QACZ,OAAO,EAAE,cAAc,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC;QAC9C,KAAK,EAAE,OAAO,CAAC,KAAK,IAAI,EAAE;QAC1B,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,OAAO,EAAE,IAAI,GAAG,EAAkC;KACnD,CAAA;IACD,GAAG,CAAC,OAAO,GAAG,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAA;IACtC,GAAG,CAAC,OAAO,GAAG,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAA;IAEtC,OAAO,GAAG,CAAA;AACZ,CAAC,CAAA;AAEH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAU,EAAE,EAAE,CAAC,SAAS,CAAA"}
|
|
@@ -1,3 +1,26 @@
|
|
|
1
1
|
import type { Context } from '../index';
|
|
2
2
|
export declare const setCookie: (headers: Headers, ctx: Context) => void;
|
|
3
3
|
export declare const getStoreCookie: (ctx: Context) => (headers: Headers) => void;
|
|
4
|
+
/**
|
|
5
|
+
* This function returns a modified copy of the original cookie header (ctx.headers.cookie from the first request)
|
|
6
|
+
* with the cookie values that comes in each request (ctx.storage.cookies).
|
|
7
|
+
* If there is no cookies in storage, the ctx.headers?.cookie is used
|
|
8
|
+
*
|
|
9
|
+
* ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
|
|
10
|
+
* [1] cookie key
|
|
11
|
+
* [2] {
|
|
12
|
+
* value: cookie value,
|
|
13
|
+
* setCookie: setCookie used in browser response
|
|
14
|
+
* }
|
|
15
|
+
*/
|
|
16
|
+
export declare const getUpdatedCookie: (ctx: Context) => string | null;
|
|
17
|
+
/**
|
|
18
|
+
* This function updates the cookie value based on its key
|
|
19
|
+
*
|
|
20
|
+
* const existingCookies = 'key=value1; key2=; key3=value3';
|
|
21
|
+
* const storageCookieKey = 'key2';
|
|
22
|
+
* const storageCookieValue = 'value2'
|
|
23
|
+
*
|
|
24
|
+
* updatesCookieValueByKey(existingCookies, storageCookieKey, storageCookieValue) returns 'key=value1; key2=value2; key3=value3';
|
|
25
|
+
*/
|
|
26
|
+
export declare const updatesCookieValueByKey: (existingCookies: string, storageCookieKey: string, storageCookieValue: string) => string;
|
|
@@ -1,11 +1,70 @@
|
|
|
1
|
-
const
|
|
1
|
+
const MATCH_FIRST_SET_COOKIE_KEY_VALUE = /^([^=]+)=([^;]*)/;
|
|
2
|
+
/**
|
|
3
|
+
* This function updates the ctx.storage.cookies, that is used in each request.
|
|
4
|
+
*
|
|
5
|
+
* ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
|
|
6
|
+
* [1] cookie key
|
|
7
|
+
* [2] {
|
|
8
|
+
* value: cookie value,
|
|
9
|
+
* setCookie: setCookie used in browser response
|
|
10
|
+
* }
|
|
11
|
+
*/
|
|
12
|
+
const updatesContextStorageCookies = (setCookieValue, ctx) => {
|
|
13
|
+
const matchCookie = setCookieValue.match(MATCH_FIRST_SET_COOKIE_KEY_VALUE);
|
|
14
|
+
if (matchCookie) {
|
|
15
|
+
const cookieKey = matchCookie[1];
|
|
16
|
+
const cookieValue = matchCookie[2];
|
|
17
|
+
ctx.storage.cookies.set(cookieKey, {
|
|
18
|
+
value: cookieValue,
|
|
19
|
+
setCookie: setCookieValue,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
};
|
|
2
23
|
export const setCookie = (headers, ctx) => {
|
|
3
|
-
const faststoreAPIHostname = new URL(`https://${ctx.headers.host}`).hostname;
|
|
4
24
|
headers
|
|
5
25
|
.getSetCookie()
|
|
6
|
-
.forEach((
|
|
7
|
-
// Replaces original cookie domain for FastStore API's domain hostname
|
|
8
|
-
cookie.replace(MATCH_DOMAIN_REGEXP, `; domain=${faststoreAPIHostname}`)));
|
|
26
|
+
.forEach((setCookieValue) => updatesContextStorageCookies(setCookieValue, ctx));
|
|
9
27
|
};
|
|
10
28
|
export const getStoreCookie = (ctx) => (headers) => setCookie(headers, ctx);
|
|
29
|
+
/**
|
|
30
|
+
* This function returns a modified copy of the original cookie header (ctx.headers.cookie from the first request)
|
|
31
|
+
* with the cookie values that comes in each request (ctx.storage.cookies).
|
|
32
|
+
* If there is no cookies in storage, the ctx.headers?.cookie is used
|
|
33
|
+
*
|
|
34
|
+
* ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
|
|
35
|
+
* [1] cookie key
|
|
36
|
+
* [2] {
|
|
37
|
+
* value: cookie value,
|
|
38
|
+
* setCookie: setCookie used in browser response
|
|
39
|
+
* }
|
|
40
|
+
*/
|
|
41
|
+
export const getUpdatedCookie = (ctx) => {
|
|
42
|
+
if (!ctx.headers?.cookie) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const contextStorageCookies = Array.from(ctx.storage.cookies.entries());
|
|
46
|
+
if (contextStorageCookies.length === 0) {
|
|
47
|
+
return ctx.headers.cookie;
|
|
48
|
+
}
|
|
49
|
+
return contextStorageCookies.reduce((existingCookies, [storageCookieKey, { value: storageCookieValue }]) => updatesCookieValueByKey(existingCookies, storageCookieKey, storageCookieValue), ctx.headers.cookie);
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* This function updates the cookie value based on its key
|
|
53
|
+
*
|
|
54
|
+
* const existingCookies = 'key=value1; key2=; key3=value3';
|
|
55
|
+
* const storageCookieKey = 'key2';
|
|
56
|
+
* const storageCookieValue = 'value2'
|
|
57
|
+
*
|
|
58
|
+
* updatesCookieValueByKey(existingCookies, storageCookieKey, storageCookieValue) returns 'key=value1; key2=value2; key3=value3';
|
|
59
|
+
*/
|
|
60
|
+
export const updatesCookieValueByKey = (existingCookies, storageCookieKey, storageCookieValue) => {
|
|
61
|
+
const MATCH_COOKIE_KEY_VALUE = new RegExp(`(${storageCookieKey})=([^;]*)`);
|
|
62
|
+
const cookieParts = existingCookies.match(MATCH_COOKIE_KEY_VALUE);
|
|
63
|
+
// replaces original cookie with the one coming from storage
|
|
64
|
+
if (cookieParts) {
|
|
65
|
+
return existingCookies.replace(MATCH_FIRST_SET_COOKIE_KEY_VALUE, `${cookieParts[1]}=${storageCookieValue}`);
|
|
66
|
+
}
|
|
67
|
+
// add new storage cookie to the original list of cookies
|
|
68
|
+
return `${existingCookies};${storageCookieKey}=${storageCookieValue}`;
|
|
69
|
+
};
|
|
11
70
|
//# sourceMappingURL=cookies.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookies.js","sourceRoot":"","sources":["../../../../../../src/platforms/vtex/utils/cookies.ts"],"names":[],"mappings":"AAEA,MAAM,
|
|
1
|
+
{"version":3,"file":"cookies.js","sourceRoot":"","sources":["../../../../../../src/platforms/vtex/utils/cookies.ts"],"names":[],"mappings":"AAEA,MAAM,gCAAgC,GAAG,kBAAkB,CAAA;AAE3D;;;;;;;;;GASG;AACH,MAAM,4BAA4B,GAAG,CAAC,cAAsB,EAAE,GAAY,EAAE,EAAE;IAC5E,MAAM,WAAW,GAAG,cAAc,CAAC,KAAK,CAAC,gCAAgC,CAAC,CAAA;IAC1E,IAAI,WAAW,EAAE,CAAC;QAChB,MAAM,SAAS,GAAG,WAAW,CAAC,CAAC,CAAC,CAAA;QACh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AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACrC,eAAuB,EACvB,gBAAwB,EACxB,kBAA0B,EAC1B,EAAE;IACF,MAAM,sBAAsB,GAAG,IAAI,MAAM,CAAC,IAAI,gBAAgB,WAAW,CAAC,CAAA;IAC1E,MAAM,WAAW,GAAG,eAAe,CAAC,KAAK,CAAC,sBAAsB,CAAC,CAAA;IAEjE,4DAA4D;IAC5D,IAAI,WAAW,EAAE,CAAC;QAChB,OAAO,eAAe,CAAC,OAAO,CAC5B,gCAAgC,EAChC,GAAG,WAAW,CAAC,CAAC,CAAC,IAAI,kBAAkB,EAAE,CAC1C,CAAA;IACH,CAAC;IAED,yDAAyD;IACzD,OAAO,GAAG,eAAe,IAAI,gBAAgB,IAAI,kBAAkB,EAAE,CAAA;AACvE,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faststore/api",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.49",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "dist/cjs/src/index.js",
|
|
6
6
|
"typings": "dist/esm/src/index.d.ts",
|
|
@@ -76,5 +76,5 @@
|
|
|
76
76
|
"volta": {
|
|
77
77
|
"extends": "../../package.json"
|
|
78
78
|
},
|
|
79
|
-
"gitHead": "
|
|
79
|
+
"gitHead": "a9996c80a4c0995ff526a1313ddba42022b81e66"
|
|
80
80
|
}
|
|
@@ -19,7 +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 { getStoreCookie } from '../../utils/cookies'
|
|
22
|
+
import { getUpdatedCookie, getStoreCookie } from '../../utils/cookies'
|
|
23
23
|
|
|
24
24
|
type ValueOf<T> = T extends Record<string, infer K> ? K : never
|
|
25
25
|
|
|
@@ -36,6 +36,8 @@ export const VtexCommerce = (
|
|
|
36
36
|
) => {
|
|
37
37
|
const base = `https://${account}.${environment}.com.br`
|
|
38
38
|
const storeCookies = getStoreCookie(ctx)
|
|
39
|
+
// replacing www. only for testing while www.vtexfaststore.com is configured with www
|
|
40
|
+
const forwardedHost = (new Headers(ctx.headers).get('x-forwarded-host') ?? ctx.headers?.host ?? '').replace('www.', '')
|
|
39
41
|
|
|
40
42
|
return {
|
|
41
43
|
catalog: {
|
|
@@ -99,16 +101,24 @@ export const VtexCommerce = (
|
|
|
99
101
|
sc: salesChannel,
|
|
100
102
|
})
|
|
101
103
|
|
|
104
|
+
const headers: HeadersInit = {
|
|
105
|
+
'content-type': 'application/json',
|
|
106
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const cookie = getUpdatedCookie(ctx)
|
|
110
|
+
if (cookie) headers.cookie = cookie
|
|
111
|
+
|
|
102
112
|
return fetchAPI(
|
|
103
113
|
`${base}/api/checkout/pub/orderForms/simulation?${params.toString()}`,
|
|
104
114
|
{
|
|
105
115
|
...BASE_INIT,
|
|
116
|
+
headers,
|
|
106
117
|
body: JSON.stringify(args),
|
|
107
118
|
},
|
|
108
119
|
{ storeCookies }
|
|
109
120
|
)
|
|
110
121
|
},
|
|
111
|
-
|
|
112
122
|
shippingData: (
|
|
113
123
|
{
|
|
114
124
|
id,
|
|
@@ -140,20 +150,25 @@ export const VtexCommerce = (
|
|
|
140
150
|
selectedAddresses: selectedAddresses,
|
|
141
151
|
clearAddressIfPostalCodeNotFound: incrementAddress,
|
|
142
152
|
}
|
|
153
|
+
|
|
154
|
+
const headers: HeadersInit = {
|
|
155
|
+
'content-type': 'application/json',
|
|
156
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const cookie = getUpdatedCookie(ctx)
|
|
160
|
+
if (cookie) headers.cookie = cookie
|
|
161
|
+
|
|
143
162
|
return fetchAPI(
|
|
144
163
|
`${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`,
|
|
145
164
|
{
|
|
146
165
|
...BASE_INIT,
|
|
166
|
+
headers,
|
|
147
167
|
body: JSON.stringify(mappedBody),
|
|
148
|
-
headers: {
|
|
149
|
-
'content-type': 'application/json',
|
|
150
|
-
cookie: ctx.headers.cookie,
|
|
151
|
-
},
|
|
152
168
|
},
|
|
153
169
|
{ storeCookies }
|
|
154
170
|
)
|
|
155
171
|
},
|
|
156
|
-
|
|
157
172
|
orderForm: ({
|
|
158
173
|
id,
|
|
159
174
|
refreshOutdatedData = true,
|
|
@@ -168,34 +183,42 @@ export const VtexCommerce = (
|
|
|
168
183
|
refreshOutdatedData: refreshOutdatedData.toString(),
|
|
169
184
|
sc: salesChannel,
|
|
170
185
|
})
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
: BASE_INIT
|
|
186
|
+
|
|
187
|
+
const headers: HeadersInit = {
|
|
188
|
+
'content-type': 'application/json',
|
|
189
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const cookie = getUpdatedCookie(ctx)
|
|
193
|
+
if (cookie) headers.cookie = cookie
|
|
180
194
|
|
|
181
195
|
return fetchAPI(
|
|
182
196
|
`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`,
|
|
183
|
-
|
|
197
|
+
{
|
|
198
|
+
...BASE_INIT,
|
|
199
|
+
headers,
|
|
200
|
+
},
|
|
184
201
|
{ storeCookies }
|
|
185
202
|
)
|
|
186
203
|
},
|
|
187
|
-
|
|
188
204
|
clearOrderFormMessages: ({ id }: { id: string }) => {
|
|
205
|
+
const headers: HeadersInit = {
|
|
206
|
+
'content-type': 'application/json',
|
|
207
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const cookie = getUpdatedCookie(ctx)
|
|
211
|
+
if (cookie) headers.cookie = cookie
|
|
212
|
+
|
|
189
213
|
return fetchAPI(
|
|
190
214
|
`${base}/api/checkout/pub/orderForm/${id}/messages/clear`,
|
|
191
215
|
{
|
|
192
216
|
...BASE_INIT,
|
|
217
|
+
headers,
|
|
193
218
|
body: '{}',
|
|
194
|
-
}
|
|
195
|
-
{ storeCookies }
|
|
219
|
+
}
|
|
196
220
|
)
|
|
197
221
|
},
|
|
198
|
-
|
|
199
222
|
updateOrderFormItems: ({
|
|
200
223
|
id,
|
|
201
224
|
orderItems,
|
|
@@ -214,13 +237,18 @@ export const VtexCommerce = (
|
|
|
214
237
|
sc: salesChannel,
|
|
215
238
|
})
|
|
216
239
|
|
|
240
|
+
const headers: HeadersInit = {
|
|
241
|
+
'content-type': 'application/json',
|
|
242
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const cookie = getUpdatedCookie(ctx)
|
|
246
|
+
if (cookie) headers.cookie = cookie
|
|
247
|
+
|
|
217
248
|
return fetchAPI(
|
|
218
249
|
`${base}/api/checkout/pub/orderForm/${id}/items?${params}`,
|
|
219
250
|
{
|
|
220
|
-
headers
|
|
221
|
-
'content-type': 'application/json',
|
|
222
|
-
cookie: ctx.headers?.cookie,
|
|
223
|
-
},
|
|
251
|
+
headers,
|
|
224
252
|
body: JSON.stringify({
|
|
225
253
|
orderItems,
|
|
226
254
|
noSplitItem: !shouldSplitItem,
|
|
@@ -241,14 +269,21 @@ export const VtexCommerce = (
|
|
|
241
269
|
key: string
|
|
242
270
|
value: string
|
|
243
271
|
}): Promise<OrderForm> => {
|
|
272
|
+
const headers: HeadersInit = {
|
|
273
|
+
'content-type': 'application/json',
|
|
274
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const cookie = getUpdatedCookie(ctx)
|
|
278
|
+
if (cookie) headers.cookie = cookie
|
|
279
|
+
|
|
244
280
|
return fetchAPI(
|
|
245
281
|
`${base}/api/checkout/pub/orderForm/${id}/customData/${appId}/${key}`,
|
|
246
282
|
{
|
|
247
|
-
|
|
283
|
+
headers,
|
|
248
284
|
body: JSON.stringify({ value }),
|
|
249
285
|
method: 'PUT',
|
|
250
|
-
}
|
|
251
|
-
{ storeCookies }
|
|
286
|
+
}
|
|
252
287
|
)
|
|
253
288
|
},
|
|
254
289
|
region: async ({
|
|
@@ -270,15 +305,39 @@ export const VtexCommerce = (
|
|
|
270
305
|
)
|
|
271
306
|
|
|
272
307
|
const url = `${base}/api/checkout/pub/regions/?${params.toString()}`
|
|
273
|
-
|
|
308
|
+
const headers: HeadersInit = {
|
|
309
|
+
'content-type': 'application/json',
|
|
310
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const cookie = getUpdatedCookie(ctx)
|
|
314
|
+
if (cookie) headers.cookie = cookie
|
|
315
|
+
|
|
316
|
+
return fetchAPI(
|
|
317
|
+
url,
|
|
318
|
+
{
|
|
319
|
+
headers,
|
|
320
|
+
},
|
|
321
|
+
{ storeCookies }
|
|
322
|
+
)
|
|
274
323
|
},
|
|
275
324
|
address: async ({
|
|
276
325
|
postalCode,
|
|
277
326
|
country,
|
|
278
327
|
}: AddressInput): Promise<Address> => {
|
|
328
|
+
const headers: HeadersInit = {
|
|
329
|
+
'content-type': 'application/json',
|
|
330
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const cookie = getUpdatedCookie(ctx)
|
|
334
|
+
if (cookie) headers.cookie = cookie
|
|
335
|
+
|
|
279
336
|
return fetchAPI(
|
|
280
337
|
`${base}/api/checkout/pub/postal-code/${country}/${postalCode}`,
|
|
281
|
-
|
|
338
|
+
{
|
|
339
|
+
headers,
|
|
340
|
+
},
|
|
282
341
|
{ storeCookies }
|
|
283
342
|
)
|
|
284
343
|
},
|
|
@@ -290,14 +349,20 @@ export const VtexCommerce = (
|
|
|
290
349
|
'items',
|
|
291
350
|
'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol'
|
|
292
351
|
)
|
|
352
|
+
const cookie = getUpdatedCookie(ctx)
|
|
353
|
+
const headers: HeadersInit = cookie
|
|
354
|
+
? {
|
|
355
|
+
'content-type': 'application/json',
|
|
356
|
+
cookie,
|
|
357
|
+
}
|
|
358
|
+
: {
|
|
359
|
+
'content-type': 'application/json',
|
|
360
|
+
}
|
|
293
361
|
return fetchAPI(
|
|
294
362
|
`${base}/api/sessions?${params.toString()}`,
|
|
295
363
|
{
|
|
296
364
|
method: 'POST',
|
|
297
|
-
headers
|
|
298
|
-
'content-type': 'application/json',
|
|
299
|
-
cookie: ctx.headers.cookie,
|
|
300
|
-
},
|
|
365
|
+
headers,
|
|
301
366
|
body: '{}',
|
|
302
367
|
},
|
|
303
368
|
{ storeCookies }
|
|
@@ -57,7 +57,7 @@ export interface Context {
|
|
|
57
57
|
locale: string
|
|
58
58
|
flags: FeatureFlags
|
|
59
59
|
searchArgs?: Omit<SearchArgs, 'type'>
|
|
60
|
-
cookies:
|
|
60
|
+
cookies: Map<string, Record<string, string>>
|
|
61
61
|
}
|
|
62
62
|
headers: Record<string, string>
|
|
63
63
|
}
|
|
@@ -98,7 +98,7 @@ export const getContextFactory =
|
|
|
98
98
|
channel: ChannelMarshal.parse(options.channel),
|
|
99
99
|
flags: options.flags ?? {},
|
|
100
100
|
locale: options.locale,
|
|
101
|
-
cookies: new
|
|
101
|
+
cookies: new Map<string, Record<string, string>>(),
|
|
102
102
|
}
|
|
103
103
|
ctx.clients = getClients(options, ctx)
|
|
104
104
|
ctx.loaders = getLoaders(options, ctx)
|
|
@@ -1,19 +1,100 @@
|
|
|
1
1
|
import type { Context } from '../index'
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const MATCH_FIRST_SET_COOKIE_KEY_VALUE = /^([^=]+)=([^;]*)/
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* This function updates the ctx.storage.cookies, that is used in each request.
|
|
7
|
+
*
|
|
8
|
+
* ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
|
|
9
|
+
* [1] cookie key
|
|
10
|
+
* [2] {
|
|
11
|
+
* value: cookie value,
|
|
12
|
+
* setCookie: setCookie used in browser response
|
|
13
|
+
* }
|
|
14
|
+
*/
|
|
15
|
+
const updatesContextStorageCookies = (setCookieValue: string, ctx: Context) => {
|
|
16
|
+
const matchCookie = setCookieValue.match(MATCH_FIRST_SET_COOKIE_KEY_VALUE)
|
|
17
|
+
if (matchCookie) {
|
|
18
|
+
const cookieKey = matchCookie[1]
|
|
19
|
+
const cookieValue = matchCookie[2]
|
|
20
|
+
|
|
21
|
+
ctx.storage.cookies.set(cookieKey, {
|
|
22
|
+
value: cookieValue,
|
|
23
|
+
setCookie: setCookieValue,
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
}
|
|
7
27
|
|
|
28
|
+
export const setCookie = (headers: Headers, ctx: Context) => {
|
|
8
29
|
headers
|
|
9
30
|
.getSetCookie()
|
|
10
|
-
.forEach((
|
|
11
|
-
ctx
|
|
12
|
-
// Replaces original cookie domain for FastStore API's domain hostname
|
|
13
|
-
cookie.replace(MATCH_DOMAIN_REGEXP, `; domain=${faststoreAPIHostname}`)
|
|
14
|
-
)
|
|
31
|
+
.forEach((setCookieValue) =>
|
|
32
|
+
updatesContextStorageCookies(setCookieValue, ctx)
|
|
15
33
|
)
|
|
16
34
|
}
|
|
17
35
|
|
|
18
36
|
export const getStoreCookie = (ctx: Context) => (headers: Headers) =>
|
|
19
37
|
setCookie(headers, ctx)
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* This function returns a modified copy of the original cookie header (ctx.headers.cookie from the first request)
|
|
41
|
+
* with the cookie values that comes in each request (ctx.storage.cookies).
|
|
42
|
+
* If there is no cookies in storage, the ctx.headers?.cookie is used
|
|
43
|
+
*
|
|
44
|
+
* ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
|
|
45
|
+
* [1] cookie key
|
|
46
|
+
* [2] {
|
|
47
|
+
* value: cookie value,
|
|
48
|
+
* setCookie: setCookie used in browser response
|
|
49
|
+
* }
|
|
50
|
+
*/
|
|
51
|
+
export const getUpdatedCookie = (ctx: Context) => {
|
|
52
|
+
if (!ctx.headers?.cookie) {
|
|
53
|
+
return null
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const contextStorageCookies = Array.from(ctx.storage.cookies.entries())
|
|
57
|
+
|
|
58
|
+
if (contextStorageCookies.length === 0) {
|
|
59
|
+
return ctx.headers.cookie
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return contextStorageCookies.reduce(
|
|
63
|
+
(existingCookies, [storageCookieKey, { value: storageCookieValue }]) =>
|
|
64
|
+
updatesCookieValueByKey(
|
|
65
|
+
existingCookies,
|
|
66
|
+
storageCookieKey,
|
|
67
|
+
storageCookieValue
|
|
68
|
+
),
|
|
69
|
+
ctx.headers.cookie
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* This function updates the cookie value based on its key
|
|
75
|
+
*
|
|
76
|
+
* const existingCookies = 'key=value1; key2=; key3=value3';
|
|
77
|
+
* const storageCookieKey = 'key2';
|
|
78
|
+
* const storageCookieValue = 'value2'
|
|
79
|
+
*
|
|
80
|
+
* updatesCookieValueByKey(existingCookies, storageCookieKey, storageCookieValue) returns 'key=value1; key2=value2; key3=value3';
|
|
81
|
+
*/
|
|
82
|
+
export const updatesCookieValueByKey = (
|
|
83
|
+
existingCookies: string,
|
|
84
|
+
storageCookieKey: string,
|
|
85
|
+
storageCookieValue: string
|
|
86
|
+
) => {
|
|
87
|
+
const MATCH_COOKIE_KEY_VALUE = new RegExp(`(${storageCookieKey})=([^;]*)`)
|
|
88
|
+
const cookieParts = existingCookies.match(MATCH_COOKIE_KEY_VALUE)
|
|
89
|
+
|
|
90
|
+
// replaces original cookie with the one coming from storage
|
|
91
|
+
if (cookieParts) {
|
|
92
|
+
return existingCookies.replace(
|
|
93
|
+
MATCH_FIRST_SET_COOKIE_KEY_VALUE,
|
|
94
|
+
`${cookieParts[1]}=${storageCookieValue}`
|
|
95
|
+
)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// add new storage cookie to the original list of cookies
|
|
99
|
+
return `${existingCookies};${storageCookieKey}=${storageCookieValue}`
|
|
100
|
+
}
|