@decocms/apps-vtex 8.0.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.
- package/package.json +7 -52
- package/src/index.ts +12 -15
- package/src/utils/constants.ts +2 -56
- package/src/utils/cookieSanitizer.ts +59 -59
- package/src/utils/operationRouter.ts +76 -72
- package/src/utils/types.ts +709 -1557
- package/src/vtexClient.ts +444 -0
- package/src/README.md +0 -6
- package/src/__tests__/client-segment-cookie.test.ts +0 -255
- package/src/__tests__/client-set-cookie-forward.test.ts +0 -257
- package/src/__tests__/schemas.test.ts +0 -71
- package/src/actions/address.ts +0 -250
- package/src/actions/analytics/sendEvent.ts +0 -86
- package/src/actions/auth.ts +0 -333
- package/src/actions/checkout.ts +0 -659
- package/src/actions/index.ts +0 -11
- package/src/actions/masterData.ts +0 -168
- package/src/actions/misc.ts +0 -188
- package/src/actions/newsletter.ts +0 -105
- package/src/actions/orders.ts +0 -34
- package/src/actions/profile.ts +0 -201
- package/src/actions/session.ts +0 -85
- package/src/actions/trigger.ts +0 -43
- package/src/actions/wishlist.ts +0 -114
- package/src/client.ts +0 -667
- package/src/commerceLoaders.ts +0 -267
- package/src/hooks/__tests__/createCart.render.test.ts +0 -114
- package/src/hooks/__tests__/createCart.test.ts +0 -92
- package/src/hooks/__tests__/createUseCart.test.ts +0 -184
- package/src/hooks/__tests__/createUseUser.test.ts +0 -48
- package/src/hooks/__tests__/createUseWishlist.test.ts +0 -87
- package/src/hooks/cartQuery.ts +0 -204
- package/src/hooks/createCart.ts +0 -427
- package/src/hooks/createUseCart.ts +0 -360
- package/src/hooks/createUseUser.ts +0 -153
- package/src/hooks/createUseWishlist.ts +0 -242
- package/src/hooks/index.ts +0 -25
- package/src/hooks/useAutocomplete.ts +0 -83
- package/src/hooks/useCart.ts +0 -243
- package/src/hooks/useUser.ts +0 -78
- package/src/hooks/useWishlist.ts +0 -119
- package/src/invoke.ts +0 -235
- package/src/loaders/ProductDetailsPage.ts +0 -1
- package/src/loaders/ProductList.ts +0 -1
- package/src/loaders/ProductListingPage.ts +0 -1
- package/src/loaders/address.ts +0 -115
- package/src/loaders/autocomplete.ts +0 -58
- package/src/loaders/brands.ts +0 -44
- package/src/loaders/cart/attachments.ts +0 -53
- package/src/loaders/cart/full.ts +0 -67
- package/src/loaders/cart/gifts.ts +0 -43
- package/src/loaders/cart/orderFormId.ts +0 -31
- package/src/loaders/cart/shipping.test.ts +0 -125
- package/src/loaders/cart/shipping.ts +0 -136
- package/src/loaders/cart/summary.ts +0 -36
- package/src/loaders/cart.ts +0 -52
- package/src/loaders/catalog.ts +0 -163
- package/src/loaders/collections.ts +0 -55
- package/src/loaders/index.ts +0 -20
- package/src/loaders/intelligentSearch/__tests__/productListingPage.test.ts +0 -51
- package/src/loaders/intelligentSearch/productDetailsPage.ts +0 -113
- package/src/loaders/intelligentSearch/productList.ts +0 -154
- package/src/loaders/intelligentSearch/productListingPage.ts +0 -526
- package/src/loaders/intelligentSearch/suggestions.ts +0 -46
- package/src/loaders/legacy/productDetailsPage.ts +0 -1
- package/src/loaders/legacy/productList.ts +0 -1
- package/src/loaders/legacy/relatedProductsLoader.ts +0 -81
- package/src/loaders/legacy.ts +0 -635
- package/src/loaders/logistics.ts +0 -111
- package/src/loaders/minicart.ts +0 -78
- package/src/loaders/navbar.ts +0 -26
- package/src/loaders/orders.ts +0 -92
- package/src/loaders/pageType.ts +0 -68
- package/src/loaders/paths/PDPDefaultPath.ts +0 -51
- package/src/loaders/payment.ts +0 -97
- package/src/loaders/productListFull.ts +0 -159
- package/src/loaders/profile.ts +0 -138
- package/src/loaders/promotion.ts +0 -30
- package/src/loaders/search.ts +0 -124
- package/src/loaders/session.ts +0 -83
- package/src/loaders/user.ts +0 -87
- package/src/loaders/wishlist.ts +0 -89
- package/src/loaders/wishlistProducts.ts +0 -69
- package/src/loaders/workflow/products.ts +0 -64
- package/src/loaders/workflow.ts +0 -316
- package/src/logo.png +0 -0
- package/src/manifest.gen.ts +0 -89
- package/src/middleware.ts +0 -240
- package/src/mod.ts +0 -157
- package/src/registry.ts +0 -9
- package/src/schemas.gen.ts +0 -3191
- package/src/schemas.ts +0 -30
- package/src/types.ts +0 -248
- package/src/utils/__tests__/cartProjection.test.ts +0 -118
- package/src/utils/__tests__/cookieSanitizer.test.ts +0 -162
- package/src/utils/__tests__/fetch.test.ts +0 -80
- package/src/utils/__tests__/fetchCache.test.ts +0 -155
- package/src/utils/__tests__/instrumentedFetch.test.ts +0 -158
- package/src/utils/__tests__/intelligentSearch.test.ts +0 -88
- package/src/utils/__tests__/minicart.test.ts +0 -184
- package/src/utils/__tests__/operationRouter.test.ts +0 -227
- package/src/utils/__tests__/resilience.test.ts +0 -205
- package/src/utils/__tests__/resourceRange.test.ts +0 -32
- package/src/utils/__tests__/simulationCache.test.ts +0 -40
- package/src/utils/__tests__/sitemap.test.ts +0 -185
- package/src/utils/__tests__/slugify.test.ts +0 -31
- package/src/utils/__tests__/transform.test.ts +0 -769
- package/src/utils/accountLoaders.ts +0 -203
- package/src/utils/authHelpers.ts +0 -85
- package/src/utils/batch.ts +0 -18
- package/src/utils/cartProjection.ts +0 -96
- package/src/utils/cookies.ts +0 -167
- package/src/utils/enrichment.ts +0 -560
- package/src/utils/fetch.ts +0 -107
- package/src/utils/fetchCache.ts +0 -199
- package/src/utils/index.ts +0 -19
- package/src/utils/instrumentedFetch.ts +0 -97
- package/src/utils/intelligentSearch.ts +0 -96
- package/src/utils/legacy.ts +0 -139
- package/src/utils/minicart.ts +0 -139
- package/src/utils/pickAndOmit.ts +0 -25
- package/src/utils/proxy.ts +0 -435
- package/src/utils/resilience.ts +0 -352
- package/src/utils/resourceRange.ts +0 -10
- package/src/utils/segment.ts +0 -152
- package/src/utils/similars.ts +0 -37
- package/src/utils/simulationCache.ts +0 -73
- package/src/utils/sitemap.ts +0 -282
- package/src/utils/slugCache.ts +0 -28
- package/src/utils/slugify.ts +0 -13
- package/src/utils/transform.ts +0 -1522
- package/src/utils/vtexId.ts +0 -138
- package/tsconfig.json +0 -7
package/src/hooks/useWishlist.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Client-side wishlist hook for VTEX.
|
|
3
|
-
*
|
|
4
|
-
* Reads the wishlist via the invoke proxy and provides
|
|
5
|
-
* add/remove mutations with automatic cache invalidation.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```tsx
|
|
9
|
-
* import { useWishlist } from "@decocms/apps/vtex/hooks/useWishlist";
|
|
10
|
-
*
|
|
11
|
-
* function WishlistButton({ productId, sku }: Props) {
|
|
12
|
-
* const { isInWishlist, toggle, isLoading } = useWishlist();
|
|
13
|
-
* const wishlisted = isInWishlist(productId);
|
|
14
|
-
* return (
|
|
15
|
-
* <button onClick={() => toggle({ productId, sku })} disabled={isLoading}>
|
|
16
|
-
* {wishlisted ? "♥" : "♡"}
|
|
17
|
-
* </button>
|
|
18
|
-
* );
|
|
19
|
-
* }
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
24
|
-
|
|
25
|
-
export interface WishlistItem {
|
|
26
|
-
id: string;
|
|
27
|
-
productId: string;
|
|
28
|
-
sku: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const WISHLIST_QUERY_KEY = ["vtex", "wishlist"] as const;
|
|
32
|
-
|
|
33
|
-
async function fetchWishlist(): Promise<WishlistItem[]> {
|
|
34
|
-
const res = await fetch("/deco/invoke/vtex/loaders/wishlist", {
|
|
35
|
-
method: "POST",
|
|
36
|
-
headers: { "Content-Type": "application/json" },
|
|
37
|
-
body: JSON.stringify({}),
|
|
38
|
-
credentials: "include",
|
|
39
|
-
});
|
|
40
|
-
if (!res.ok) {
|
|
41
|
-
if (res.status === 401) return [];
|
|
42
|
-
throw new Error(`Wishlist fetch failed: ${res.status}`);
|
|
43
|
-
}
|
|
44
|
-
return res.json();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async function addToWishlist(item: { productId: string; sku: string }): Promise<void> {
|
|
48
|
-
const res = await fetch("/deco/invoke/vtex/actions/wishlist/addItem", {
|
|
49
|
-
method: "POST",
|
|
50
|
-
headers: { "Content-Type": "application/json" },
|
|
51
|
-
body: JSON.stringify(item),
|
|
52
|
-
credentials: "include",
|
|
53
|
-
});
|
|
54
|
-
if (!res.ok) throw new Error(`Add to wishlist failed: ${res.status}`);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
async function removeFromWishlist(id: string): Promise<void> {
|
|
58
|
-
const res = await fetch("/deco/invoke/vtex/actions/wishlist/removeItem", {
|
|
59
|
-
method: "POST",
|
|
60
|
-
headers: { "Content-Type": "application/json" },
|
|
61
|
-
body: JSON.stringify({ id }),
|
|
62
|
-
credentials: "include",
|
|
63
|
-
});
|
|
64
|
-
if (!res.ok) throw new Error(`Remove from wishlist failed: ${res.status}`);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export interface UseWishlistOptions {
|
|
68
|
-
enabled?: boolean;
|
|
69
|
-
staleTime?: number;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function useWishlist(options?: UseWishlistOptions) {
|
|
73
|
-
const queryClient = useQueryClient();
|
|
74
|
-
|
|
75
|
-
const query = useQuery({
|
|
76
|
-
queryKey: WISHLIST_QUERY_KEY,
|
|
77
|
-
queryFn: fetchWishlist,
|
|
78
|
-
staleTime: options?.staleTime ?? 60_000,
|
|
79
|
-
enabled: options?.enabled !== false,
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
const items = query.data ?? [];
|
|
83
|
-
const productIdSet = new Set(items.map((i) => i.productId));
|
|
84
|
-
|
|
85
|
-
const addMutation = useMutation({
|
|
86
|
-
mutationFn: addToWishlist,
|
|
87
|
-
onSuccess: () => queryClient.invalidateQueries({ queryKey: WISHLIST_QUERY_KEY }),
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
const removeMutation = useMutation({
|
|
91
|
-
mutationFn: removeFromWishlist,
|
|
92
|
-
onSuccess: () => queryClient.invalidateQueries({ queryKey: WISHLIST_QUERY_KEY }),
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
function isInWishlist(productId: string): boolean {
|
|
96
|
-
return productIdSet.has(productId);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function toggle(item: { productId: string; sku: string }) {
|
|
100
|
-
const existing = items.find((i) => i.productId === item.productId);
|
|
101
|
-
if (existing) {
|
|
102
|
-
removeMutation.mutate(existing.id);
|
|
103
|
-
} else {
|
|
104
|
-
addMutation.mutate(item);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return {
|
|
109
|
-
items,
|
|
110
|
-
isLoading: query.isLoading || addMutation.isPending || removeMutation.isPending,
|
|
111
|
-
isError: query.isError,
|
|
112
|
-
isInWishlist,
|
|
113
|
-
toggle,
|
|
114
|
-
add: addMutation,
|
|
115
|
-
remove: removeMutation,
|
|
116
|
-
refetch: query.refetch,
|
|
117
|
-
count: items.length,
|
|
118
|
-
};
|
|
119
|
-
}
|
package/src/invoke.ts
DELETED
|
@@ -1,235 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Typed `invoke.vtex.actions.*` object — generator contract.
|
|
3
|
-
*
|
|
4
|
-
* This file is the source of truth that `@decocms/start/scripts/generate-invoke.ts`
|
|
5
|
-
* scans to emit the site-local `src/server/invoke.gen.ts`. The generator:
|
|
6
|
-
*
|
|
7
|
-
* 1. parses the imports in this file to learn which action lives where,
|
|
8
|
-
* 2. walks `invoke.vtex.actions.*` and extracts each entry's:
|
|
9
|
-
* - validated input type (the arrow function's first parameter),
|
|
10
|
-
* - imported action function (matched against the import map by name),
|
|
11
|
-
* - output type (the outermost `as` cast's `Promise<...>` payload),
|
|
12
|
-
* 3. emits a top-level `createServerFn` per action so TanStack Start's
|
|
13
|
-
* compiler can transform `.handler()` into client RPC stubs (the
|
|
14
|
-
* compiler only walks top-level decls, not factory-returned ones).
|
|
15
|
-
*
|
|
16
|
-
* Every action gets a `forwardResponseCookies()` call in the generated
|
|
17
|
-
* handler — that bridges `Set-Cookie` headers captured by
|
|
18
|
-
* `vtexFetchWithCookies` into TanStack Start's HTTP response. Without it,
|
|
19
|
-
* `checkout.vtex.com` and `CheckoutOrderFormOwnership` never reach the
|
|
20
|
-
* browser, and the storefront's mini-cart drifts away from VTEX's
|
|
21
|
-
* server-side orderForm.
|
|
22
|
-
*
|
|
23
|
-
* To add a new action:
|
|
24
|
-
* 1. Add an entry below with the input/output types and the action call,
|
|
25
|
-
* 2. From a site repo: `npm run generate:invoke`.
|
|
26
|
-
*/
|
|
27
|
-
|
|
28
|
-
import type { CartProjection, CartSection } from "@decocms/apps-commerce/types";
|
|
29
|
-
import { createInvokeFn } from "@decocms/tanstack/sdk/createInvoke";
|
|
30
|
-
import {
|
|
31
|
-
addCouponToCart,
|
|
32
|
-
addCouponToCartV2,
|
|
33
|
-
addItemsToCart,
|
|
34
|
-
addItemsToCartV2,
|
|
35
|
-
getOrCreateCart,
|
|
36
|
-
getOrCreateCartV2,
|
|
37
|
-
getSellersByRegion,
|
|
38
|
-
type RegionResult,
|
|
39
|
-
type SimulationItem,
|
|
40
|
-
setShippingPostalCode,
|
|
41
|
-
simulateCart,
|
|
42
|
-
updateCartItems,
|
|
43
|
-
updateCartItemsV2,
|
|
44
|
-
updateOrderFormAttachment,
|
|
45
|
-
} from "./actions/checkout";
|
|
46
|
-
import { type NotifyMeProps, notifyMe } from "./actions/misc";
|
|
47
|
-
import { type SubscribeProps, subscribe } from "./actions/newsletter";
|
|
48
|
-
import { createSession, editSession, type SessionData } from "./actions/session";
|
|
49
|
-
import type { OrderForm } from "./types";
|
|
50
|
-
import type { VtexCartProjectionResult } from "./utils/cartProjection";
|
|
51
|
-
|
|
52
|
-
// ---------------------------------------------------------------------------
|
|
53
|
-
// invoke.vtex.actions — typed server functions callable from client
|
|
54
|
-
//
|
|
55
|
-
// Action bodies receive the validated input object directly and pass it
|
|
56
|
-
// straight to the action function (which expects a single `props` object).
|
|
57
|
-
// The arrow function body is what the generator parses for "which action
|
|
58
|
-
// is this entry calling" — keep the call site shaped as `actionName(data)`
|
|
59
|
-
// so the matcher in `generate-invoke.ts` picks the right importedFn.
|
|
60
|
-
// ---------------------------------------------------------------------------
|
|
61
|
-
|
|
62
|
-
export const invoke = {
|
|
63
|
-
vtex: {
|
|
64
|
-
actions: {
|
|
65
|
-
// -- Cart (OrderForm CRUD) --------------------------------------------
|
|
66
|
-
|
|
67
|
-
getOrCreateCart: createInvokeFn((data: { orderFormId?: string }) =>
|
|
68
|
-
getOrCreateCart(data),
|
|
69
|
-
) as unknown as (ctx: { data: { orderFormId?: string } }) => Promise<OrderForm>,
|
|
70
|
-
|
|
71
|
-
addItemsToCart: createInvokeFn(
|
|
72
|
-
(data: {
|
|
73
|
-
orderFormId: string;
|
|
74
|
-
orderItems: Array<{
|
|
75
|
-
id: string;
|
|
76
|
-
seller: string;
|
|
77
|
-
quantity: number;
|
|
78
|
-
}>;
|
|
79
|
-
}) => addItemsToCart(data),
|
|
80
|
-
) as unknown as (ctx: {
|
|
81
|
-
data: {
|
|
82
|
-
orderFormId: string;
|
|
83
|
-
orderItems: Array<{
|
|
84
|
-
id: string;
|
|
85
|
-
seller: string;
|
|
86
|
-
quantity: number;
|
|
87
|
-
}>;
|
|
88
|
-
};
|
|
89
|
-
}) => Promise<OrderForm>,
|
|
90
|
-
|
|
91
|
-
updateCartItems: createInvokeFn(
|
|
92
|
-
(data: { orderFormId: string; orderItems: Array<{ index: number; quantity: number }> }) =>
|
|
93
|
-
updateCartItems(data),
|
|
94
|
-
) as unknown as (ctx: {
|
|
95
|
-
data: { orderFormId: string; orderItems: Array<{ index: number; quantity: number }> };
|
|
96
|
-
}) => Promise<OrderForm>,
|
|
97
|
-
|
|
98
|
-
addCouponToCart: createInvokeFn((data: { orderFormId: string; text: string }) =>
|
|
99
|
-
addCouponToCart(data),
|
|
100
|
-
) as unknown as (ctx: { data: { orderFormId: string; text: string } }) => Promise<OrderForm>,
|
|
101
|
-
|
|
102
|
-
// -- Cart v2 (fragmented sections + projected response) ----------------
|
|
103
|
-
|
|
104
|
-
getOrCreateCartV2: createInvokeFn(
|
|
105
|
-
(data: { orderFormId?: string; projection?: CartProjection; sections?: CartSection[] }) =>
|
|
106
|
-
getOrCreateCartV2(data),
|
|
107
|
-
) as unknown as (ctx: {
|
|
108
|
-
data: { orderFormId?: string; projection?: CartProjection; sections?: CartSection[] };
|
|
109
|
-
}) => Promise<VtexCartProjectionResult>,
|
|
110
|
-
|
|
111
|
-
addItemsToCartV2: createInvokeFn(
|
|
112
|
-
(data: {
|
|
113
|
-
orderFormId: string;
|
|
114
|
-
orderItems: Array<{ id: string; seller: string; quantity: number }>;
|
|
115
|
-
projection?: CartProjection;
|
|
116
|
-
sections?: CartSection[];
|
|
117
|
-
}) => addItemsToCartV2(data),
|
|
118
|
-
) as unknown as (ctx: {
|
|
119
|
-
data: {
|
|
120
|
-
orderFormId: string;
|
|
121
|
-
orderItems: Array<{ id: string; seller: string; quantity: number }>;
|
|
122
|
-
projection?: CartProjection;
|
|
123
|
-
sections?: CartSection[];
|
|
124
|
-
};
|
|
125
|
-
}) => Promise<VtexCartProjectionResult>,
|
|
126
|
-
|
|
127
|
-
updateCartItemsV2: createInvokeFn(
|
|
128
|
-
(data: {
|
|
129
|
-
orderFormId: string;
|
|
130
|
-
orderItems: Array<{ index: number; quantity: number }>;
|
|
131
|
-
projection?: CartProjection;
|
|
132
|
-
sections?: CartSection[];
|
|
133
|
-
}) => updateCartItemsV2(data),
|
|
134
|
-
) as unknown as (ctx: {
|
|
135
|
-
data: {
|
|
136
|
-
orderFormId: string;
|
|
137
|
-
orderItems: Array<{ index: number; quantity: number }>;
|
|
138
|
-
projection?: CartProjection;
|
|
139
|
-
sections?: CartSection[];
|
|
140
|
-
};
|
|
141
|
-
}) => Promise<VtexCartProjectionResult>,
|
|
142
|
-
|
|
143
|
-
addCouponToCartV2: createInvokeFn(
|
|
144
|
-
(data: {
|
|
145
|
-
orderFormId: string;
|
|
146
|
-
text: string;
|
|
147
|
-
projection?: CartProjection;
|
|
148
|
-
sections?: CartSection[];
|
|
149
|
-
}) => addCouponToCartV2(data),
|
|
150
|
-
) as unknown as (ctx: {
|
|
151
|
-
data: {
|
|
152
|
-
orderFormId: string;
|
|
153
|
-
text: string;
|
|
154
|
-
projection?: CartProjection;
|
|
155
|
-
sections?: CartSection[];
|
|
156
|
-
};
|
|
157
|
-
}) => Promise<VtexCartProjectionResult>,
|
|
158
|
-
|
|
159
|
-
simulateCart: createInvokeFn(
|
|
160
|
-
(data: { items: SimulationItem[]; postalCode: string; country?: string }) =>
|
|
161
|
-
simulateCart(data),
|
|
162
|
-
),
|
|
163
|
-
|
|
164
|
-
// -- Shipping / Region ------------------------------------------------
|
|
165
|
-
|
|
166
|
-
getSellersByRegion: createInvokeFn((data: { postalCode: string; salesChannel?: string }) =>
|
|
167
|
-
getSellersByRegion(data),
|
|
168
|
-
) as unknown as (ctx: {
|
|
169
|
-
data: { postalCode: string; salesChannel?: string };
|
|
170
|
-
}) => Promise<RegionResult | null>,
|
|
171
|
-
|
|
172
|
-
setShippingPostalCode: createInvokeFn(
|
|
173
|
-
(data: { orderFormId: string; postalCode: string; country?: string }) =>
|
|
174
|
-
setShippingPostalCode(data),
|
|
175
|
-
) as unknown as (ctx: {
|
|
176
|
-
data: { orderFormId: string; postalCode: string; country?: string };
|
|
177
|
-
}) => Promise<boolean>,
|
|
178
|
-
|
|
179
|
-
updateOrderFormAttachment: createInvokeFn(
|
|
180
|
-
(data: { orderFormId: string; attachment: string; body: Record<string, unknown> }) =>
|
|
181
|
-
updateOrderFormAttachment(data),
|
|
182
|
-
) as unknown as (ctx: {
|
|
183
|
-
data: { orderFormId: string; attachment: string; body: Record<string, unknown> };
|
|
184
|
-
}) => Promise<OrderForm>,
|
|
185
|
-
|
|
186
|
-
// -- Session ----------------------------------------------------------
|
|
187
|
-
|
|
188
|
-
createSession: createInvokeFn((data: Record<string, any>) => createSession({ data })),
|
|
189
|
-
|
|
190
|
-
editSession: createInvokeFn((data: { public: Record<string, { value: string }> }) =>
|
|
191
|
-
editSession(data),
|
|
192
|
-
) as unknown as (ctx: {
|
|
193
|
-
data: { public: Record<string, { value: string }> };
|
|
194
|
-
}) => Promise<SessionData>,
|
|
195
|
-
|
|
196
|
-
// -- MasterData -------------------------------------------------------
|
|
197
|
-
//
|
|
198
|
-
// Generic MasterData CRUD (createDocument / getDocument / patchDocument /
|
|
199
|
-
// searchDocuments / uploadAttachment) is deliberately NOT exposed here.
|
|
200
|
-
//
|
|
201
|
-
// Every entry in this object is emitted by @decocms/blocks-cli's
|
|
202
|
-
// generate-invoke.ts as a top-level createServerFn, which TanStack Start
|
|
203
|
-
// compiles into a public, unauthenticated /_serverFn/<hash> endpoint. A
|
|
204
|
-
// generic action that takes a client-supplied `entity` and proxies to the
|
|
205
|
-
// VTEX MasterData API with the app's admin appKey/appToken lets any caller
|
|
206
|
-
// read/enumerate/write arbitrary entities — e.g. searchDocuments({ entity:
|
|
207
|
-
// "CL", ... }) dumps the Clients (customer PII) table. Authentication alone
|
|
208
|
-
// can't make this safe: the generic (entity, filter) shape has no room for
|
|
209
|
-
// per-entity/per-record authorization.
|
|
210
|
-
//
|
|
211
|
-
// The underlying functions remain exported from ./actions/masterData for
|
|
212
|
-
// SERVER-SIDE use. A site that needs MasterData defines a narrow,
|
|
213
|
-
// entity-pinned, validated action in its own src/server/invoke.ts: pin
|
|
214
|
-
// `entity` server-side (never take it from the client), validate input, and
|
|
215
|
-
// for user-scoped data derive identity from the session cookie
|
|
216
|
-
// (parseVtexAuthJwt) rather than a client-supplied id. See `subscribe` /
|
|
217
|
-
// `notifyMe` below for the purpose-specific pattern.
|
|
218
|
-
//
|
|
219
|
-
// generate-invoke.ts also carries a PRIVILEGED_ACTIONS guard that refuses
|
|
220
|
-
// to emit these names even if re-added here, so this stays enforced.
|
|
221
|
-
|
|
222
|
-
// -- Newsletter -------------------------------------------------------
|
|
223
|
-
|
|
224
|
-
subscribe: createInvokeFn((data: SubscribeProps) => subscribe(data)) as unknown as (ctx: {
|
|
225
|
-
data: SubscribeProps;
|
|
226
|
-
}) => Promise<void>,
|
|
227
|
-
|
|
228
|
-
// -- Misc -------------------------------------------------------------
|
|
229
|
-
|
|
230
|
-
notifyMe: createInvokeFn((data: NotifyMeProps) => notifyMe(data)) as unknown as (ctx: {
|
|
231
|
-
data: NotifyMeProps;
|
|
232
|
-
}) => Promise<void>,
|
|
233
|
-
},
|
|
234
|
-
},
|
|
235
|
-
} as const;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default, type PDPProps } from "./intelligentSearch/productDetailsPage";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default, type ProductListProps } from "./intelligentSearch/productList";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { default, type PLPProps } from "./intelligentSearch/productListingPage";
|
package/src/loaders/address.ts
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* VTEX Address API loaders.
|
|
3
|
-
* Pure async functions — require configureVtex() to have been called.
|
|
4
|
-
*
|
|
5
|
-
* Ported from deco-cx/apps:
|
|
6
|
-
* vtex/loaders/address/getAddressByPostalCode.ts
|
|
7
|
-
* vtex/loaders/address/getUserAddresses.ts
|
|
8
|
-
*
|
|
9
|
-
* @see https://developers.vtex.com/docs/api-reference/checkout-api
|
|
10
|
-
*/
|
|
11
|
-
import { vtexFetch, vtexIOGraphQL } from "../client";
|
|
12
|
-
|
|
13
|
-
// ---------------------------------------------------------------------------
|
|
14
|
-
// Types
|
|
15
|
-
// ---------------------------------------------------------------------------
|
|
16
|
-
|
|
17
|
-
export interface PostalAddress {
|
|
18
|
-
"@type": "PostalAddress";
|
|
19
|
-
postalCode?: string;
|
|
20
|
-
addressLocality?: string;
|
|
21
|
-
addressRegion?: string;
|
|
22
|
-
addressCountry?: string;
|
|
23
|
-
streetAddress?: string;
|
|
24
|
-
identifier?: string;
|
|
25
|
-
areaServed?: string;
|
|
26
|
-
description?: string;
|
|
27
|
-
disambiguatingDescription?: string;
|
|
28
|
-
latitude?: number;
|
|
29
|
-
longitude?: number;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface VtexAddress {
|
|
33
|
-
addressId: string;
|
|
34
|
-
addressType: string;
|
|
35
|
-
addressName: string;
|
|
36
|
-
city: string;
|
|
37
|
-
complement: string;
|
|
38
|
-
country: string;
|
|
39
|
-
neighborhood: string;
|
|
40
|
-
number: string;
|
|
41
|
-
postalCode: string;
|
|
42
|
-
geoCoordinates: number[];
|
|
43
|
-
receiverName: string;
|
|
44
|
-
state: string;
|
|
45
|
-
street: string;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// ---------------------------------------------------------------------------
|
|
49
|
-
// getAddressByPostalCode
|
|
50
|
-
// ---------------------------------------------------------------------------
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Look up a postal address by country + postal code (public API).
|
|
54
|
-
* @see https://developers.vtex.com/docs/api-reference/checkout-api#get-/api/checkout/pub/postal-code/-countryCode-/-postalCode-
|
|
55
|
-
*/
|
|
56
|
-
export async function getAddressByPostalCode(
|
|
57
|
-
countryCode: string,
|
|
58
|
-
postalCode: string,
|
|
59
|
-
): Promise<PostalAddress> {
|
|
60
|
-
const data = await vtexFetch<Record<string, any>>(
|
|
61
|
-
`/api/checkout/pub/postal-code/${countryCode}/${postalCode}`,
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
return {
|
|
65
|
-
"@type": "PostalAddress",
|
|
66
|
-
postalCode: data.postalCode,
|
|
67
|
-
addressLocality: data.city,
|
|
68
|
-
addressRegion: data.state,
|
|
69
|
-
addressCountry: data.country,
|
|
70
|
-
streetAddress: data.street || undefined,
|
|
71
|
-
identifier: data.number || undefined,
|
|
72
|
-
areaServed: data.neighborhood || undefined,
|
|
73
|
-
description: data.complement || undefined,
|
|
74
|
-
disambiguatingDescription: data.reference || undefined,
|
|
75
|
-
latitude: data.geoCoordinates?.[0] ?? undefined,
|
|
76
|
-
longitude: data.geoCoordinates?.[1] ?? undefined,
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// ---------------------------------------------------------------------------
|
|
81
|
-
// getUserAddresses (authenticated — VTEX IO GraphQL)
|
|
82
|
-
// ---------------------------------------------------------------------------
|
|
83
|
-
|
|
84
|
-
const USER_ADDRESSES_QUERY = `query Addresses @context(scope: "private") {
|
|
85
|
-
profile {
|
|
86
|
-
cacheId
|
|
87
|
-
addresses {
|
|
88
|
-
addressId: id
|
|
89
|
-
addressType
|
|
90
|
-
addressName
|
|
91
|
-
city
|
|
92
|
-
complement
|
|
93
|
-
country
|
|
94
|
-
neighborhood
|
|
95
|
-
number
|
|
96
|
-
postalCode
|
|
97
|
-
geoCoordinates
|
|
98
|
-
receiverName
|
|
99
|
-
state
|
|
100
|
-
street
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}`;
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Fetch addresses for the currently authenticated user.
|
|
107
|
-
* Requires a valid VTEX auth cookie.
|
|
108
|
-
*/
|
|
109
|
-
export async function getUserAddresses(authCookie: string): Promise<VtexAddress[]> {
|
|
110
|
-
const { profile } = await vtexIOGraphQL<{
|
|
111
|
-
profile: { addresses: VtexAddress[] };
|
|
112
|
-
}>({ query: USER_ADDRESSES_QUERY }, { cookie: authCookie });
|
|
113
|
-
|
|
114
|
-
return profile?.addresses ?? [];
|
|
115
|
-
}
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* VTEX Intelligent Search autocomplete — suggestions + product results.
|
|
3
|
-
*
|
|
4
|
-
* Combines /autocomplete_suggestions/ and /product_search/ in parallel,
|
|
5
|
-
* transforms IS products to schema.org via pickSku + toProduct.
|
|
6
|
-
*/
|
|
7
|
-
import { getVtexConfig, intelligentSearch as vtexIS } from "../client";
|
|
8
|
-
import { pickSku, toProduct as toSchemaProduct } from "../utils/transform";
|
|
9
|
-
|
|
10
|
-
export interface AutocompleteProps {
|
|
11
|
-
query: string;
|
|
12
|
-
count?: number;
|
|
13
|
-
showSponsored?: boolean;
|
|
14
|
-
placement?: string;
|
|
15
|
-
fuzzy?: string;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface AutocompleteResult {
|
|
19
|
-
searches: Array<{ term: string; count: number; attributes?: any[] }>;
|
|
20
|
-
products: any[];
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export async function autocompleteSearch(props: AutocompleteProps): Promise<AutocompleteResult> {
|
|
24
|
-
const query = props.query || "";
|
|
25
|
-
const count = props.count ?? 4;
|
|
26
|
-
if (!query.trim()) return { searches: [], products: [] };
|
|
27
|
-
|
|
28
|
-
try {
|
|
29
|
-
const [suggestionsData, productsData] = await Promise.all([
|
|
30
|
-
vtexIS<{
|
|
31
|
-
searches: Array<{ term: string; count: number; attributes?: any[] }>;
|
|
32
|
-
}>("/autocomplete_suggestions/", { query }),
|
|
33
|
-
vtexIS<{ products: any[] }>("/product_search/", {
|
|
34
|
-
query,
|
|
35
|
-
count: String(count),
|
|
36
|
-
showSponsored: props.showSponsored !== false ? "true" : "false",
|
|
37
|
-
placement: props.placement ?? "top-search",
|
|
38
|
-
fuzzy: props.fuzzy ?? "0",
|
|
39
|
-
}),
|
|
40
|
-
]);
|
|
41
|
-
|
|
42
|
-
const config = getVtexConfig();
|
|
43
|
-
const baseUrl = config.publicUrl
|
|
44
|
-
? `https://${config.publicUrl}`
|
|
45
|
-
: `https://${config.account}.vtexcommercestable.${config.domain ?? "com.br"}`;
|
|
46
|
-
|
|
47
|
-
return {
|
|
48
|
-
searches: suggestionsData.searches ?? [],
|
|
49
|
-
products: (productsData.products ?? []).slice(0, count).map((p: any) => {
|
|
50
|
-
const sku = pickSku(p);
|
|
51
|
-
return toSchemaProduct(p, sku, 0, { baseUrl, priceCurrency: "BRL" });
|
|
52
|
-
}),
|
|
53
|
-
};
|
|
54
|
-
} catch (error) {
|
|
55
|
-
console.error("[vtex] autocompleteSearch error:", error);
|
|
56
|
-
return { searches: [], products: [] };
|
|
57
|
-
}
|
|
58
|
-
}
|
package/src/loaders/brands.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* VTEX brand loaders.
|
|
3
|
-
* Returns schema.org Brand[] matching the original deco-cx/apps format.
|
|
4
|
-
*
|
|
5
|
-
* @see https://developers.vtex.com/docs/api-reference/catalog-api#get-/api/catalog_system/pub/brand/list
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { Brand } from "@decocms/apps-commerce/types";
|
|
9
|
-
import { getVtexConfig, vtexFetch } from "../client";
|
|
10
|
-
import { toBrand } from "../utils/transform";
|
|
11
|
-
import type { Brand as BrandVTEX } from "../utils/types";
|
|
12
|
-
|
|
13
|
-
export interface ListBrandsOpts {
|
|
14
|
-
/** When true, only returns active brands. @default false */
|
|
15
|
-
filterInactive?: boolean;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* List brands from the VTEX catalog, transformed to schema.org Brand format.
|
|
20
|
-
*/
|
|
21
|
-
export async function listBrands(opts?: ListBrandsOpts): Promise<Brand[]> {
|
|
22
|
-
const config = getVtexConfig();
|
|
23
|
-
const baseUrl = `https://${config.account}.vteximg.com.br/arquivos/ids`;
|
|
24
|
-
|
|
25
|
-
const brands = await vtexFetch<BrandVTEX[]>("/api/catalog_system/pub/brand/list");
|
|
26
|
-
|
|
27
|
-
const filtered = opts?.filterInactive ? brands.filter((b) => b.isActive) : brands;
|
|
28
|
-
|
|
29
|
-
return filtered.map((b) => toBrand(b, baseUrl));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Get a single brand by ID, as schema.org Brand.
|
|
34
|
-
*/
|
|
35
|
-
export async function getBrandById(brandId: number): Promise<Brand | null> {
|
|
36
|
-
try {
|
|
37
|
-
const config = getVtexConfig();
|
|
38
|
-
const baseUrl = `https://${config.account}.vteximg.com.br/arquivos/ids`;
|
|
39
|
-
const brand = await vtexFetch<BrandVTEX>(`/api/catalog_system/pub/brand/${brandId}`);
|
|
40
|
-
return toBrand(brand, baseUrl);
|
|
41
|
-
} catch {
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `vtex/loaders/cart/attachments` — one item's attachments + offered slots.
|
|
3
|
-
*
|
|
4
|
-
* For a customization UI (engraving, gift wrap, ...) that only needs a single
|
|
5
|
-
* line's attachment state. Fetches just `items` and returns that item's
|
|
6
|
-
* `attachments` (applied) and `attachmentOfferings` (available slots).
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import { getOrCreateCartV2 } from "../../actions/checkout";
|
|
10
|
-
import type { OrderForm, OrderFormItem } from "../../types";
|
|
11
|
-
import { resolveOrderFormId } from "./orderFormId";
|
|
12
|
-
|
|
13
|
-
export interface CartItemAttachmentsProps {
|
|
14
|
-
orderFormId?: string;
|
|
15
|
-
/** Index of the line whose attachments to read. */
|
|
16
|
-
itemIndex: number;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface CartItemAttachments {
|
|
20
|
-
orderFormId: string | null;
|
|
21
|
-
itemIndex: number;
|
|
22
|
-
attachments: NonNullable<OrderFormItem["attachments"]>;
|
|
23
|
-
attachmentOfferings: NonNullable<OrderFormItem["attachmentOfferings"]>;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export default async function cartItemAttachments(
|
|
27
|
-
props: CartItemAttachmentsProps,
|
|
28
|
-
): Promise<CartItemAttachments> {
|
|
29
|
-
const orderFormId = resolveOrderFormId(props.orderFormId);
|
|
30
|
-
const empty: CartItemAttachments = {
|
|
31
|
-
orderFormId: null,
|
|
32
|
-
itemIndex: props.itemIndex,
|
|
33
|
-
attachments: [],
|
|
34
|
-
attachmentOfferings: [],
|
|
35
|
-
};
|
|
36
|
-
if (!orderFormId) return empty;
|
|
37
|
-
|
|
38
|
-
const orderForm = (await getOrCreateCartV2({
|
|
39
|
-
orderFormId,
|
|
40
|
-
projection: "raw",
|
|
41
|
-
sections: ["items"],
|
|
42
|
-
})) as OrderForm;
|
|
43
|
-
|
|
44
|
-
const item = orderForm.items?.[props.itemIndex];
|
|
45
|
-
if (!item) return { ...empty, orderFormId: orderForm.orderFormId ?? null };
|
|
46
|
-
|
|
47
|
-
return {
|
|
48
|
-
orderFormId: orderForm.orderFormId ?? null,
|
|
49
|
-
itemIndex: props.itemIndex,
|
|
50
|
-
attachments: item.attachments ?? [],
|
|
51
|
-
attachmentOfferings: item.attachmentOfferings ?? [],
|
|
52
|
-
};
|
|
53
|
-
}
|