@decocms/apps-vtex 8.1.0-next.0 → 8.1.0-next.1

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 (139) hide show
  1. package/package.json +7 -52
  2. package/src/index.ts +10 -28
  3. package/src/utils/constants.ts +2 -56
  4. package/src/utils/cookieSanitizer.ts +59 -59
  5. package/src/utils/operationRouter.ts +75 -65
  6. package/src/utils/types.ts +709 -1557
  7. package/src/vtexClient.ts +354 -343
  8. package/src/README.md +0 -6
  9. package/src/__conformance__/upstream.test.ts +0 -158
  10. package/src/__tests__/client-segment-cookie.test.ts +0 -255
  11. package/src/__tests__/client-set-cookie-forward.test.ts +0 -257
  12. package/src/__tests__/schemas.test.ts +0 -71
  13. package/src/actions/address.ts +0 -250
  14. package/src/actions/analytics/sendEvent.ts +0 -86
  15. package/src/actions/auth.ts +0 -333
  16. package/src/actions/checkout.ts +0 -659
  17. package/src/actions/index.ts +0 -11
  18. package/src/actions/masterData.ts +0 -168
  19. package/src/actions/misc.ts +0 -188
  20. package/src/actions/newsletter.ts +0 -105
  21. package/src/actions/orders.ts +0 -34
  22. package/src/actions/profile.ts +0 -201
  23. package/src/actions/session.ts +0 -85
  24. package/src/actions/trigger.ts +0 -43
  25. package/src/actions/wishlist.ts +0 -114
  26. package/src/client.ts +0 -724
  27. package/src/commerceLoaders.ts +0 -267
  28. package/src/hooks/__tests__/createCart.render.test.ts +0 -114
  29. package/src/hooks/__tests__/createCart.test.ts +0 -92
  30. package/src/hooks/__tests__/createUseCart.test.ts +0 -184
  31. package/src/hooks/__tests__/createUseUser.test.ts +0 -48
  32. package/src/hooks/__tests__/createUseWishlist.test.ts +0 -87
  33. package/src/hooks/cartQuery.ts +0 -204
  34. package/src/hooks/createCart.ts +0 -427
  35. package/src/hooks/createUseCart.ts +0 -360
  36. package/src/hooks/createUseUser.ts +0 -153
  37. package/src/hooks/createUseWishlist.ts +0 -242
  38. package/src/hooks/index.ts +0 -25
  39. package/src/hooks/useAutocomplete.ts +0 -83
  40. package/src/hooks/useCart.ts +0 -243
  41. package/src/hooks/useUser.ts +0 -78
  42. package/src/hooks/useWishlist.ts +0 -119
  43. package/src/invoke.ts +0 -235
  44. package/src/loaders/ProductDetailsPage.ts +0 -1
  45. package/src/loaders/ProductList.ts +0 -1
  46. package/src/loaders/ProductListingPage.ts +0 -1
  47. package/src/loaders/__tests__/legacyProductList.test.ts +0 -157
  48. package/src/loaders/address.ts +0 -115
  49. package/src/loaders/autocomplete.ts +0 -56
  50. package/src/loaders/brands.ts +0 -44
  51. package/src/loaders/cart/attachments.ts +0 -53
  52. package/src/loaders/cart/full.ts +0 -67
  53. package/src/loaders/cart/gifts.ts +0 -43
  54. package/src/loaders/cart/orderFormId.ts +0 -31
  55. package/src/loaders/cart/shipping.test.ts +0 -125
  56. package/src/loaders/cart/shipping.ts +0 -136
  57. package/src/loaders/cart/summary.ts +0 -36
  58. package/src/loaders/cart.ts +0 -52
  59. package/src/loaders/catalog.ts +0 -163
  60. package/src/loaders/collections.ts +0 -55
  61. package/src/loaders/index.ts +0 -20
  62. package/src/loaders/intelligentSearch/__tests__/productListingPage.test.ts +0 -51
  63. package/src/loaders/intelligentSearch/productDetailsPage.ts +0 -111
  64. package/src/loaders/intelligentSearch/productList.ts +0 -170
  65. package/src/loaders/intelligentSearch/productListingPage.ts +0 -525
  66. package/src/loaders/intelligentSearch/suggestions.ts +0 -44
  67. package/src/loaders/legacy/productDetailsPage.ts +0 -1
  68. package/src/loaders/legacy/productList.ts +0 -1
  69. package/src/loaders/legacy/relatedProductsLoader.ts +0 -79
  70. package/src/loaders/legacy.ts +0 -710
  71. package/src/loaders/logistics.ts +0 -111
  72. package/src/loaders/minicart.ts +0 -78
  73. package/src/loaders/navbar.ts +0 -26
  74. package/src/loaders/orders.ts +0 -92
  75. package/src/loaders/pageType.ts +0 -68
  76. package/src/loaders/paths/PDPDefaultPath.ts +0 -51
  77. package/src/loaders/payment.ts +0 -97
  78. package/src/loaders/productListFull.ts +0 -157
  79. package/src/loaders/profile.ts +0 -138
  80. package/src/loaders/promotion.ts +0 -30
  81. package/src/loaders/search.ts +0 -124
  82. package/src/loaders/session.ts +0 -83
  83. package/src/loaders/user.ts +0 -87
  84. package/src/loaders/wishlist.ts +0 -89
  85. package/src/loaders/wishlistProducts.ts +0 -69
  86. package/src/loaders/workflow/products.ts +0 -62
  87. package/src/loaders/workflow.ts +0 -316
  88. package/src/logo.png +0 -0
  89. package/src/manifest.gen.ts +0 -89
  90. package/src/middleware.cacheHeaders.test.ts +0 -44
  91. package/src/middleware.ts +0 -240
  92. package/src/mod.ts +0 -173
  93. package/src/registry.ts +0 -9
  94. package/src/schemas.gen.ts +0 -3191
  95. package/src/schemas.ts +0 -30
  96. package/src/types.ts +0 -248
  97. package/src/utils/__tests__/cartProjection.test.ts +0 -118
  98. package/src/utils/__tests__/cookieSanitizer.test.ts +0 -162
  99. package/src/utils/__tests__/fetch.test.ts +0 -80
  100. package/src/utils/__tests__/fetchCache.test.ts +0 -155
  101. package/src/utils/__tests__/instrumentedFetch.test.ts +0 -158
  102. package/src/utils/__tests__/intelligentSearch.test.ts +0 -88
  103. package/src/utils/__tests__/minicart.test.ts +0 -184
  104. package/src/utils/__tests__/operationRouter.test.ts +0 -227
  105. package/src/utils/__tests__/resilience.test.ts +0 -205
  106. package/src/utils/__tests__/resourceRange.test.ts +0 -32
  107. package/src/utils/__tests__/simulationCache.test.ts +0 -40
  108. package/src/utils/__tests__/sitemap.test.ts +0 -185
  109. package/src/utils/__tests__/slugify.test.ts +0 -31
  110. package/src/utils/__tests__/storefrontBaseUrl.test.ts +0 -61
  111. package/src/utils/__tests__/transform.test.ts +0 -1111
  112. package/src/utils/accountLoaders.ts +0 -203
  113. package/src/utils/authHelpers.ts +0 -85
  114. package/src/utils/batch.ts +0 -18
  115. package/src/utils/buildOfferShelf.test.ts +0 -59
  116. package/src/utils/cartProjection.ts +0 -96
  117. package/src/utils/cookies.ts +0 -167
  118. package/src/utils/enrichment.ts +0 -556
  119. package/src/utils/fetch.ts +0 -111
  120. package/src/utils/fetchCache.ts +0 -55
  121. package/src/utils/index.ts +0 -19
  122. package/src/utils/instrumentedFetch.ts +0 -98
  123. package/src/utils/intelligentSearch.ts +0 -96
  124. package/src/utils/legacy.ts +0 -139
  125. package/src/utils/minicart.ts +0 -139
  126. package/src/utils/pickAndOmit.ts +0 -25
  127. package/src/utils/proxy.ts +0 -495
  128. package/src/utils/resilience.ts +0 -352
  129. package/src/utils/resourceRange.ts +0 -10
  130. package/src/utils/segment.ts +0 -152
  131. package/src/utils/similars.ts +0 -35
  132. package/src/utils/simulationCache.ts +0 -73
  133. package/src/utils/sitemap.ts +0 -283
  134. package/src/utils/slugCache.ts +0 -28
  135. package/src/utils/slugify.ts +0 -13
  136. package/src/utils/transform.ts +0 -1691
  137. package/src/utils/vtexId.ts +0 -151
  138. package/src/vtexClient.test.ts +0 -204
  139. package/tsconfig.json +0 -7
