@behio/storefront-sdk 2.1.0 → 2.2.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.
@@ -478,6 +478,7 @@ var BehioStorefront = class {
478
478
  async request(method, path, options) {
479
479
  try {
480
480
  const data = await this.rawRequest(method, path, options);
481
+ if (path === "/cart" || path.startsWith("/cart/")) emitCartEvent(data);
481
482
  return ok(data);
482
483
  } catch (err2) {
483
484
  if (this.throwOnAvailabilityError && isAvailabilityFailure(err2)) {
@@ -2103,6 +2104,23 @@ var NewsletterModule = class {
2103
2104
  );
2104
2105
  }
2105
2106
  };
2107
+ function emitCartEvent(data) {
2108
+ if (typeof window === "undefined" || typeof window.dispatchEvent !== "function") return;
2109
+ const cart = data;
2110
+ if (!cart || typeof cart !== "object" || !Array.isArray(cart.items)) return;
2111
+ const items = cart.items;
2112
+ const total = typeof cart.grandTotal === "number" ? cart.grandTotal : typeof cart.subtotal === "number" ? cart.subtotal : 0;
2113
+ const detail = {
2114
+ total,
2115
+ currency: typeof cart.currency === "string" ? cart.currency : null,
2116
+ itemCount: items.reduce((n, i) => n + (typeof i?.quantity === "number" ? i.quantity : 1), 0)
2117
+ };
2118
+ try {
2119
+ window.__behioCart = detail;
2120
+ window.dispatchEvent(new CustomEvent("behio:cart", { detail }));
2121
+ } catch {
2122
+ }
2123
+ }
2106
2124
 
2107
2125
  export {
2108
2126
  ProductSort,
@@ -478,6 +478,7 @@ var BehioStorefront = class {
478
478
  async request(method, path, options) {
479
479
  try {
480
480
  const data = await this.rawRequest(method, path, options);
481
+ if (path === "/cart" || path.startsWith("/cart/")) emitCartEvent(data);
481
482
  return ok(data);
482
483
  } catch (err2) {
483
484
  if (this.throwOnAvailabilityError && isAvailabilityFailure(err2)) {
@@ -2103,6 +2104,23 @@ var NewsletterModule = class {
2103
2104
  );
2104
2105
  }
2105
2106
  };
2107
+ function emitCartEvent(data) {
2108
+ if (typeof window === "undefined" || typeof window.dispatchEvent !== "function") return;
2109
+ const cart = data;
2110
+ if (!cart || typeof cart !== "object" || !Array.isArray(cart.items)) return;
2111
+ const items = cart.items;
2112
+ const total = typeof cart.grandTotal === "number" ? cart.grandTotal : typeof cart.subtotal === "number" ? cart.subtotal : 0;
2113
+ const detail = {
2114
+ total,
2115
+ currency: typeof cart.currency === "string" ? cart.currency : null,
2116
+ itemCount: items.reduce((n, i) => n + (typeof _optionalChain([i, 'optionalAccess', _53 => _53.quantity]) === "number" ? i.quantity : 1), 0)
2117
+ };
2118
+ try {
2119
+ window.__behioCart = detail;
2120
+ window.dispatchEvent(new CustomEvent("behio:cart", { detail }));
2121
+ } catch (e6) {
2122
+ }
2123
+ }
2106
2124
 
2107
2125
 
2108
2126
 
package/dist/index.js CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
 
12
12
 
13
- var _chunkM6TCQUXZjs = require('./chunk-M6TCQUXZ.js');
13
+ var _chunkPUAQHLBVjs = require('./chunk-PUAQHLBV.js');
14
14
 
15
15
  // src/react/utils/format-price.ts
16
16
  function formatPrice(amount, currency, locale, display) {
@@ -948,7 +948,7 @@ var GENERIC = {
948
948
  en: "Something went wrong, please try again."
949
949
  };
950
950
  function bodyOf(err2) {
951
- const body = err2 instanceof _chunkM6TCQUXZjs.BehioApiError ? err2.body : err2 && typeof err2 === "object" && "body" in err2 ? err2.body : err2;
951
+ const body = err2 instanceof _chunkPUAQHLBVjs.BehioApiError ? err2.body : err2 && typeof err2 === "object" && "body" in err2 ? err2.body : err2;
952
952
  return body && typeof body === "object" ? body : null;
953
953
  }
954
954
  function errorCode(err2) {
@@ -1060,4 +1060,4 @@ function formFieldErrors(err2) {
1060
1060
 
1061
1061
 
1062
1062
 
1063
- exports.AddressTypes = _chunkM6TCQUXZjs.AddressTypes; exports.BehioApiError = _chunkM6TCQUXZjs.BehioApiError; exports.BehioNetworkError = _chunkM6TCQUXZjs.BehioNetworkError; exports.BehioStorefront = _chunkM6TCQUXZjs.BehioStorefront; exports.FulfillmentStatuses = _chunkM6TCQUXZjs.FulfillmentStatuses; exports.OrderStatuses = _chunkM6TCQUXZjs.OrderStatuses; exports.PaymentStatuses = _chunkM6TCQUXZjs.PaymentStatuses; exports.ProductSort = _chunkM6TCQUXZjs.ProductSort; exports.VARIANT_QUERY_PARAM = VARIANT_QUERY_PARAM; exports.availableAxisValues = availableAxisValues; exports.buildVariantComparison = buildVariantComparison; exports.err = _chunkM6TCQUXZjs.err; exports.errorCode = errorCode; exports.errorMessage = errorMessage; exports.errorParams = errorParams; exports.findVariantByAttributes = findVariantByAttributes; exports.formFieldErrors = formFieldErrors; exports.formatPrice = formatPrice; exports.generateVisitorId = generateVisitorId; exports.getStoredVisitorId = getStoredVisitorId; exports.grantAnalyticsConsent = grantAnalyticsConsent; exports.ok = _chunkM6TCQUXZjs.ok; exports.resolveVariantContent = resolveVariantContent; exports.revokeAnalyticsConsent = revokeAnalyticsConsent; exports.toSdkError = _chunkM6TCQUXZjs.toSdkError; exports.trackEcommerceEvent = trackEcommerceEvent; exports.validationFieldMessages = validationFieldMessages; exports.variantFromQuery = variantFromQuery; exports.variantHref = variantHref;
1063
+ exports.AddressTypes = _chunkPUAQHLBVjs.AddressTypes; exports.BehioApiError = _chunkPUAQHLBVjs.BehioApiError; exports.BehioNetworkError = _chunkPUAQHLBVjs.BehioNetworkError; exports.BehioStorefront = _chunkPUAQHLBVjs.BehioStorefront; exports.FulfillmentStatuses = _chunkPUAQHLBVjs.FulfillmentStatuses; exports.OrderStatuses = _chunkPUAQHLBVjs.OrderStatuses; exports.PaymentStatuses = _chunkPUAQHLBVjs.PaymentStatuses; exports.ProductSort = _chunkPUAQHLBVjs.ProductSort; exports.VARIANT_QUERY_PARAM = VARIANT_QUERY_PARAM; exports.availableAxisValues = availableAxisValues; exports.buildVariantComparison = buildVariantComparison; exports.err = _chunkPUAQHLBVjs.err; exports.errorCode = errorCode; exports.errorMessage = errorMessage; exports.errorParams = errorParams; exports.findVariantByAttributes = findVariantByAttributes; exports.formFieldErrors = formFieldErrors; exports.formatPrice = formatPrice; exports.generateVisitorId = generateVisitorId; exports.getStoredVisitorId = getStoredVisitorId; exports.grantAnalyticsConsent = grantAnalyticsConsent; exports.ok = _chunkPUAQHLBVjs.ok; exports.resolveVariantContent = resolveVariantContent; exports.revokeAnalyticsConsent = revokeAnalyticsConsent; exports.toSdkError = _chunkPUAQHLBVjs.toSdkError; exports.trackEcommerceEvent = trackEcommerceEvent; exports.validationFieldMessages = validationFieldMessages; exports.variantFromQuery = variantFromQuery; exports.variantHref = variantHref;
package/dist/index.mjs CHANGED
@@ -10,7 +10,7 @@ import {
10
10
  err,
11
11
  ok,
12
12
  toSdkError
13
- } from "./chunk-EXHIA7ST.mjs";
13
+ } from "./chunk-5C43BIAZ.mjs";
14
14
 
15
15
  // src/react/utils/format-price.ts
16
16
  function formatPrice(amount, currency, locale, display) {
package/dist/next.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
2
 
3
- var _chunkM6TCQUXZjs = require('./chunk-M6TCQUXZ.js');
3
+ var _chunkPUAQHLBVjs = require('./chunk-PUAQHLBV.js');
4
4
 
5
5
  // src/next.ts
6
6
  var _headers = require('next/headers');
@@ -19,7 +19,7 @@ async function getBehio(options = {}) {
19
19
  const locale = _nullishCoalesce(options.locale, () => ( process.env.BEHIO_LOCALE));
20
20
  const currency = _nullishCoalesce(options.currency, () => ( process.env.BEHIO_CURRENCY));
21
21
  const cookieName = _nullishCoalesce(options.cartCookieName, () => ( CART_COOKIE_NAME));
22
- const client = new (0, _chunkM6TCQUXZjs.BehioStorefront)({
22
+ const client = new (0, _chunkPUAQHLBVjs.BehioStorefront)({
23
23
  apiKey,
24
24
  ...baseUrl ? { baseUrl } : {},
25
25
  ...locale ? { locale } : {},
package/dist/next.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  BehioStorefront
3
- } from "./chunk-EXHIA7ST.mjs";
3
+ } from "./chunk-5C43BIAZ.mjs";
4
4
 
5
5
  // src/next.ts
6
6
  import { cookies, headers } from "next/headers";
package/dist/react.js CHANGED
@@ -557,6 +557,7 @@ var BehioStorefront = class {
557
557
  async request(method, path, options) {
558
558
  try {
559
559
  const data = await this.rawRequest(method, path, options);
560
+ if (path === "/cart" || path.startsWith("/cart/")) emitCartEvent(data);
560
561
  return ok(data);
561
562
  } catch (err) {
562
563
  if (this.throwOnAvailabilityError && isAvailabilityFailure(err)) {
@@ -2182,6 +2183,23 @@ var NewsletterModule = class {
2182
2183
  );
2183
2184
  }
2184
2185
  };
2186
+ function emitCartEvent(data) {
2187
+ if (typeof window === "undefined" || typeof window.dispatchEvent !== "function") return;
2188
+ const cart = data;
2189
+ if (!cart || typeof cart !== "object" || !Array.isArray(cart.items)) return;
2190
+ const items = cart.items;
2191
+ const total = typeof cart.grandTotal === "number" ? cart.grandTotal : typeof cart.subtotal === "number" ? cart.subtotal : 0;
2192
+ const detail = {
2193
+ total,
2194
+ currency: typeof cart.currency === "string" ? cart.currency : null,
2195
+ itemCount: items.reduce((n, i) => n + (typeof i?.quantity === "number" ? i.quantity : 1), 0)
2196
+ };
2197
+ try {
2198
+ window.__behioCart = detail;
2199
+ window.dispatchEvent(new CustomEvent("behio:cart", { detail }));
2200
+ } catch {
2201
+ }
2202
+ }
2185
2203
 
2186
2204
  // src/react/context.ts
2187
2205
  var import_react = require("react");
package/dist/react.mjs CHANGED
@@ -447,6 +447,7 @@ var BehioStorefront = class {
447
447
  async request(method, path, options) {
448
448
  try {
449
449
  const data = await this.rawRequest(method, path, options);
450
+ if (path === "/cart" || path.startsWith("/cart/")) emitCartEvent(data);
450
451
  return ok(data);
451
452
  } catch (err) {
452
453
  if (this.throwOnAvailabilityError && isAvailabilityFailure(err)) {
@@ -2072,6 +2073,23 @@ var NewsletterModule = class {
2072
2073
  );
2073
2074
  }
2074
2075
  };
2076
+ function emitCartEvent(data) {
2077
+ if (typeof window === "undefined" || typeof window.dispatchEvent !== "function") return;
2078
+ const cart = data;
2079
+ if (!cart || typeof cart !== "object" || !Array.isArray(cart.items)) return;
2080
+ const items = cart.items;
2081
+ const total = typeof cart.grandTotal === "number" ? cart.grandTotal : typeof cart.subtotal === "number" ? cart.subtotal : 0;
2082
+ const detail = {
2083
+ total,
2084
+ currency: typeof cart.currency === "string" ? cart.currency : null,
2085
+ itemCount: items.reduce((n, i) => n + (typeof i?.quantity === "number" ? i.quantity : 1), 0)
2086
+ };
2087
+ try {
2088
+ window.__behioCart = detail;
2089
+ window.dispatchEvent(new CustomEvent("behio:cart", { detail }));
2090
+ } catch {
2091
+ }
2092
+ }
2075
2093
 
2076
2094
  // src/react/context.ts
2077
2095
  import { createContext, useContext } from "react";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@behio/storefront-sdk",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "TypeScript SDK for Behio headless e-commerce: core client + React hooks",
5
5
  "author": "Behio",
6
6
  "license": "MIT",