@decocms/apps 1.6.4 → 1.6.5
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/package.json +2 -2
- package/vtex/client.ts +50 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decocms/apps",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Deco commerce apps for TanStack Start - Shopify, VTEX, commerce types, analytics utils",
|
|
6
6
|
"exports": {
|
|
@@ -116,7 +116,7 @@
|
|
|
116
116
|
},
|
|
117
117
|
"devDependencies": {
|
|
118
118
|
"@biomejs/biome": "^2.4.7",
|
|
119
|
-
"@decocms/start": "^
|
|
119
|
+
"@decocms/start": "^2.5.0",
|
|
120
120
|
"@semantic-release/exec": "^7.1.0",
|
|
121
121
|
"@semantic-release/git": "^10.0.1",
|
|
122
122
|
"@tanstack/react-query": "^5.90.21",
|
package/vtex/client.ts
CHANGED
|
@@ -9,22 +9,15 @@ import { ANONYMOUS_COOKIE, SESSION_COOKIE } from "./utils/intelligentSearch";
|
|
|
9
9
|
import { parseSegment, SEGMENT_COOKIE_NAME } from "./utils/segment";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
12
|
+
* Outgoing response headers for the active request, or `null` when
|
|
13
|
+
* called outside a request scope (which happens during module init).
|
|
14
|
+
* `RequestContext.responseHeaders` was added to `@decocms/start` in
|
|
15
|
+
* v0.39.0; we now require >=2.5.0 as a devDep so the property is
|
|
16
|
+
* always typed/present.
|
|
16
17
|
*/
|
|
17
18
|
function getResponseHeaders(): Headers | null {
|
|
18
19
|
const ctx = RequestContext.current;
|
|
19
|
-
|
|
20
|
-
// biome-ignore lint/suspicious/noExplicitAny: forward-compat with upcoming responseHeaders property
|
|
21
|
-
if ((ctx as any).responseHeaders instanceof Headers) return (ctx as any).responseHeaders;
|
|
22
|
-
let headers = ctx.bag.get("responseHeaders") as Headers | undefined;
|
|
23
|
-
if (!headers) {
|
|
24
|
-
headers = new Headers();
|
|
25
|
-
ctx.bag.set("responseHeaders", headers);
|
|
26
|
-
}
|
|
27
|
-
return headers;
|
|
20
|
+
return ctx ? ctx.responseHeaders : null;
|
|
28
21
|
}
|
|
29
22
|
|
|
30
23
|
// ---------------------------------------------------------------------------
|
|
@@ -175,12 +168,55 @@ function extractRegionIdFromCookies(): string | null {
|
|
|
175
168
|
return segment?.regionId ?? null;
|
|
176
169
|
}
|
|
177
170
|
|
|
171
|
+
/**
|
|
172
|
+
* Read the raw `vtex_segment=<token>` cookie from the active request.
|
|
173
|
+
* Returns null when outside a request context or no segment cookie is set.
|
|
174
|
+
*
|
|
175
|
+
* Used to forward the segment cookie on outgoing VTEX API calls so
|
|
176
|
+
* Legacy Catalog endpoints (which gate on the cookie, not on
|
|
177
|
+
* `?regionId=` query params) see the right region for products
|
|
178
|
+
* available only through regional sellers.
|
|
179
|
+
*/
|
|
180
|
+
function getSegmentCookieHeader(): string | null {
|
|
181
|
+
const ctx = RequestContext.current;
|
|
182
|
+
if (!ctx) return null;
|
|
183
|
+
const cookies = ctx.request.headers.get("cookie");
|
|
184
|
+
if (!cookies) return null;
|
|
185
|
+
const match = cookies.match(new RegExp(`(?:^|;\\s*)${SEGMENT_COOKIE_NAME}=([^;]+)`));
|
|
186
|
+
if (!match?.[1]) return null;
|
|
187
|
+
return `${SEGMENT_COOKIE_NAME}=${match[1]}`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Case-insensitive lookup for `cookie` / `Cookie` in a headers init. */
|
|
191
|
+
function hasCookieHeader(headers: HeadersInit | undefined): boolean {
|
|
192
|
+
if (!headers) return false;
|
|
193
|
+
if (headers instanceof Headers) return headers.has("cookie");
|
|
194
|
+
if (Array.isArray(headers)) {
|
|
195
|
+
return headers.some(([k]) => k.toLowerCase() === "cookie");
|
|
196
|
+
}
|
|
197
|
+
return Object.keys(headers).some((k) => k.toLowerCase() === "cookie");
|
|
198
|
+
}
|
|
199
|
+
|
|
178
200
|
export async function vtexFetchResponse(path: string, init?: RequestInit): Promise<Response> {
|
|
179
201
|
const raw = path.startsWith("http") ? path : `${baseUrl()}${path}`;
|
|
180
202
|
const url = sanitizeUrl(raw);
|
|
203
|
+
|
|
204
|
+
// Forward the incoming `vtex_segment` cookie on outgoing calls when
|
|
205
|
+
// the caller hasn't set a cookie header explicitly. This is what the
|
|
206
|
+
// Legacy Catalog API (and a handful of other VTEX endpoints) needs
|
|
207
|
+
// to resolve regional sellers correctly. Without it, products only
|
|
208
|
+
// available via a region's seller appear as OutOfStock on PDPs even
|
|
209
|
+
// for users with the cookie. Sites used to wrap `_fetch` themselves
|
|
210
|
+
// to do this — see https://github.com/decocms/apps-start#regional-sellers
|
|
211
|
+
const segmentCookie = !hasCookieHeader(init?.headers) ? getSegmentCookieHeader() : null;
|
|
212
|
+
|
|
181
213
|
const response = await _fetch(url, {
|
|
182
214
|
...init,
|
|
183
|
-
headers: {
|
|
215
|
+
headers: {
|
|
216
|
+
...authHeaders(),
|
|
217
|
+
...(segmentCookie ? { cookie: segmentCookie } : {}),
|
|
218
|
+
...init?.headers,
|
|
219
|
+
},
|
|
184
220
|
});
|
|
185
221
|
if (!response.ok) {
|
|
186
222
|
throw new Error(`VTEX API error: ${response.status} ${response.statusText} - ${url}`);
|