@ikas/bp-storefront 1.4.0-beta.164 → 1.4.0-beta.166
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.
|
@@ -92,12 +92,79 @@ export declare function setLocalization(baseStore: BaseStore, localeOption: Ikas
|
|
|
92
92
|
* ```
|
|
93
93
|
*/
|
|
94
94
|
export declare function setLanguage(baseStore: BaseStore, language: IkasLanguageOption): void;
|
|
95
|
+
/**
|
|
96
|
+
* Fetches a single product by id and returns it as a full `IkasProduct`.
|
|
97
|
+
*
|
|
98
|
+
* Use this when a lightweight reference (e.g. a cart line's `IkasOrderLineVariant`, which carries
|
|
99
|
+
* neither `salesChannels` nor `stock`) needs product-level data: the returned product includes
|
|
100
|
+
* `salesChannels` filtered to the active sales channel (with `minQuantityPerCart` /
|
|
101
|
+
* `maxQuantityPerCart`) and per-variant `stock` (summed over the sales channel's configured
|
|
102
|
+
* stock locations). `selectedVariantValues` is seeded from the first active variant (or the
|
|
103
|
+
* first variant when none is active). For multiple ids, prefer `bs_searchProductsById` — one
|
|
104
|
+
* batched request.
|
|
105
|
+
*
|
|
106
|
+
* @ai-category Product, Search
|
|
107
|
+
* @ai-related bs_searchProductsById
|
|
108
|
+
*
|
|
109
|
+
* @param baseStore - The base store instance
|
|
110
|
+
* @param params - `{ productId }` — id of the product to fetch
|
|
111
|
+
* @returns `{ isSuccess, product }` — `product` is null when not found or on failure
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```tsx
|
|
115
|
+
* import { baseStore, bs_searchProductById, IkasStorefrontConfig } from "@ikas/bp-storefront";
|
|
116
|
+
*
|
|
117
|
+
* const { isSuccess, product } = await bs_searchProductById(baseStore, { productId });
|
|
118
|
+
* if (isSuccess && product) {
|
|
119
|
+
* const channel = product.salesChannels?.find(sc => sc.id === IkasStorefrontConfig.salesChannelId);
|
|
120
|
+
* console.log(channel?.maxQuantityPerCart);
|
|
121
|
+
* }
|
|
122
|
+
* ```
|
|
123
|
+
*/
|
|
95
124
|
export declare function bs_searchProductById(baseStore: BaseStore, params: {
|
|
96
125
|
productId: string;
|
|
97
126
|
}): Promise<{
|
|
98
127
|
isSuccess: boolean;
|
|
99
128
|
product: IkasProduct | null;
|
|
100
129
|
}>;
|
|
130
|
+
/**
|
|
131
|
+
* Batch-fetches products by id in a single request and returns them as full `IkasProduct` objects.
|
|
132
|
+
*
|
|
133
|
+
* The go-to function when cart lines (or any lightweight reference) need product-level data they
|
|
134
|
+
* do not carry: `IkasOrderLineVariant` has neither `salesChannels` nor `stock`, while the products
|
|
135
|
+
* returned here include `salesChannels` filtered to the active sales channel (with
|
|
136
|
+
* `minQuantityPerCart` / `maxQuantityPerCart`) and per-variant `stock` (summed over the sales
|
|
137
|
+
* channel's configured stock locations). Collect the distinct `productId`s of the lines and
|
|
138
|
+
* fetch them in ONE call instead of one request per line. Called with an empty `productIds`,
|
|
139
|
+
* it returns `{ isSuccess: true, products: [] }` without a network request.
|
|
140
|
+
*
|
|
141
|
+
* @ai-category Product, Cart, Search
|
|
142
|
+
* @ai-related bs_searchProductById, changeItemQuantity
|
|
143
|
+
*
|
|
144
|
+
* @param baseStore - The base store instance
|
|
145
|
+
* @param params - `{ productIds }` — product ids to fetch (deduplicate before calling)
|
|
146
|
+
* @returns `{ isSuccess, products }` — `products` is null on failure
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```tsx
|
|
150
|
+
* import { baseStore, bs_searchProductsById, cartStore, IkasStorefrontConfig } from "@ikas/bp-storefront";
|
|
151
|
+
*
|
|
152
|
+
* // Enforce per-cart quantity limits on cart lines:
|
|
153
|
+
* const productIds = [
|
|
154
|
+
* ...new Set(
|
|
155
|
+
* (cartStore.cart?.orderLineItems ?? [])
|
|
156
|
+
* .map(item => item.variant.productId)
|
|
157
|
+
* .filter((id): id is string => !!id)
|
|
158
|
+
* ),
|
|
159
|
+
* ];
|
|
160
|
+
* const { products } = await bs_searchProductsById(baseStore, { productIds });
|
|
161
|
+
* const maxByProductId = new Map<string, number | null>();
|
|
162
|
+
* for (const p of products ?? []) {
|
|
163
|
+
* const channel = p.salesChannels?.find(sc => sc.id === IkasStorefrontConfig.salesChannelId);
|
|
164
|
+
* maxByProductId.set(p.id, channel?.maxQuantityPerCart ?? null);
|
|
165
|
+
* }
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
101
168
|
export declare function bs_searchProductsById(baseStore: BaseStore, params: {
|
|
102
169
|
productIds: string[];
|
|
103
170
|
}): Promise<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{__awaiter as n,__generator as i}from'./../../../ext/tslib/tslib.es6.mjs.js';import{IkasStorefrontConfig as t}from"../../../packages/storefront-config/src/index.js";import o from'./../../../ext/lodash/flatten.js';import e from'./../../../ext/lodash/sortBy.js';import{baseStore as r}from"../../../stores/base/index.js";function a(a){r.currentPageType=a,function(){n(this,void 0,void 0,function(){var n,a,c,u,s,d,l,v;return i(this,function(i){switch(i.label){case 0:return[4,import("../../api/location/index.js")];case 1:return[4,(0,i.sent().apiGetMyCountry)()];case 2:return n=i.sent(),a=n.data,n.isSuccess&&a?(c=t.routings.filter(function(n){var i;return null===(i=n.countryCodes)||void 0===i?void 0:i.includes(a)}),c.length?(u=o(t.routings.map(function(n){return n.countryCodes||[]})),[4,import("../../api/location/index.js")]):[2]):[2];case 3:return[4,(0,i.sent().apiListCountry)({iso2:{in:u}})];case 4:return(s=i.sent()).isSuccess?(d=s.data||[],l=[],v=[],t.routings.forEach(function(n){var i;if(null===(i=n.countryCodes)||void 0===i?void 0:i.length)n.countryCodes.forEach(function(i){var t=d.find(function(n){return n.iso2===i});if(!v.some(function(n){return n.iso2===(null==t?void 0:t.iso2)})){var o={id:(null==t?void 0:t.iso2)||(null==t?void 0:t.iso3)||Date.now()+"",countryName:(null==t?void 0:t.native)||(null==t?void 0:t.name)||"",iso2:(null==t?void 0:t.iso2)||void 0,iso3:(null==t?void 0:t.iso3)||void 0,routing:n,isRecommended:i===a};v.push(o)}});else{var o={id:n.id,routing:n,isRecommended:!t.routings.some(function(n){var i;return null===(i=n.countryCodes)||void 0===i?void 0:i.includes(a)})};v.push(o)}}),t.routings.forEach(function(n){var i;if(c.some(function(i){return i.id===n.id})){var o={id:n.id,currencyCode:n.currencyCode,currencySymbol:n.currencySymbol,language:"undefined"!=typeof Intl&&void 0!==Intl.DisplayNames&&new Intl.DisplayNames([],{type:"language"}).of(n.locale)||n.locale,locale:n.locale,isSelected:n.id===(null===(i=t.getCurrentRouting())||void 0===i?void 0:i.id)||!1};l.push(o)}}),r.languageOptions=e(l,"language"),r.localeOptions=e(v,"countryName"),[2]):[2]}})})}()}function c(n,i){var t=s(i.routing);window.location.replace(t)}function u(n,i){var o=t.routings.find(function(n){return n.id===i.id});if(o){var e=s(o);window.location.replace(e)}}function s(n){var i,o,e,a;if(n.domain)return"https://".concat(n.domain);var c=window.location,u=c.origin,s=c.pathname,d=c.search,l=c.hash,v=n.path?"/".concat(n.path):"",f=r.currentPageType;if("CATEGORY"===f||"BRAND"===f||"PRODUCT"===f){var p=null===(i=window.__IKAS_DATA__)||void 0===i?void 0:i.pageSpecificData,m=null===(e=null===(o=null==p?void 0:p.metaData)||void 0===o?void 0:o.translations)||void 0===e?void 0:e.find(function(i){return i.locale===n.locale});if(null==m?void 0:m.slug)return"".concat(u).concat(v,"/").concat(m.slug).concat(d).concat(l)}var g=null===(a=t.getCurrentRouting())||void 0===a?void 0:a.path,h=s;if(g){var y="/".concat(g);s===y?h="/":s.startsWith("".concat(y,"/"))&&(h=s.slice(y.length))}return"".concat(u).concat(v).concat(h).concat(d).concat(l)}function d(t,o){return n(this,void 0,void 0,function(){var n,t,e,r,a;return i(this,function(i){switch(i.label){case 0:return[4,import("../../api/product/index.js")];case 1:return[4,(0,i.sent().apiSearchProducts)({input:{productIdList:[o.productId]}})];case 2:return n=i.sent(),(t=(null===(a=null===(r=n.data)||void 0===r?void 0:r.data)||void 0===a?void 0:a[0])||null)&&(e=t.variants.find(function(n){return n.isActive})
|
|
1
|
+
import{__awaiter as n,__generator as i}from'./../../../ext/tslib/tslib.es6.mjs.js';import{IkasStorefrontConfig as t}from"../../../packages/storefront-config/src/index.js";import o from'./../../../ext/lodash/flatten.js';import e from'./../../../ext/lodash/sortBy.js';import{baseStore as r}from"../../../stores/base/index.js";function a(a){r.currentPageType=a,function(){n(this,void 0,void 0,function(){var n,a,c,u,s,d,l,v;return i(this,function(i){switch(i.label){case 0:return[4,import("../../api/location/index.js")];case 1:return[4,(0,i.sent().apiGetMyCountry)()];case 2:return n=i.sent(),a=n.data,n.isSuccess&&a?(c=t.routings.filter(function(n){var i;return null===(i=n.countryCodes)||void 0===i?void 0:i.includes(a)}),c.length?(u=o(t.routings.map(function(n){return n.countryCodes||[]})),[4,import("../../api/location/index.js")]):[2]):[2];case 3:return[4,(0,i.sent().apiListCountry)({iso2:{in:u}})];case 4:return(s=i.sent()).isSuccess?(d=s.data||[],l=[],v=[],t.routings.forEach(function(n){var i;if(null===(i=n.countryCodes)||void 0===i?void 0:i.length)n.countryCodes.forEach(function(i){var t=d.find(function(n){return n.iso2===i});if(!v.some(function(n){return n.iso2===(null==t?void 0:t.iso2)})){var o={id:(null==t?void 0:t.iso2)||(null==t?void 0:t.iso3)||Date.now()+"",countryName:(null==t?void 0:t.native)||(null==t?void 0:t.name)||"",iso2:(null==t?void 0:t.iso2)||void 0,iso3:(null==t?void 0:t.iso3)||void 0,routing:n,isRecommended:i===a};v.push(o)}});else{var o={id:n.id,routing:n,isRecommended:!t.routings.some(function(n){var i;return null===(i=n.countryCodes)||void 0===i?void 0:i.includes(a)})};v.push(o)}}),t.routings.forEach(function(n){var i;if(c.some(function(i){return i.id===n.id})){var o={id:n.id,currencyCode:n.currencyCode,currencySymbol:n.currencySymbol,language:"undefined"!=typeof Intl&&void 0!==Intl.DisplayNames&&new Intl.DisplayNames([],{type:"language"}).of(n.locale)||n.locale,locale:n.locale,isSelected:n.id===(null===(i=t.getCurrentRouting())||void 0===i?void 0:i.id)||!1};l.push(o)}}),r.languageOptions=e(l,"language"),r.localeOptions=e(v,"countryName"),[2]):[2]}})})}()}function c(n,i){var t=s(i.routing);window.location.replace(t)}function u(n,i){var o=t.routings.find(function(n){return n.id===i.id});if(o){var e=s(o);window.location.replace(e)}}function s(n){var i,o,e,a;if(n.domain)return"https://".concat(n.domain);var c=window.location,u=c.origin,s=c.pathname,d=c.search,l=c.hash,v=n.path?"/".concat(n.path):"",f=r.currentPageType;if("CATEGORY"===f||"BRAND"===f||"PRODUCT"===f){var p=null===(i=window.__IKAS_DATA__)||void 0===i?void 0:i.pageSpecificData,m=null===(e=null===(o=null==p?void 0:p.metaData)||void 0===o?void 0:o.translations)||void 0===e?void 0:e.find(function(i){return i.locale===n.locale});if(null==m?void 0:m.slug)return"".concat(u).concat(v,"/").concat(m.slug).concat(d).concat(l)}var g=null===(a=t.getCurrentRouting())||void 0===a?void 0:a.path,h=s;if(g){var y="/".concat(g);s===y?h="/":s.startsWith("".concat(y,"/"))&&(h=s.slice(y.length))}return"".concat(u).concat(v).concat(h).concat(d).concat(l)}function d(t,o){return n(this,void 0,void 0,function(){var n,t,e,r,a;return i(this,function(i){switch(i.label){case 0:return[4,import("../../api/product/index.js")];case 1:return[4,(0,i.sent().apiSearchProducts)({input:{productIdList:[o.productId]}})];case 2:return n=i.sent(),(t=(null===(a=null===(r=n.data)||void 0===r?void 0:r.data)||void 0===a?void 0:a[0])||null)&&(e=t.variants.find(function(n){return n.isActive})||t.variants[0])&&(t.selectedVariantValues=e.variantValues),[2,{isSuccess:n.isSuccess,product:t}]}})})}function l(t,o){return n(this,void 0,void 0,function(){var n,t,e;return i(this,function(i){switch(i.label){case 0:return o.productIds.length?[4,import("../../api/product/index.js")]:[2,{isSuccess:!0,products:[]}];case 1:return[4,(0,i.sent().apiSearchProducts)({input:{productIdList:o.productIds}})];case 2:return n=i.sent(),(t=(null===(e=n.data)||void 0===e?void 0:e.data)||null)&&t.forEach(function(n){var i=n.variants.find(function(n){return n.isActive})||n.variants[0];i&&(n.selectedVariantValues=i.variantValues)}),[2,{isSuccess:n.isSuccess,products:t}]}})})}export{d as bs_searchProductById,l as bs_searchProductsById,a as initBaseStore,u as setLanguage,c as setLocalization};
|