@jay-framework/wix-stores 0.19.7 → 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.
@@ -4,9 +4,7 @@ import { makeJayStackComponent, makeJayInit } from "@jay-framework/fullstack-com
4
4
  import { registerReactiveGlobalContext, createSignal, createMemo, createEffect } from "@jay-framework/component";
5
5
  import { patch, REPLACE } from "@jay-framework/json-patch";
6
6
  import { createJayContext, useGlobalContext } from "@jay-framework/runtime";
7
- import { WIX_CLIENT_CONTEXT } from "@jay-framework/wix-server-client/client";
8
- import { productsV3 } from "@wix/stores";
9
- import "@wix/categories";
7
+ import { wixFetch, WIX_CLIENT_CONTEXT } from "@jay-framework/wix-server-client/client";
10
8
  import { createActionCaller } from "@jay-framework/stack-client-runtime";
11
9
  var StockStatus = /* @__PURE__ */ ((StockStatus2) => {
12
10
  StockStatus2[StockStatus2["OUT_OF_STOCK"] = 0] = "OUT_OF_STOCK";
@@ -18,27 +16,145 @@ var Selected = /* @__PURE__ */ ((Selected2) => {
18
16
  Selected2[Selected2["notSelected"] = 1] = "notSelected";
19
17
  return Selected2;
20
18
  })(Selected || {});
21
- const instances = {
22
- productsV3ClientInstance: void 0
23
- };
24
- function getProductsV3Client(wixClient) {
25
- if (!instances.productsV3ClientInstance) {
26
- instances.productsV3ClientInstance = wixClient.use(productsV3);
19
+ function raw(value) {
20
+ return value;
21
+ }
22
+ function rawArray(value) {
23
+ return value;
24
+ }
25
+ function str(value) {
26
+ return value;
27
+ }
28
+ function normalizeProduct(product) {
29
+ if (!product) return product;
30
+ const normalized = { ...product };
31
+ if (normalized.id && !normalized._id) {
32
+ normalized._id = normalized.id;
33
+ }
34
+ const media = raw(normalized.media);
35
+ if (media?.main) {
36
+ const main = raw(media.main);
37
+ const mainImage = raw(main.image);
38
+ normalized.media = {
39
+ ...media,
40
+ main: {
41
+ _id: main.id || main._id,
42
+ url: mainImage?.url || main.url,
43
+ altText: mainImage?.altText || main.altText,
44
+ mediaType: main.mediaType,
45
+ width: mainImage?.width,
46
+ height: mainImage?.height
47
+ }
48
+ };
49
+ }
50
+ const updatedMedia = raw(normalized.media);
51
+ const itemsInfo = raw(updatedMedia?.itemsInfo);
52
+ if (itemsInfo?.items) {
53
+ itemsInfo.items = rawArray(itemsInfo.items).map((item) => {
54
+ const image = raw(item.image);
55
+ return {
56
+ _id: item.id || item._id,
57
+ url: image?.url || item.url,
58
+ altText: image?.altText || item.altText,
59
+ mediaType: item.mediaType,
60
+ width: image?.width,
61
+ height: image?.height
62
+ };
63
+ });
64
+ }
65
+ if (normalized.options) {
66
+ normalized.options = rawArray(normalized.options).map((opt) => {
67
+ const choicesSettings = raw(opt.choicesSettings);
68
+ return {
69
+ ...opt,
70
+ _id: opt.id || opt._id,
71
+ choices: choicesSettings?.choices ? rawArray(choicesSettings.choices).map((c) => {
72
+ const linkedMedia = c.linkedMedia;
73
+ const firstMedia = linkedMedia?.[0];
74
+ const firstMediaImage = firstMedia ? raw(firstMedia.image) : void 0;
75
+ return {
76
+ ...c,
77
+ _id: c.choiceId || c._id,
78
+ value: c.name || c.value,
79
+ media: firstMedia ? {
80
+ _id: firstMedia.id,
81
+ url: str(firstMediaImage?.url) || firstMedia.url,
82
+ mediaType: firstMedia.mediaType
83
+ } : void 0
84
+ };
85
+ }) : void 0
86
+ };
87
+ });
88
+ }
89
+ if (normalized.modifiers) {
90
+ normalized.modifiers = rawArray(normalized.modifiers).map((mod) => {
91
+ const choicesSettings = raw(mod.choicesSettings);
92
+ return {
93
+ ...mod,
94
+ _id: mod.id || mod._id,
95
+ choices: choicesSettings?.choices ? rawArray(choicesSettings.choices).map((c) => ({
96
+ ...c,
97
+ _id: c.choiceId || c._id,
98
+ value: c.name || c.value
99
+ })) : void 0
100
+ };
101
+ });
102
+ }
103
+ if (normalized.infoSections) {
104
+ normalized.infoSections = rawArray(normalized.infoSections).map((s) => ({
105
+ ...s,
106
+ _id: s.id || s._id
107
+ }));
108
+ }
109
+ const variantsInfo = raw(normalized.variantsInfo);
110
+ if (variantsInfo?.variants) {
111
+ variantsInfo.variants = rawArray(variantsInfo.variants).map((v) => ({
112
+ ...v,
113
+ _id: v.id || v._id
114
+ }));
115
+ }
116
+ if (normalized.ribbon) {
117
+ const ribbon = raw(normalized.ribbon);
118
+ normalized.ribbon = {
119
+ ...ribbon,
120
+ _id: ribbon.id || ribbon._id
121
+ };
27
122
  }
28
- return instances.productsV3ClientInstance;
123
+ return normalized;
124
+ }
125
+ async function getProduct(client, productId, options) {
126
+ const params = new URLSearchParams();
127
+ {
128
+ params.set("includeMerchantSpecificData", "true");
129
+ }
130
+ if (options?.fields) {
131
+ for (const f of options.fields) {
132
+ params.append("fields", f);
133
+ }
134
+ }
135
+ const qs = params.toString();
136
+ const result = await wixFetch(
137
+ client,
138
+ `/stores/v3/products/${productId}${qs ? "?" + qs : ""}`,
139
+ {
140
+ method: "GET"
141
+ }
142
+ );
143
+ if (result.product) {
144
+ result.product = normalizeProduct(result.product);
145
+ }
146
+ return result;
29
147
  }
30
148
  const WIX_STORES_CONTEXT = createJayContext("wix:stores");
31
149
  function provideWixStoresContext() {
32
150
  const wixClientContext = useGlobalContext(WIX_CLIENT_CONTEXT);
33
151
  const wixClient = wixClientContext.client;
34
152
  const cartContext = useGlobalContext(WIX_CART_CONTEXT);
35
- const catalogClient = getProductsV3Client(wixClient);
36
153
  const storesContext = registerReactiveGlobalContext(WIX_STORES_CONTEXT, () => {
37
154
  async function addToCart(productId, quantity = 1, selections) {
38
155
  console.log(`[WixStores] Adding to cart: ${productId} x ${quantity}`, selections);
39
- const product = await catalogClient.getProduct(productId, {
40
- fields: ["VARIANT_OPTION_CHOICE_NAMES"]
41
- });
156
+ const result = await getProduct(wixClient, productId, {});
157
+ const product = result.product;
42
158
  let variant = product.variantsInfo?.variants?.find(
43
159
  (v) => v.choices?.every(
44
160
  (choice) => selections?.options?.[choice.optionChoiceIds?.optionId] === choice.optionChoiceIds?.choiceId
package/dist/index.d.ts CHANGED
@@ -6,12 +6,7 @@ import * as _jay_framework_component from '@jay-framework/component';
6
6
  import * as _jay_framework_runtime from '@jay-framework/runtime';
7
7
  import { HTMLElementCollectionProxy, HTMLElementProxy } from '@jay-framework/runtime';
8
8
  import { WixClient } from '@wix/sdk';
9
- import { BuildDescriptors } from '@wix/sdk-types';
10
- import { productsV3, inventoryItemsV3, customizationsV3 } from '@wix/stores';
11
- import { categories } from '@wix/categories';
12
- import { Customization } from '@wix/auto_sdk_stores_customizations-v-3';
13
9
  import { Getter } from '@jay-framework/reactive';
14
- import { OptionChoice } from '@wix/auto_sdk_stores_products-v-3';
15
10
  import { PluginSetupContext, PluginSetupResult, PluginReferencesContext, PluginReferencesResult } from '@jay-framework/stack-server-runtime';
16
11
 
17
12
  declare enum OptionRenderType$2 {
@@ -472,11 +467,42 @@ interface DataExtensionSchema {
472
467
  revision?: string | null;
473
468
  }
474
469
 
470
+ interface VariantChoice {
471
+ optionChoiceIds: {
472
+ optionId: string;
473
+ choiceId: string;
474
+ };
475
+ optionChoiceNames?: {
476
+ optionName?: string;
477
+ choiceName?: string;
478
+ renderType?: string;
479
+ };
480
+ }
481
+ interface Customization {
482
+ _id?: string;
483
+ name?: string;
484
+ productId?: string;
485
+ title?: string;
486
+ customizationType?: string;
487
+ customizationRenderType?: string;
488
+ visible?: boolean;
489
+ choicesSettings?: {
490
+ choices?: CustomizationChoice[];
491
+ };
492
+ }
493
+ interface CustomizationChoice {
494
+ _id?: string;
495
+ name?: string;
496
+ value?: string;
497
+ description?: string;
498
+ colorCode?: string;
499
+ inStock?: boolean;
500
+ surcharge?: number;
501
+ }
502
+
475
503
  interface WixStoresService {
476
- products: BuildDescriptors<typeof productsV3, {}>;
477
- categories: BuildDescriptors<typeof categories, {}>;
478
- inventory: BuildDescriptors<typeof inventoryItemsV3, {}>;
479
- customizations: BuildDescriptors<typeof customizationsV3, {}>;
504
+ /** The authenticated Wix client (server-side, API key auth) */
505
+ wixClient: WixClient;
480
506
  /** URL templates for building canonical links */
481
507
  urls: UrlTemplates;
482
508
  /** Slug of the fallback category for pages without category context */
@@ -487,6 +513,8 @@ interface WixStoresService {
487
513
  getCustomizations(): Promise<Customization[]>;
488
514
  /** Get cached data extension schemas for products. Lazily loaded. */
489
515
  getDataExtensionSchemas(): Promise<DataExtensionSchema[]>;
516
+ /** Get the "All Products" system category ID. Lazily fetched and cached. */
517
+ getAllProductsCategoryId(): Promise<string | null>;
490
518
  }
491
519
  /**
492
520
  * Server service marker for Wix Stores.
@@ -844,7 +872,7 @@ interface InteractiveVariant {
844
872
  sku: string;
845
873
  price: string;
846
874
  strikethroughPrice: string;
847
- choices: OptionChoice[];
875
+ choices: VariantChoice[];
848
876
  mediaId?: string;
849
877
  inventoryStatus: StockStatus;
850
878
  }