@decocms/apps 0.21.3 → 0.21.4

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/vtex/client.ts +53 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@decocms/apps",
3
- "version": "0.21.3",
3
+ "version": "0.21.4",
4
4
  "type": "module",
5
5
  "description": "Deco commerce apps for TanStack Start - Shopify, VTEX, commerce types, analytics utils",
6
6
  "exports": {
package/vtex/client.ts CHANGED
@@ -82,6 +82,8 @@ export interface VtexConfig {
82
82
 
83
83
  let _config: VtexConfig | null = null;
84
84
  let _fetch: typeof fetch = globalThis.fetch;
85
+ let _getCookieHeader: (() => string | undefined) | null = null;
86
+ let _forwardSetCookies: ((cookies: string[]) => void) | null = null;
85
87
 
86
88
  export function configureVtex(config: VtexConfig) {
87
89
  _config = config;
@@ -103,6 +105,37 @@ export function setVtexFetch(fetchFn: typeof fetch) {
103
105
  _fetch = fetchFn;
104
106
  }
105
107
 
108
+ /**
109
+ * Register a provider that returns the Cookie header from the current request.
110
+ * Called automatically by vtexFetchWithCookies when no explicit cookieHeader
111
+ * is present in the request init — so checkout/session/auth actions
112
+ * transparently forward browser cookies to the VTEX API.
113
+ *
114
+ * @example
115
+ * ```ts
116
+ * import { getRequestHeader } from "@tanstack/react-start/server";
117
+ * setRequestCookieProvider(() => getRequestHeader("cookie") ?? undefined);
118
+ * ```
119
+ */
120
+ export function setRequestCookieProvider(fn: () => string | undefined) {
121
+ _getCookieHeader = fn;
122
+ }
123
+
124
+ /**
125
+ * Register a callback that forwards VTEX Set-Cookie headers back to the browser.
126
+ * Called automatically by vtexFetchWithCookies after every response that
127
+ * carries Set-Cookie headers.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * import { setResponseHeader } from "@tanstack/react-start/server";
132
+ * setResponseCookieForwarder((cookies) => setResponseHeader("set-cookie", cookies));
133
+ * ```
134
+ */
135
+ export function setResponseCookieForwarder(fn: (cookies: string[]) => void) {
136
+ _forwardSetCookies = fn;
137
+ }
138
+
106
139
  export function getVtexConfig(): VtexConfig {
107
140
  if (!_config)
108
141
  throw new Error("VTEX not configured. Call configureVtex() first.");
@@ -222,6 +255,20 @@ export async function vtexFetchWithCookies<T>(
222
255
  path: string,
223
256
  init?: RequestInit,
224
257
  ): Promise<VtexFetchResult<T>> {
258
+ // Auto-inject request cookies when no explicit cookie header is set
259
+ if (_getCookieHeader) {
260
+ const existingHeaders = init?.headers as Record<string, string> | undefined;
261
+ if (!existingHeaders?.["cookie"]) {
262
+ const cookies = _getCookieHeader();
263
+ if (cookies) {
264
+ init = {
265
+ ...init,
266
+ headers: { ...existingHeaders, cookie: cookies },
267
+ };
268
+ }
269
+ }
270
+ }
271
+
225
272
  const response = await vtexFetchResponse(path, init);
226
273
  const data = (await response.json()) as T;
227
274
  const setCookies: string[] = [];
@@ -234,6 +281,12 @@ export async function vtexFetchWithCookies<T>(
234
281
  }
235
282
  });
236
283
  }
284
+
285
+ // Auto-forward Set-Cookie headers to the browser response
286
+ if (_forwardSetCookies && setCookies.length) {
287
+ _forwardSetCookies(setCookies);
288
+ }
289
+
237
290
  return { data, setCookies };
238
291
  }
239
292