@faststore/api 2.2.48 → 2.2.50
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 +58 -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 +43 -2
- package/dist/cjs/src/platforms/vtex/utils/cookies.js +83 -4
- 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 +59 -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 +43 -2
- package/dist/esm/src/platforms/vtex/utils/cookies.js +78 -3
- 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 +79 -37
- package/src/platforms/vtex/index.ts +2 -2
- package/src/platforms/vtex/utils/cookies.ts +122 -4
package/dist/cjs/package.json
CHANGED
|
@@ -12,9 +12,11 @@ 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
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const withCookie = (0, cookies_1.getWithCookie)(ctx);
|
|
16
|
+
// replacing www. only for testing while www.vtexfaststore.com is configured with www
|
|
17
|
+
const forwardedHost = (new Headers(ctx.headers).get('x-forwarded-host') ??
|
|
18
|
+
ctx.headers?.host ??
|
|
19
|
+
'').replace('www.', '');
|
|
18
20
|
return {
|
|
19
21
|
catalog: {
|
|
20
22
|
salesChannel: (sc) => (0, fetch_1.fetchAPI)(`${base}/api/catalog_system/pub/saleschannel/${sc}`, undefined, { storeCookies }),
|
|
@@ -44,10 +46,15 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
44
46
|
const params = new URLSearchParams({
|
|
45
47
|
sc: salesChannel,
|
|
46
48
|
});
|
|
49
|
+
const headers = withCookie({
|
|
50
|
+
'content-type': 'application/json',
|
|
51
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
52
|
+
});
|
|
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,15 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
66
73
|
selectedAddresses: selectedAddresses,
|
|
67
74
|
clearAddressIfPostalCodeNotFound: incrementAddress,
|
|
68
75
|
};
|
|
76
|
+
const headers = withCookie({
|
|
77
|
+
'content-type': 'application/json',
|
|
78
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
79
|
+
});
|
|
69
80
|
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`, {
|
|
70
81
|
...BASE_INIT,
|
|
82
|
+
headers,
|
|
71
83
|
body: JSON.stringify(mappedBody),
|
|
72
|
-
|
|
73
|
-
'content-type': 'application/json',
|
|
74
|
-
cookie: ctx.headers.cookie,
|
|
75
|
-
},
|
|
76
|
-
});
|
|
84
|
+
}, { storeCookies });
|
|
77
85
|
},
|
|
78
86
|
orderForm: ({ id, refreshOutdatedData = true, channel = ctx.storage.channel, }) => {
|
|
79
87
|
const { salesChannel } = channel;
|
|
@@ -81,21 +89,23 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
81
89
|
refreshOutdatedData: refreshOutdatedData.toString(),
|
|
82
90
|
sc: salesChannel,
|
|
83
91
|
});
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
: BASE_INIT;
|
|
94
|
-
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, requestInit, { storeCookies });
|
|
92
|
+
const headers = withCookie({
|
|
93
|
+
'content-type': 'application/json',
|
|
94
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
95
|
+
});
|
|
96
|
+
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, {
|
|
97
|
+
...BASE_INIT,
|
|
98
|
+
headers,
|
|
99
|
+
}, { storeCookies });
|
|
95
100
|
},
|
|
96
101
|
clearOrderFormMessages: ({ id }) => {
|
|
102
|
+
const headers = withCookie({
|
|
103
|
+
'content-type': 'application/json',
|
|
104
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
105
|
+
});
|
|
97
106
|
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}/messages/clear`, {
|
|
98
107
|
...BASE_INIT,
|
|
108
|
+
headers,
|
|
99
109
|
body: '{}',
|
|
100
110
|
});
|
|
101
111
|
},
|
|
@@ -104,12 +114,12 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
104
114
|
allowOutdatedData,
|
|
105
115
|
sc: salesChannel,
|
|
106
116
|
});
|
|
117
|
+
const headers = withCookie({
|
|
118
|
+
'content-type': 'application/json',
|
|
119
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
120
|
+
});
|
|
107
121
|
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
|
-
},
|
|
122
|
+
headers,
|
|
113
123
|
body: JSON.stringify({
|
|
114
124
|
orderItems,
|
|
115
125
|
noSplitItem: !shouldSplitItem,
|
|
@@ -118,8 +128,12 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
118
128
|
}, { storeCookies });
|
|
119
129
|
},
|
|
120
130
|
setCustomData: ({ id, appId, key, value, }) => {
|
|
131
|
+
const headers = withCookie({
|
|
132
|
+
'content-type': 'application/json',
|
|
133
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
134
|
+
});
|
|
121
135
|
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/orderForm/${id}/customData/${appId}/${key}`, {
|
|
122
|
-
|
|
136
|
+
headers,
|
|
123
137
|
body: JSON.stringify({ value }),
|
|
124
138
|
method: 'PUT',
|
|
125
139
|
});
|
|
@@ -133,21 +147,33 @@ const VtexCommerce = ({ account, environment, incrementAddress }, ctx) => {
|
|
|
133
147
|
? params.append('postalCode', postalCode)
|
|
134
148
|
: params.append('geoCoordinates', `${geoCoordinates?.longitude};${geoCoordinates?.latitude}`);
|
|
135
149
|
const url = `${base}/api/checkout/pub/regions/?${params.toString()}`;
|
|
136
|
-
|
|
150
|
+
const headers = withCookie({
|
|
151
|
+
'content-type': 'application/json',
|
|
152
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
153
|
+
});
|
|
154
|
+
return (0, fetch_1.fetchAPI)(url, {
|
|
155
|
+
headers,
|
|
156
|
+
}, { storeCookies });
|
|
137
157
|
},
|
|
138
158
|
address: async ({ postalCode, country, }) => {
|
|
139
|
-
|
|
159
|
+
const headers = withCookie({
|
|
160
|
+
'content-type': 'application/json',
|
|
161
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
162
|
+
});
|
|
163
|
+
return (0, fetch_1.fetchAPI)(`${base}/api/checkout/pub/postal-code/${country}/${postalCode}`, {
|
|
164
|
+
headers,
|
|
165
|
+
}, { storeCookies });
|
|
140
166
|
},
|
|
141
167
|
},
|
|
142
168
|
session: (search) => {
|
|
143
169
|
const params = new URLSearchParams(search);
|
|
144
170
|
params.set('items', 'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol');
|
|
171
|
+
const headers = withCookie({
|
|
172
|
+
'content-type': 'application/json',
|
|
173
|
+
});
|
|
145
174
|
return (0, fetch_1.fetchAPI)(`${base}/api/sessions?${params.toString()}`, {
|
|
146
175
|
method: 'POST',
|
|
147
|
-
headers
|
|
148
|
-
'content-type': 'application/json',
|
|
149
|
-
cookie: ctx.headers.cookie,
|
|
150
|
-
},
|
|
176
|
+
headers,
|
|
151
177
|
body: '{}',
|
|
152
178
|
}, { storeCookies });
|
|
153
179
|
},
|
|
@@ -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,iDAAmE;AAInE,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;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Ac,EAAoB,EAAE;YAC5C,MAAM,MAAM,GAAG,IAAI,eAAe,CAAC,MAAM,CAAC,CAAA;YAE1C,MAAM,CAAC,GAAG,CACR,OAAO,EACP,uJAAuJ,CACxJ,CAAA;YAED,MAAM,OAAO,GAAgB,UAAU,CAAC;gBACtC,cAAc,EAAE,kBAAkB;aACnC,CAAC,CAAA;YAEF,OAAO,IAAA,gBAAQ,EACb,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,IAAA,gBAAQ,EACb,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;AA1UY,QAAA,YAAY,gBA0UxB"}
|
|
@@ -48,7 +48,7 @@ const getContextFactory = (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,iEA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O,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,44 @@
|
|
|
1
1
|
import type { Context } from '../index';
|
|
2
|
-
export
|
|
3
|
-
|
|
2
|
+
export interface ContextForCookies {
|
|
3
|
+
headers: Context['headers'];
|
|
4
|
+
storage: Pick<Context['storage'], 'cookies'>;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* This function updates the ctx.storage.cookies, that is used in each request.
|
|
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 updatesContextStorageCookies: (ctx: Pick<Context, 'storage'>, setCookieValue: string) => void;
|
|
17
|
+
export declare const setCookie: (ctx: Pick<Context, 'storage'>, headers: Headers) => void;
|
|
18
|
+
export declare const getStoreCookie: (ctx: Pick<Context, 'storage'>) => (headers: Headers) => void;
|
|
19
|
+
/**
|
|
20
|
+
* This function returns a modified copy of the original cookie header (ctx.headers.cookie from the first request)
|
|
21
|
+
* with the cookie values that comes in each request (ctx.storage.cookies).
|
|
22
|
+
* If there is no cookies in storage, the ctx.headers?.cookie is used
|
|
23
|
+
*
|
|
24
|
+
* ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
|
|
25
|
+
* [1] cookie key
|
|
26
|
+
* [2] {
|
|
27
|
+
* value: cookie value,
|
|
28
|
+
* setCookie: setCookie used in browser response
|
|
29
|
+
* }
|
|
30
|
+
*/
|
|
31
|
+
export declare const getUpdatedCookie: (ctx: ContextForCookies) => string | null;
|
|
32
|
+
export declare const getWithCookie: (ctx: ContextForCookies) => <T extends Record<string, any>>(headers: T) => T & {
|
|
33
|
+
cookie?: string | undefined;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* This function updates the cookie value based on its key
|
|
37
|
+
*
|
|
38
|
+
* const existingCookies = 'key=value1; key2=; key3=value3';
|
|
39
|
+
* const storageCookieKey = 'key2';
|
|
40
|
+
* const storageCookieValue = 'value2'
|
|
41
|
+
*
|
|
42
|
+
* updatesCookieValueByKey(existingCookies, storageCookieKey, storageCookieValue) returns 'key=value1; key2=value2; key3=value3';
|
|
43
|
+
*/
|
|
44
|
+
export declare const updatesCookieValueByKey: (existingCookies: string, storageCookieKey: string, storageCookieValue: string) => string;
|
|
@@ -1,10 +1,89 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getStoreCookie = exports.setCookie = void 0;
|
|
4
|
-
const
|
|
5
|
-
|
|
3
|
+
exports.updatesCookieValueByKey = exports.getWithCookie = exports.getUpdatedCookie = exports.getStoreCookie = exports.setCookie = exports.updatesContextStorageCookies = 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 = (ctx, setCookieValue) => {
|
|
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
|
+
};
|
|
26
|
+
exports.updatesContextStorageCookies = updatesContextStorageCookies;
|
|
27
|
+
const setCookie = (ctx, headers) => {
|
|
28
|
+
headers
|
|
29
|
+
.getSetCookie()
|
|
30
|
+
.forEach((setCookieValue) => (0, exports.updatesContextStorageCookies)(ctx, setCookieValue));
|
|
6
31
|
};
|
|
7
32
|
exports.setCookie = setCookie;
|
|
8
|
-
const getStoreCookie = (ctx) => (headers) => (0, exports.setCookie)(
|
|
33
|
+
const getStoreCookie = (ctx) => (headers) => (0, exports.setCookie)(ctx, headers);
|
|
9
34
|
exports.getStoreCookie = getStoreCookie;
|
|
35
|
+
/**
|
|
36
|
+
* This function returns a modified copy of the original cookie header (ctx.headers.cookie from the first request)
|
|
37
|
+
* with the cookie values that comes in each request (ctx.storage.cookies).
|
|
38
|
+
* If there is no cookies in storage, the ctx.headers?.cookie is used
|
|
39
|
+
*
|
|
40
|
+
* ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
|
|
41
|
+
* [1] cookie key
|
|
42
|
+
* [2] {
|
|
43
|
+
* value: cookie value,
|
|
44
|
+
* setCookie: setCookie used in browser response
|
|
45
|
+
* }
|
|
46
|
+
*/
|
|
47
|
+
const getUpdatedCookie = (ctx) => {
|
|
48
|
+
if (!ctx.headers?.cookie) {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
const contextStorageCookies = Array.from(ctx.storage.cookies.entries());
|
|
52
|
+
if (contextStorageCookies.length === 0) {
|
|
53
|
+
return ctx.headers.cookie;
|
|
54
|
+
}
|
|
55
|
+
return contextStorageCookies.reduce((existingCookies, [storageCookieKey, { value: storageCookieValue }]) => (0, exports.updatesCookieValueByKey)(existingCookies, storageCookieKey, storageCookieValue), ctx.headers.cookie);
|
|
56
|
+
};
|
|
57
|
+
exports.getUpdatedCookie = getUpdatedCookie;
|
|
58
|
+
const getWithCookie = (ctx) => function withCookie(headers) {
|
|
59
|
+
const updatedCookie = (0, exports.getUpdatedCookie)(ctx);
|
|
60
|
+
if (!updatedCookie) {
|
|
61
|
+
return headers;
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
...headers,
|
|
65
|
+
cookie: updatedCookie,
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
exports.getWithCookie = getWithCookie;
|
|
69
|
+
/**
|
|
70
|
+
* This function updates the cookie value based on its key
|
|
71
|
+
*
|
|
72
|
+
* const existingCookies = 'key=value1; key2=; key3=value3';
|
|
73
|
+
* const storageCookieKey = 'key2';
|
|
74
|
+
* const storageCookieValue = 'value2'
|
|
75
|
+
*
|
|
76
|
+
* updatesCookieValueByKey(existingCookies, storageCookieKey, storageCookieValue) returns 'key=value1; key2=value2; key3=value3';
|
|
77
|
+
*/
|
|
78
|
+
const updatesCookieValueByKey = (existingCookies, storageCookieKey, storageCookieValue) => {
|
|
79
|
+
const MATCH_COOKIE_KEY_VALUE = new RegExp(`(${storageCookieKey})=([^;]*)`);
|
|
80
|
+
const cookieParts = existingCookies.match(MATCH_COOKIE_KEY_VALUE);
|
|
81
|
+
// replaces original cookie with the one coming from storage
|
|
82
|
+
if (cookieParts) {
|
|
83
|
+
return existingCookies.replace(cookieParts[0], `${cookieParts[1]}=${storageCookieValue}`);
|
|
84
|
+
}
|
|
85
|
+
// add new storage cookie to the original list of cookies
|
|
86
|
+
return `${existingCookies};${storageCookieKey}=${storageCookieValue}`;
|
|
87
|
+
};
|
|
88
|
+
exports.updatesCookieValueByKey = updatesCookieValueByKey;
|
|
10
89
|
//# 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":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I,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 { getStoreCookie, getWithCookie } from '../../utils/cookies';
|
|
3
3
|
const BASE_INIT = {
|
|
4
4
|
method: 'POST',
|
|
5
5
|
headers: {
|
|
@@ -9,9 +9,11 @@ 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
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
const withCookie = getWithCookie(ctx);
|
|
13
|
+
// replacing www. only for testing while www.vtexfaststore.com is configured with www
|
|
14
|
+
const forwardedHost = (new Headers(ctx.headers).get('x-forwarded-host') ??
|
|
15
|
+
ctx.headers?.host ??
|
|
16
|
+
'').replace('www.', '');
|
|
15
17
|
return {
|
|
16
18
|
catalog: {
|
|
17
19
|
salesChannel: (sc) => fetchAPI(`${base}/api/catalog_system/pub/saleschannel/${sc}`, undefined, { storeCookies }),
|
|
@@ -41,10 +43,15 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
41
43
|
const params = new URLSearchParams({
|
|
42
44
|
sc: salesChannel,
|
|
43
45
|
});
|
|
46
|
+
const headers = withCookie({
|
|
47
|
+
'content-type': 'application/json',
|
|
48
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
49
|
+
});
|
|
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,15 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
63
70
|
selectedAddresses: selectedAddresses,
|
|
64
71
|
clearAddressIfPostalCodeNotFound: incrementAddress,
|
|
65
72
|
};
|
|
73
|
+
const headers = withCookie({
|
|
74
|
+
'content-type': 'application/json',
|
|
75
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
76
|
+
});
|
|
66
77
|
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`, {
|
|
67
78
|
...BASE_INIT,
|
|
79
|
+
headers,
|
|
68
80
|
body: JSON.stringify(mappedBody),
|
|
69
|
-
|
|
70
|
-
'content-type': 'application/json',
|
|
71
|
-
cookie: ctx.headers.cookie,
|
|
72
|
-
},
|
|
73
|
-
});
|
|
81
|
+
}, { storeCookies });
|
|
74
82
|
},
|
|
75
83
|
orderForm: ({ id, refreshOutdatedData = true, channel = ctx.storage.channel, }) => {
|
|
76
84
|
const { salesChannel } = channel;
|
|
@@ -78,21 +86,23 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
78
86
|
refreshOutdatedData: refreshOutdatedData.toString(),
|
|
79
87
|
sc: salesChannel,
|
|
80
88
|
});
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
: BASE_INIT;
|
|
91
|
-
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, requestInit, { storeCookies });
|
|
89
|
+
const headers = withCookie({
|
|
90
|
+
'content-type': 'application/json',
|
|
91
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
92
|
+
});
|
|
93
|
+
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, {
|
|
94
|
+
...BASE_INIT,
|
|
95
|
+
headers,
|
|
96
|
+
}, { storeCookies });
|
|
92
97
|
},
|
|
93
98
|
clearOrderFormMessages: ({ id }) => {
|
|
99
|
+
const headers = withCookie({
|
|
100
|
+
'content-type': 'application/json',
|
|
101
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
102
|
+
});
|
|
94
103
|
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/messages/clear`, {
|
|
95
104
|
...BASE_INIT,
|
|
105
|
+
headers,
|
|
96
106
|
body: '{}',
|
|
97
107
|
});
|
|
98
108
|
},
|
|
@@ -101,12 +111,12 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
101
111
|
allowOutdatedData,
|
|
102
112
|
sc: salesChannel,
|
|
103
113
|
});
|
|
114
|
+
const headers = withCookie({
|
|
115
|
+
'content-type': 'application/json',
|
|
116
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
117
|
+
});
|
|
104
118
|
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
|
-
},
|
|
119
|
+
headers,
|
|
110
120
|
body: JSON.stringify({
|
|
111
121
|
orderItems,
|
|
112
122
|
noSplitItem: !shouldSplitItem,
|
|
@@ -115,8 +125,12 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
115
125
|
}, { storeCookies });
|
|
116
126
|
},
|
|
117
127
|
setCustomData: ({ id, appId, key, value, }) => {
|
|
128
|
+
const headers = withCookie({
|
|
129
|
+
'content-type': 'application/json',
|
|
130
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
131
|
+
});
|
|
118
132
|
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/customData/${appId}/${key}`, {
|
|
119
|
-
|
|
133
|
+
headers,
|
|
120
134
|
body: JSON.stringify({ value }),
|
|
121
135
|
method: 'PUT',
|
|
122
136
|
});
|
|
@@ -130,21 +144,33 @@ export const VtexCommerce = ({ account, environment, incrementAddress }, ctx) =>
|
|
|
130
144
|
? params.append('postalCode', postalCode)
|
|
131
145
|
: params.append('geoCoordinates', `${geoCoordinates?.longitude};${geoCoordinates?.latitude}`);
|
|
132
146
|
const url = `${base}/api/checkout/pub/regions/?${params.toString()}`;
|
|
133
|
-
|
|
147
|
+
const headers = withCookie({
|
|
148
|
+
'content-type': 'application/json',
|
|
149
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
150
|
+
});
|
|
151
|
+
return fetchAPI(url, {
|
|
152
|
+
headers,
|
|
153
|
+
}, { storeCookies });
|
|
134
154
|
},
|
|
135
155
|
address: async ({ postalCode, country, }) => {
|
|
136
|
-
|
|
156
|
+
const headers = withCookie({
|
|
157
|
+
'content-type': 'application/json',
|
|
158
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
159
|
+
});
|
|
160
|
+
return fetchAPI(`${base}/api/checkout/pub/postal-code/${country}/${postalCode}`, {
|
|
161
|
+
headers,
|
|
162
|
+
}, { storeCookies });
|
|
137
163
|
},
|
|
138
164
|
},
|
|
139
165
|
session: (search) => {
|
|
140
166
|
const params = new URLSearchParams(search);
|
|
141
167
|
params.set('items', 'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol');
|
|
168
|
+
const headers = withCookie({
|
|
169
|
+
'content-type': 'application/json',
|
|
170
|
+
});
|
|
142
171
|
return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
|
|
143
172
|
method: 'POST',
|
|
144
|
-
headers
|
|
145
|
-
'content-type': 'application/json',
|
|
146
|
-
cookie: ctx.headers.cookie,
|
|
147
|
-
},
|
|
173
|
+
headers,
|
|
148
174
|
body: '{}',
|
|
149
175
|
}, { storeCookies });
|
|
150
176
|
},
|
|
@@ -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,EAAE,MAAM,UAAU,CAAA;AAoBnC,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAInE,MAAM,SAAS,GAAG;IAChB,MAAM,EAAE,MAAM;IACd,OAAO,EAAE;QACP,cAAc,EAAE,kBAAkB;KACnC;CACF,CAAA;AAED,MAAM,CAAC,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,cAAc,CAAC,GAAG,CAAC,CAAA;IACxC,MAAM,UAAU,GAAG,aAAa,CAAC,GAAG,CAAC,CAAA;IACrC,qFAAqF;IACrF,MAAM,aAAa,GAAG,CACpB,IAAI,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,kBAAkB,CAAC;QAChD,GAAG,CAAC,OAAO,EAAE,IAAI;QACjB,EAAE,CACH,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAA;IAErB,OAAO;QACL,OAAO,EAAE;YACP,YAAY,EAAE,CAAC,EAAU,EAAyB,EAAE,CAClD,QAAQ,CACN,GAAG,IAAI,wCAAwC,EAAE,EAAE,EACnD,SAAS,EACT,EAAE,YAAY,EAAE,CACjB;YACH,KAAK,EAAE;gBACL,IAAI,EAAE,GAAqB,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ACH,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,CAAA;AACxD,OAAO,EACL,UAAU,EACV,iBAAiB,EACjB,eAAe,GAChB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAA;AAC9D,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAA;AAC/C,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAA;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAClD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAA;AAC9D,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAA;AACzC,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAChD,OAAO,EAAE,iB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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,44 @@
|
|
|
1
1
|
import type { Context } from '../index';
|
|
2
|
-
export
|
|
3
|
-
|
|
2
|
+
export interface ContextForCookies {
|
|
3
|
+
headers: Context['headers'];
|
|
4
|
+
storage: Pick<Context['storage'], 'cookies'>;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* This function updates the ctx.storage.cookies, that is used in each request.
|
|
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 updatesContextStorageCookies: (ctx: Pick<Context, 'storage'>, setCookieValue: string) => void;
|
|
17
|
+
export declare const setCookie: (ctx: Pick<Context, 'storage'>, headers: Headers) => void;
|
|
18
|
+
export declare const getStoreCookie: (ctx: Pick<Context, 'storage'>) => (headers: Headers) => void;
|
|
19
|
+
/**
|
|
20
|
+
* This function returns a modified copy of the original cookie header (ctx.headers.cookie from the first request)
|
|
21
|
+
* with the cookie values that comes in each request (ctx.storage.cookies).
|
|
22
|
+
* If there is no cookies in storage, the ctx.headers?.cookie is used
|
|
23
|
+
*
|
|
24
|
+
* ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
|
|
25
|
+
* [1] cookie key
|
|
26
|
+
* [2] {
|
|
27
|
+
* value: cookie value,
|
|
28
|
+
* setCookie: setCookie used in browser response
|
|
29
|
+
* }
|
|
30
|
+
*/
|
|
31
|
+
export declare const getUpdatedCookie: (ctx: ContextForCookies) => string | null;
|
|
32
|
+
export declare const getWithCookie: (ctx: ContextForCookies) => <T extends Record<string, any>>(headers: T) => T & {
|
|
33
|
+
cookie?: string | undefined;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* This function updates the cookie value based on its key
|
|
37
|
+
*
|
|
38
|
+
* const existingCookies = 'key=value1; key2=; key3=value3';
|
|
39
|
+
* const storageCookieKey = 'key2';
|
|
40
|
+
* const storageCookieValue = 'value2'
|
|
41
|
+
*
|
|
42
|
+
* updatesCookieValueByKey(existingCookies, storageCookieKey, storageCookieValue) returns 'key=value1; key2=value2; key3=value3';
|
|
43
|
+
*/
|
|
44
|
+
export declare const updatesCookieValueByKey: (existingCookies: string, storageCookieKey: string, storageCookieValue: string) => string;
|
|
@@ -1,5 +1,80 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
+
export const updatesContextStorageCookies = (ctx, setCookieValue) => {
|
|
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
|
+
};
|
|
23
|
+
export const setCookie = (ctx, headers) => {
|
|
24
|
+
headers
|
|
25
|
+
.getSetCookie()
|
|
26
|
+
.forEach((setCookieValue) => updatesContextStorageCookies(ctx, setCookieValue));
|
|
27
|
+
};
|
|
28
|
+
export const getStoreCookie = (ctx) => (headers) => setCookie(ctx, headers);
|
|
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
|
+
export const getWithCookie = (ctx) => function withCookie(headers) {
|
|
52
|
+
const updatedCookie = getUpdatedCookie(ctx);
|
|
53
|
+
if (!updatedCookie) {
|
|
54
|
+
return headers;
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
...headers,
|
|
58
|
+
cookie: updatedCookie,
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* This function updates the cookie value based on its key
|
|
63
|
+
*
|
|
64
|
+
* const existingCookies = 'key=value1; key2=; key3=value3';
|
|
65
|
+
* const storageCookieKey = 'key2';
|
|
66
|
+
* const storageCookieValue = 'value2'
|
|
67
|
+
*
|
|
68
|
+
* updatesCookieValueByKey(existingCookies, storageCookieKey, storageCookieValue) returns 'key=value1; key2=value2; key3=value3';
|
|
69
|
+
*/
|
|
70
|
+
export const updatesCookieValueByKey = (existingCookies, storageCookieKey, storageCookieValue) => {
|
|
71
|
+
const MATCH_COOKIE_KEY_VALUE = new RegExp(`(${storageCookieKey})=([^;]*)`);
|
|
72
|
+
const cookieParts = existingCookies.match(MATCH_COOKIE_KEY_VALUE);
|
|
73
|
+
// replaces original cookie with the one coming from storage
|
|
74
|
+
if (cookieParts) {
|
|
75
|
+
return existingCookies.replace(cookieParts[0], `${cookieParts[1]}=${storageCookieValue}`);
|
|
76
|
+
}
|
|
77
|
+
// add new storage cookie to the original list of cookies
|
|
78
|
+
return `${existingCookies};${storageCookieKey}=${storageCookieValue}`;
|
|
3
79
|
};
|
|
4
|
-
export const getStoreCookie = (ctx) => (headers) => setCookie(headers, ctx);
|
|
5
80
|
//# 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":"AAOA,MAAM,gCAAgC,GAAG,kBAAkB,CAAA;AAE3D;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAC1C,GAA6B,EAC7B,cAAsB,EACtB,EAAE;IACF,MAAM,WAAW,GAAG,cAAc,CAAC,KAAK,CAAC,gCAAgC,CAAC,CAAA;IAE1E,IAAI,WAAW,EAAE,CAAC;QAChB,MAAM,SAAS,GAAG,WAAW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M,CACnB,CAAA;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,GAAsB,EAAE,EAAE,CACtD,SAAS,UAAU,CACjB,OAAU;IAEV,MAAM,aAAa,GAAG,gBAAgB,CAAC,GAAG,CAAC,CAAA;IAE3C,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,OAAO,OAAO,CAAA;IAChB,CAAC;IAED,OAAO;QACL,GAAG,OAAO;QACV,MAAM,EAAE,aAAa;KACtB,CAAA;AACH,CAAC,CAAA;AAEH;;;;;;;;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,WAAW,CAAC,CAAC,CAAC,EACd,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.50",
|
|
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": "d89ce02086b19706719b595fcb6efd68cbc430ca"
|
|
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 { getStoreCookie, getWithCookie } from '../../utils/cookies'
|
|
23
23
|
|
|
24
24
|
type ValueOf<T> = T extends Record<string, infer K> ? K : never
|
|
25
25
|
|
|
@@ -36,9 +36,13 @@ export const VtexCommerce = (
|
|
|
36
36
|
) => {
|
|
37
37
|
const base = `https://${account}.${environment}.com.br`
|
|
38
38
|
const storeCookies = getStoreCookie(ctx)
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
39
|
+
const withCookie = getWithCookie(ctx)
|
|
40
|
+
// replacing www. only for testing while www.vtexfaststore.com is configured with www
|
|
41
|
+
const forwardedHost = (
|
|
42
|
+
new Headers(ctx.headers).get('x-forwarded-host') ??
|
|
43
|
+
ctx.headers?.host ??
|
|
44
|
+
''
|
|
45
|
+
).replace('www.', '')
|
|
42
46
|
|
|
43
47
|
return {
|
|
44
48
|
catalog: {
|
|
@@ -102,15 +106,21 @@ export const VtexCommerce = (
|
|
|
102
106
|
sc: salesChannel,
|
|
103
107
|
})
|
|
104
108
|
|
|
109
|
+
const headers: HeadersInit = withCookie({
|
|
110
|
+
'content-type': 'application/json',
|
|
111
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
112
|
+
})
|
|
113
|
+
|
|
105
114
|
return fetchAPI(
|
|
106
115
|
`${base}/api/checkout/pub/orderForms/simulation?${params.toString()}`,
|
|
107
116
|
{
|
|
108
117
|
...BASE_INIT,
|
|
118
|
+
headers,
|
|
109
119
|
body: JSON.stringify(args),
|
|
110
|
-
}
|
|
120
|
+
},
|
|
121
|
+
{ storeCookies }
|
|
111
122
|
)
|
|
112
123
|
},
|
|
113
|
-
|
|
114
124
|
shippingData: (
|
|
115
125
|
{
|
|
116
126
|
id,
|
|
@@ -142,19 +152,22 @@ export const VtexCommerce = (
|
|
|
142
152
|
selectedAddresses: selectedAddresses,
|
|
143
153
|
clearAddressIfPostalCodeNotFound: incrementAddress,
|
|
144
154
|
}
|
|
155
|
+
|
|
156
|
+
const headers: HeadersInit = withCookie({
|
|
157
|
+
'content-type': 'application/json',
|
|
158
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
159
|
+
})
|
|
160
|
+
|
|
145
161
|
return fetchAPI(
|
|
146
162
|
`${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`,
|
|
147
163
|
{
|
|
148
164
|
...BASE_INIT,
|
|
165
|
+
headers,
|
|
149
166
|
body: JSON.stringify(mappedBody),
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
cookie: ctx.headers.cookie,
|
|
153
|
-
},
|
|
154
|
-
}
|
|
167
|
+
},
|
|
168
|
+
{ storeCookies }
|
|
155
169
|
)
|
|
156
170
|
},
|
|
157
|
-
|
|
158
171
|
orderForm: ({
|
|
159
172
|
id,
|
|
160
173
|
refreshOutdatedData = true,
|
|
@@ -169,34 +182,36 @@ export const VtexCommerce = (
|
|
|
169
182
|
refreshOutdatedData: refreshOutdatedData.toString(),
|
|
170
183
|
sc: salesChannel,
|
|
171
184
|
})
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
'X-FORWARDED-HOST': forwardedHost,
|
|
178
|
-
cookie: ctx.headers.cookie,
|
|
179
|
-
},
|
|
180
|
-
}
|
|
181
|
-
: BASE_INIT
|
|
185
|
+
|
|
186
|
+
const headers: HeadersInit = withCookie({
|
|
187
|
+
'content-type': 'application/json',
|
|
188
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
189
|
+
})
|
|
182
190
|
|
|
183
191
|
return fetchAPI(
|
|
184
192
|
`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`,
|
|
185
|
-
|
|
193
|
+
{
|
|
194
|
+
...BASE_INIT,
|
|
195
|
+
headers,
|
|
196
|
+
},
|
|
186
197
|
{ storeCookies }
|
|
187
198
|
)
|
|
188
199
|
},
|
|
189
|
-
|
|
190
200
|
clearOrderFormMessages: ({ id }: { id: string }) => {
|
|
201
|
+
const headers: HeadersInit = withCookie({
|
|
202
|
+
'content-type': 'application/json',
|
|
203
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
204
|
+
})
|
|
205
|
+
|
|
191
206
|
return fetchAPI(
|
|
192
207
|
`${base}/api/checkout/pub/orderForm/${id}/messages/clear`,
|
|
193
208
|
{
|
|
194
209
|
...BASE_INIT,
|
|
210
|
+
headers,
|
|
195
211
|
body: '{}',
|
|
196
212
|
}
|
|
197
213
|
)
|
|
198
214
|
},
|
|
199
|
-
|
|
200
215
|
updateOrderFormItems: ({
|
|
201
216
|
id,
|
|
202
217
|
orderItems,
|
|
@@ -215,14 +230,15 @@ export const VtexCommerce = (
|
|
|
215
230
|
sc: salesChannel,
|
|
216
231
|
})
|
|
217
232
|
|
|
233
|
+
const headers: HeadersInit = withCookie({
|
|
234
|
+
'content-type': 'application/json',
|
|
235
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
236
|
+
})
|
|
237
|
+
|
|
218
238
|
return fetchAPI(
|
|
219
239
|
`${base}/api/checkout/pub/orderForm/${id}/items?${params}`,
|
|
220
240
|
{
|
|
221
|
-
headers
|
|
222
|
-
'content-type': 'application/json',
|
|
223
|
-
cookie: ctx.headers?.cookie,
|
|
224
|
-
'X-FORWARDED-HOST': forwardedHost,
|
|
225
|
-
},
|
|
241
|
+
headers,
|
|
226
242
|
body: JSON.stringify({
|
|
227
243
|
orderItems,
|
|
228
244
|
noSplitItem: !shouldSplitItem,
|
|
@@ -243,10 +259,15 @@ export const VtexCommerce = (
|
|
|
243
259
|
key: string
|
|
244
260
|
value: string
|
|
245
261
|
}): Promise<OrderForm> => {
|
|
262
|
+
const headers: HeadersInit = withCookie({
|
|
263
|
+
'content-type': 'application/json',
|
|
264
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
265
|
+
})
|
|
266
|
+
|
|
246
267
|
return fetchAPI(
|
|
247
268
|
`${base}/api/checkout/pub/orderForm/${id}/customData/${appId}/${key}`,
|
|
248
269
|
{
|
|
249
|
-
|
|
270
|
+
headers,
|
|
250
271
|
body: JSON.stringify({ value }),
|
|
251
272
|
method: 'PUT',
|
|
252
273
|
}
|
|
@@ -271,15 +292,34 @@ export const VtexCommerce = (
|
|
|
271
292
|
)
|
|
272
293
|
|
|
273
294
|
const url = `${base}/api/checkout/pub/regions/?${params.toString()}`
|
|
274
|
-
|
|
295
|
+
const headers: HeadersInit = withCookie({
|
|
296
|
+
'content-type': 'application/json',
|
|
297
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
return fetchAPI(
|
|
301
|
+
url,
|
|
302
|
+
{
|
|
303
|
+
headers,
|
|
304
|
+
},
|
|
305
|
+
{ storeCookies }
|
|
306
|
+
)
|
|
275
307
|
},
|
|
276
308
|
address: async ({
|
|
277
309
|
postalCode,
|
|
278
310
|
country,
|
|
279
311
|
}: AddressInput): Promise<Address> => {
|
|
312
|
+
const headers: HeadersInit = withCookie({
|
|
313
|
+
'content-type': 'application/json',
|
|
314
|
+
'X-FORWARDED-HOST': forwardedHost,
|
|
315
|
+
})
|
|
316
|
+
|
|
280
317
|
return fetchAPI(
|
|
281
318
|
`${base}/api/checkout/pub/postal-code/${country}/${postalCode}`,
|
|
282
|
-
|
|
319
|
+
{
|
|
320
|
+
headers,
|
|
321
|
+
},
|
|
322
|
+
{ storeCookies }
|
|
283
323
|
)
|
|
284
324
|
},
|
|
285
325
|
},
|
|
@@ -290,14 +330,16 @@ export const VtexCommerce = (
|
|
|
290
330
|
'items',
|
|
291
331
|
'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol'
|
|
292
332
|
)
|
|
333
|
+
|
|
334
|
+
const headers: HeadersInit = withCookie({
|
|
335
|
+
'content-type': 'application/json',
|
|
336
|
+
})
|
|
337
|
+
|
|
293
338
|
return fetchAPI(
|
|
294
339
|
`${base}/api/sessions?${params.toString()}`,
|
|
295
340
|
{
|
|
296
341
|
method: 'POST',
|
|
297
|
-
headers
|
|
298
|
-
'content-type': 'application/json',
|
|
299
|
-
cookie: ctx.headers.cookie,
|
|
300
|
-
},
|
|
342
|
+
headers,
|
|
301
343
|
body: '{}',
|
|
302
344
|
},
|
|
303
345
|
{ 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,126 @@
|
|
|
1
1
|
import type { Context } from '../index'
|
|
2
2
|
|
|
3
|
-
export
|
|
4
|
-
headers
|
|
3
|
+
export interface ContextForCookies {
|
|
4
|
+
headers: Context['headers']
|
|
5
|
+
storage: Pick<Context['storage'], 'cookies'>
|
|
5
6
|
}
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
const MATCH_FIRST_SET_COOKIE_KEY_VALUE = /^([^=]+)=([^;]*)/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* This function updates the ctx.storage.cookies, that is used in each request.
|
|
12
|
+
*
|
|
13
|
+
* ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
|
|
14
|
+
* [1] cookie key
|
|
15
|
+
* [2] {
|
|
16
|
+
* value: cookie value,
|
|
17
|
+
* setCookie: setCookie used in browser response
|
|
18
|
+
* }
|
|
19
|
+
*/
|
|
20
|
+
export const updatesContextStorageCookies = (
|
|
21
|
+
ctx: Pick<Context, 'storage'>,
|
|
22
|
+
setCookieValue: string
|
|
23
|
+
) => {
|
|
24
|
+
const matchCookie = setCookieValue.match(MATCH_FIRST_SET_COOKIE_KEY_VALUE)
|
|
25
|
+
|
|
26
|
+
if (matchCookie) {
|
|
27
|
+
const cookieKey = matchCookie[1]
|
|
28
|
+
const cookieValue = matchCookie[2]
|
|
29
|
+
|
|
30
|
+
ctx.storage.cookies.set(cookieKey, {
|
|
31
|
+
value: cookieValue,
|
|
32
|
+
setCookie: setCookieValue,
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const setCookie = (ctx: Pick<Context, 'storage'>, headers: Headers) => {
|
|
38
|
+
headers
|
|
39
|
+
.getSetCookie()
|
|
40
|
+
.forEach((setCookieValue) =>
|
|
41
|
+
updatesContextStorageCookies(ctx, setCookieValue)
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const getStoreCookie =
|
|
46
|
+
(ctx: Pick<Context, 'storage'>) => (headers: Headers) =>
|
|
47
|
+
setCookie(ctx, headers)
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* This function returns a modified copy of the original cookie header (ctx.headers.cookie from the first request)
|
|
51
|
+
* with the cookie values that comes in each request (ctx.storage.cookies).
|
|
52
|
+
* If there is no cookies in storage, the ctx.headers?.cookie is used
|
|
53
|
+
*
|
|
54
|
+
* ctx.storage.cookies is a Map<string[1], Record<string, string>[2]> where,
|
|
55
|
+
* [1] cookie key
|
|
56
|
+
* [2] {
|
|
57
|
+
* value: cookie value,
|
|
58
|
+
* setCookie: setCookie used in browser response
|
|
59
|
+
* }
|
|
60
|
+
*/
|
|
61
|
+
export const getUpdatedCookie = (ctx: ContextForCookies) => {
|
|
62
|
+
if (!ctx.headers?.cookie) {
|
|
63
|
+
return null
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const contextStorageCookies = Array.from(ctx.storage.cookies.entries())
|
|
67
|
+
|
|
68
|
+
if (contextStorageCookies.length === 0) {
|
|
69
|
+
return ctx.headers.cookie
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return contextStorageCookies.reduce(
|
|
73
|
+
(existingCookies, [storageCookieKey, { value: storageCookieValue }]) =>
|
|
74
|
+
updatesCookieValueByKey(
|
|
75
|
+
existingCookies,
|
|
76
|
+
storageCookieKey,
|
|
77
|
+
storageCookieValue
|
|
78
|
+
),
|
|
79
|
+
ctx.headers.cookie
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export const getWithCookie = (ctx: ContextForCookies) =>
|
|
84
|
+
function withCookie<T extends Record<string, any>>(
|
|
85
|
+
headers: T
|
|
86
|
+
): T & { cookie?: string } {
|
|
87
|
+
const updatedCookie = getUpdatedCookie(ctx)
|
|
88
|
+
|
|
89
|
+
if (!updatedCookie) {
|
|
90
|
+
return headers
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
...headers,
|
|
95
|
+
cookie: updatedCookie,
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* This function updates the cookie value based on its key
|
|
101
|
+
*
|
|
102
|
+
* const existingCookies = 'key=value1; key2=; key3=value3';
|
|
103
|
+
* const storageCookieKey = 'key2';
|
|
104
|
+
* const storageCookieValue = 'value2'
|
|
105
|
+
*
|
|
106
|
+
* updatesCookieValueByKey(existingCookies, storageCookieKey, storageCookieValue) returns 'key=value1; key2=value2; key3=value3';
|
|
107
|
+
*/
|
|
108
|
+
export const updatesCookieValueByKey = (
|
|
109
|
+
existingCookies: string,
|
|
110
|
+
storageCookieKey: string,
|
|
111
|
+
storageCookieValue: string
|
|
112
|
+
) => {
|
|
113
|
+
const MATCH_COOKIE_KEY_VALUE = new RegExp(`(${storageCookieKey})=([^;]*)`)
|
|
114
|
+
const cookieParts = existingCookies.match(MATCH_COOKIE_KEY_VALUE)
|
|
115
|
+
|
|
116
|
+
// replaces original cookie with the one coming from storage
|
|
117
|
+
if (cookieParts) {
|
|
118
|
+
return existingCookies.replace(
|
|
119
|
+
cookieParts[0],
|
|
120
|
+
`${cookieParts[1]}=${storageCookieValue}`
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// add new storage cookie to the original list of cookies
|
|
125
|
+
return `${existingCookies};${storageCookieKey}=${storageCookieValue}`
|
|
126
|
+
}
|