@zappie/sdk 0.15.0 → 0.17.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.
Files changed (56) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +52 -0
  3. package/dist/cart/index.js +30 -39
  4. package/dist/cart/index.js.map +1 -1
  5. package/dist/cart/item.d.ts +1 -1
  6. package/dist/cart/quote-client.js +4 -20
  7. package/dist/cart/quote-client.js.map +1 -1
  8. package/dist/cart/quote-response.js +10 -5
  9. package/dist/cart/quote-response.js.map +1 -1
  10. package/dist/cart/storage.js +2 -0
  11. package/dist/cart/storage.js.map +1 -1
  12. package/dist/checkout/embed.d.ts +2 -0
  13. package/dist/checkout/index.d.ts +2 -0
  14. package/dist/checkout/react.d.ts +2 -0
  15. package/dist/checkout-defaults/index.d.ts +3 -0
  16. package/dist/{client-C47e44tl.d.ts → client-uRLLtZJU.d.ts} +1 -1
  17. package/dist/customer/client.js +34 -12
  18. package/dist/customer/client.js.map +1 -1
  19. package/dist/customer/index.d.ts +181 -10
  20. package/dist/customer/reorder.d.ts +40 -0
  21. package/dist/customer/reorder.js +61 -0
  22. package/dist/customer/reorder.js.map +6 -0
  23. package/dist/debug.js +1 -1
  24. package/dist/index.d.ts +4 -4
  25. package/dist/index.js +1 -1
  26. package/dist/integrations/index.d.ts +1 -1
  27. package/dist/internal/perk-hold.js +35 -0
  28. package/dist/internal/perk-hold.js.map +6 -0
  29. package/dist/internal/remembered-address.js +8 -2
  30. package/dist/internal/remembered-address.js.map +1 -1
  31. package/dist/internal.d.ts +1 -1
  32. package/dist/loyalty/cart.js +56 -0
  33. package/dist/loyalty/cart.js.map +6 -0
  34. package/dist/loyalty/client.js +89 -0
  35. package/dist/loyalty/client.js.map +6 -0
  36. package/dist/loyalty/index.d.ts +526 -0
  37. package/dist/loyalty/index.js +13 -0
  38. package/dist/loyalty/index.js.map +6 -0
  39. package/dist/loyalty/react.d.ts +116 -0
  40. package/dist/loyalty/react.js +200 -0
  41. package/dist/loyalty/react.js.map +6 -0
  42. package/dist/loyalty/release.js +43 -0
  43. package/dist/loyalty/release.js.map +6 -0
  44. package/dist/loyalty/types.js +1 -0
  45. package/dist/loyalty/types.js.map +6 -0
  46. package/dist/persist/catalogue.d.ts +2 -2
  47. package/dist/react/index.d.ts +2 -1
  48. package/dist/server/index.d.ts +1 -1
  49. package/dist/testing/index.d.ts +92 -4
  50. package/dist/testing/index.js +2 -0
  51. package/dist/testing/loyalty.js +110 -0
  52. package/dist/testing/loyalty.js.map +6 -0
  53. package/dist/transport/search.d.ts +1 -1
  54. package/dist/transport/selected-menu.d.ts +2 -2
  55. package/dist/{types-ooHA0lwH.d.ts → types-K01NHM_a.d.ts} +28 -3
  56. package/package.json +14 -2
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/loyalty/cart.ts"],
4
+ "mappings": ";AACA,SAAS,mBAAmB;AAE5B,SAAS,MAAM,oBAAoB;AAkBnC,eAAsB,UACpB,MACA,SACA,MACA,UAAuC,CAAC,GAClB;AACtB,MAAI,KAAK,YAAY,SAAS,WAAW;AACvC,UAAM,IAAI;AAAA,MACR;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACA,QAAM,KAAK,OAAO,SAAS,WAAW,OAAO,KAAK;AAClD,QAAM,aAAa,MAAM,QAAQ,OAAO,IAAI,OAAO;AACnD,QAAM,UAAuB;AAAA,IAC3B,cAAc,WAAW;AAAA,IACzB,QAAQ,WAAW,MAAM,MAAM;AAAA,IAC/B,MAAM,WAAW,MAAM,SAAS,OAAO,SAAS,WAAW,KAAK,KAAK;AAAA,IACrE,aAAa,WAAW,gBAAgB;AAAA,IACxC,WACE,WAAW,cAAc,IAAI,KAAK,KAAK,IAAI,IAAI,KAAU,EAAE,YAAY;AAAA,IACzE,GAAI,OAAO,SAAS,WAAW,CAAC,IAAI,EAAE,QAAQ,KAAK,OAAO,KAAK;AAAA,EACjE;AACA,MAAI;AACF,SAAK,SAAS;AAAA,MACZ,GAAG,KACA,YAAY,EACZ,MAAM,OAAO,CAAC,MAAM,EAAE,iBAAiB,QAAQ,YAAY;AAAA,MAC9D;AAAA,IACF,CAAC;AAAA,EACH,SAAS,OAAO;AACd,UAAM,aAAa,CAAC,OAAO,GAAG,QAAQ,IAAI;AAC1C,UAAM;AAAA,EACR;AACA,SAAO;AACT;AAYA,eAAsB,WACpB,MACA,SACA,cACe;AACf,MAAI,MAAM,KAAK,cAAc,QAAQ,IAAI,GAAG;AAC1C,UAAM,IAAI;AAAA,MACR;AAAA,MACA;AAAA,IACF;AAAA,EACF;AACA,MAAI;AACF,UAAM,QAAQ,OAAO,YAAY;AAAA,EACnC,SAAS,OAAO;AAEd,QACE,CAAC,CAAC,mBAAmB,WAAW,EAAE;AAAA,MAC/B,MAA+B,WAAW;AAAA,IAC7C,GACA;AACA,YAAM;AAAA,IACR;AAAA,EACF;AACA,OAAK;AAAA,IACH,KAAK,YAAY,EAAE,MAAM,OAAO,CAAC,MAAM,EAAE,iBAAiB,YAAY;AAAA,EACxE;AACF;",
5
+ "names": []
6
+ }
@@ -0,0 +1,89 @@
1
+ // src/loyalty/client.ts
2
+ import { CustomerError } from "../customer/error.js";
3
+ import { apiError, requestWithRetry } from "../internal/customer-http.js";
4
+ import { releasePerks } from "./release.js";
5
+ var branchQuery = (branchId) => branchId ? `?branch=${encodeURIComponent(branchId)}` : "";
6
+ function createLoyalty(options = {}) {
7
+ const base = (options.customerBase ?? "/_zappie/api").replace(/\/+$/, "");
8
+ const fetcher = options.fetch ?? globalThis.fetch;
9
+ const listeners = /* @__PURE__ */ new Set();
10
+ globalThis.__zappieReleasePerks ?? (globalThis.__zappieReleasePerks = (perks) => releasePerks(perks, base, fetcher));
11
+ const request = async (path, init = {}) => {
12
+ const response = await requestWithRetry(
13
+ fetcher,
14
+ base + path,
15
+ init,
16
+ "Could not reach the loyalty service"
17
+ );
18
+ if (!response.ok) {
19
+ const value = await apiError(
20
+ response,
21
+ `The loyalty service answered ${response.status}`
22
+ );
23
+ throw new CustomerError(
24
+ response.status,
25
+ value.apiCode,
26
+ value.message,
27
+ value.param
28
+ );
29
+ }
30
+ return await response.json();
31
+ };
32
+ const write = async (path, method, body, key) => {
33
+ const result = await request(path, {
34
+ method,
35
+ headers: {
36
+ ...body ? { "Content-Type": "application/json" } : {},
37
+ ...key ? { "Idempotency-Key": key } : {}
38
+ },
39
+ ...body ? { body: JSON.stringify(body) } : {}
40
+ });
41
+ for (const listener of [...listeners]) {
42
+ listener();
43
+ }
44
+ return result;
45
+ };
46
+ return {
47
+ /** The same-origin customer API base the client calls. */
48
+ base,
49
+ /** The customer's points and whether the business's programme is on. */
50
+ points: () => request("/me/loyalty"),
51
+ /** The active perks at the branch. Optional when the site's key reaches one branch. */
52
+ perks: (branchId) => request(
53
+ `/me/loyalty/perks${branchQuery(branchId)}`
54
+ ).then((result) => result.perks),
55
+ /**
56
+ * Spend points on a perk. The points leave the balance at once. Each call makes its own
57
+ * Idempotency-Key unless you pass one, so retry a failed tap with the same key.
58
+ */
59
+ redeem: (perkId, redeem = {}) => write(
60
+ "/me/loyalty/redemptions",
61
+ "POST",
62
+ { perk_id: perkId },
63
+ redeem.idempotencyKey ?? crypto.randomUUID()
64
+ ),
65
+ /**
66
+ * Give back a reserved redemption's points. Cancelling one already cancelled succeeds again.
67
+ * One an order used answers `redemption_used`. Don't cancel one an open checkout carries.
68
+ */
69
+ cancel: (redemptionId) => write(
70
+ `/me/loyalty/redemptions/${encodeURIComponent(redemptionId)}`,
71
+ "DELETE"
72
+ ).then(() => void 0),
73
+ /** Award a paid order's points once. A second claim earns 0. */
74
+ claim: (orderId) => write("/me/loyalty/claims", "POST", { order_id: orderId }),
75
+ /** The customer's store credit at the branch. Paying with it happens on the payment page. */
76
+ storeCredit: (branchId) => request(`/me/store-credit${branchQuery(branchId)}`),
77
+ /** Call `listener` after each redeem, cancel or claim, whose balance has changed. */
78
+ onChange(listener) {
79
+ listeners.add(listener);
80
+ return () => {
81
+ listeners.delete(listener);
82
+ };
83
+ }
84
+ };
85
+ }
86
+ export {
87
+ createLoyalty
88
+ };
89
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/loyalty/client.ts"],
4
+ "mappings": ";AAAA,SAAS,qBAAqB;AAC9B,SAAS,UAAU,wBAAwB;AAC3C,SAAS,oBAAoB;AAU7B,IAAM,cAAc,CAAC,aACnB,WAAW,WAAW,mBAAmB,QAAQ,CAAC,KAAK;AAWlD,SAAS,cAAc,UAAgC,CAAC,GAAG;AAChE,QAAM,QAAQ,QAAQ,gBAAgB,gBAAgB,QAAQ,QAAQ,EAAE;AACxE,QAAM,UAAU,QAAQ,SAAS,WAAW;AAC5C,QAAM,YAAY,oBAAI,IAAgB;AAItC,EACE,WAKA,yBALA,WAKA,uBAAyB,CAAC,UAAU,aAAa,OAAO,MAAM,OAAO;AACvE,QAAM,UAAU,OACd,MACA,OAAoB,CAAC,MACN;AACf,UAAM,WAAW,MAAM;AAAA,MACrB;AAAA,MACA,OAAO;AAAA,MACP;AAAA,MACA;AAAA,IACF;AACA,QAAI,CAAC,SAAS,IAAI;AAChB,YAAM,QAAQ,MAAM;AAAA,QAClB;AAAA,QACA,gCAAgC,SAAS,MAAM;AAAA,MACjD;AACA,YAAM,IAAI;AAAA,QACR,SAAS;AAAA,QACT,MAAM;AAAA,QACN,MAAM;AAAA,QACN,MAAM;AAAA,MACR;AAAA,IACF;AACA,WAAQ,MAAM,SAAS,KAAK;AAAA,EAC9B;AACA,QAAM,QAAQ,OACZ,MACA,QACA,MACA,QACe;AACf,UAAM,SAAS,MAAM,QAAW,MAAM;AAAA,MACpC;AAAA,MACA,SAAS;AAAA,QACP,GAAI,OAAO,EAAE,gBAAgB,mBAAmB,IAAI,CAAC;AAAA,QACrD,GAAI,MAAM,EAAE,mBAAmB,IAAI,IAAI,CAAC;AAAA,MAC1C;AAAA,MACA,GAAI,OAAO,EAAE,MAAM,KAAK,UAAU,IAAI,EAAE,IAAI,CAAC;AAAA,IAC/C,CAAC;AACD,eAAW,YAAY,CAAC,GAAG,SAAS,GAAG;AACrC,eAAS;AAAA,IACX;AACA,WAAO;AAAA,EACT;AACA,SAAO;AAAA;AAAA,IAEL;AAAA;AAAA,IAEA,QAAQ,MAAM,QAAiB,aAAa;AAAA;AAAA,IAE5C,OAAO,CAAC,aACN;AAAA,MACE,oBAAoB,YAAY,QAAQ,CAAC;AAAA,IAC3C,EAAE,KAAK,CAAC,WAAW,OAAO,KAAK;AAAA;AAAA;AAAA;AAAA;AAAA,IAKjC,QAAQ,CAAC,QAAgB,SAAsC,CAAC,MAC9D;AAAA,MACE;AAAA,MACA;AAAA,MACA,EAAE,SAAS,OAAO;AAAA,MAClB,OAAO,kBAAkB,OAAO,WAAW;AAAA,IAC7C;AAAA;AAAA;AAAA;AAAA;AAAA,IAKF,QAAQ,CAAC,iBACP;AAAA,MACE,2BAA2B,mBAAmB,YAAY,CAAC;AAAA,MAC3D;AAAA,IACF,EAAE,KAAK,MAAM,MAAS;AAAA;AAAA,IAExB,OAAO,CAAC,YACN,MAAoB,sBAAsB,QAAQ,EAAE,UAAU,QAAQ,CAAC;AAAA;AAAA,IAEzE,aAAa,CAAC,aACZ,QAAqB,mBAAmB,YAAY,QAAQ,CAAC,EAAE;AAAA;AAAA,IAEjE,SAAS,UAAsB;AAC7B,gBAAU,IAAI,QAAQ;AACtB,aAAO,MAAM;AACX,kBAAU,OAAO,QAAQ;AAAA,MAC3B;AAAA,IACF;AAAA,EACF;AACF;",
5
+ "names": []
6
+ }
@@ -0,0 +1,526 @@
1
+ /**
2
+ * Record that a checkout session you just opened carries the bag's perks, so the SDK never gives
3
+ * their points back while it is open. Call it with the request's `perk_redemption_ids` and the
4
+ * session. Does nothing where storage is blocked.
5
+ */
6
+ declare function holdPerks(ids: readonly string[] | undefined, session: {
7
+ id: string;
8
+ expires_at: string;
9
+ }): void;
10
+
11
+ declare const MoneyBrand: unique symbol;
12
+ /** A normalized ISO 4217 currency code. */
13
+ type Currency = string;
14
+ /** A frozen amount in safe integer minor units. Create it with money(). */
15
+ type Money = {
16
+ readonly amountMinor: number;
17
+ readonly currency: Currency;
18
+ readonly [MoneyBrand]: true;
19
+ };
20
+
21
+ /** An exact variant with its selected attributes, price and availability. */
22
+ type Variant = {
23
+ /** A low-stock hint for shoppers. This does not give a stock balance. */
24
+ lowStock?: boolean;
25
+ id: string;
26
+ attributeChoices: Record<string, string>;
27
+ price: Money;
28
+ isAvailable: boolean;
29
+ /**
30
+ * Photos of this exact variant, such as one colour. Empty when the variant
31
+ * shows the item's `images`, and when the API predates variant photos.
32
+ */
33
+ images: string[];
34
+ };
35
+ /** Item details and choices, with at least one exact variant. */
36
+ type Item = {
37
+ /** Fulfilment modes returned for this item. */
38
+ fulfilment?: string[];
39
+ /** Exact source placements returned by targeted item reads. */
40
+ placements?: {
41
+ menuId: string;
42
+ categoryId: string;
43
+ }[];
44
+ /** Stock text for display. Use isAvailable and the server quote to check eligibility. */
45
+ stockLabel?: string;
46
+ promo?: {
47
+ name: string;
48
+ };
49
+ id: string;
50
+ name: string;
51
+ description?: string;
52
+ images: string[];
53
+ price: Money;
54
+ compareAtPrice?: Money;
55
+ isAvailable: boolean;
56
+ quantity: {
57
+ min: number;
58
+ max?: number;
59
+ step: number;
60
+ };
61
+ variants: [Variant, ...Variant[]];
62
+ optionGroups: OptionGroup[];
63
+ tags: string[];
64
+ };
65
+ /** Conditions that change option rules when selected choices match. */
66
+ type OptionCondition = {
67
+ groupId: string;
68
+ choiceIds: string[];
69
+ match: "any" | "all";
70
+ count?: {
71
+ op: "eq" | "gte" | "lte";
72
+ value: number;
73
+ };
74
+ setRequired?: boolean | null;
75
+ setMin?: number | null;
76
+ setMax?: number | null;
77
+ };
78
+ /** Addon or exact-variant attribute selection rules. */
79
+ type OptionGroup = {
80
+ id: string;
81
+ name: string;
82
+ helpText?: string;
83
+ kind: "select" | "text" | "number" | "date" | "time" | "toggle";
84
+ required: boolean;
85
+ isAvailable?: boolean;
86
+ isInformational?: boolean;
87
+ selection: {
88
+ min: number;
89
+ max?: number;
90
+ };
91
+ isAttribute: boolean;
92
+ choices: Choice[];
93
+ input?: {
94
+ maxLength?: number;
95
+ placeholder?: string;
96
+ };
97
+ conditions?: OptionCondition[];
98
+ };
99
+ /** A selectable option. Attribute prices are zero to avoid charging twice. */
100
+ type Choice = {
101
+ /** An attribute price adjustment for display. Cart calculations exclude it. */
102
+ priceHint?: Money;
103
+ id: string;
104
+ name: string;
105
+ price: Money;
106
+ isAvailable: boolean;
107
+ quantity?: {
108
+ min: number;
109
+ max?: number;
110
+ };
111
+ };
112
+
113
+ /**
114
+ * An item selection you submit to the cart, with optional quantity, choices, inputs and source menu.
115
+ */
116
+ type CartLineInput = {
117
+ itemId: string;
118
+ /** The source menu the shopper selected. The SDK checks catalogue membership. */
119
+ menuId?: string;
120
+ quantity?: number;
121
+ choices?: {
122
+ groupId: string;
123
+ choiceId: string;
124
+ quantity?: number;
125
+ }[];
126
+ inputs?: Record<string, string>;
127
+ variantId?: string;
128
+ note?: string;
129
+ };
130
+ /**
131
+ * A readonly cart line with its selection, local price estimate and time added.
132
+ */
133
+ type CartLine = Readonly<{
134
+ id: string;
135
+ itemId: string;
136
+ /** The source menu the shopper selected. The SDK checks catalogue membership. */
137
+ menuId?: string;
138
+ name: string;
139
+ quantity: number;
140
+ choices: readonly Readonly<{
141
+ groupId: string;
142
+ choiceId: string;
143
+ quantity: number;
144
+ }>[];
145
+ inputs: Readonly<Record<string, string>>;
146
+ variantId?: string;
147
+ note?: string;
148
+ unitPrice: Money;
149
+ lineSubtotal: Money;
150
+ addedAt: string;
151
+ }>;
152
+ /**
153
+ * A cart delivery address with optional place coordinates and instructions.
154
+ */
155
+ type DeliveryAddress = {
156
+ line1: string;
157
+ line2?: string;
158
+ city?: string;
159
+ placeId?: string;
160
+ coordinates?: {
161
+ lat: number;
162
+ lng: number;
163
+ };
164
+ instructions?: string;
165
+ };
166
+ /** A shopper's selection, distinct from the store's Fulfilment capabilities. */
167
+ type CartFulfilment = {
168
+ type: "pickup";
169
+ branchId: string;
170
+ scheduledFor?: string;
171
+ } | {
172
+ type: "delivery";
173
+ branchId: string;
174
+ address: DeliveryAddress;
175
+ /** The delivery region you selected from the branch. */
176
+ deliveryRegionId?: string;
177
+ scheduledFor?: string;
178
+ } | {
179
+ type: "dine_in";
180
+ branchId: string;
181
+ table?: string;
182
+ /** The table the guest scanned. It stays with the saved cart. Quotes price the cart at its branch. */
183
+ payPointId?: string;
184
+ };
185
+ /**
186
+ * Stable identifiers for cart validation and quote issues you can handle in your UI.
187
+ */
188
+ type CartIssueCode = "item_unavailable" | "item_not_found" | "option_unavailable" | "option_required" | "option_limit" | "quantity_limit" | "price_changed" | "branch_closed" | "fulfilment_unavailable" | "outside_delivery_zone" | "delivery_region_required" | "schedule_invalid" | "promo_invalid" | "promo_min_spend" | "min_order" | "menu_version_stale" | "perk_invalid";
189
+ /**
190
+ * A suggested cart correction, including any option, quantity or fulfilment details it needs.
191
+ */
192
+ type CartFix = {
193
+ action: "remove_line" | "accept_price" | "remove_promo" | "change_address" | "choose_region" | "remove_perks";
194
+ } | {
195
+ action: "choose_option" | "choose_other_option";
196
+ groupId: string;
197
+ } | {
198
+ action: "set_quantity";
199
+ quantity: number;
200
+ } | {
201
+ action: "schedule";
202
+ nextOpenAt?: string;
203
+ slots?: string[];
204
+ } | {
205
+ action: "switch_fulfilment";
206
+ types: CartFulfilment["type"][];
207
+ };
208
+ /**
209
+ * A local or quoted cart issue with severity, display message and suggested fixes.
210
+ */
211
+ type CartIssue = {
212
+ code: CartIssueCode;
213
+ severity: "blocking" | "warning" | "info";
214
+ lineId?: string;
215
+ groupId?: string;
216
+ message: string;
217
+ data?: Record<string, unknown>;
218
+ fixes: CartFix[];
219
+ source: "local" | "quote";
220
+ };
221
+ /**
222
+ * Server-provided cart pricing, fees, discounts, issues and checkout readiness with an expiry.
223
+ */
224
+ type Quote = {
225
+ /** Null prices mean the service could not split the subtotal across lines. */
226
+ lines: {
227
+ lineId: string;
228
+ unitPrice: Money | null;
229
+ total: Money | null;
230
+ /** Item identity echoed by the quote. */
231
+ itemId?: string;
232
+ /** Item name returned by the quote. */
233
+ name?: string;
234
+ /** Quantity priced by the quote. */
235
+ quantity?: number;
236
+ /** Authoritative option total, or null when the line could not be priced. */
237
+ optionsTotal?: Money | null;
238
+ /** Line status returned by the service. */
239
+ status?: string;
240
+ }[];
241
+ /** Whether the submitted menu version differs from the current version. */
242
+ menuVersionStale?: boolean;
243
+ /** Fulfilment details and arrival estimate returned by the service. */
244
+ fulfilment?: {
245
+ type: string;
246
+ scheduledFor: string | null;
247
+ etaMinutes?: number[];
248
+ deliveryRegionId?: string;
249
+ };
250
+ linesUnpriced?: boolean;
251
+ subtotal: Money;
252
+ fees: {
253
+ code: string;
254
+ label: string;
255
+ amount: Money;
256
+ }[];
257
+ /** The sum of `fees`, so you can show one fees line. Zero when there are none. Delivery is not included. */
258
+ feesTotal: Money;
259
+ delivery?: Money;
260
+ /** A perk's discount has code `perk` and the `redemptionId` it comes from. */
261
+ discounts: {
262
+ code: string;
263
+ label: string;
264
+ amount: Money;
265
+ redemptionId?: string;
266
+ }[];
267
+ total: Money;
268
+ issues: CartIssue[];
269
+ canCheckout: boolean;
270
+ checkout: {
271
+ ready: boolean;
272
+ reason?: "issues" | "customer_required";
273
+ };
274
+ menuVersion: string;
275
+ expiresAt: string;
276
+ };
277
+ /**
278
+ * The cart validation and quoting state, with a quote or issues when available.
279
+ */
280
+ type CartState = {
281
+ status: "ready";
282
+ } | {
283
+ status: "quoting";
284
+ } | {
285
+ status: "quoted";
286
+ quote: Quote;
287
+ } | {
288
+ status: "needs_attention";
289
+ quote?: Quote;
290
+ issues: readonly CartIssue[];
291
+ };
292
+ /**
293
+ * A loyalty perk the shopper redeemed into the bag. The quote prices it, and checkout spends it.
294
+ */
295
+ type AppliedPerk = Readonly<{
296
+ redemptionId: string;
297
+ perkId: string;
298
+ name: string;
299
+ pointsSpent: number;
300
+ /** When the reservation gives its points back if no order uses it. */
301
+ expiresAt: string;
302
+ /** What the perk gives, when the perk was applied from the perk list. */
303
+ reward?: "order_discount" | "free_item";
304
+ }>;
305
+ /**
306
+ * The current readonly cart view, including lines, branch, fulfilment and hydration state.
307
+ */
308
+ type CartSnapshot = {
309
+ /** Selected branch remains available before a fulfilment mode is chosen. */
310
+ readonly branchId?: string | undefined;
311
+ readonly lines: readonly CartLine[];
312
+ readonly count: number;
313
+ readonly subtotal: Money | null;
314
+ readonly fulfilment: CartFulfilment | null;
315
+ readonly promoCode: string | null;
316
+ /** The perks redeemed into the bag, sent with each quote and checkout. Never on an in-venue order. */
317
+ readonly perks: readonly AppliedPerk[];
318
+ readonly state: CartState;
319
+ readonly issues: readonly CartIssue[];
320
+ readonly isHydrated: boolean;
321
+ };
322
+ /**
323
+ * Cart state and operations for editing lines, quoting, persistence and subscriptions.
324
+ */
325
+ interface CartModule extends CartSnapshot {
326
+ validate(input: CartLineInput): {
327
+ ok: true;
328
+ } | {
329
+ ok: false;
330
+ issues: CartIssue[];
331
+ };
332
+ add(item: Item | string, input?: Omit<CartLineInput, "itemId">): CartLine;
333
+ update(lineId: string, patch: Partial<Omit<CartLineInput, "itemId">>): CartLine;
334
+ remove(lineId: string): void;
335
+ clear(): void;
336
+ setFulfilment(fulfilment: CartFulfilment): void;
337
+ setPromoCode(code: string | null): void;
338
+ /**
339
+ * Replace the perks the cart sends. Use `applyPerk` and `removePerk` from `@zappie/sdk/loyalty`,
340
+ * which redeem and cancel them with the API first.
341
+ */
342
+ setPerks(perks: readonly AppliedPerk[]): void;
343
+ quote(opts?: {
344
+ force?: boolean;
345
+ }): Promise<Quote>;
346
+ /** Handle interactive fixes by calling the option, address or time setter. */
347
+ applyFix(issue: CartIssue, fix?: CartFix): void;
348
+ moveTo(branchId: string): Promise<void>;
349
+ hydrate(): Promise<void>;
350
+ /** Attach browser listeners. Use the returned cleanup in your component effect. */
351
+ mount(): () => void;
352
+ dispose(): void;
353
+ subscribe(fn: () => void): () => void;
354
+ getSnapshot(): CartSnapshot;
355
+ toCheckoutPayload(): {
356
+ lines: WireLine[];
357
+ fulfilment: CartFulfilment;
358
+ promo_code?: string;
359
+ perk_redemption_ids?: string[];
360
+ menu_version: string;
361
+ };
362
+ }
363
+ /**
364
+ * A cart line serialized with API field names for quote and checkout requests.
365
+ */
366
+ type WireLine = {
367
+ line_id: string;
368
+ item_id: string;
369
+ menu_id?: string;
370
+ quantity: number;
371
+ variant_id?: string;
372
+ options: {
373
+ group_id: string;
374
+ choice_id: string;
375
+ quantity: number;
376
+ }[];
377
+ inputs: Record<string, string>;
378
+ note?: string;
379
+ };
380
+ /**
381
+ * An API monetary amount in minor units with its currency code.
382
+ */
383
+ type WireMoney = {
384
+ amount_minor: number;
385
+ currency: Currency;
386
+ };
387
+
388
+ /** The customer's points with the business. `enabled` is false while it has no loyalty programme set up. */
389
+ type Loyalty = {
390
+ object: "loyalty";
391
+ enabled: boolean;
392
+ points: number;
393
+ };
394
+ /** What a perk gives, to show the shopper. The quote prices it exactly. */
395
+ type PerkReward = {
396
+ type: "order_discount" | "free_item";
397
+ /** A percentage off the order. */
398
+ percent_off?: number;
399
+ /** A fixed amount off the order, in the branch's currency. */
400
+ amount_off?: WireMoney;
401
+ /** The free items, when `type` is `free_item`. */
402
+ items?: {
403
+ item_id: string;
404
+ quantity: number;
405
+ }[];
406
+ };
407
+ /** A perk the customer can spend points on at a branch. */
408
+ type Perk = {
409
+ id: string;
410
+ object: "perk";
411
+ name: string;
412
+ description: string | null;
413
+ points_cost: number;
414
+ reward: PerkReward;
415
+ /** The order types the business made the perk for. */
416
+ fulfilments: ("delivery" | "pickup" | "dine_in")[];
417
+ };
418
+ /** A redemption: points spent on a perk, reserved until an order uses it or it is cancelled. */
419
+ type PerkRedemption = {
420
+ id: string;
421
+ object: "perk_redemption";
422
+ status: "reserved" | "cancelled";
423
+ /** Sent when it is reserved. */
424
+ points_spent?: number;
425
+ /** Sent when it is reserved. */
426
+ perk?: {
427
+ id: string;
428
+ name: string;
429
+ };
430
+ /** When an unused reservation gives its points back. Sent when it is reserved. */
431
+ expires_at?: string;
432
+ };
433
+ /** What claiming an order's points earned, and the balance after it. */
434
+ type LoyaltyClaim = {
435
+ object: "loyalty_claim";
436
+ order_id: string;
437
+ points_earned: number;
438
+ points: number;
439
+ };
440
+ /** The customer's store credit at a branch. `enabled` is false when the branch doesn't take it. */
441
+ type StoreCredit = {
442
+ object: "store_credit";
443
+ enabled: boolean;
444
+ balance: WireMoney;
445
+ };
446
+ /** Where the loyalty client sends its requests. */
447
+ type LoyaltyClientOptions = {
448
+ /** The same-origin customer API base. Defaults to `/_zappie/api`. */
449
+ customerBase?: string;
450
+ fetch?: typeof fetch;
451
+ };
452
+
453
+ /**
454
+ * Create a client for the signed-in customer's points, perks, redemptions and store credit with
455
+ * this business, over the site's same-origin customer API. Every call needs the `loyalty` scope:
456
+ * a shopper who signed in before it existed gets a `CustomerError` with `apiCode` `scope_required`
457
+ * and signs in again once. Points are never priced here. The quote prices a perk.
458
+ *
459
+ * @param options - The customer API base and fetch implementation.
460
+ * @returns The loyalty client, with `onChange` for balance changes it makes.
461
+ */
462
+ declare function createLoyalty(options?: LoyaltyClientOptions): {
463
+ /** The same-origin customer API base the client calls. */
464
+ base: string;
465
+ /** The customer's points and whether the business's programme is on. */
466
+ points: () => Promise<Loyalty>;
467
+ /** The active perks at the branch. Optional when the site's key reaches one branch. */
468
+ perks: (branchId?: string) => Promise<Perk[]>;
469
+ /**
470
+ * Spend points on a perk. The points leave the balance at once. Each call makes its own
471
+ * Idempotency-Key unless you pass one, so retry a failed tap with the same key.
472
+ */
473
+ redeem: (perkId: string, redeem?: {
474
+ idempotencyKey?: string;
475
+ }) => Promise<PerkRedemption>;
476
+ /**
477
+ * Give back a reserved redemption's points. Cancelling one already cancelled succeeds again.
478
+ * One an order used answers `redemption_used`. Don't cancel one an open checkout carries.
479
+ */
480
+ cancel: (redemptionId: string) => Promise<undefined>;
481
+ /** Award a paid order's points once. A second claim earns 0. */
482
+ claim: (orderId: string) => Promise<LoyaltyClaim>;
483
+ /** The customer's store credit at the branch. Paying with it happens on the payment page. */
484
+ storeCredit: (branchId?: string) => Promise<StoreCredit>;
485
+ /** Call `listener` after each redeem, cancel or claim, whose balance has changed. */
486
+ onChange(listener: () => void): () => void;
487
+ };
488
+ /** The loyalty operations returned by createLoyalty. */
489
+ type LoyaltyClient = ReturnType<typeof createLoyalty>;
490
+
491
+ type PerkCart = Pick<CartModule, "perks" | "fulfilment" | "setPerks" | "getSnapshot">;
492
+ /**
493
+ * Redeem a perk into the bag: spend its points, add it to `cart.perks` and quote again. The quote
494
+ * shows its discount. Pass the perk from `perks()` so the bag knows what it gives.
495
+ *
496
+ * @param cart - The storefront cart.
497
+ * @param loyalty - The loyalty client.
498
+ * @param perk - The perk, or its ID.
499
+ * @param options - An Idempotency-Key to retry the same tap with. Each tap makes its own by default.
500
+ * @returns The perk as the bag holds it.
501
+ */
502
+ declare function applyPerk(cart: PerkCart, loyalty: LoyaltyClient, perk: Perk | string, options?: {
503
+ idempotencyKey?: string;
504
+ }): Promise<AppliedPerk>;
505
+ /**
506
+ * Take a perk out of the bag: give its points back, drop it from `cart.perks` and quote again. A
507
+ * redemption an order already used, or that no longer exists, is dropped all the same. While a
508
+ * checkout session that carries it is open, it stays, because the payment page charges the total
509
+ * with the perk: finish or leave that payment first.
510
+ *
511
+ * @param cart - The storefront cart.
512
+ * @param loyalty - The loyalty client.
513
+ * @param redemptionId - The applied perk's `redemptionId`.
514
+ */
515
+ declare function removePerk(cart: PerkCart, loyalty: LoyaltyClient, redemptionId: string): Promise<void>;
516
+
517
+ /**
518
+ * Give back the points of perks the bag dropped, except those an open checkout carries. A used or
519
+ * unknown redemption has nothing to give back, so every failure is ignored.
520
+ */
521
+ declare function releasePerks(perks: readonly {
522
+ redemptionId: string;
523
+ }[], customerBase?: string, fetcher?: typeof fetch): Promise<void>;
524
+
525
+ export { applyPerk, createLoyalty, holdPerks, releasePerks, removePerk };
526
+ export type { Loyalty, LoyaltyClaim, LoyaltyClient, LoyaltyClientOptions, Perk, PerkRedemption, PerkReward, StoreCredit };
@@ -0,0 +1,13 @@
1
+ // src/loyalty/index.ts
2
+ import { holdPerks } from "../internal/perk-hold.js";
3
+ import { applyPerk, removePerk } from "./cart.js";
4
+ import { createLoyalty } from "./client.js";
5
+ import { releasePerks } from "./release.js";
6
+ export {
7
+ applyPerk,
8
+ createLoyalty,
9
+ holdPerks,
10
+ releasePerks,
11
+ removePerk
12
+ };
13
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/loyalty/index.ts"],
4
+ "mappings": ";AAAA,SAAS,iBAAiB;AAC1B,SAAS,WAAW,kBAAkB;AACtC,SAAS,qBAAqB;AAE9B,SAAS,oBAAoB;",
5
+ "names": []
6
+ }