@jay-framework/wix-cart 0.20.0 → 0.21.0

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/index.d.ts CHANGED
@@ -1,48 +1,17 @@
1
1
  import { WixClient } from '@wix/sdk';
2
- import { W as WixCartService } from './index.client-C7UwcsSn.js';
3
- export { A as AddToCartOptions, q as CartIndicatorState, n as CartLineItem, C as CartOperationResult, l as CartState, o as CartSummary, R as ReactiveCartIndicator, b as WIX_CART_CONTEXT, a as WIX_CART_SERVICE, c as WixCartContext, d as WixCartInitData, r as cartIndicator, s as cartPage, j as estimateCurrentCartTotalsOrNull, i as getCurrentCartOrNull, h as getEmptyCartState, u as init, e as mapCartSummary, g as mapCartToIndicator, f as mapCartToState, k as mapEstimateTotalsToState, m as mapLineItem, t as miniCart, p as provideWixCartContext } from './index.client-C7UwcsSn.js';
4
- import { currentCart } from '@wix/ecom';
5
- import { BuildDescriptors } from '@wix/sdk-types';
2
+ import { W as WixCartService } from './index.client-Ca5v2viE.js';
3
+ export { A as AddToCartOptions, q as CartIndicatorState, n as CartLineItem, C as CartOperationResult, l as CartState, o as CartSummary, R as ReactiveCartIndicator, b as WIX_CART_CONTEXT, a as WIX_CART_SERVICE, c as WixCartContext, d as WixCartInitData, r as cartIndicator, s as cartPage, j as estimateCurrentCartTotalsOrNull, i as getCurrentCartOrNull, h as getEmptyCartState, u as init, e as mapCartSummary, g as mapCartToIndicator, f as mapCartToState, k as mapEstimateTotalsToState, m as mapLineItem, t as miniCart, p as provideWixCartContext } from './index.client-Ca5v2viE.js';
6
4
  import '@jay-framework/runtime';
7
5
  import '@jay-framework/reactive';
8
- import '@wix/auto_sdk_ecom_current-cart';
9
6
  import '@jay-framework/fullstack-component';
10
7
  import '@jay-framework/component';
11
- import '@wix/redirects';
12
-
13
- /**
14
- * Wix Cart Client Factory
15
- *
16
- * Creates a singleton instance of the Wix Cart client.
17
- * Used by both server service and client context.
18
- */
19
-
20
- /**
21
- * Get a configured Wix eCommerce Current Cart client (singleton)
22
- *
23
- * The Current Cart API allows you to manage the visitor's shopping cart.
24
- *
25
- * @returns Current Cart client instance from @wix/ecom
26
- * @see https://dev.wix.com/docs/sdk/backend-modules/ecom/current-cart/introduction
27
- */
28
- declare function getCurrentCartClient(wixClient: WixClient): BuildDescriptors<typeof currentCart, {}>;
29
8
 
30
9
  /**
31
10
  * Server-side Wix Cart Service
32
11
  *
33
- * Provides access to Wix Cart APIs on the server using API Key authentication.
34
- * This file contains server-only code (registerService).
12
+ * Provides the WixClient for cart operations on the server.
35
13
  */
36
14
 
37
- interface WixCartServiceOptions {
38
- urls?: {
39
- thankYou?: string;
40
- };
41
- }
42
- /**
43
- * Creates, registers, and returns a Wix Cart service instance.
44
- * Called during server initialization.
45
- */
46
- declare function provideWixCartService(wixClient: WixClient, options?: WixCartServiceOptions): WixCartService;
15
+ declare function provideWixCartService(wixClient: WixClient): WixCartService;
47
16
 
48
- export { WixCartService, getCurrentCartClient, provideWixCartService };
17
+ export { WixCartService, provideWixCartService };
package/dist/index.js CHANGED
@@ -1,37 +1,76 @@
1
1
  import { registerService, getService } from "@jay-framework/stack-server-runtime";
2
- import { currentCart } from "@wix/ecom";
3
- import { redirects } from "@wix/redirects";
4
2
  import { createJayService, makeJayStackComponent, RenderPipeline, makeJayInit } from "@jay-framework/fullstack-component";
5
3
  import { createJayContext, useGlobalContext } from "@jay-framework/runtime";
6
4
  import { registerReactiveGlobalContext, createSignal, useReactive, createEvent } from "@jay-framework/component";
