@faststore/api 2.2.48 → 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 -32
- 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 +69 -2
- 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 -33
- 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 +66 -1
- 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 +102 -37
- package/src/platforms/vtex/index.ts +2 -2
- package/src/platforms/vtex/utils/cookies.ts +93 -1
package/dist/cjs/package.json
CHANGED
|
@@ -12,9 +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
|
-
|
|
16
|
-
|
|
17
|
-
: '';
|
|
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.', '');
|
|
18
17
|
return {
|
|
19
18
|
catalog: {
|
|
20
19
|
salesChannel: (sc) => (0, fetch_1.fetchAPI)(`${base}/api/catalog_system/pub/saleschannel/${sc}`, undefined, { storeCookies }),
|
|
@@ -44,10 +43,18 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
44
43
|
const params = new URLSearchParams({
|
|
45
44
|
sc: salesChannel,
|
|
46
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;
|
|
47
53
|
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForms/simulation?${params.toString()}`, {
|
|
48
54
|
...BASE_INIT,
|
|
55
|
+
headers,
|
|
49
56
|
body: JSON.stringify(args),
|
|
50
|
-
});
|
|
57
|
+
}, { storeCookies });
|
|
51
58
|
},
|
|
52
59
|
shippingData: ({ id, index, deliveryMode, selectedAddresses, }, setDeliveryWindow) => {
|
|
53
60
|
const deliveryWindow = setDeliveryWindow
|
|
@@ -66,14 +73,18 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
66
73
|
selectedAddresses: selectedAddresses,
|
|
67
74
|
clearAddressIfPostalCodeNotFound: incrementAddress,
|
|
68
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;
|
|
69
83
|
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`, {
|
|
70
84
|
...BASE_INIT,
|
|
85
|
+
headers,
|
|
71
86
|
body: JSON.stringify(mappedBody),
|
|
72
|
-
|
|
73
|
-
'content-type': 'application/json',
|
|
74
|
-
cookie: ctx.headers.cookie,
|
|
75
|
-
},
|
|
76
|
-
});
|
|
87
|
+
}, { storeCookies });
|
|
77
88
|
},
|
|
78
89
|
orderForm: ({ id, refreshOutdatedData = true, channel = ctx.storage.channel, }) => {
|
|
79
90
|
const { salesChannel } = channel;
|
|
@@ -81,21 +92,29 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
81
92
|
refreshOutdatedData: refreshOutdatedData.toString(),
|
|
82
93
|
sc: salesChannel,
|
|
83
94
|
});
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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 });
|
|
95
106
|
},
|
|
96
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;
|
|
97
115
|
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}/messages/clear`, {
|
|
98
116
|
...BASE_INIT,
|
|
117
|
+
headers,
|
|
99
118
|
body: '{}',
|
|
100
119
|
});
|
|
101
120
|
},
|
|
@@ -104,12 +123,15 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
104
123
|
allowOutdatedData,
|
|
105
124
|
sc: salesChannel,
|
|
106
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;
|
|
107
133
|
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}/items?${params}`, {
|
|
108
|
-
headers
|
|
109
|
-
'content-type': 'application/json',
|
|
110
|
-
cookie: ctx.headers?.cookie,
|
|
111
|
-
'X-FORWARDED-HOST': forwardedHost,
|
|
112
|
-
},
|
|
134
|
+
headers,
|
|
113
135
|
body: JSON.stringify({
|
|
114
136
|
orderItems,
|
|
115
137
|
noSplitItem: !shouldSplitItem,
|
|
@@ -118,8 +140,15 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
118
140
|
}, { storeCookies });
|
|
119
141
|
},
|
|
120
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;
|
|
121
150
|
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}/customData/${appId}/${key}`, {
|
|
122
|
-
|
|
151
|
+
headers,
|
|
123
152
|
body: JSON.stringify({ value }),
|
|
124
153
|
method: 'PUT',
|
|
125
154
|
});
|
|
@@ -133,21 +162,45 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
133
162
|
? params.append('postalCode', postalCode)
|
|
134
163
|
: params.append('geoCoordinates', `${geoCoordinates?.longitude};${geoCoordinates?.latitude}`);
|
|
135
164
|
const url = `${base}/api/checkout/pub/regions/?${params.toString()}`;
|
|
136
|
-
|
|
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 });
|
|
137
175
|
},
|
|
138
176
|
address: async ({ postalCode, country, }) => {
|
|
139
|
-
|
|
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 });
|
|
140
187
|
},
|
|
141
188
|
},
|
|
142
189
|
session: (search) => {
|
|
143
190
|
const params = new URLSearchParams(search);
|
|
144
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
|
+
};
|
|
145
201
|
return (0, fetch_1.fetchAPI)(`${base}/api/sessions?${params.toString()}`, {
|
|
146
202
|
method: 'POST',
|
|
147
|
-
headers
|
|
148
|
-
'content-type': 'application/json',
|
|
149
|
-
cookie: ctx.headers.cookie,
|
|
150
|
-
},
|
|
203
|
+
headers,
|
|
151
204
|
body: '{}',
|
|
152
205
|
}, { storeCookies });
|
|
153
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;gB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ory = (options) => (ctx) => {
|
|
|
48
48
|
channel: channel_1.default.parse(options.channel),
|
|
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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C,OAAgB,EAAE,EAAE,CACrB,CAAC,GAAQ,EAAW,EAAE;IACpB,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,10 +1,77 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getStoreCookie = exports.setCookie = void 0;
|
|
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
|
+
};
|
|
4
26
|
const setCookie = (headers, ctx) => {
|
|
5
|
-
headers
|
|
27
|
+
headers
|
|
28
|
+
.getSetCookie()
|
|
29
|
+
.forEach((setCookieValue) => updatesContextStorageCookies(setCookieValue, ctx));
|
|
6
30
|
};
|
|
7
31
|
exports.setCookie = setCookie;
|
|
8
32
|
const getStoreCookie = (ctx) => (headers) => (0, exports.setCookie)(headers, ctx);
|
|
9
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;
|
|
10
77
|
//# sourceMappingURL=cookies.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookies.js","sourceRoot":"","sources":["../../../../../../src/platforms/vtex/utils/cookies.ts"],"names":[],"mappings":";;;
|
|
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,GAAG,CAAC,SAAS,EAAE;YACjC,KAAK,EAAE,WAAW;YAClB,SAAS,EAAE,cAAc;SAC1B,CAAC,CAAA;IACJ,CAAC;AACH,CAAC,CAAA;AAEM,MAAM,SAAS,GAAG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C1E,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,9 +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
|
-
|
|
13
|
-
|
|
14
|
-
: '';
|
|
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.', '');
|
|
15
14
|
return {
|
|
16
15
|
catalog: {
|
|
17
16
|
salesChannel: (sc) => fetchAPI(`${base}/api/catalog_system/pub/saleschannel/${sc}`, undefined, { storeCookies }),
|
|
@@ -41,10 +40,18 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
41
40
|
const params = new URLSearchParams({
|
|
42
41
|
sc: salesChannel,
|
|
43
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;
|
|
44
50
|
return fetchAPI(`${base}/api/checkout/pub/orderForms/simulation?${params.toString()}`, {
|
|
45
51
|
...BASE_INIT,
|
|
52
|
+
headers,
|
|
46
53
|
body: JSON.stringify(args),
|
|
47
|
-
});
|
|
54
|
+
}, { storeCookies });
|
|
48
55
|
},
|
|
49
56
|
shippingData: ({ id, index, deliveryMode, selectedAddresses, }, setDeliveryWindow) => {
|
|
50
57
|
const deliveryWindow = setDeliveryWindow
|
|
@@ -63,14 +70,18 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
63
70
|
selectedAddresses: selectedAddresses,
|
|
64
71
|
clearAddressIfPostalCodeNotFound: incrementAddress,
|
|
65
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;
|
|
66
80
|
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`, {
|
|
67
81
|
...BASE_INIT,
|
|
82
|
+
headers,
|
|
68
83
|
body: JSON.stringify(mappedBody),
|
|
69
|
-
|
|
70
|
-
'content-type': 'application/json',
|
|
71
|
-
cookie: ctx.headers.cookie,
|
|
72
|
-
},
|
|
73
|
-
});
|
|
84
|
+
}, { storeCookies });
|
|
74
85
|
},
|
|
75
86
|
orderForm: ({ id, refreshOutdatedData = true, channel = ctx.storage.channel, }) => {
|
|
76
87
|
const { salesChannel } = channel;
|
|
@@ -78,21 +89,29 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
78
89
|
refreshOutdatedData: refreshOutdatedData.toString(),
|
|
79
90
|
sc: salesChannel,
|
|
80
91
|
});
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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 });
|
|
92
103
|
},
|
|
93
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;
|
|
94
112
|
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/messages/clear`, {
|
|
95
113
|
...BASE_INIT,
|
|
114
|
+
headers,
|
|
96
115
|
body: '{}',
|
|
97
116
|
});
|
|
98
117
|
},
|
|
@@ -101,12 +120,15 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
101
120
|
allowOutdatedData,
|
|
102
121
|
sc: salesChannel,
|
|
103
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;
|
|
104
130
|
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/items?${params}`, {
|
|
105
|
-
headers
|
|
106
|
-
'content-type': 'application/json',
|
|
107
|
-
cookie: ctx.headers?.cookie,
|
|
108
|
-
'X-FORWARDED-HOST': forwardedHost,
|
|
109
|
-
},
|
|
131
|
+
headers,
|
|
110
132
|
body: JSON.stringify({
|
|
111
133
|
orderItems,
|
|
112
134
|
noSplitItem: !shouldSplitItem,
|
|
@@ -115,8 +137,15 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
115
137
|
}, { storeCookies });
|
|
116
138
|
},
|
|
117
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;
|
|
118
147
|
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/customData/${appId}/${key}`, {
|
|
119
|
-
|
|
148
|
+
headers,
|
|
120
149
|
body: JSON.stringify({ value }),
|
|
121
150
|
method: 'PUT',
|
|
122
151
|
});
|
|
@@ -130,21 +159,45 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
130
159
|
? params.append('postalCode', postalCode)
|
|
131
160
|
: params.append('geoCoordinates', `${geoCoordinates?.longitude};${geoCoordinates?.latitude}`);
|
|
132
161
|
const url = `${base}/api/checkout/pub/regions/?${params.toString()}`;
|
|
133
|
-
|
|
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 });
|
|
134
172
|
},
|
|
135
173
|
address: async ({ postalCode, country, }) => {
|
|
136
|
-
|
|
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 });
|
|
137
184
|
},
|
|
138
185
|
},
|
|
139
186
|
session: (search) => {
|
|
140
187
|
const params = new URLSearchParams(search);
|
|
141
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
|
+
};
|
|
142
198
|
return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
|
|
143
199
|
method: 'POST',
|
|
144
|
-
headers
|
|
145
|
-
'content-type': 'application/json',
|
|
146
|
-
cookie: ctx.headers.cookie,
|
|
147
|
-
},
|
|
200
|
+
headers,
|
|
148
201
|
body: '{}',
|
|
149
202
|
}, { storeCookies });
|
|
150
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","sourceRoot":"","sources":["../../../../../../../src/platforms/vtex/clients/commerce/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ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,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,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAA;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB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B,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,5 +1,70 @@
|
|
|
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
|
+
};
|
|
1
23
|
export const setCookie = (headers, ctx) => {
|
|
2
|
-
headers
|
|
24
|
+
headers
|
|
25
|
+
.getSetCookie()
|
|
26
|
+
.forEach((setCookieValue) => updatesContextStorageCookies(setCookieValue, ctx));
|
|
3
27
|
};
|
|
4
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
|
+
};
|
|
5
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;QAChC,MAAM,WAAW,GAAG,WAAW,CAAC,CAAC,CAAC,CAAA;QAElC,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,SAAS,EAAE;YACjC,KAAK,EAAE,WAAW;YAClB,SAAS,EAAE,cAAc;SAC1B,CAAC,CAAA;IACJ,CAAC;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,OAAgB,EAAE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DAA4D;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,9 +36,8 @@ export const VtexCommerce = (
|
|
|
36
36
|
) => {
|
|
37
37
|
const base = `https://${account}.${environment}.com.br`
|
|
38
38
|
const storeCookies = getStoreCookie(ctx)
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
: ''
|
|
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.', '')
|
|
42
41
|
|
|
43
42
|
return {
|
|
44
43
|
catalog: {
|
|
@@ -102,15 +101,24 @@ export const VtexCommerce = (
|
|
|
102
101
|
sc: salesChannel,
|
|
103
102
|
})
|
|
104
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
|
+
|
|
105
112
|
return fetchAPI(
|
|
106
113
|
`${base}/api/checkout/pub/orderForms/simulation?${params.toString()}`,
|
|
107
114
|
{
|
|
108
115
|
...BASE_INIT,
|
|
116
|
+
headers,
|
|
109
117
|
body: JSON.stringify(args),
|
|
110
|
-
}
|
|
118
|
+
},
|
|
119
|
+
{ storeCookies }
|
|
111
120
|
)
|
|
112
121
|
},
|
|
113
|
-
|
|
114
122
|
shippingData: (
|
|
115
123
|
{
|
|
116
124
|
id,
|
|
@@ -142,19 +150,25 @@ export const VtexCommerce = (
|
|
|
142
150
|
selectedAddresses: selectedAddresses,
|
|
143
151
|
clearAddressIfPostalCodeNotFound: incrementAddress,
|
|
144
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
|
+
|
|
145
162
|
return fetchAPI(
|
|
146
163
|
`${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`,
|
|
147
164
|
{
|
|
148
165
|
...BASE_INIT,
|
|
166
|
+
headers,
|
|
149
167
|
body: JSON.stringify(mappedBody),
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
cookie: ctx.headers.cookie,
|
|
153
|
-
},
|
|
154
|
-
}
|
|
168
|
+
},
|
|
169
|
+
{ storeCookies }
|
|
155
170
|
)
|
|
156
171
|
},
|
|
157
|
-
|
|
158
172
|
orderForm: ({
|
|
159
173
|
id,
|
|
160
174
|
refreshOutdatedData = true,
|
|
@@ -169,34 +183,42 @@ export const VtexCommerce = (
|
|
|
169
183
|
refreshOutdatedData: refreshOutdatedData.toString(),
|
|
170
184
|
sc: salesChannel,
|
|
171
185
|
})
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
: 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
|
|
182
194
|
|
|
183
195
|
return fetchAPI(
|
|
184
196
|
`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`,
|
|
185
|
-
|
|
197
|
+
{
|
|
198
|
+
...BASE_INIT,
|
|
199
|
+
headers,
|
|
200
|
+
},
|
|
186
201
|
{ storeCookies }
|
|
187
202
|
)
|
|
188
203
|
},
|
|
189
|
-
|
|
190
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
|
+
|
|
191
213
|
return fetchAPI(
|
|
192
214
|
`${base}/api/checkout/pub/orderForm/${id}/messages/clear`,
|
|
193
215
|
{
|
|
194
216
|
...BASE_INIT,
|
|
217
|
+
headers,
|
|
195
218
|
body: '{}',
|
|
196
219
|
}
|
|
197
220
|
)
|
|
198
221
|
},
|
|
199
|
-
|
|
200
222
|
updateOrderFormItems: ({
|
|
201
223
|
id,
|
|
202
224
|
orderItems,
|
|
@@ -215,14 +237,18 @@ export const VtexCommerce = (
|
|
|
215
237
|
sc: salesChannel,
|
|
216
238
|
})
|
|
217
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
|
+
|
|
218
248
|
return fetchAPI(
|
|
219
249
|
`${base}/api/checkout/pub/orderForm/${id}/items?${params}`,
|
|
220
250
|
{
|
|
221
|
-
headers
|
|
222
|
-
'content-type': 'application/json',
|
|
223
|
-
cookie: ctx.headers?.cookie,
|
|
224
|
-
'X-FORWARDED-HOST': forwardedHost,
|
|
225
|
-
},
|
|
251
|
+
headers,
|
|
226
252
|
body: JSON.stringify({
|
|
227
253
|
orderItems,
|
|
228
254
|
noSplitItem: !shouldSplitItem,
|
|
@@ -243,10 +269,18 @@ export const VtexCommerce = (
|
|
|
243
269
|
key: string
|
|
244
270
|
value: string
|
|
245
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
|
+
|
|
246
280
|
return fetchAPI(
|
|
247
281
|
`${base}/api/checkout/pub/orderForm/${id}/customData/${appId}/${key}`,
|
|
248
282
|
{
|
|
249
|
-
|
|
283
|
+
headers,
|
|
250
284
|
body: JSON.stringify({ value }),
|
|
251
285
|
method: 'PUT',
|
|
252
286
|
}
|
|
@@ -271,15 +305,40 @@ export const VtexCommerce = (
|
|
|
271
305
|
)
|
|
272
306
|
|
|
273
307
|
const url = `${base}/api/checkout/pub/regions/?${params.toString()}`
|
|
274
|
-
|
|
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
|
+
)
|
|
275
323
|
},
|
|
276
324
|
address: async ({
|
|
277
325
|
postalCode,
|
|
278
326
|
country,
|
|
279
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
|
+
|
|
280
336
|
return fetchAPI(
|
|
281
337
|
`${base}/api/checkout/pub/postal-code/${country}/${postalCode}`,
|
|
282
|
-
|
|
338
|
+
{
|
|
339
|
+
headers,
|
|
340
|
+
},
|
|
341
|
+
{ storeCookies }
|
|
283
342
|
)
|
|
284
343
|
},
|
|
285
344
|
},
|
|
@@ -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,8 +1,100 @@
|
|
|
1
1
|
import type { Context } from '../index'
|
|
2
2
|
|
|
3
|
+
const MATCH_FIRST_SET_COOKIE_KEY_VALUE = /^([^=]+)=([^;]*)/
|
|
4
|
+
|
|
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
|
+
}
|
|
27
|
+
|
|
3
28
|
export const setCookie = (headers: Headers, ctx: Context) => {
|
|
4
|
-
headers
|
|
29
|
+
headers
|
|
30
|
+
.getSetCookie()
|
|
31
|
+
.forEach((setCookieValue) =>
|
|
32
|
+
updatesContextStorageCookies(setCookieValue, ctx)
|
|
33
|
+
)
|
|
5
34
|
}
|
|
6
35
|
|
|
7
36
|
export const getStoreCookie = (ctx: Context) => (headers: Headers) =>
|
|
8
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
|
+
}
|