@zappie/sdk 0.7.2 → 0.8.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/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # @zappie/sdk
2
2
 
3
+ ## 0.8.0
4
+
5
+ ### Minor Changes
6
+
7
+ - a444bdf: A cart change shows at once, and the cart puts it back when the server refuses it. Before, a quantity the server wouldn't sell stayed in the bag with an issue, and a site that wanted the change undone wrote its own optimistic state.
8
+
9
+ `useCart().actions` from `@zappie/sdk/react` now tracks each `add`, `update` and `remove`. When the next quote refuses a line the shopper just changed, with `quantity_limit`, `item_unavailable`, `item_not_found` or an option issue, the cart puts that line back: a new line is removed, and a changed line returns to what the last quote accepted, or to the server's nearest allowed quantity when that sits between the two. Only that line goes back. A newer change to the same line is never undone, because a quote for an older cart is never applied. Rapid changes still go to the server as one quote. A line restored from storage keeps its issue and fixes, as before.
10
+
11
+ `useCartNotice()` returns `{ notice, dismiss }`: one notice per quote, with the server's `message` (such as "Only 2 left"), its `code`, the `lineId` and `count`, the number of lines put back. The SDK also writes the message to one hidden polite live region, `[data-zappie-announcer]`, so screen readers hear the rollback whether or not you show a toast. `actions.add(item, input, { key })` takes a key for one shopper action: a second add with the same key returns the first line and adds nothing. Make the key with `crypto.randomUUID()` when the shopper acts, and reuse it only to retry that action. Never build it from the line. `CartActions`, `CartChangeOptions` and `CartNotice` are exported.
12
+
13
+ `@zappie/foundations`: `Cart.Line` gives its render function `total`, the line total from the current quote, and `pending`, true while a change waits for the server, and sets `aria-busy` while it waits. `ItemForm` adds through the tracked actions, so a refused add from the item sheet goes back too.
14
+
15
+ `eslint-plugin-zappie`: `cart-via-sdk` reports `useOptimistic`, `useState` or `useReducer` seeded from `useCart()` or `useCartLine()` data, and a `useOptimistic` whose value is named for a cart, bag or basket. Change the cart through `useCart().actions` and read refusals with `useCartNotice()`.
16
+
3
17
  ## 0.7.2
4
18
 
5
19
  ### Patch Changes
@@ -1,6 +1,6 @@
1
1
  import { Zappie } from '../index.js';
2
2
  import { Item } from '../index.js';
3
- import '../types-CxY5C6AN.js';
3
+ import '../types-BzyzhqYz.js';
4
4
 
5
5
  /** Register an item already read by a partial/selected catalogue, without fetching it again. */