7
- import { WIX_CLIENT_CONTEXT, WIX_CLIENT_SERVICE } from "@jay-framework/wix-server-client";
5
+ import { wixFetch, WIX_CLIENT_CONTEXT, WIX_CLIENT_SERVICE } from "@jay-framework/wix-server-client";
8
6
  import * as fs from "fs";
9
7
  import * as path from "path";
10
8
  import * as yaml from "js-yaml";
11
- let currentCartInstance;
12
- function getCurrentCartClient(wixClient) {
13
- if (!currentCartInstance) {
14
- currentCartInstance = wixClient.use(currentCart);
15
- }
16
- return currentCartInstance;
17
- }
18
- let redirectsInstance;
19
- function getRedirectsClient(wixClient) {
20
- if (!redirectsInstance) {
21
- redirectsInstance = wixClient.use(redirects);
22
- }
23
- return redirectsInstance;
24
- }
25
9
  const WIX_CART_SERVICE = createJayService("Wix Cart Service");
26
- function provideWixCartService(wixClient, options) {
27
- const service = {
28
- cart: getCurrentCartClient(wixClient),
29
- redirects: getRedirectsClient(wixClient),
30
- urls: { thankYou: options?.urls?.thankYou || "/thank-you" }
31
- };
10
+ function provideWixCartService(wixClient) {
11
+ const service = { wixClient };
32
12
  registerService(WIX_CART_SERVICE, service);
33
13
  return service;
34
14
  }
15
+ async function getCurrentCart(client) {
16
+ return wixFetch(client, "/ecom/v1/carts/current", { method: "GET" });
17
+ }
18
+ async function addToCurrentCart(client, lineItems) {
19
+ return wixFetch(client, "/ecom/v1/carts/current/add-to-cart", {
20
+ method: "POST",
21
+ body: { lineItems }
22
+ });
23
+ }
24
+ async function removeLineItemsFromCurrentCart(client, lineItemIds) {
25
+ return wixFetch(client, "/ecom/v1/carts/current/remove-line-items", {
26
+ method: "POST",
27
+ body: { lineItemIds }
28
+ });
29
+ }
30
+ async function updateCurrentCartLineItemQuantity(client, lineItems) {
31
+ return wixFetch(client, "/ecom/v1/carts/current/update-line-items-quantity", {
32
+ method: "POST",
33
+ body: {
34
+ lineItems: lineItems.map((item) => ({
35
+ id: item._id,
36
+ quantity: item.quantity
37
+ }))
38
+ }
39
+ });
40
+ }
41
+ async function updateCurrentCart(client, cartInfo) {
42
+ return wixFetch(client, "/ecom/v1/carts/current", {
43
+ method: "PATCH",
44
+ body: { cartInfo }
45
+ });
46
+ }
47
+ async function removeCouponFromCurrentCart(client) {
48
+ return wixFetch(client, "/ecom/v1/carts/current/remove-coupon", {
49
+ method: "POST",
50
+ body: {}
51
+ });
52
+ }
53
+ async function estimateCurrentCartTotals(client) {
54
+ return wixFetch(client, "/ecom/v1/carts/current/estimate-totals", {
55
+ method: "POST",
56
+ body: {}
57
+ });
58
+ }
59
+ async function createCheckoutFromCurrentCart(client) {
60
+ return wixFetch(client, "/ecom/v1/carts/current/create-checkout", {
61
+ method: "POST",
62
+ body: {}
63
+ });
64
+ }
65
+ async function createRedirectSession(client, options) {
66
+ return wixFetch(client, "/redirects-api/v1/redirect-session", {
67
+ method: "POST",
68
+ body: options
69
+ });
70
+ }
71
+ function stripWixMediaResize(url) {
72
+ return url.replace(/\/v1\/(?:fit|fill|crop)\/[^/]+\/file\.\w+$/, "");
73
+ }
35
74
  function parseWixMediaUrl(url) {
36
75
  if (!url) return null;
37
76
  const match = url.match(
@@ -68,7 +107,10 @@ function formatWixMediaUrl(_id, url, resize) {
68
107
  }
69
108
  }
70
109
  if ((url == null ? void 0 : url.startsWith("http://")) || (url == null ? void 0 : url.startsWith("https://"))) {
71
- return url;
110
+ return stripWixMediaResize(url) + resizeFragment;
111
+ }
112
+ if (_id) {
113
+ return `https://static.wixstatic.com/media/${_id}${resizeFragment}`;
72
114
  }
73
115
  return "";
74
116
  }
@@ -80,16 +122,21 @@ function mapLineItem(item) {
80
122
  const variantParts = descriptionLines.filter((line) => line.name?.translated).map(
81
123
  (line) => `${line.name?.translated}: ${line.colorInfo?.translated || line.plainText?.translated || ""}`
82
124
  );
83
- const slug = item.url?.split("/").pop() || "";
125
+ const rawUrl = item.url;
126
+ const urlString = typeof rawUrl === "string" ? rawUrl : rawUrl?.relativePath || rawUrl?.url || "";
127
+ const slug = urlString.split("/").pop() || "";
128
+ const rawImage = item.image;
129
+ const imageUrl = typeof rawImage === "string" ? rawImage : rawImage?.url || "";
130
+ const imageId = typeof rawImage === "string" ? "" : rawImage?.id || "";
84
131
  return {
85
- lineItemId: item._id || "",
132
+ lineItemId: item._id || item.id || "",
86
133
  productId: catalogRef?.catalogItemId || "",
87
134
  productName: item.productName?.translated || item.productName?.original || "",
88
135
  productUrl: slug ? `/products/${slug}` : "",
89
136
  variantName: variantParts.join(" / "),
90
137
  sku: physicalProperties?.sku || "",
91
138
  image: {
92
- url: formatWixMediaUrl("", item.image || ""),
139
+ url: formatWixMediaUrl(imageId, imageUrl),
93
140
  altText: item.productName?.translated || ""
94
141
  },
95
142
  quantity: item.quantity || 1,
@@ -167,7 +214,7 @@ function mapCartToState(cart) {
167
214
  const lineItems = (cart.lineItems || []).map(mapLineItem);
168
215
  const appliedCoupon = cart.appliedDiscounts?.find((d) => d.coupon?.code)?.coupon?.code || "";
169
216
  return {
170
- cartId: cart._id || "",
217
+ cartId: cart._id || cart.id || "",
171
218
  isEmpty: lineItems.length === 0,
172
219
  lineItems,
173
220
  summary: mapCartSummary(cart),
@@ -207,10 +254,10 @@ function isCartNotFoundError(error) {
207
254
  if (message.includes("not found") && message.includes("cart")) return true;
208
255
  return false;
209
256
  }
210
- async function getCurrentCartOrNull(cartClient) {
257
+ async function getCurrentCartOrNull(wixClient) {
211
258
  try {
212
- const response = await cartClient.getCurrentCart();
213
- return response ?? null;
259
+ const response = await getCurrentCart(wixClient);
260
+ return response.cart ?? null;
214
261
  } catch (error) {
215
262
  if (isCartNotFoundError(error)) {
216
263
  return null;
@@ -218,9 +265,9 @@ async function getCurrentCartOrNull(cartClient) {
218
265
  throw error;
219
266
  }
220
267
  }
221
- async function estimateCurrentCartTotalsOrNull(cartClient) {
268
+ async function estimateCurrentCartTotalsOrNull(wixClient) {
222
269
  try {
223
- const response = await cartClient.estimateCurrentCartTotals({});
270
+ const response = await estimateCurrentCartTotals(wixClient);
224
271
  return response ?? null;
225
272
  } catch (error) {
226
273
  if (isCartNotFoundError(error)) {
@@ -242,7 +289,7 @@ function mapEstimateTotalsToState(estimate) {
242
289
  const discountAmount = parseFloat(priceSummary?.discount?.amount || "0");
243
290
  const hasTax = parseFloat(taxSummary?.totalTax?.amount || "0") > 0;
244
291
  return {
245
- cartId: cart._id || "",
292
+ cartId: cart._id || cart.id || "",
246
293
  isEmpty: lineItems.length === 0,
247
294
  lineItems,
248
295
  summary: {
@@ -276,8 +323,6 @@ const WIX_CART_CONTEXT = createJayContext("wix:cart");
276
323
  function provideWixCartContext(thankYouUrl = "/thank-you") {
277
324
  const wixClientContext = useGlobalContext(WIX_CLIENT_CONTEXT);
278
325
  const wixClient = wixClientContext.client;
279
- const cartClient = getCurrentCartClient(wixClient);
280
- const redirectsClient = getRedirectsClient(wixClient);
281
326
  const cartContext = registerReactiveGlobalContext(WIX_CART_CONTEXT, () => {
282
327
  const [itemCount, setItemCount] = createSignal(0);
283
328
  const [hasItems, setHasItems] = createSignal(false);
@@ -291,77 +336,74 @@ function provideWixCartContext(thankYouUrl = "/thank-you") {
291
336
  });
292
337
  }
293
338
  async function refreshCartIndicator() {
294
- const cart = await getCurrentCartOrNull(cartClient);
339
+ const cart = await getCurrentCartOrNull(wixClient);
295
340
  updateIndicatorFromCart(cart);
296
341
  }
297
342
  async function getEstimatedCart() {
298
- const estimate = await estimateCurrentCartTotalsOrNull(cartClient);
343
+ const estimate = await estimateCurrentCartTotalsOrNull(wixClient);
299
344
  return mapEstimateTotalsToState(estimate);
300
345
  }
301
346
  async function addToCart(productId, quantity = 1, options) {
302
347
  console.log(`[WixCart] Adding to cart: ${productId} x ${quantity}`, options);
348
+ const catalogOptions = {};
349
+ if (options?.variantId) catalogOptions.variantId = options.variantId;
350
+ if (options?.modifiers) catalogOptions.options = options.modifiers;
351
+ if (options?.customTextFields)
352
+ catalogOptions.customTextFields = options.customTextFields;
303
353
  const lineItem = {
304
354
  catalogReference: {
305
355
  catalogItemId: productId,
306
356
  appId: WIX_STORES_APP_ID,
307
- options: {
308
- variantId: options?.variantId,
309
- options: options?.modifiers,
310
- customTextFields: options?.customTextFields
311
- }
357
+ ...Object.keys(catalogOptions).length > 0 ? { options: catalogOptions } : {}
312
358
  },
313
359
  quantity
314
360
  };
315
- const result = await cartClient.addToCurrentCart({
316
- lineItems: [lineItem]
317
- });
361
+ const result = await addToCurrentCart(wixClient, [lineItem]);
318
362
  updateIndicatorFromCart(result.cart ?? null);
319
363
  onItemAddedToCart.emit();
320
364
  return { cartState: mapCartToState(result.cart ?? null) };
321
365
  }
322
366
  async function removeLineItems(lineItemIds) {
323
- const result = await cartClient.removeLineItemsFromCurrentCart(lineItemIds);
367
+ const result = await removeLineItemsFromCurrentCart(wixClient, lineItemIds);
324
368
  updateIndicatorFromCart(result.cart ?? null);
325
369
  return { cartState: mapCartToState(result.cart ?? null) };
326
370
  }
327
371
  async function updateLineItemQuantity(lineItemId, quantity) {
328
372
  let result;
329
373
  if (quantity === 0) {
330
- result = await cartClient.removeLineItemsFromCurrentCart([lineItemId]);
374
+ result = await removeLineItemsFromCurrentCart(wixClient, [lineItemId]);
331
375
  } else {
332
- result = await cartClient.updateCurrentCartLineItemQuantity([
333
- { _id: lineItemId, quantity }
334
- ]);
376
+ result = await updateCurrentCartLineItemQuantity(wixClient, [{ _id: lineItemId, quantity }]);
335
377
  }
336
378
  updateIndicatorFromCart(result.cart ?? null);
337
379
  return { cartState: mapCartToState(result.cart ?? null) };
338
380
  }
339
381
  async function clearCart() {
340
- const cart = await getCurrentCartOrNull(cartClient);
382
+ const cart = await getCurrentCartOrNull(wixClient);
341
383
  if (cart?.lineItems?.length) {
342
384
  const lineItemIds = cart.lineItems.map((item) => item._id || "").filter(Boolean);
343
385
  if (lineItemIds.length > 0) {
344
- await cartClient.removeLineItemsFromCurrentCart(lineItemIds);
386
+ await removeLineItemsFromCurrentCart(wixClient, lineItemIds);
345
387
  }
346
388
  }
347
389
  setItemCount(0);
348
390
  setHasItems(false);
349
391
  }
350
392
  async function applyCoupon(couponCode) {
351
- const result = await cartClient.updateCurrentCart({ couponCode });
393
+ const result = await updateCurrentCart(wixClient, { couponCode });
352
394
  updateIndicatorFromCart(result ?? null);
353
395
  return { cartState: mapCartToState(result ?? null) };
354
396
  }
355
397
  async function removeCoupon() {
356
- const result = await cartClient.removeCouponFromCurrentCart();
398
+ const result = await removeCouponFromCurrentCart(wixClient);
357
399
  updateIndicatorFromCart(result.cart ?? null);
358
400
  return { cartState: mapCartToState(result.cart ?? null) };
359
401
  }
360
402
  async function checkout() {
361
- const { checkoutId } = await cartClient.createCheckoutFromCurrentCart({});
403
+ const { checkoutId } = await createCheckoutFromCurrentCart(wixClient);
362
404
  if (!checkoutId) throw new Error("Failed to create checkout from cart");
363
405
  const postFlowUrl = window.location.origin + thankYouUrl;
364
- const { redirectSession } = await redirectsClient.createRedirectSession({
406
+ const { redirectSession } = await createRedirectSession(wixClient, {
365
407
  ecomCheckout: { checkoutId },
366
408
  callbacks: { postFlowUrl }
367
409
  });
@@ -478,7 +520,7 @@ const init = makeJayInit().withServer(async () => {
478
520
  console.log("[wix-cart] Initializing Wix Cart service...");
479
521
  const wixClient = getService(WIX_CLIENT_SERVICE);
480
522
  const config = loadWixCartConfig();
481
- provideWixCartService(wixClient, { urls: config.urls });
523
+ provideWixCartService(wixClient);
482
524
  console.log("[wix-cart] Server initialization complete");
483
525
  return {
484
526
  enableClientCart: true,
@@ -491,7 +533,6 @@ export {
491
533
  cartIndicator,
492
534
  cartPage,
493
535
  estimateCurrentCartTotalsOrNull,
494
- getCurrentCartClient,
495
536
  getCurrentCartOrNull,
496
537
  getEmptyCartState,
497
538
  init,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jay-framework/wix-cart",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "type": "module",
5
5
  "description": "Wix Cart/Ecom shared package for Jay Framework",
6
6
  "license": "Apache-2.0",
@@ -37,27 +37,24 @@
37
37
  "test": ":"
38
38
  },
39
39
  "dependencies": {
40
- "@jay-framework/component": "^0.20.0",
41
- "@jay-framework/fullstack-component": "^0.20.0",
42
- "@jay-framework/reactive": "^0.20.0",
43
- "@jay-framework/runtime": "^0.20.0",
44
- "@jay-framework/secure": "^0.20.0",
45
- "@jay-framework/stack-client-runtime": "^0.20.0",
46
- "@jay-framework/stack-server-runtime": "^0.20.0",
47
- "@jay-framework/wix-server-client": "^0.20.0",
48
- "@jay-framework/wix-utils": "^0.20.0",
49
- "@wix/ecom": "^1.0.1996",
50
- "@wix/redirects": "^1.0.77",
51
- "@wix/sdk": "^1.21.5",
52
- "@wix/sdk-runtime": "^1.0.11"
40
+ "@jay-framework/component": "^0.21.0",
41
+ "@jay-framework/fullstack-component": "^0.21.0",
42
+ "@jay-framework/reactive": "^0.21.0",
43
+ "@jay-framework/runtime": "^0.21.0",
44
+ "@jay-framework/secure": "^0.21.0",
45
+ "@jay-framework/stack-client-runtime": "^0.21.0",
46
+ "@jay-framework/stack-server-runtime": "^0.21.0",
47
+ "@jay-framework/wix-server-client": "^0.21.0",
48
+ "@jay-framework/wix-utils": "^0.21.0",
49
+ "@wix/sdk": "^1.21.5"
53
50
  },
54
51
  "devDependencies": {
55
52
  "@babel/core": "^7.23.7",
56
53
  "@babel/preset-env": "^7.23.8",
57
54
  "@babel/preset-typescript": "^7.23.3",
58
- "@jay-framework/compiler-jay-stack": "^0.20.0",
59
- "@jay-framework/jay-cli": "^0.20.0",
60
- "@jay-framework/vite-plugin": "^0.20.0",
55
+ "@jay-framework/compiler-jay-stack": "^0.21.0",
56
+ "@jay-framework/jay-cli": "^0.21.0",
57
+ "@jay-framework/vite-plugin": "^0.21.0",
61
58
  "@jay-framework/wix-dev-environment": "^0.6.12",
62
59
  "nodemon": "^3.0.3",
63
60
  "rimraf": "^5.0.5",