@@ -1,659 +0,0 @@
1
- /**
2
- * VTEX Checkout API actions.
3
- * Each function receives a single props object — matching the invoke handler
4
- * pattern from deco-cx/apps where `setupApps()` registers handlers directly.
5
- *
6
- * Ported from deco-cx/apps vtex/actions/cart/*.ts
7
- * @see https://developers.vtex.com/docs/api-reference/checkout-api
8
- */
9
-
10
- import {
11
- type CartProjection,
12
- type CartSection,
13
- defaultSectionsFor,
14
- SECTIONS_FULL,
15
- } from "@decocms/apps-commerce/types";
16
- import { getVtexConfig, vtexFetchWithCookies } from "../client";
17
- import type { OrderForm } from "../types";
18
- import {
19
- type ProjectOrderFormOptions,
20
- projectOrderForm,
21
- type VtexCartProjectionResult,
22
- } from "../utils/cartProjection";
23
-
24
- /**
25
- * Legacy full-section default. Kept as an exported symbol (loaders/cart.ts and
26
- * older callers import it), but sourced from the agnostic `SECTIONS_FULL`
27
- * preset so the "which sections" list lives in exactly one place.
28
- */
29
- export const DEFAULT_EXPECTED_SECTIONS: string[] = SECTIONS_FULL;
30
-
31
- function scParam(): string {
32
- const sc = getVtexConfig().salesChannel;
33
- return sc ? `sc=${sc}` : "";
34
- }
35
-
36
- function appendSc(params: URLSearchParams): URLSearchParams {
37
- const sc = getVtexConfig().salesChannel;
38
- if (sc) params.set("sc", sc);
39
- return params;
40
- }
41
-
42
- function forceHttpsOnAssets(orderForm: OrderForm): OrderForm {
43
- if (!orderForm?.items) return orderForm;
44
- return {
45
- ...orderForm,
46
- items: orderForm.items.map((item: any) => ({
47
- ...item,
48
- imageUrl: item.imageUrl?.replace(/^http:/, "https:"),
49
- })),
50
- };
51
- }
52
-
53
- // ---------------------------------------------------------------------------
54
- // Cart (OrderForm) — core CRUD
55
- // ---------------------------------------------------------------------------
56
-
57
- export interface GetOrCreateCartProps {
58
- orderFormId?: string;
59
- }
60
-
61
- export async function getOrCreateCart(props: GetOrCreateCartProps): Promise<OrderForm> {
62
- const { orderFormId } = props;
63
- const sc = scParam();
64
-
65
- if (orderFormId) {
66
- const result = await vtexFetchWithCookies<OrderForm>(
67
- `/api/checkout/pub/orderForm/${orderFormId}${sc ? `?${sc}` : ""}`,
68
- );
69
- return forceHttpsOnAssets(result);
70
- }
71
- const result = await vtexFetchWithCookies<OrderForm>(
72
- `/api/checkout/pub/orderForm${sc ? `?${sc}` : ""}`,
73
- {
74
- method: "POST",
75
- body: JSON.stringify({
76
- expectedOrderFormSections: DEFAULT_EXPECTED_SECTIONS,
77
- }),
78
- },
79
- );
80
- return forceHttpsOnAssets(result);
81
- }
82
-
83
- export interface AddItemsToCartProps {
84
- orderFormId: string;
85
- orderItems: Array<{
86
- id: string;
87
- seller: string;
88
- quantity: number;
89
- index?: number;
90
- price?: number;
91
- }>;
92
- allowedOutdatedData?: string[];
93
- }
94
-
95
- export async function addItemsToCart(props: AddItemsToCartProps): Promise<OrderForm> {
96
- const { orderFormId, orderItems, allowedOutdatedData = ["paymentData"] } = props;
97
- const params = appendSc(new URLSearchParams());
98
- for (const d of allowedOutdatedData) params.append("allowedOutdatedData", d);
99
- const result = await vtexFetchWithCookies<OrderForm>(
100
- `/api/checkout/pub/orderForm/${orderFormId}/items?${params}`,
101
- { method: "POST", body: JSON.stringify({ orderItems }) },
102
- );
103
- return forceHttpsOnAssets(result);
104
- }
105
-
106
- export interface UpdateCartItemsProps {
107
- orderFormId: string;
108
- orderItems: Array<{ index: number; quantity: number }>;
109
- allowedOutdatedData?: string[];
110
- noSplitItem?: boolean;
111
- }
112
-
113
- export async function updateCartItems(props: UpdateCartItemsProps): Promise<OrderForm> {
114
- const { orderFormId, orderItems, allowedOutdatedData = ["paymentData"], noSplitItem } = props;
115
- const params = appendSc(new URLSearchParams());
116
- for (const d of allowedOutdatedData) {
117
- params.append("allowedOutdatedData", d);
118
- }
119
- const result = await vtexFetchWithCookies<OrderForm>(
120
- `/api/checkout/pub/orderForm/${orderFormId}/items/update?${params}`,
121
- {
122
- method: "POST",
123
- body: JSON.stringify({
124
- orderItems,
125
- noSplitItem: Boolean(noSplitItem),
126
- }),
127
- },
128
- );
129
- return forceHttpsOnAssets(result);
130
- }
131
-
132
- export interface RemoveAllItemsProps {
133
- orderFormId: string;
134
- }
135
-
136
- export async function removeAllItems(props: RemoveAllItemsProps): Promise<OrderForm> {
137
- const { orderFormId } = props;
138
- const sc = scParam();
139
- const result = await vtexFetchWithCookies<OrderForm>(
140
- `/api/checkout/pub/orderForm/${orderFormId}/items/removeAll${sc ? `?${sc}` : ""}`,
141
- { method: "POST", body: JSON.stringify({}) },
142
- );
143
- return forceHttpsOnAssets(result);
144
- }
145
-
146
- export interface AddCouponToCartProps {
147
- orderFormId: string;
148
- text: string;
149
- }
150
-
151
- export async function addCouponToCart(props: AddCouponToCartProps): Promise<OrderForm> {
152
- const { orderFormId, text } = props;
153
- const sc = scParam();
154
- const result = await vtexFetchWithCookies<OrderForm>(
155
- `/api/checkout/pub/orderForm/${orderFormId}/coupons${sc ? `?${sc}` : ""}`,
156
- { method: "POST", body: JSON.stringify({ text }) },
157
- );
158
- return forceHttpsOnAssets(result);
159
- }
160
-
161
- // ---------------------------------------------------------------------------
162
- // Cart — simulation
163
- // ---------------------------------------------------------------------------
164
-
165
- export interface SimulationItem {
166
- id: number | string;
167
- quantity: number;
168
- seller: string;
169
- }
170
-
171
- export interface SimulateCartProps {
172
- items: SimulationItem[];
173
- postalCode: string;
174
- country?: string;
175
- RnbBehavior?: 0 | 1;
176
- }
177
-
178
- export async function simulateCart(props: SimulateCartProps) {
179
- const { items, postalCode, country, RnbBehavior = 1 } = props;
180
- const config = getVtexConfig();
181
- const params = appendSc(new URLSearchParams({ RnbBehavior: String(RnbBehavior) }));
182
- // Uses vtexFetchWithCookies so any Set-Cookie VTEX returns on the
183
- // orderForm-scoped simulation reaches the browser via RequestContext.
184
- // Without this, the segment/ownership cookies VTEX may rotate during
185
- // simulation are dropped, and the storefront's local orderFormId
186
- // drifts away from VTEX's checkout.vtex.com server cookie.
187
- return vtexFetchWithCookies<any>(`/api/checkout/pub/orderForms/simulation?${params}`, {
188
- method: "POST",
189
- body: JSON.stringify({
190
- items,
191
- postalCode,
192
- country: country ?? config.country ?? "BRA",
193
- }),
194
- });
195
- }
196
-
197
- // ---------------------------------------------------------------------------
198
- // Cart — offerings (services attached to items)
199
- // ---------------------------------------------------------------------------
200
-
201
- export interface AddOfferingProps {
202
- orderFormId: string;
203
- itemIndex: number;
204
- offeringId: string | number;
205
- expectedOrderFormSections?: string[];
206
- }
207
-
208
- export async function addOffering(props: AddOfferingProps): Promise<OrderForm> {
209
- const {
210
- orderFormId,
211
- itemIndex,
212
- offeringId,
213
- expectedOrderFormSections = DEFAULT_EXPECTED_SECTIONS,
214
- } = props;
215
- const result = await vtexFetchWithCookies<OrderForm>(
216
- `/api/checkout/pub/orderForm/${orderFormId}/items/${itemIndex}/offerings`,
217
- {
218
- method: "POST",
219
- body: JSON.stringify({
220
- expectedOrderFormSections,
221
- id: offeringId,
222
- info: null,
223
- }),
224
- },
225
- );
226
- return forceHttpsOnAssets(result);
227
- }
228
-
229
- export interface RemoveOfferingProps {
230
- orderFormId: string;
231
- itemIndex: number;
232
- offeringId: string | number;
233
- expectedOrderFormSections?: string[];
234
- }
235
-
236
- export async function removeOffering(props: RemoveOfferingProps): Promise<OrderForm> {
237
- const {
238
- orderFormId,
239
- itemIndex,
240
- offeringId,
241
- expectedOrderFormSections = DEFAULT_EXPECTED_SECTIONS,
242
- } = props;
243
- const result = await vtexFetchWithCookies<OrderForm>(
244
- `/api/checkout/pub/orderForm/${orderFormId}/items/${itemIndex}/offerings/${offeringId}/remove`,
245
- {
246
- method: "POST",
247
- body: JSON.stringify({ expectedOrderFormSections }),
248
- },
249
- );
250
- return forceHttpsOnAssets(result);
251
- }
252
-
253
- // ---------------------------------------------------------------------------
254
- // Cart — attachments
255
- // ---------------------------------------------------------------------------
256
-
257
- export interface UpdateOrderFormAttachmentProps {
258
- orderFormId: string;
259
- attachment: string;
260
- body: Record<string, unknown>;
261
- expectedOrderFormSections?: string[];
262
- }
263
-
264
- export async function updateOrderFormAttachment(
265
- props: UpdateOrderFormAttachmentProps,
266
- ): Promise<OrderForm> {
267
- const {
268
- orderFormId,
269
- attachment,
270
- body,
271
- expectedOrderFormSections = DEFAULT_EXPECTED_SECTIONS,
272
- } = props;
273
- if (!orderFormId) throw new Error("Order form ID is required");
274
- const result = await vtexFetchWithCookies<OrderForm>(
275
- `/api/checkout/pub/orderForm/${orderFormId}/attachments/${attachment}`,
276
- {
277
- method: "POST",
278
- body: JSON.stringify({ expectedOrderFormSections, ...body }),
279
- },
280
- );
281
- return forceHttpsOnAssets(result);
282
- }
283
-
284
- export interface UpdateItemAttachmentProps {
285
- orderFormId: string;
286
- itemIndex: number;
287
- attachment: string;
288
- content: Record<string, unknown>;
289
- noSplitItem?: boolean;
290
- expectedOrderFormSections?: string[];
291
- }
292
-
293
- export async function updateItemAttachment(props: UpdateItemAttachmentProps): Promise<OrderForm> {
294
- const {
295
- orderFormId,
296
- itemIndex,
297
- attachment,
298
- content,
299
- noSplitItem = true,
300
- expectedOrderFormSections = DEFAULT_EXPECTED_SECTIONS,
301
- } = props;
302
- const result = await vtexFetchWithCookies<OrderForm>(
303
- `/api/checkout/pub/orderForm/${orderFormId}/items/${itemIndex}/attachments/${attachment}`,
304
- {
305
- method: "POST",
306
- body: JSON.stringify({
307
- content,
308
- noSplitItem,
309
- expectedOrderFormSections,
310
- }),
311
- },
312
- );
313
- return forceHttpsOnAssets(result);
314
- }
315
-
316
- export interface RemoveItemAttachmentProps {
317
- orderFormId: string;
318
- itemIndex: number;
319
- attachment: string;
320
- content: Record<string, unknown>;
321
- noSplitItem?: boolean;
322
- expectedOrderFormSections?: string[];
323
- }
324
-
325
- export async function removeItemAttachment(props: RemoveItemAttachmentProps): Promise<OrderForm> {
326
- const {
327
- orderFormId,
328
- itemIndex,
329
- attachment,
330
- content,
331
- noSplitItem = true,
332
- expectedOrderFormSections = DEFAULT_EXPECTED_SECTIONS,
333
- } = props;
334
- const result = await vtexFetchWithCookies<OrderForm>(
335
- `/api/checkout/pub/orderForm/${orderFormId}/items/${itemIndex}/attachments/${attachment}`,
336
- {
337
- method: "DELETE",
338
- body: JSON.stringify({
339
- content,
340
- noSplitItem,
341
- expectedOrderFormSections,
342
- }),
343
- },
344
- );
345
- return forceHttpsOnAssets(result);
346
- }
347
-
348
- // ---------------------------------------------------------------------------
349
- // Cart — price override
350
- // ---------------------------------------------------------------------------
351
-
352
- export interface UpdateItemPriceProps {
353
- orderFormId: string;
354
- itemIndex: number;
355
- price: number;
356
- }
357
-
358
- export async function updateItemPrice(props: UpdateItemPriceProps): Promise<OrderForm> {
359
- const { orderFormId, itemIndex, price } = props;
360
- return vtexFetchWithCookies<OrderForm>(
361
- `/api/checkout/pub/orderForm/${orderFormId}/items/${itemIndex}/price`,
362
- { method: "PUT", body: JSON.stringify({ price }) },
363
- );
364
- }
365
-
366
- // ---------------------------------------------------------------------------
367
- // Cart — selectable gifts
368
- // ---------------------------------------------------------------------------
369
-
370
- export interface UpdateSelectableGiftsProps {
371
- orderFormId: string;
372
- giftId: string;
373
- selectedGifts: Array<{ id: string; seller: string; quantity: number }>;
374
- expectedOrderFormSections?: string[];
375
- }
376
-
377
- export async function updateSelectableGifts(props: UpdateSelectableGiftsProps): Promise<OrderForm> {
378
- const {
379
- orderFormId,
380
- giftId,
381
- selectedGifts,
382
- expectedOrderFormSections = DEFAULT_EXPECTED_SECTIONS,
383
- } = props;
384
- const result = await vtexFetchWithCookies<OrderForm>(
385
- `/api/checkout/pub/orderForm/${orderFormId}/selectable-gifts/${giftId}`,
386
- {
387
- method: "POST",
388
- body: JSON.stringify({
389
- expectedOrderFormSections,
390
- selectedGifts,
391
- id: giftId,
392
- }),
393
- },
394
- );
395
- return forceHttpsOnAssets(result);
396
- }
397
-
398
- // ---------------------------------------------------------------------------
399
- // Cart — installments
400
- // ---------------------------------------------------------------------------
401
-
402
- export interface GetInstallmentsProps {
403
- orderFormId: string;
404
- paymentSystem: number;
405
- }
406
-
407
- export async function getInstallments(props: GetInstallmentsProps) {
408
- const { orderFormId, paymentSystem } = props;
409
- const params = new URLSearchParams({ paymentSystem: String(paymentSystem) });
410
- appendSc(params);
411
- return vtexFetchWithCookies<any>(
412
- `/api/checkout/pub/orderForm/${orderFormId}/installments?${params}`,
413
- );
414
- }
415
-
416
- // ---------------------------------------------------------------------------
417
- // Cart — profile & messages
418
- // ---------------------------------------------------------------------------
419
-
420
- export interface UpdateOrderFormProfileProps {
421
- orderFormId: string;
422
- fields: Record<string, unknown>;
423
- ignoreProfileData?: boolean;
424
- }
425
-
426
- export async function updateOrderFormProfile(
427
- props: UpdateOrderFormProfileProps,
428
- ): Promise<OrderForm> {
429
- const { orderFormId, fields, ignoreProfileData } = props;
430
- const body = ignoreProfileData ? { ...fields, ignoreProfileData: true } : fields;
431
- const result = await vtexFetchWithCookies<OrderForm>(
432
- `/api/checkout/pub/orderForm/${orderFormId}/profile`,
433
- { method: "PATCH", body: JSON.stringify(body) },
434
- );
435
- return forceHttpsOnAssets(result);
436
- }
437
-
438
- export interface ChangeToAnonymousUserProps {
439
- orderFormId: string;
440
- }
441
-
442
- export async function changeToAnonymousUser(props: ChangeToAnonymousUserProps): Promise<OrderForm> {
443
- const { orderFormId } = props;
444
- // This endpoint rotates the orderForm ownership cookies — must use
445
- // vtexFetchWithCookies so the new cookies reach the browser.
446
- return vtexFetchWithCookies<OrderForm>(`/api/checkout/changeToAnonymousUser/${orderFormId}`);
447
- }
448
-
449
- export interface ClearOrderFormMessagesProps {
450
- orderFormId: string;
451
- }
452
-
453
- export async function clearOrderFormMessages(
454
- props: ClearOrderFormMessagesProps,
455
- ): Promise<OrderForm> {
456
- const { orderFormId } = props;
457
- return vtexFetchWithCookies<OrderForm>(
458
- `/api/checkout/pub/orderForm/${orderFormId}/messages/clear`,
459
- {
460
- method: "POST",
461
- body: JSON.stringify({}),
462
- },
463
- );
464
- }
465
-
466
- // ---------------------------------------------------------------------------
467
- // Shipping / Regions
468
- // ---------------------------------------------------------------------------
469
-
470
- export interface Seller {
471
- id: string;
472
- name: string;
473
- }
474
-
475
- export interface RegionResult {
476
- id: string;
477
- sellers: Seller[];
478
- }
479
-
480
- export interface GetSellersByRegionProps {
481
- postalCode: string;
482
- salesChannel?: string;
483
- }
484
-
485
- export async function getSellersByRegion(
486
- props: GetSellersByRegionProps,
487
- ): Promise<RegionResult | null> {
488
- const { postalCode, salesChannel } = props;
489
- const params = new URLSearchParams({ country: "BRA", postalCode });
490
- const sc = salesChannel ?? getVtexConfig().salesChannel;
491
- if (sc) params.set("sc", sc);
492
- const resp = await vtexFetchWithCookies<RegionResult[]>(`/api/checkout/pub/regions/?${params}`);
493
- return resp[0]?.sellers?.length > 0 ? resp[0] : null;
494
- }
495
-
496
- export interface SetShippingPostalCodeProps {
497
- orderFormId: string;
498
- postalCode: string;
499
- country?: string;
500
- }
501
-
502
- export async function setShippingPostalCode(props: SetShippingPostalCodeProps): Promise<boolean> {
503
- const { orderFormId, postalCode, country = "BRA" } = props;
504
- try {
505
- // VTEX docs note that /attachments/shippingData can rotate the
506
- // CheckoutOrderFormOwnership cookie. vtexFetchWithCookies ensures
507
- // any such Set-Cookie reaches the browser via RequestContext,
508
- // keeping the storefront and VTEX bound to the same orderForm.
509
- await vtexFetchWithCookies<any>(
510
- `/api/checkout/pub/orderForm/${orderFormId}/attachments/shippingData`,
511
- {
512
- method: "POST",
513
- body: JSON.stringify({
514
- selectedAddresses: [{ postalCode, country }],
515
- }),
516
- },
517
- );
518
- return true;
519
- } catch {
520
- return false;
521
- }
522
- }
523
-
524
- // ---------------------------------------------------------------------------
525
- // Cart v2 — granular sections + server-side projection
526
- //
527
- // The v1 CRUD above always returns the raw OrderForm with all 15 sections.
528
- // The v2 variants let the caller pick (a) which `sections` VTEX computes and
529
- // (b) which `projection` reaches the client — defaulting to the minimum
530
- // (`SECTIONS_MINIMAL` + `"summary+items"`). They are additive: v1 stays for
531
- // back-compat, v2 is opt-in via the new loaders/hooks.
532
- //
533
- // @see `@decocms/apps-commerce/types/cart` for the contract.
534
- // ---------------------------------------------------------------------------
535
-
536
- /** Shared fragmentation knobs for every v2 cart operation. */
537
- export interface CartV2Options {
538
- /** Sections to request from VTEX. Defaults to the projection's preset. */
539
- sections?: CartSection[];
540
- /** Client-facing shape. Default: `"summary+items"`. */
541
- projection?: CartProjection;
542
- /** Storefront overrides forwarded when `projection === "minicart"`. */
543
- minicartOptions?: ProjectOrderFormOptions;
544
- }
545
-
546
- function resolveFragmentation(opts: CartV2Options): {
547
- sections: string[];
548
- projection: CartProjection;
549
- } {
550
- const projection = opts.projection ?? "summary+items";
551
- const sections = opts.sections ?? defaultSectionsFor(projection);
552
- return { sections, projection };
553
- }
554
-
555
- export interface GetOrCreateCartV2Props extends CartV2Options {
556
- orderFormId?: string;
557
- }
558
-
559
- /**
560
- * v2 of {@link getOrCreateCart}. Fetches (or creates, when no id) the cart with
561
- * only the requested sections and returns the requested projection.
562
- */
563
- export async function getOrCreateCartV2(
564
- props: GetOrCreateCartV2Props,
565
- ): Promise<VtexCartProjectionResult> {
566
- const { orderFormId } = props;
567
- const { sections, projection } = resolveFragmentation(props);
568
- const sc = scParam();
569
- const body = JSON.stringify({ expectedOrderFormSections: sections });
570
-
571
- const result = orderFormId
572
- ? await vtexFetchWithCookies<OrderForm>(
573
- `/api/checkout/pub/orderForm/${orderFormId}${sc ? `?${sc}` : ""}`,
574
- { method: "POST", body },
575
- )
576
- : await vtexFetchWithCookies<OrderForm>(`/api/checkout/pub/orderForm${sc ? `?${sc}` : ""}`, {
577
- method: "POST",
578
- body,
579
- });
580
- return projectOrderForm(forceHttpsOnAssets(result), projection, props.minicartOptions);
581
- }
582
-
583
- export interface AddItemsToCartV2Props extends CartV2Options {
584
- orderFormId: string;
585
- orderItems: Array<{
586
- id: string;
587
- seller: string;
588
- quantity: number;
589
- index?: number;
590
- price?: number;
591
- }>;
592
- allowedOutdatedData?: string[];
593
- }
594
-
595
- /** v2 of {@link addItemsToCart} — fragmented request + projected response. */
596
- export async function addItemsToCartV2(
597
- props: AddItemsToCartV2Props,
598
- ): Promise<VtexCartProjectionResult> {
599
- const { orderFormId, orderItems, allowedOutdatedData = ["paymentData"] } = props;
600
- const { sections, projection } = resolveFragmentation(props);
601
- const params = appendSc(new URLSearchParams());
602
- for (const d of allowedOutdatedData) params.append("allowedOutdatedData", d);
603
- const result = await vtexFetchWithCookies<OrderForm>(
604
- `/api/checkout/pub/orderForm/${orderFormId}/items?${params}`,
605
- {
606
- method: "POST",
607
- body: JSON.stringify({ orderItems, expectedOrderFormSections: sections }),
608
- },
609
- );
610
- return projectOrderForm(forceHttpsOnAssets(result), projection, props.minicartOptions);
611
- }
612
-
613
- export interface UpdateCartItemsV2Props extends CartV2Options {
614
- orderFormId: string;
615
- orderItems: Array<{ index: number; quantity: number }>;
616
- allowedOutdatedData?: string[];
617
- noSplitItem?: boolean;
618
- }
619
-
620
- /** v2 of {@link updateCartItems} — used for quantity change and item removal (`quantity: 0`). */
621
- export async function updateCartItemsV2(
622
- props: UpdateCartItemsV2Props,
623
- ): Promise<VtexCartProjectionResult> {
624
- const { orderFormId, orderItems, allowedOutdatedData = ["paymentData"], noSplitItem } = props;
625
- const { sections, projection } = resolveFragmentation(props);
626
- const params = appendSc(new URLSearchParams());
627
- for (const d of allowedOutdatedData) params.append("allowedOutdatedData", d);
628
- const result = await vtexFetchWithCookies<OrderForm>(
629
- `/api/checkout/pub/orderForm/${orderFormId}/items/update?${params}`,
630
- {
631
- method: "POST",
632
- body: JSON.stringify({
633
- orderItems,
634
- noSplitItem: Boolean(noSplitItem),
635
- expectedOrderFormSections: sections,
636
- }),
637
- },
638
- );
639
- return projectOrderForm(forceHttpsOnAssets(result), projection, props.minicartOptions);
640
- }
641
-
642
- export interface AddCouponToCartV2Props extends CartV2Options {
643
- orderFormId: string;
644
- text: string;
645
- }
646
-
647
- /** v2 of {@link addCouponToCart} — a coupon typically needs the drawer projection. */
648
- export async function addCouponToCartV2(
649
- props: AddCouponToCartV2Props,
650
- ): Promise<VtexCartProjectionResult> {
651
- const { orderFormId, text } = props;
652
- const { sections, projection } = resolveFragmentation(props);
653
- const sc = scParam();
654
- const result = await vtexFetchWithCookies<OrderForm>(
655
- `/api/checkout/pub/orderForm/${orderFormId}/coupons${sc ? `?${sc}` : ""}`,
656
- { method: "POST", body: JSON.stringify({ text, expectedOrderFormSections: sections }) },
657
- );
658
- return projectOrderForm(forceHttpsOnAssets(result), projection, props.minicartOptions);
659
- }
@@ -1,11 +0,0 @@
1
- export * from "./address";
2
- export * from "./auth";
3
- export * from "./checkout";
4
- export * from "./masterData";
5
- export * from "./misc";
6
- export * from "./newsletter";
7
- export * from "./orders";
8
- export * from "./profile";
9
- export * from "./session";
10
- export * from "./trigger";
11
- export * from "./wishlist";