6
6
  declare function prepareItem(client: Zappie, item: Item, options: {
@@ -1,5 +1,5 @@
1
1
  import { a as Menu, C as CategoryPage, S as Store, I as Item, M as Money, F as Formatters } from './models-C-kEg1Id.js';
2
- import { Q as QuoteRequest, g as WireQuote, i as CartIssueCode, d as CartModule, b as CartLineInput } from './types-CxY5C6AN.js';
2
+ import { Q as QuoteRequest, h as WireQuote, e as CartIssueCode, d as CartModule, b as CartLineInput } from './types-BzyzhqYz.js';
3
3
 
4
4
  /** Stable public error codes. */
5
5
  type ZappieErrorCode = "network_error" | "timeout" | "rate_limited" | "unauthorized" | "forbidden" | "forbidden_origin" | "scope_required" | "not_found" | "store_not_found" | "branch_not_found" | "item_not_found" | "cart_invalid" | "invalid_request" | "version_unsupported" | "server_error";
package/dist/debug.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/debug.ts
2
- var VERSION = true ? "0.7.2" : "0.0.0-development";
2
+ var VERSION = true ? "0.8.0" : "0.0.0-development";
3
3
  function installDebug(client) {
4
4
  if (typeof window === "undefined" || client.mode !== "test") {
5
5
  return;
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { a as ZappieError, T as Transport, R as RequestCtx, f as MenuFulfilment, g as MenuResponse, j as SearchRequest, k as SearchPage, l as MenuVersion } from './client-DInn0X0F.js';
1
+ import { a as ZappieError, T as Transport, R as RequestCtx, f as MenuFulfilment, g as MenuResponse, j as SearchRequest, k as SearchPage, l as MenuVersion } from './client-75aFOuQi.js';
2
2
  import { SelectedMenuRequest, SelectedMenuResponse } from './transport/selected-menu.js';
3
- export { A as API_VERSION, E as ErrorDetails, m as MenuReadOptions, M as MenuResource, n as ReadOptions, S as StorageLike, c as StoreResource, Z as Zappie, b as ZappieClientOptions, b as ZappieConfig, e as ZappieErrorCode, d as ZappieEvents, o as createZappie, p as isZappieError } from './client-DInn0X0F.js';
4
- import { f as CartIssue, b as CartLineInput, C as CartLine, Q as QuoteRequest, g as WireQuote } from './types-CxY5C6AN.js';
5
- export { h as CartFix, c as CartFulfilment, i as CartIssueCode, d as CartModule, j as CartSnapshot, e as CartState, k as CartSyncAdapter, l as CartSyncDocument, D as DeliveryAddress, a as Quote, m as WireFix, W as WireFulfilment, n as WireIssue, o as WireLine, p as WireMoney } from './types-CxY5C6AN.js';
3
+ export { A as API_VERSION, E as ErrorDetails, m as MenuReadOptions, M as MenuResource, n as ReadOptions, S as StorageLike, c as StoreResource, Z as Zappie, b as ZappieClientOptions, b as ZappieConfig, e as ZappieErrorCode, d as ZappieEvents, o as createZappie, p as isZappieError } from './client-75aFOuQi.js';
4
+ import { g as CartIssue, b as CartLineInput, C as CartLine, Q as QuoteRequest, h as WireQuote } from './types-BzyzhqYz.js';
5
+ export { i as CartFix, c as CartFulfilment, e as CartIssueCode, d as CartModule, j as CartSnapshot, f as CartState, k as CartSyncAdapter, l as CartSyncDocument, D as DeliveryAddress, a as Quote, m as WireFix, W as WireFulfilment, n as WireIssue, o as WireLine, p as WireMoney } from './types-BzyzhqYz.js';
6
6
  import { I as Item, O as OptionGroup, M as Money, a as Menu, B as Branch, S as Store, C as CategoryPage } from './models-C-kEg1Id.js';
7
7
  export { b as Category, c as Choice, d as Currency, F as Formatters, e as Fulfilment, f as MenuSchedule, g as MenuSection, h as MoneyFormatOptions, i as OpeningTime, j as OptionCondition, P as PaymentMethodKind, k as SpecialHours, V as Variant, l as format, m as money } from './models-C-kEg1Id.js';
8
8
 
package/dist/index.js CHANGED
@@ -10,7 +10,7 @@ import { ZappieError, isZappieError } from "./errors.js";
10
10
  export * from "./models.js";
11
11
  import { format, money } from "./money.js";
12
12
  import { RestTransport } from "./transport/rest.js";
13
- var VERSION = true ? "0.7.2" : "0.0.0-development";
13
+ var VERSION = true ? "0.8.0" : "0.0.0-development";
14
14
  export {
15
15
  API_VERSION,
16
16
  RestTransport,
@@ -1,6 +1,6 @@
1
1
  import { RequestCtx } from '../index.js';
2
2
  import '../models-C-kEg1Id.js';
3
- import '../types-CxY5C6AN.js';
3
+ import '../types-BzyzhqYz.js';
4
4
 
5
5
  /** Public integration IDs and browser keys. Your app decides when to load scripts. */
6
6
  type Integrations = {
@@ -1,5 +1,5 @@
1
1
  import { CartFulfilment, WireFulfilment } from './index.js';
2
- export { v as validateConfig } from './client-DInn0X0F.js';
2
+ export { v as validateConfig } from './client-75aFOuQi.js';
3
3
  import './models-C-kEg1Id.js';
4
4
 
5
5
  /**
@@ -1,3 +1,3 @@
1
- export { q as CATALOGUE_MAX_AGE_MS, r as CATALOGUE_MAX_BYTES, s as CatalogueScope, t as SavedCatalogue, u as catalogueDigest, w as catalogueKey, x as createCatalogueStorage, y as createSavedCatalogue } from '../client-DInn0X0F.js';
1
+ export { q as CATALOGUE_MAX_AGE_MS, r as CATALOGUE_MAX_BYTES, s as CatalogueScope, t as SavedCatalogue, u as catalogueDigest, w as catalogueKey, x as createCatalogueStorage, y as createSavedCatalogue } from '../client-75aFOuQi.js';
2
2
  import '../models-C-kEg1Id.js';
3
- import '../types-CxY5C6AN.js';
3
+ import '../types-BzyzhqYz.js';
@@ -3,6 +3,7 @@
3
3
  // src/react/cart.ts
4
4
  import { useEffect, useSyncExternalStore } from "react";
5
5
  import { EMPTY_CART } from "../cart/index.js";
6
+ import { cartActions } from "./changes.js";
6
7
  import { useZappie } from "./provider.js";
7
8
  function useCart() {
8
9
  const { cart } = useZappie();
@@ -12,7 +13,7 @@ function useCart() {
12
13
  () => EMPTY_CART
13
14
  );
14
15
  useEffect(() => cart.mount(), [cart]);
15
- return { ...snapshot, actions: cart };
16
+ return { ...snapshot, actions: cartActions(cart).actions };
16
17
  }
17
18
  function useCartLine(lineId) {
18
19
  return useCart().lines.find((line) => line.id === lineId);
@@ -22,9 +23,19 @@ function useCartCatalogue() {
22
23
  useCart();
23
24
  return client._internal.cartCatalogue();
24
25
  }
26
+ function useCartNotice() {
27
+ const tracker = cartActions(useZappie().cart);
28
+ const notice = useSyncExternalStore(
29
+ tracker.subscribe,
30
+ tracker.notice,
31
+ () => null
32
+ );
33
+ return { notice, dismiss: tracker.dismiss };
34
+ }
25
35
  export {
26
36
  useCart,
27
37
  useCartCatalogue,
28
- useCartLine
38
+ useCartLine,
39
+ useCartNotice
29
40
  };
30
41
  //# sourceMappingURL=cart.js.map
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/react/cart.ts"],
4
- "mappings": ";;;AACA,SAAS,WAAW,4BAA4B;AAChD,SAAS,kBAAkB;AAC3B,SAAS,iBAAiB;AAEnB,SAAS,UAAU;AACxB,QAAM,EAAE,KAAK,IAAI,UAAU;AAC3B,QAAM,WAAW;AAAA,IACf,KAAK;AAAA,IACL,KAAK;AAAA,IACL,MAAM;AAAA,EACR;AACA,YAAU,MAAM,KAAK,MAAM,GAAG,CAAC,IAAI,CAAC;AACpC,SAAO,EAAE,GAAG,UAAU,SAAS,KAAK;AACtC;AAKO,SAAS,YAAY,QAAgB;AAC1C,SAAO,QAAQ,EAAE,MAAM,KAAK,CAAC,SAAS,KAAK,OAAO,MAAM;AAC1D;AAEO,SAAS,mBAAmB;AACjC,QAAM,SAAS,UAAU;AACzB,UAAQ;AACR,SAAO,OAAO,UAAU,cAAc;AACxC;",
4
+ "mappings": ";;;AACA,SAAS,WAAW,4BAA4B;AAChD,SAAS,kBAAkB;AAC3B,SAAS,mBAAmB;AAC5B,SAAS,iBAAiB;AAMnB,SAAS,UAAU;AACxB,QAAM,EAAE,KAAK,IAAI,UAAU;AAC3B,QAAM,WAAW;AAAA,IACf,KAAK;AAAA,IACL,KAAK;AAAA,IACL,MAAM;AAAA,EACR;AACA,YAAU,MAAM,KAAK,MAAM,GAAG,CAAC,IAAI,CAAC;AACpC,SAAO,EAAE,GAAG,UAAU,SAAS,YAAY,IAAI,EAAE,QAAQ;AAC3D;AAKO,SAAS,YAAY,QAAgB;AAC1C,SAAO,QAAQ,EAAE,MAAM,KAAK,CAAC,SAAS,KAAK,OAAO,MAAM;AAC1D;AAEO,SAAS,mBAAmB;AACjC,QAAM,SAAS,UAAU;AACzB,UAAQ;AACR,SAAO,OAAO,UAAU,cAAc;AACxC;AAKO,SAAS,gBAAgB;AAC9B,QAAM,UAAU,YAAY,UAAU,EAAE,IAAI;AAC5C,QAAM,SAAS;AAAA,IACb,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,MAAM;AAAA,EACR;AACA,SAAO,EAAE,QAAQ,SAAS,QAAQ,QAAQ;AAC5C;",
5
5
  "names": []
6
6
  }
@@ -0,0 +1,94 @@
1
+ // src/react/changes.ts
2
+ var trackers = /* @__PURE__ */ new WeakMap();
3
+ function cartActions(cart) {
4
+ let tracker = trackers.get(cart);
5
+ if (tracker) {
6
+ return tracker;
7
+ }
8
+ const confirmed = /* @__PURE__ */ new Map();
9
+ const keyed = /* @__PURE__ */ new Map();
10
+ const listeners = /* @__PURE__ */ new Set();
11
+ let notice = null;
12
+ let watching = false;
13
+ const emit = () => {
14
+ for (const fn of [...listeners]) {
15
+ fn();
16
+ }
17
+ };
18
+ const find = (lineId) => cart.lines.find((candidate) => candidate.id === lineId);
19
+ const remember = (lineId, prior) => {
20
+ if (!confirmed.has(lineId)) {
21
+ confirmed.set(lineId, prior ?? null);
22
+ }
23
+ if (!watching) {
24
+ watching = true;
25
+ void import("./rollback.js").then(
26
+ ({ watch }) => watch(cart, confirmed, (value) => {
27
+ notice = value;
28
+ emit();
29
+ })
30
+ );
31
+ }
32
+ };
33
+ const actions = Object.freeze(
34
+ Object.create(cart, {
35
+ add: {
36
+ value(item, input, { key } = {}) {
37
+ const done = key ? keyed.get(key) : void 0;
38
+ if (done) {
39
+ return find(done.id) ?? done;
40
+ }
41
+ const before = cart.lines;
42
+ const line = cart.add(item, input);
43
+ remember(
44
+ line.id,
45
+ before.find((candidate) => candidate.id === line.id)
46
+ );
47
+ if (key) {
48
+ keyed.set(key, line);
49
+ if (keyed.size > 100) {
50
+ keyed.delete(keyed.keys().next().value);
51
+ }
52
+ }
53
+ return line;
54
+ }
55
+ },
56
+ update: {
57
+ value(lineId, patch) {
58
+ const prior = find(lineId);
59
+ const line = cart.update(lineId, patch);
60
+ remember(lineId, prior);
61
+ return line;
62
+ }
63
+ },
64
+ remove: {
65
+ value(lineId) {
66
+ confirmed.delete(lineId);
67
+ cart.remove(lineId);
68
+ }
69
+ }
70
+ })
71
+ );
72
+ tracker = {
73
+ actions,
74
+ notice: () => notice,
75
+ dismiss() {
76
+ if (notice) {
77
+ notice = null;
78
+ emit();
79
+ }
80
+ },
81
+ subscribe(fn) {
82
+ listeners.add(fn);
83
+ return () => {
84
+ listeners.delete(fn);
85
+ };
86
+ }
87
+ };
88
+ trackers.set(cart, tracker);
89
+ return tracker;
90
+ }
91
+ export {
92
+ cartActions
93
+ };
94
+ //# sourceMappingURL=changes.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/react/changes.ts"],
4
+ "mappings": ";AAmDA,IAAM,WAAW,oBAAI,QAA6B;AAE3C,SAAS,YAAY,MAA2B;AACrD,MAAI,UAAU,SAAS,IAAI,IAAI;AAC/B,MAAI,SAAS;AACX,WAAO;AAAA,EACT;AAEA,QAAM,YAAY,oBAAI,IAA6B;AACnD,QAAM,QAAQ,oBAAI,IAAsB;AACxC,QAAM,YAAY,oBAAI,IAAgB;AACtC,MAAI,SAA4B;AAChC,MAAI,WAAW;AACf,QAAM,OAAO,MAAM;AACjB,eAAW,MAAM,CAAC,GAAG,SAAS,GAAG;AAC/B,SAAG;AAAA,IACL;AAAA,EACF;AACA,QAAM,OAAO,CAAC,WACZ,KAAK,MAAM,KAAK,CAAC,cAAc,UAAU,OAAO,MAAM;AAGxD,QAAM,WAAW,CAAC,QAAgB,UAAgC;AAChE,QAAI,CAAC,UAAU,IAAI,MAAM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;AAAA,IACd,UAAU;AACR,UAAI,QAAQ;AACV,iBAAS;AACT,aAAK;AAAA,MACP;AAAA,IACF;AAAA,IACA,UAAU,IAAI;AACZ,gBAAU,IAAI,EAAE;AAChB,aAAO,MAAM;AACX,kBAAU,OAAO,EAAE;AAAA,MACrB;AAAA,IACF;AAAA,EACF;AACA,WAAS,IAAI,MAAM,OAAO;AAC1B,SAAO;AACT;",
5
+ "names": []
6
+ }
@@ -3,7 +3,7 @@ import { ZappieError, Zappie } from '../index.js';
3
3
  import { Item, Menu, Store, Money } from '../index.js';
4
4
  import * as react from 'react';
5
5
  import { ReactNode } from 'react';
6
- import { CartModule, CartLine, CartFulfilment, CartState, CartIssue } from '../index.js';
6
+ import { CartModule, CartLineInput, CartLine, CartIssueCode, CartFulfilment, CartState, CartIssue } from '../index.js';
7
7
 
8
8
  /** Subscribe to storefront identity and branch-directory data. */
9
9
  declare function useStore(): {
@@ -39,9 +39,43 @@ declare function ZappieProvider({ client, initialData, children, }: {
39
39
  /** Read your storefront client from the nearest ZappieProvider. */
40
40
  declare function useZappie(): Zappie;
41
41
 
42
- /** Load saved cart state after mount. Server and initial hydration snapshots are empty. */
42
+ /**
43
+ * Options for one cart change.
44
+ */
45
+ type CartChangeOptions = {
46
+ /**
47
+ * A key for one shopper action. A second add with the same key returns the first line and adds nothing,
48
+ * so a retried add never adds twice. Make it with `crypto.randomUUID()` when the shopper acts and reuse it
49
+ * only to retry that action. Never build it from the line's contents.
50
+ */
51
+ key?: string;
52
+ };
53
+ /**
54
+ * The cart's operations as `useCart().actions` returns them. Each add, update and remove shows at once.
55
+ * When the next quote refuses the change, the cart puts that line back and raises one notice.
56
+ */
57
+ type CartActions = Omit<CartModule, "add"> & {
58
+ add(item: Item | string, input?: Omit<CartLineInput, "itemId">, options?: CartChangeOptions): CartLine;
59
+ };
60
+ /**
61
+ * The one message for changes the server refused and the cart undid. `message` is the server's reason.
62
+ * `count` says how many lines the cart put back. Each new notice gets a new `id`.
63
+ */
64
+ type CartNotice = Readonly<{
65
+ id: number;
66
+ code: CartIssueCode;
67
+ message: string;
68
+ lineId?: string;
69
+ count: number;
70
+ }>;
71
+
72
+ /**
73
+ * Load saved cart state after mount. Server and initial hydration snapshots are empty.
74
+ * `actions` shows each change at once and puts a line back when the next quote refuses it.
75
+ * Read the refusal with `useCartNotice()`.
76
+ */
43
77
  declare function useCart(): {
44
- actions: Readonly<CartModule>;
78
+ actions: CartActions;
45
79
  branchId?: string | undefined;
46
80
  lines: readonly CartLine[];
47
81
  count: number;
@@ -76,5 +110,20 @@ declare function useCartLine(lineId: string): Readonly<{
76
110
  }> | undefined;
77
111
  /** Read the cart validation catalogue, including held items, without fetching it. */
78
112
  declare function useCartCatalogue(): Menu | undefined;
113
+ /**
114
+ * Read the one notice for cart changes the server refused. The cart has already put those lines back
115
+ * and told screen readers. Show `notice.message` once, as a toast, and call `dismiss` to clear it.
116
+ */
117
+ declare function useCartNotice(): {
118
+ notice: Readonly<{
119
+ id: number;
120
+ code: CartIssueCode;
121
+ message: string;
122
+ lineId?: string;
123
+ count: number;
124
+ }> | null;
125
+ dismiss: () => void;
126
+ };
79
127
 
80
- export { ZappieProvider, useCart, useCartCatalogue, useCartLine, useItem, useMenu, useStore, useZappie };
128
+ export { ZappieProvider, useCart, useCartCatalogue, useCartLine, useCartNotice, useItem, useMenu, useStore, useZappie };
129
+ export type { CartActions, CartChangeOptions, CartNotice };
@@ -4,13 +4,14 @@
4
4
  import { VERSION } from "../index.js";
5
5
  import { useItem, useMenu, useStore } from "./hooks.js";
6
6
  import { ZappieProvider, useZappie } from "./provider.js";
7
- import { useCart, useCartCatalogue, useCartLine } from "./cart.js";
7
+ import { useCart, useCartCatalogue, useCartLine, useCartNotice } from "./cart.js";
8
8
  export {
9
9
  VERSION,
10
10
  ZappieProvider,
11
11
  useCart,
12
12
  useCartCatalogue,
13
13
  useCartLine,
14
+ useCartNotice,
14
15
  useItem,
15
16
  useMenu,
16
17
  useStore,
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/react/index.ts"],
4
- "mappings": ";;;AACA,SAAS,eAAe;AACxB,SAAS,SAAS,SAAS,gBAAgB;AAC3C,SAAS,gBAAgB,iBAAiB;AAE1C,SAAS,SAAS,kBAAkB,mBAAmB;",
4
+ "mappings": ";;;AACA,SAAS,eAAe;AACxB,SAAS,SAAS,SAAS,gBAAgB;AAC3C,SAAS,gBAAgB,iBAAiB;AAE1C,SAAS,SAAS,kBAAkB,aAAa,qBAAqB;",
5
5
  "names": []
6
6
  }
@@ -0,0 +1,88 @@
1
+ // src/react/rollback.ts
2
+ var refusals = /^(quantity_limit|item_unavailable|item_not_found|option_)/;
3
+ var region;
4
+ function announce(message) {
5
+ if (typeof document === "undefined") {
6
+ return;
7
+ }
8
+ if (!region?.isConnected) {
9
+ region = document.createElement("div");
10
+ region.setAttribute("role", "status");
11
+ region.setAttribute("aria-live", "polite");
12
+ region.setAttribute("data-zappie-announcer", "");
13
+ region.style.cssText = "position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%)";
14
+ document.body.appendChild(region);
15
+ }
16
+ const target = region;
17
+ target.textContent = "";
18
+ setTimeout(() => {
19
+ target.textContent = message;
20
+ }, 50);
21
+ }
22
+ function watch(cart, confirmed, show) {
23
+ let seen;
24
+ let sequence = 0;
25
+ const settle = () => {
26
+ const snapshot = cart.getSnapshot();
27
+ const quote = "quote" in snapshot.state ? snapshot.state.quote : void 0;
28
+ if (!quote || quote === seen) {
29
+ return;
30
+ }
31
+ seen = quote;
32
+ let first;
33
+ let count = 0;
34
+ for (const [lineId, prior] of [...confirmed]) {
35
+ const line = snapshot.lines.find((candidate) => candidate.id === lineId);
36
+ const quoted = quote.lines.find(
37
+ (candidate) => candidate.lineId === lineId
38
+ )?.quantity;
39
+ if (line && quoted !== void 0 && quoted !== line.quantity) {
40
+ continue;
41
+ }
42
+ confirmed.delete(lineId);
43
+ const problem = quote.issues.find(
44
+ (issue) => issue.lineId === lineId && issue.severity === "blocking" && refusals.test(issue.code)
45
+ );
46
+ if (!line || !problem) {
47
+ continue;
48
+ }
49
+ const fix = problem.fixes.find(
50
+ (candidate) => candidate.action === "set_quantity" && candidate.quantity > (prior?.quantity ?? 0) && candidate.quantity < line.quantity
51
+ );
52
+ try {
53
+ if (fix?.action === "set_quantity") {
54
+ cart.update(lineId, { quantity: fix.quantity });
55
+ } else if (prior) {
56
+ cart.update(lineId, {
57
+ quantity: prior.quantity,
58
+ choices: prior.choices,
59
+ inputs: prior.inputs,
60
+ variantId: prior.variantId,
61
+ note: prior.note
62
+ });
63
+ } else {
64
+ cart.remove(lineId);
65
+ }
66
+ first ?? (first = problem);
67
+ count++;
68
+ } catch {
69
+ }
70
+ }
71
+ if (first) {
72
+ announce(first.message);
73
+ show({
74
+ id: ++sequence,
75
+ code: first.code,
76
+ message: first.message,
77
+ ...first.lineId ? { lineId: first.lineId } : {},
78
+ count
79
+ });
80
+ }
81
+ };
82
+ cart.subscribe(settle);
83
+ settle();
84
+ }
85
+ export {
86
+ watch
87
+ };
88
+ //# sourceMappingURL=rollback.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/react/rollback.ts"],
4
+ "mappings": ";AAKA,IAAM,WAAW;AACjB,IAAI;AAIJ,SAAS,SAAS,SAAiB;AACjC,MAAI,OAAO,aAAa,aAAa;AACnC;AAAA,EACF;AACA,MAAI,CAAC,QAAQ,aAAa;AACxB,aAAS,SAAS,cAAc,KAAK;AACrC,WAAO,aAAa,QAAQ,QAAQ;AACpC,WAAO,aAAa,aAAa,QAAQ;AACzC,WAAO,aAAa,yBAAyB,EAAE;AAC/C,WAAO,MAAM,UACX;AACF,aAAS,KAAK,YAAY,MAAM;AAAA,EAClC;AACA,QAAM,SAAS;AAEf,SAAO,cAAc;AACrB,aAAW,MAAM;AACf,WAAO,cAAc;AAAA,EACvB,GAAG,EAAE;AACP;AAOO,SAAS,MACd,MACA,WACA,MACA;AACA,MAAI;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IACF;AACA,QAAI,OAAO;AACT,eAAS,MAAM,OAAO;AACtB,WAAK;AAAA,QACH,IAAI,EAAE;AAAA,QACN,MAAM,MAAM;AAAA,QACZ,SAAS,MAAM;AAAA,QACf,GAAI,MAAM,SAAS,EAAE,QAAQ,MAAM,OAAO,IAAI,CAAC;AAAA,QAC/C;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACA,OAAK,UAAU,MAAM;AACrB,SAAO;AACT;",
5
+ "names": []
6
+ }
@@ -1,5 +1,5 @@
1
1
  import { Menu, Store, Formatters } from '../index.js';
2
- import { C as Cache } from '../client-DInn0X0F.js';
2
+ import { C as Cache } from '../client-75aFOuQi.js';
3
3
  import { ZappieError, ZappieConfig, Transport, StorageLike, StoreResource, MenuResource, ZappieEvents } from '../index.js';
4
4
  export { VERSION } from '../index.js';
5
5
  import { CartModule, CartLineInput } from '../index.js';
@@ -17,6 +17,7 @@ var KIT_REGION_NAMES = {
17
17
  CartDrawer: "Cart",
18
18
  CartIssueList: "Cart notices",
19
19
  CartLine: "Cart item",
20
+ CartNotice: "Cart message",
20
21
  CartTotals: "Cart totals",
21
22
  CategoryNav: "Categories",
22
23
  CheckoutButton: "Checkout button",
@@ -1,4 +1,4 @@
1
- import { C as Cache } from '../client-DInn0X0F.js';
1
+ import { C as Cache } from '../client-75aFOuQi.js';
2
2
  import { Transport, ZappieErrorCode, StorageLike, StoreResource, MenuResource, ZappieEvents } from '../index.js';
3
3
  import { Store, Menu, Formatters } from '../index.js';
4
4
  export { VERSION } from '../index.js';
@@ -1,6 +1,6 @@
1
1
  import { Menu } from '../index.js';
2
2
  import { SearchRequest, SearchPage } from '../index.js';
3
- import '../types-CxY5C6AN.js';
3
+ import '../types-BzyzhqYz.js';
4
4
 
5
5
  /** Loaded only when search is used; request authentication/retries stay in REST. */
6
6
  declare function searchPage(ctx: SearchRequest, read: (path: string) => Promise<unknown>, mapMenu: (value: unknown, branchId: string) => Menu, invalid: () => Error): Promise<SearchPage>;
@@ -1,3 +1,3 @@
1
- export { z as SelectedMenu, h as SelectedMenuRequest, i as SelectedMenuResponse, B as SelectedMenuTransport, D as createSelectedMenuReader, F as selectedMenu, G as selectedMenuKey } from '../client-DInn0X0F.js';
1
+ export { z as SelectedMenu, h as SelectedMenuRequest, i as SelectedMenuResponse, B as SelectedMenuTransport, D as createSelectedMenuReader, F as selectedMenu, G as selectedMenuKey } from '../client-75aFOuQi.js';
2
2
  import '../models-C-kEg1Id.js';
3
- import '../types-CxY5C6AN.js';
3
+ import '../types-BzyzhqYz.js';
@@ -379,4 +379,4 @@ interface CartSyncAdapter {
379
379
  }>;
380
380
  }
381
381
 
382
- export type { CartLine as C, DeliveryAddress as D, QuoteRequest as Q, WireFulfilment as W, Quote as a, CartLineInput as b, CartFulfilment as c, CartModule as d, CartState as e, CartIssue as f, WireQuote as g, CartFix as h, CartIssueCode as i, CartSnapshot as j, CartSyncAdapter as k, CartSyncDocument as l, WireFix as m, WireIssue as n, WireLine as o, WireMoney as p };
382
+ export type { CartLine as C, DeliveryAddress as D, QuoteRequest as Q, WireFulfilment as W, Quote as a, CartLineInput as b, CartFulfilment as c, CartModule as d, CartIssueCode as e, CartState as f, CartIssue as g, WireQuote as h, CartFix as i, CartSnapshot as j, CartSyncAdapter as k, CartSyncDocument as l, WireFix as m, WireIssue as n, WireLine as o, WireMoney as p };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zappie/sdk",
3
- "version": "0.7.2",
3
+ "version": "0.8.0",
4
4
  "description": "Typed, framework-agnostic client for Zappie menus, carts, checkout, auth and events.",
5
5
  "type": "module",
6
6
  "sideEffects": false,